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" |
| 37 | |
| 38 | static int g_verbose = 0; |
| 39 | |
| 40 | #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__) |
| 41 | |
| 42 | /* |
| 43 | * A fs_path is a helper to dynamically build path names with unknown size. |
| 44 | * It reallocates the internal buffer on demand. |
| 45 | * It allows fast adding of path elements on the right side (normal path) and |
| 46 | * fast adding to the left side (reversed path). A reversed path can also be |
| 47 | * unreversed if needed. |
| 48 | */ |
| 49 | struct fs_path { |
| 50 | union { |
| 51 | struct { |
| 52 | char *start; |
| 53 | char *end; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 54 | |
| 55 | char *buf; |
David Sterba | 1f5a7ff | 2014-02-03 19:23:47 +0100 | [diff] [blame] | 56 | unsigned short buf_len:15; |
| 57 | unsigned short reversed:1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 58 | char inline_buf[]; |
| 59 | }; |
| 60 | char pad[PAGE_SIZE]; |
| 61 | }; |
| 62 | }; |
| 63 | #define FS_PATH_INLINE_SIZE \ |
| 64 | (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf)) |
| 65 | |
| 66 | |
| 67 | /* reused for each extent */ |
| 68 | struct clone_root { |
| 69 | struct btrfs_root *root; |
| 70 | u64 ino; |
| 71 | u64 offset; |
| 72 | |
| 73 | u64 found_refs; |
| 74 | }; |
| 75 | |
| 76 | #define SEND_CTX_MAX_NAME_CACHE_SIZE 128 |
| 77 | #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2) |
| 78 | |
| 79 | struct send_ctx { |
| 80 | struct file *send_filp; |
| 81 | loff_t send_off; |
| 82 | char *send_buf; |
| 83 | u32 send_size; |
| 84 | u32 send_max_size; |
| 85 | u64 total_send_size; |
| 86 | u64 cmd_send_size[BTRFS_SEND_C_MAX + 1]; |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 87 | u64 flags; /* 'flags' member of btrfs_ioctl_send_args is u64 */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 88 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 89 | struct btrfs_root *send_root; |
| 90 | struct btrfs_root *parent_root; |
| 91 | struct clone_root *clone_roots; |
| 92 | int clone_roots_cnt; |
| 93 | |
| 94 | /* current state of the compare_tree call */ |
| 95 | struct btrfs_path *left_path; |
| 96 | struct btrfs_path *right_path; |
| 97 | struct btrfs_key *cmp_key; |
| 98 | |
| 99 | /* |
| 100 | * infos of the currently processed inode. In case of deleted inodes, |
| 101 | * these are the values from the deleted inode. |
| 102 | */ |
| 103 | u64 cur_ino; |
| 104 | u64 cur_inode_gen; |
| 105 | int cur_inode_new; |
| 106 | int cur_inode_new_gen; |
| 107 | int cur_inode_deleted; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 108 | u64 cur_inode_size; |
| 109 | u64 cur_inode_mode; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 110 | u64 cur_inode_last_extent; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 111 | |
| 112 | u64 send_progress; |
| 113 | |
| 114 | struct list_head new_refs; |
| 115 | struct list_head deleted_refs; |
| 116 | |
| 117 | struct radix_tree_root name_cache; |
| 118 | struct list_head name_cache_list; |
| 119 | int name_cache_size; |
| 120 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 121 | char *read_buf; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * We process inodes by their increasing order, so if before an |
| 125 | * incremental send we reverse the parent/child relationship of |
| 126 | * directories such that a directory with a lower inode number was |
| 127 | * the parent of a directory with a higher inode number, and the one |
| 128 | * becoming the new parent got renamed too, we can't rename/move the |
| 129 | * directory with lower inode number when we finish processing it - we |
| 130 | * must process the directory with higher inode number first, then |
| 131 | * rename/move it and then rename/move the directory with lower inode |
| 132 | * number. Example follows. |
| 133 | * |
| 134 | * Tree state when the first send was performed: |
| 135 | * |
| 136 | * . |
| 137 | * |-- a (ino 257) |
| 138 | * |-- b (ino 258) |
| 139 | * | |
| 140 | * | |
| 141 | * |-- c (ino 259) |
| 142 | * | |-- d (ino 260) |
| 143 | * | |
| 144 | * |-- c2 (ino 261) |
| 145 | * |
| 146 | * Tree state when the second (incremental) send is performed: |
| 147 | * |
| 148 | * . |
| 149 | * |-- a (ino 257) |
| 150 | * |-- b (ino 258) |
| 151 | * |-- c2 (ino 261) |
| 152 | * |-- d2 (ino 260) |
| 153 | * |-- cc (ino 259) |
| 154 | * |
| 155 | * The sequence of steps that lead to the second state was: |
| 156 | * |
| 157 | * mv /a/b/c/d /a/b/c2/d2 |
| 158 | * mv /a/b/c /a/b/c2/d2/cc |
| 159 | * |
| 160 | * "c" has lower inode number, but we can't move it (2nd mv operation) |
| 161 | * before we move "d", which has higher inode number. |
| 162 | * |
| 163 | * So we just memorize which move/rename operations must be performed |
| 164 | * later when their respective parent is processed and moved/renamed. |
| 165 | */ |
| 166 | |
| 167 | /* Indexed by parent directory inode number. */ |
| 168 | struct rb_root pending_dir_moves; |
| 169 | |
| 170 | /* |
| 171 | * Reverse index, indexed by the inode number of a directory that |
| 172 | * is waiting for the move/rename of its immediate parent before its |
| 173 | * own move/rename can be performed. |
| 174 | */ |
| 175 | struct rb_root waiting_dir_moves; |
| 176 | }; |
| 177 | |
| 178 | struct pending_dir_move { |
| 179 | struct rb_node node; |
| 180 | struct list_head list; |
| 181 | u64 parent_ino; |
| 182 | u64 ino; |
| 183 | u64 gen; |
| 184 | struct list_head update_refs; |
| 185 | }; |
| 186 | |
| 187 | struct waiting_dir_move { |
| 188 | struct rb_node node; |
| 189 | u64 ino; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | struct name_cache_entry { |
| 193 | struct list_head list; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 194 | /* |
| 195 | * radix_tree has only 32bit entries but we need to handle 64bit inums. |
| 196 | * We use the lower 32bit of the 64bit inum to store it in the tree. If |
| 197 | * more then one inum would fall into the same entry, we use radix_list |
| 198 | * to store the additional entries. radix_list is also used to store |
| 199 | * entries where two entries have the same inum but different |
| 200 | * generations. |
| 201 | */ |
| 202 | struct list_head radix_list; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 203 | u64 ino; |
| 204 | u64 gen; |
| 205 | u64 parent_ino; |
| 206 | u64 parent_gen; |
| 207 | int ret; |
| 208 | int need_later_update; |
| 209 | int name_len; |
| 210 | char name[]; |
| 211 | }; |
| 212 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 213 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino); |
| 214 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 215 | static int need_send_hole(struct send_ctx *sctx) |
| 216 | { |
| 217 | return (sctx->parent_root && !sctx->cur_inode_new && |
| 218 | !sctx->cur_inode_new_gen && !sctx->cur_inode_deleted && |
| 219 | S_ISREG(sctx->cur_inode_mode)); |
| 220 | } |
| 221 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 222 | static void fs_path_reset(struct fs_path *p) |
| 223 | { |
| 224 | if (p->reversed) { |
| 225 | p->start = p->buf + p->buf_len - 1; |
| 226 | p->end = p->start; |
| 227 | *p->start = 0; |
| 228 | } else { |
| 229 | p->start = p->buf; |
| 230 | p->end = p->start; |
| 231 | *p->start = 0; |
| 232 | } |
| 233 | } |
| 234 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 235 | static struct fs_path *fs_path_alloc(void) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 236 | { |
| 237 | struct fs_path *p; |
| 238 | |
| 239 | p = kmalloc(sizeof(*p), GFP_NOFS); |
| 240 | if (!p) |
| 241 | return NULL; |
| 242 | p->reversed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 243 | p->buf = p->inline_buf; |
| 244 | p->buf_len = FS_PATH_INLINE_SIZE; |
| 245 | fs_path_reset(p); |
| 246 | return p; |
| 247 | } |
| 248 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 249 | static struct fs_path *fs_path_alloc_reversed(void) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 250 | { |
| 251 | struct fs_path *p; |
| 252 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 253 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 254 | if (!p) |
| 255 | return NULL; |
| 256 | p->reversed = 1; |
| 257 | fs_path_reset(p); |
| 258 | return p; |
| 259 | } |
| 260 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 261 | static void fs_path_free(struct fs_path *p) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 262 | { |
| 263 | if (!p) |
| 264 | return; |
| 265 | if (p->buf != p->inline_buf) { |
David Sterba | e25a812 | 2014-02-03 19:23:33 +0100 | [diff] [blame] | 266 | if (is_vmalloc_addr(p->buf)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 267 | vfree(p->buf); |
| 268 | else |
| 269 | kfree(p->buf); |
| 270 | } |
| 271 | kfree(p); |
| 272 | } |
| 273 | |
| 274 | static int fs_path_len(struct fs_path *p) |
| 275 | { |
| 276 | return p->end - p->start; |
| 277 | } |
| 278 | |
| 279 | static int fs_path_ensure_buf(struct fs_path *p, int len) |
| 280 | { |
| 281 | char *tmp_buf; |
| 282 | int path_len; |
| 283 | int old_buf_len; |
| 284 | |
| 285 | len++; |
| 286 | |
| 287 | if (p->buf_len >= len) |
| 288 | return 0; |
| 289 | |
| 290 | path_len = p->end - p->start; |
| 291 | old_buf_len = p->buf_len; |
| 292 | len = PAGE_ALIGN(len); |
| 293 | |
| 294 | if (p->buf == p->inline_buf) { |
Joe Perches | 8be04b9 | 2013-06-19 12:15:53 -0700 | [diff] [blame] | 295 | tmp_buf = kmalloc(len, GFP_NOFS | __GFP_NOWARN); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 296 | if (!tmp_buf) { |
| 297 | tmp_buf = vmalloc(len); |
| 298 | if (!tmp_buf) |
| 299 | return -ENOMEM; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 300 | } |
| 301 | memcpy(tmp_buf, p->buf, p->buf_len); |
| 302 | p->buf = tmp_buf; |
| 303 | p->buf_len = len; |
| 304 | } else { |
David Sterba | e25a812 | 2014-02-03 19:23:33 +0100 | [diff] [blame] | 305 | if (is_vmalloc_addr(p->buf)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 306 | tmp_buf = vmalloc(len); |
| 307 | if (!tmp_buf) |
| 308 | return -ENOMEM; |
| 309 | memcpy(tmp_buf, p->buf, p->buf_len); |
| 310 | vfree(p->buf); |
| 311 | } else { |
| 312 | tmp_buf = krealloc(p->buf, len, GFP_NOFS); |
| 313 | if (!tmp_buf) { |
| 314 | tmp_buf = vmalloc(len); |
| 315 | if (!tmp_buf) |
| 316 | return -ENOMEM; |
| 317 | memcpy(tmp_buf, p->buf, p->buf_len); |
| 318 | kfree(p->buf); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | p->buf = tmp_buf; |
| 322 | p->buf_len = len; |
| 323 | } |
| 324 | if (p->reversed) { |
| 325 | tmp_buf = p->buf + old_buf_len - path_len - 1; |
| 326 | p->end = p->buf + p->buf_len - 1; |
| 327 | p->start = p->end - path_len; |
| 328 | memmove(p->start, tmp_buf, path_len + 1); |
| 329 | } else { |
| 330 | p->start = p->buf; |
| 331 | p->end = p->start + path_len; |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 336 | static int fs_path_prepare_for_add(struct fs_path *p, int name_len, |
| 337 | char **prepared) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 338 | { |
| 339 | int ret; |
| 340 | int new_len; |
| 341 | |
| 342 | new_len = p->end - p->start + name_len; |
| 343 | if (p->start != p->end) |
| 344 | new_len++; |
| 345 | ret = fs_path_ensure_buf(p, new_len); |
| 346 | if (ret < 0) |
| 347 | goto out; |
| 348 | |
| 349 | if (p->reversed) { |
| 350 | if (p->start != p->end) |
| 351 | *--p->start = '/'; |
| 352 | p->start -= name_len; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 353 | *prepared = p->start; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 354 | } else { |
| 355 | if (p->start != p->end) |
| 356 | *p->end++ = '/'; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 357 | *prepared = p->end; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 358 | p->end += name_len; |
| 359 | *p->end = 0; |
| 360 | } |
| 361 | |
| 362 | out: |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | static int fs_path_add(struct fs_path *p, const char *name, int name_len) |
| 367 | { |
| 368 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 369 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 370 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 371 | ret = fs_path_prepare_for_add(p, name_len, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 372 | if (ret < 0) |
| 373 | goto out; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 374 | memcpy(prepared, name, name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 375 | |
| 376 | out: |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) |
| 381 | { |
| 382 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 383 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 384 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 385 | ret = fs_path_prepare_for_add(p, p2->end - p2->start, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 386 | if (ret < 0) |
| 387 | goto out; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 388 | memcpy(prepared, p2->start, p2->end - p2->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 389 | |
| 390 | out: |
| 391 | return ret; |
| 392 | } |
| 393 | |
| 394 | static int fs_path_add_from_extent_buffer(struct fs_path *p, |
| 395 | struct extent_buffer *eb, |
| 396 | unsigned long off, int len) |
| 397 | { |
| 398 | int ret; |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 399 | char *prepared; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 400 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 401 | ret = fs_path_prepare_for_add(p, len, &prepared); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 402 | if (ret < 0) |
| 403 | goto out; |
| 404 | |
David Sterba | b23ab57 | 2014-02-03 19:23:19 +0100 | [diff] [blame] | 405 | read_extent_buffer(eb, prepared, off, len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 406 | |
| 407 | out: |
| 408 | return ret; |
| 409 | } |
| 410 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 411 | static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
| 412 | { |
| 413 | int ret; |
| 414 | |
| 415 | p->reversed = from->reversed; |
| 416 | fs_path_reset(p); |
| 417 | |
| 418 | ret = fs_path_add_path(p, from); |
| 419 | |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | static void fs_path_unreverse(struct fs_path *p) |
| 425 | { |
| 426 | char *tmp; |
| 427 | int len; |
| 428 | |
| 429 | if (!p->reversed) |
| 430 | return; |
| 431 | |
| 432 | tmp = p->start; |
| 433 | len = p->end - p->start; |
| 434 | p->start = p->buf; |
| 435 | p->end = p->start + len; |
| 436 | memmove(p->start, tmp, len + 1); |
| 437 | p->reversed = 0; |
| 438 | } |
| 439 | |
| 440 | static struct btrfs_path *alloc_path_for_send(void) |
| 441 | { |
| 442 | struct btrfs_path *path; |
| 443 | |
| 444 | path = btrfs_alloc_path(); |
| 445 | if (!path) |
| 446 | return NULL; |
| 447 | path->search_commit_root = 1; |
| 448 | path->skip_locking = 1; |
| 449 | return path; |
| 450 | } |
| 451 | |
Eric Sandeen | 48a3b63 | 2013-04-25 20:41:01 +0000 | [diff] [blame] | 452 | 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] | 453 | { |
| 454 | int ret; |
| 455 | mm_segment_t old_fs; |
| 456 | u32 pos = 0; |
| 457 | |
| 458 | old_fs = get_fs(); |
| 459 | set_fs(KERNEL_DS); |
| 460 | |
| 461 | while (pos < len) { |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 462 | ret = vfs_write(filp, (char *)buf + pos, len - pos, off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 463 | /* TODO handle that correctly */ |
| 464 | /*if (ret == -ERESTARTSYS) { |
| 465 | continue; |
| 466 | }*/ |
| 467 | if (ret < 0) |
| 468 | goto out; |
| 469 | if (ret == 0) { |
| 470 | ret = -EIO; |
| 471 | goto out; |
| 472 | } |
| 473 | pos += ret; |
| 474 | } |
| 475 | |
| 476 | ret = 0; |
| 477 | |
| 478 | out: |
| 479 | set_fs(old_fs); |
| 480 | return ret; |
| 481 | } |
| 482 | |
| 483 | static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) |
| 484 | { |
| 485 | struct btrfs_tlv_header *hdr; |
| 486 | int total_len = sizeof(*hdr) + len; |
| 487 | int left = sctx->send_max_size - sctx->send_size; |
| 488 | |
| 489 | if (unlikely(left < total_len)) |
| 490 | return -EOVERFLOW; |
| 491 | |
| 492 | hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); |
| 493 | hdr->tlv_type = cpu_to_le16(attr); |
| 494 | hdr->tlv_len = cpu_to_le16(len); |
| 495 | memcpy(hdr + 1, data, len); |
| 496 | sctx->send_size += total_len; |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
David Sterba | 95bc79d | 2013-12-16 17:34:10 +0100 | [diff] [blame] | 501 | #define TLV_PUT_DEFINE_INT(bits) \ |
| 502 | static int tlv_put_u##bits(struct send_ctx *sctx, \ |
| 503 | u##bits attr, u##bits value) \ |
| 504 | { \ |
| 505 | __le##bits __tmp = cpu_to_le##bits(value); \ |
| 506 | return tlv_put(sctx, attr, &__tmp, sizeof(__tmp)); \ |
| 507 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 508 | |
David Sterba | 95bc79d | 2013-12-16 17:34:10 +0100 | [diff] [blame] | 509 | TLV_PUT_DEFINE_INT(64) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 510 | |
| 511 | static int tlv_put_string(struct send_ctx *sctx, u16 attr, |
| 512 | const char *str, int len) |
| 513 | { |
| 514 | if (len == -1) |
| 515 | len = strlen(str); |
| 516 | return tlv_put(sctx, attr, str, len); |
| 517 | } |
| 518 | |
| 519 | static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, |
| 520 | const u8 *uuid) |
| 521 | { |
| 522 | return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); |
| 523 | } |
| 524 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 525 | static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
| 526 | struct extent_buffer *eb, |
| 527 | struct btrfs_timespec *ts) |
| 528 | { |
| 529 | struct btrfs_timespec bts; |
| 530 | read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); |
| 531 | return tlv_put(sctx, attr, &bts, sizeof(bts)); |
| 532 | } |
| 533 | |
| 534 | |
| 535 | #define TLV_PUT(sctx, attrtype, attrlen, data) \ |
| 536 | do { \ |
| 537 | ret = tlv_put(sctx, attrtype, attrlen, data); \ |
| 538 | if (ret < 0) \ |
| 539 | goto tlv_put_failure; \ |
| 540 | } while (0) |
| 541 | |
| 542 | #define TLV_PUT_INT(sctx, attrtype, bits, value) \ |
| 543 | do { \ |
| 544 | ret = tlv_put_u##bits(sctx, attrtype, value); \ |
| 545 | if (ret < 0) \ |
| 546 | goto tlv_put_failure; \ |
| 547 | } while (0) |
| 548 | |
| 549 | #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) |
| 550 | #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) |
| 551 | #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) |
| 552 | #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) |
| 553 | #define TLV_PUT_STRING(sctx, attrtype, str, len) \ |
| 554 | do { \ |
| 555 | ret = tlv_put_string(sctx, attrtype, str, len); \ |
| 556 | if (ret < 0) \ |
| 557 | goto tlv_put_failure; \ |
| 558 | } while (0) |
| 559 | #define TLV_PUT_PATH(sctx, attrtype, p) \ |
| 560 | do { \ |
| 561 | ret = tlv_put_string(sctx, attrtype, p->start, \ |
| 562 | p->end - p->start); \ |
| 563 | if (ret < 0) \ |
| 564 | goto tlv_put_failure; \ |
| 565 | } while(0) |
| 566 | #define TLV_PUT_UUID(sctx, attrtype, uuid) \ |
| 567 | do { \ |
| 568 | ret = tlv_put_uuid(sctx, attrtype, uuid); \ |
| 569 | if (ret < 0) \ |
| 570 | goto tlv_put_failure; \ |
| 571 | } while (0) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 572 | #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
| 573 | do { \ |
| 574 | ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ |
| 575 | if (ret < 0) \ |
| 576 | goto tlv_put_failure; \ |
| 577 | } while (0) |
| 578 | |
| 579 | static int send_header(struct send_ctx *sctx) |
| 580 | { |
| 581 | struct btrfs_stream_header hdr; |
| 582 | |
| 583 | strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); |
| 584 | hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION); |
| 585 | |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 586 | return write_buf(sctx->send_filp, &hdr, sizeof(hdr), |
| 587 | &sctx->send_off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /* |
| 591 | * For each command/item we want to send to userspace, we call this function. |
| 592 | */ |
| 593 | static int begin_cmd(struct send_ctx *sctx, int cmd) |
| 594 | { |
| 595 | struct btrfs_cmd_header *hdr; |
| 596 | |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 597 | if (WARN_ON(!sctx->send_buf)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 598 | return -EINVAL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 599 | |
| 600 | BUG_ON(sctx->send_size); |
| 601 | |
| 602 | sctx->send_size += sizeof(*hdr); |
| 603 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 604 | hdr->cmd = cpu_to_le16(cmd); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static int send_cmd(struct send_ctx *sctx) |
| 610 | { |
| 611 | int ret; |
| 612 | struct btrfs_cmd_header *hdr; |
| 613 | u32 crc; |
| 614 | |
| 615 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 616 | hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr)); |
| 617 | hdr->crc = 0; |
| 618 | |
Filipe David Borba Manana | 0b947af | 2014-01-29 21:06:04 +0000 | [diff] [blame] | 619 | crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 620 | hdr->crc = cpu_to_le32(crc); |
| 621 | |
Anand Jain | 1bcea35 | 2012-09-14 00:04:21 -0600 | [diff] [blame] | 622 | ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size, |
| 623 | &sctx->send_off); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 624 | |
| 625 | sctx->total_send_size += sctx->send_size; |
| 626 | sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size; |
| 627 | sctx->send_size = 0; |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | /* |
| 633 | * Sends a move instruction to user space |
| 634 | */ |
| 635 | static int send_rename(struct send_ctx *sctx, |
| 636 | struct fs_path *from, struct fs_path *to) |
| 637 | { |
| 638 | int ret; |
| 639 | |
| 640 | verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start); |
| 641 | |
| 642 | ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); |
| 643 | if (ret < 0) |
| 644 | goto out; |
| 645 | |
| 646 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); |
| 647 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); |
| 648 | |
| 649 | ret = send_cmd(sctx); |
| 650 | |
| 651 | tlv_put_failure: |
| 652 | out: |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Sends a link instruction to user space |
| 658 | */ |
| 659 | static int send_link(struct send_ctx *sctx, |
| 660 | struct fs_path *path, struct fs_path *lnk) |
| 661 | { |
| 662 | int ret; |
| 663 | |
| 664 | verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start); |
| 665 | |
| 666 | ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); |
| 667 | if (ret < 0) |
| 668 | goto out; |
| 669 | |
| 670 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 671 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); |
| 672 | |
| 673 | ret = send_cmd(sctx); |
| 674 | |
| 675 | tlv_put_failure: |
| 676 | out: |
| 677 | return ret; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * Sends an unlink instruction to user space |
| 682 | */ |
| 683 | static int send_unlink(struct send_ctx *sctx, struct fs_path *path) |
| 684 | { |
| 685 | int ret; |
| 686 | |
| 687 | verbose_printk("btrfs: send_unlink %s\n", path->start); |
| 688 | |
| 689 | ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); |
| 690 | if (ret < 0) |
| 691 | goto out; |
| 692 | |
| 693 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 694 | |
| 695 | ret = send_cmd(sctx); |
| 696 | |
| 697 | tlv_put_failure: |
| 698 | out: |
| 699 | return ret; |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Sends a rmdir instruction to user space |
| 704 | */ |
| 705 | static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) |
| 706 | { |
| 707 | int ret; |
| 708 | |
| 709 | verbose_printk("btrfs: send_rmdir %s\n", path->start); |
| 710 | |
| 711 | ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); |
| 712 | if (ret < 0) |
| 713 | goto out; |
| 714 | |
| 715 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 716 | |
| 717 | ret = send_cmd(sctx); |
| 718 | |
| 719 | tlv_put_failure: |
| 720 | out: |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * Helper function to retrieve some fields from an inode item. |
| 726 | */ |
| 727 | static int get_inode_info(struct btrfs_root *root, |
| 728 | u64 ino, u64 *size, u64 *gen, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 729 | u64 *mode, u64 *uid, u64 *gid, |
| 730 | u64 *rdev) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 731 | { |
| 732 | int ret; |
| 733 | struct btrfs_inode_item *ii; |
| 734 | struct btrfs_key key; |
| 735 | struct btrfs_path *path; |
| 736 | |
| 737 | path = alloc_path_for_send(); |
| 738 | if (!path) |
| 739 | return -ENOMEM; |
| 740 | |
| 741 | key.objectid = ino; |
| 742 | key.type = BTRFS_INODE_ITEM_KEY; |
| 743 | key.offset = 0; |
| 744 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 745 | if (ret < 0) |
| 746 | goto out; |
| 747 | if (ret) { |
| 748 | ret = -ENOENT; |
| 749 | goto out; |
| 750 | } |
| 751 | |
| 752 | ii = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 753 | struct btrfs_inode_item); |
| 754 | if (size) |
| 755 | *size = btrfs_inode_size(path->nodes[0], ii); |
| 756 | if (gen) |
| 757 | *gen = btrfs_inode_generation(path->nodes[0], ii); |
| 758 | if (mode) |
| 759 | *mode = btrfs_inode_mode(path->nodes[0], ii); |
| 760 | if (uid) |
| 761 | *uid = btrfs_inode_uid(path->nodes[0], ii); |
| 762 | if (gid) |
| 763 | *gid = btrfs_inode_gid(path->nodes[0], ii); |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 764 | if (rdev) |
| 765 | *rdev = btrfs_inode_rdev(path->nodes[0], ii); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 766 | |
| 767 | out: |
| 768 | btrfs_free_path(path); |
| 769 | return ret; |
| 770 | } |
| 771 | |
| 772 | typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, |
| 773 | struct fs_path *p, |
| 774 | void *ctx); |
| 775 | |
| 776 | /* |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 777 | * Helper function to iterate the entries in ONE btrfs_inode_ref or |
| 778 | * btrfs_inode_extref. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 779 | * The iterate callback may return a non zero value to stop iteration. This can |
| 780 | * be a negative value for error codes or 1 to simply stop it. |
| 781 | * |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 782 | * path must point to the INODE_REF or INODE_EXTREF when called. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 783 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 784 | 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] | 785 | struct btrfs_key *found_key, int resolve, |
| 786 | iterate_inode_ref_t iterate, void *ctx) |
| 787 | { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 788 | struct extent_buffer *eb = path->nodes[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 789 | struct btrfs_item *item; |
| 790 | struct btrfs_inode_ref *iref; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 791 | struct btrfs_inode_extref *extref; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 792 | struct btrfs_path *tmp_path; |
| 793 | struct fs_path *p; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 794 | u32 cur = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 795 | u32 total; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 796 | int slot = path->slots[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 797 | u32 name_len; |
| 798 | char *start; |
| 799 | int ret = 0; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 800 | int num = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 801 | int index; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 802 | u64 dir; |
| 803 | unsigned long name_off; |
| 804 | unsigned long elem_size; |
| 805 | unsigned long ptr; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 806 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 807 | p = fs_path_alloc_reversed(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 808 | if (!p) |
| 809 | return -ENOMEM; |
| 810 | |
| 811 | tmp_path = alloc_path_for_send(); |
| 812 | if (!tmp_path) { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 813 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 814 | return -ENOMEM; |
| 815 | } |
| 816 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 817 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 818 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
| 819 | ptr = (unsigned long)btrfs_item_ptr(eb, slot, |
| 820 | struct btrfs_inode_ref); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 821 | item = btrfs_item_nr(slot); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 822 | total = btrfs_item_size(eb, item); |
| 823 | elem_size = sizeof(*iref); |
| 824 | } else { |
| 825 | ptr = btrfs_item_ptr_offset(eb, slot); |
| 826 | total = btrfs_item_size_nr(eb, slot); |
| 827 | elem_size = sizeof(*extref); |
| 828 | } |
| 829 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 830 | while (cur < total) { |
| 831 | fs_path_reset(p); |
| 832 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 833 | if (found_key->type == BTRFS_INODE_REF_KEY) { |
| 834 | iref = (struct btrfs_inode_ref *)(ptr + cur); |
| 835 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 836 | name_off = (unsigned long)(iref + 1); |
| 837 | index = btrfs_inode_ref_index(eb, iref); |
| 838 | dir = found_key->offset; |
| 839 | } else { |
| 840 | extref = (struct btrfs_inode_extref *)(ptr + cur); |
| 841 | name_len = btrfs_inode_extref_name_len(eb, extref); |
| 842 | name_off = (unsigned long)&extref->name; |
| 843 | index = btrfs_inode_extref_index(eb, extref); |
| 844 | dir = btrfs_inode_extref_parent(eb, extref); |
| 845 | } |
| 846 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 847 | if (resolve) { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 848 | start = btrfs_ref_to_path(root, tmp_path, name_len, |
| 849 | name_off, eb, dir, |
| 850 | p->buf, p->buf_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 851 | if (IS_ERR(start)) { |
| 852 | ret = PTR_ERR(start); |
| 853 | goto out; |
| 854 | } |
| 855 | if (start < p->buf) { |
| 856 | /* overflow , try again with larger buffer */ |
| 857 | ret = fs_path_ensure_buf(p, |
| 858 | p->buf_len + p->buf - start); |
| 859 | if (ret < 0) |
| 860 | goto out; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 861 | start = btrfs_ref_to_path(root, tmp_path, |
| 862 | name_len, name_off, |
| 863 | eb, dir, |
| 864 | p->buf, p->buf_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 865 | if (IS_ERR(start)) { |
| 866 | ret = PTR_ERR(start); |
| 867 | goto out; |
| 868 | } |
| 869 | BUG_ON(start < p->buf); |
| 870 | } |
| 871 | p->start = start; |
| 872 | } else { |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 873 | ret = fs_path_add_from_extent_buffer(p, eb, name_off, |
| 874 | name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 875 | if (ret < 0) |
| 876 | goto out; |
| 877 | } |
| 878 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 879 | cur += elem_size + name_len; |
| 880 | ret = iterate(num, dir, index, p, ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 881 | if (ret) |
| 882 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 883 | num++; |
| 884 | } |
| 885 | |
| 886 | out: |
| 887 | btrfs_free_path(tmp_path); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 888 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 889 | return ret; |
| 890 | } |
| 891 | |
| 892 | typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, |
| 893 | const char *name, int name_len, |
| 894 | const char *data, int data_len, |
| 895 | u8 type, void *ctx); |
| 896 | |
| 897 | /* |
| 898 | * Helper function to iterate the entries in ONE btrfs_dir_item. |
| 899 | * The iterate callback may return a non zero value to stop iteration. This can |
| 900 | * be a negative value for error codes or 1 to simply stop it. |
| 901 | * |
| 902 | * path must point to the dir item when called. |
| 903 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 904 | 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] | 905 | struct btrfs_key *found_key, |
| 906 | iterate_dir_item_t iterate, void *ctx) |
| 907 | { |
| 908 | int ret = 0; |
| 909 | struct extent_buffer *eb; |
| 910 | struct btrfs_item *item; |
| 911 | struct btrfs_dir_item *di; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 912 | struct btrfs_key di_key; |
| 913 | char *buf = NULL; |
| 914 | char *buf2 = NULL; |
| 915 | int buf_len; |
| 916 | int buf_virtual = 0; |
| 917 | u32 name_len; |
| 918 | u32 data_len; |
| 919 | u32 cur; |
| 920 | u32 len; |
| 921 | u32 total; |
| 922 | int slot; |
| 923 | int num; |
| 924 | u8 type; |
| 925 | |
| 926 | buf_len = PAGE_SIZE; |
| 927 | buf = kmalloc(buf_len, GFP_NOFS); |
| 928 | if (!buf) { |
| 929 | ret = -ENOMEM; |
| 930 | goto out; |
| 931 | } |
| 932 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 933 | eb = path->nodes[0]; |
| 934 | slot = path->slots[0]; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 935 | item = btrfs_item_nr(slot); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 936 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 937 | cur = 0; |
| 938 | len = 0; |
| 939 | total = btrfs_item_size(eb, item); |
| 940 | |
| 941 | num = 0; |
| 942 | while (cur < total) { |
| 943 | name_len = btrfs_dir_name_len(eb, di); |
| 944 | data_len = btrfs_dir_data_len(eb, di); |
| 945 | type = btrfs_dir_type(eb, di); |
| 946 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 947 | |
| 948 | if (name_len + data_len > buf_len) { |
| 949 | buf_len = PAGE_ALIGN(name_len + data_len); |
| 950 | if (buf_virtual) { |
| 951 | buf2 = vmalloc(buf_len); |
| 952 | if (!buf2) { |
| 953 | ret = -ENOMEM; |
| 954 | goto out; |
| 955 | } |
| 956 | vfree(buf); |
| 957 | } else { |
| 958 | buf2 = krealloc(buf, buf_len, GFP_NOFS); |
| 959 | if (!buf2) { |
| 960 | buf2 = vmalloc(buf_len); |
| 961 | if (!buf2) { |
| 962 | ret = -ENOMEM; |
| 963 | goto out; |
| 964 | } |
| 965 | kfree(buf); |
| 966 | buf_virtual = 1; |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | buf = buf2; |
| 971 | buf2 = NULL; |
| 972 | } |
| 973 | |
| 974 | read_extent_buffer(eb, buf, (unsigned long)(di + 1), |
| 975 | name_len + data_len); |
| 976 | |
| 977 | len = sizeof(*di) + name_len + data_len; |
| 978 | di = (struct btrfs_dir_item *)((char *)di + len); |
| 979 | cur += len; |
| 980 | |
| 981 | ret = iterate(num, &di_key, buf, name_len, buf + name_len, |
| 982 | data_len, type, ctx); |
| 983 | if (ret < 0) |
| 984 | goto out; |
| 985 | if (ret) { |
| 986 | ret = 0; |
| 987 | goto out; |
| 988 | } |
| 989 | |
| 990 | num++; |
| 991 | } |
| 992 | |
| 993 | out: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 994 | if (buf_virtual) |
| 995 | vfree(buf); |
| 996 | else |
| 997 | kfree(buf); |
| 998 | return ret; |
| 999 | } |
| 1000 | |
| 1001 | static int __copy_first_ref(int num, u64 dir, int index, |
| 1002 | struct fs_path *p, void *ctx) |
| 1003 | { |
| 1004 | int ret; |
| 1005 | struct fs_path *pt = ctx; |
| 1006 | |
| 1007 | ret = fs_path_copy(pt, p); |
| 1008 | if (ret < 0) |
| 1009 | return ret; |
| 1010 | |
| 1011 | /* we want the first only */ |
| 1012 | return 1; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Retrieve the first path of an inode. If an inode has more then one |
| 1017 | * ref/hardlink, this is ignored. |
| 1018 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1019 | static int get_inode_path(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1020 | u64 ino, struct fs_path *path) |
| 1021 | { |
| 1022 | int ret; |
| 1023 | struct btrfs_key key, found_key; |
| 1024 | struct btrfs_path *p; |
| 1025 | |
| 1026 | p = alloc_path_for_send(); |
| 1027 | if (!p) |
| 1028 | return -ENOMEM; |
| 1029 | |
| 1030 | fs_path_reset(path); |
| 1031 | |
| 1032 | key.objectid = ino; |
| 1033 | key.type = BTRFS_INODE_REF_KEY; |
| 1034 | key.offset = 0; |
| 1035 | |
| 1036 | ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); |
| 1037 | if (ret < 0) |
| 1038 | goto out; |
| 1039 | if (ret) { |
| 1040 | ret = 1; |
| 1041 | goto out; |
| 1042 | } |
| 1043 | btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); |
| 1044 | if (found_key.objectid != ino || |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1045 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 1046 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1047 | ret = -ENOENT; |
| 1048 | goto out; |
| 1049 | } |
| 1050 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1051 | ret = iterate_inode_ref(root, p, &found_key, 1, |
| 1052 | __copy_first_ref, path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1053 | if (ret < 0) |
| 1054 | goto out; |
| 1055 | ret = 0; |
| 1056 | |
| 1057 | out: |
| 1058 | btrfs_free_path(p); |
| 1059 | return ret; |
| 1060 | } |
| 1061 | |
| 1062 | struct backref_ctx { |
| 1063 | struct send_ctx *sctx; |
| 1064 | |
| 1065 | /* number of total found references */ |
| 1066 | u64 found; |
| 1067 | |
| 1068 | /* |
| 1069 | * used for clones found in send_root. clones found behind cur_objectid |
| 1070 | * and cur_offset are not considered as allowed clones. |
| 1071 | */ |
| 1072 | u64 cur_objectid; |
| 1073 | u64 cur_offset; |
| 1074 | |
| 1075 | /* may be truncated in case it's the last extent in a file */ |
| 1076 | u64 extent_len; |
| 1077 | |
| 1078 | /* Just to check for bugs in backref resolving */ |
Alexander Block | ee849c0 | 2012-07-28 12:42:05 +0200 | [diff] [blame] | 1079 | int found_itself; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1080 | }; |
| 1081 | |
| 1082 | static int __clone_root_cmp_bsearch(const void *key, const void *elt) |
| 1083 | { |
Jan Schmidt | 995e01b | 2012-08-13 02:52:38 -0600 | [diff] [blame] | 1084 | u64 root = (u64)(uintptr_t)key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1085 | struct clone_root *cr = (struct clone_root *)elt; |
| 1086 | |
| 1087 | if (root < cr->root->objectid) |
| 1088 | return -1; |
| 1089 | if (root > cr->root->objectid) |
| 1090 | return 1; |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static int __clone_root_cmp_sort(const void *e1, const void *e2) |
| 1095 | { |
| 1096 | struct clone_root *cr1 = (struct clone_root *)e1; |
| 1097 | struct clone_root *cr2 = (struct clone_root *)e2; |
| 1098 | |
| 1099 | if (cr1->root->objectid < cr2->root->objectid) |
| 1100 | return -1; |
| 1101 | if (cr1->root->objectid > cr2->root->objectid) |
| 1102 | return 1; |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | /* |
| 1107 | * Called for every backref that is found for the current extent. |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1108 | * Results are collected in sctx->clone_roots->ino/offset/found_refs |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1109 | */ |
| 1110 | static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) |
| 1111 | { |
| 1112 | struct backref_ctx *bctx = ctx_; |
| 1113 | struct clone_root *found; |
| 1114 | int ret; |
| 1115 | u64 i_size; |
| 1116 | |
| 1117 | /* 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] | 1118 | found = bsearch((void *)(uintptr_t)root, bctx->sctx->clone_roots, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1119 | bctx->sctx->clone_roots_cnt, |
| 1120 | sizeof(struct clone_root), |
| 1121 | __clone_root_cmp_bsearch); |
| 1122 | if (!found) |
| 1123 | return 0; |
| 1124 | |
| 1125 | if (found->root == bctx->sctx->send_root && |
| 1126 | ino == bctx->cur_objectid && |
| 1127 | offset == bctx->cur_offset) { |
Alexander Block | ee849c0 | 2012-07-28 12:42:05 +0200 | [diff] [blame] | 1128 | bctx->found_itself = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1132 | * 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] | 1133 | * accept clones from these extents. |
| 1134 | */ |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1135 | ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL, |
| 1136 | NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1137 | if (ret < 0) |
| 1138 | return ret; |
| 1139 | |
| 1140 | if (offset + bctx->extent_len > i_size) |
| 1141 | return 0; |
| 1142 | |
| 1143 | /* |
| 1144 | * Make sure we don't consider clones from send_root that are |
| 1145 | * behind the current inode/offset. |
| 1146 | */ |
| 1147 | if (found->root == bctx->sctx->send_root) { |
| 1148 | /* |
| 1149 | * TODO for the moment we don't accept clones from the inode |
| 1150 | * that is currently send. We may change this when |
| 1151 | * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same |
| 1152 | * file. |
| 1153 | */ |
| 1154 | if (ino >= bctx->cur_objectid) |
| 1155 | return 0; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1156 | #if 0 |
| 1157 | if (ino > bctx->cur_objectid) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1158 | return 0; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1159 | if (offset + bctx->extent_len > bctx->cur_offset) |
| 1160 | return 0; |
| 1161 | #endif |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | bctx->found++; |
| 1165 | found->found_refs++; |
| 1166 | if (ino < found->ino) { |
| 1167 | found->ino = ino; |
| 1168 | found->offset = offset; |
| 1169 | } else if (found->ino == ino) { |
| 1170 | /* |
| 1171 | * same extent found more then once in the same file. |
| 1172 | */ |
| 1173 | if (found->offset > offset + bctx->extent_len) |
| 1174 | found->offset = offset; |
| 1175 | } |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
| 1180 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1181 | * Given an inode, offset and extent item, it finds a good clone for a clone |
| 1182 | * instruction. Returns -ENOENT when none could be found. The function makes |
| 1183 | * sure that the returned clone is usable at the point where sending is at the |
| 1184 | * moment. This means, that no clones are accepted which lie behind the current |
| 1185 | * inode+offset. |
| 1186 | * |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1187 | * path must point to the extent item when called. |
| 1188 | */ |
| 1189 | static int find_extent_clone(struct send_ctx *sctx, |
| 1190 | struct btrfs_path *path, |
| 1191 | u64 ino, u64 data_offset, |
| 1192 | u64 ino_size, |
| 1193 | struct clone_root **found) |
| 1194 | { |
| 1195 | int ret; |
| 1196 | int extent_type; |
| 1197 | u64 logical; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1198 | u64 disk_byte; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1199 | u64 num_bytes; |
| 1200 | u64 extent_item_pos; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1201 | u64 flags = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1202 | struct btrfs_file_extent_item *fi; |
| 1203 | struct extent_buffer *eb = path->nodes[0]; |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1204 | struct backref_ctx *backref_ctx = NULL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1205 | struct clone_root *cur_clone_root; |
| 1206 | struct btrfs_key found_key; |
| 1207 | struct btrfs_path *tmp_path; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1208 | int compressed; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1209 | u32 i; |
| 1210 | |
| 1211 | tmp_path = alloc_path_for_send(); |
| 1212 | if (!tmp_path) |
| 1213 | return -ENOMEM; |
| 1214 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1215 | backref_ctx = kmalloc(sizeof(*backref_ctx), GFP_NOFS); |
| 1216 | if (!backref_ctx) { |
| 1217 | ret = -ENOMEM; |
| 1218 | goto out; |
| 1219 | } |
| 1220 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1221 | if (data_offset >= ino_size) { |
| 1222 | /* |
| 1223 | * There may be extents that lie behind the file's size. |
| 1224 | * I at least had this in combination with snapshotting while |
| 1225 | * writing large files. |
| 1226 | */ |
| 1227 | ret = 0; |
| 1228 | goto out; |
| 1229 | } |
| 1230 | |
| 1231 | fi = btrfs_item_ptr(eb, path->slots[0], |
| 1232 | struct btrfs_file_extent_item); |
| 1233 | extent_type = btrfs_file_extent_type(eb, fi); |
| 1234 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1235 | ret = -ENOENT; |
| 1236 | goto out; |
| 1237 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1238 | compressed = btrfs_file_extent_compression(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1239 | |
| 1240 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1241 | disk_byte = btrfs_file_extent_disk_bytenr(eb, fi); |
| 1242 | if (disk_byte == 0) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1243 | ret = -ENOENT; |
| 1244 | goto out; |
| 1245 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1246 | logical = disk_byte + btrfs_file_extent_offset(eb, fi); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1247 | |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1248 | ret = extent_from_logical(sctx->send_root->fs_info, disk_byte, tmp_path, |
| 1249 | &found_key, &flags); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1250 | btrfs_release_path(tmp_path); |
| 1251 | |
| 1252 | if (ret < 0) |
| 1253 | goto out; |
Liu Bo | 69917e4 | 2012-09-07 20:01:28 -0600 | [diff] [blame] | 1254 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1255 | ret = -EIO; |
| 1256 | goto out; |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * Setup the clone roots. |
| 1261 | */ |
| 1262 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1263 | cur_clone_root = sctx->clone_roots + i; |
| 1264 | cur_clone_root->ino = (u64)-1; |
| 1265 | cur_clone_root->offset = 0; |
| 1266 | cur_clone_root->found_refs = 0; |
| 1267 | } |
| 1268 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1269 | backref_ctx->sctx = sctx; |
| 1270 | backref_ctx->found = 0; |
| 1271 | backref_ctx->cur_objectid = ino; |
| 1272 | backref_ctx->cur_offset = data_offset; |
| 1273 | backref_ctx->found_itself = 0; |
| 1274 | backref_ctx->extent_len = num_bytes; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1275 | |
| 1276 | /* |
| 1277 | * The last extent of a file may be too large due to page alignment. |
| 1278 | * We need to adjust extent_len in this case so that the checks in |
| 1279 | * __iterate_backrefs work. |
| 1280 | */ |
| 1281 | if (data_offset + num_bytes >= ino_size) |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1282 | backref_ctx->extent_len = ino_size - data_offset; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1283 | |
| 1284 | /* |
| 1285 | * Now collect all backrefs. |
| 1286 | */ |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1287 | if (compressed == BTRFS_COMPRESS_NONE) |
| 1288 | extent_item_pos = logical - found_key.objectid; |
| 1289 | else |
| 1290 | extent_item_pos = 0; |
| 1291 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1292 | extent_item_pos = logical - found_key.objectid; |
| 1293 | ret = iterate_extent_inodes(sctx->send_root->fs_info, |
| 1294 | found_key.objectid, extent_item_pos, 1, |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1295 | __iterate_backrefs, backref_ctx); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1296 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1297 | if (ret < 0) |
| 1298 | goto out; |
| 1299 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1300 | if (!backref_ctx->found_itself) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1301 | /* found a bug in backref code? */ |
| 1302 | ret = -EIO; |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 1303 | btrfs_err(sctx->send_root->fs_info, "did not find backref in " |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1304 | "send_root. inode=%llu, offset=%llu, " |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 1305 | "disk_byte=%llu found extent=%llu\n", |
| 1306 | ino, data_offset, disk_byte, found_key.objectid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1307 | goto out; |
| 1308 | } |
| 1309 | |
| 1310 | verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, " |
| 1311 | "ino=%llu, " |
| 1312 | "num_bytes=%llu, logical=%llu\n", |
| 1313 | data_offset, ino, num_bytes, logical); |
| 1314 | |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1315 | if (!backref_ctx->found) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1316 | verbose_printk("btrfs: no clones found\n"); |
| 1317 | |
| 1318 | cur_clone_root = NULL; |
| 1319 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1320 | if (sctx->clone_roots[i].found_refs) { |
| 1321 | if (!cur_clone_root) |
| 1322 | cur_clone_root = sctx->clone_roots + i; |
| 1323 | else if (sctx->clone_roots[i].root == sctx->send_root) |
| 1324 | /* prefer clones from send_root over others */ |
| 1325 | cur_clone_root = sctx->clone_roots + i; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | } |
| 1329 | |
| 1330 | if (cur_clone_root) { |
Filipe David Borba Manana | 93de4ba | 2014-02-15 15:53:16 +0000 | [diff] [blame] | 1331 | if (compressed != BTRFS_COMPRESS_NONE) { |
| 1332 | /* |
| 1333 | * Offsets given by iterate_extent_inodes() are relative |
| 1334 | * to the start of the extent, we need to add logical |
| 1335 | * offset from the file extent item. |
| 1336 | * (See why at backref.c:check_extent_in_eb()) |
| 1337 | */ |
| 1338 | cur_clone_root->offset += btrfs_file_extent_offset(eb, |
| 1339 | fi); |
| 1340 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1341 | *found = cur_clone_root; |
| 1342 | ret = 0; |
| 1343 | } else { |
| 1344 | ret = -ENOENT; |
| 1345 | } |
| 1346 | |
| 1347 | out: |
| 1348 | btrfs_free_path(tmp_path); |
Alexander Block | 35075bb | 2012-07-28 12:44:34 +0200 | [diff] [blame] | 1349 | kfree(backref_ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1350 | return ret; |
| 1351 | } |
| 1352 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1353 | static int read_symlink(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1354 | u64 ino, |
| 1355 | struct fs_path *dest) |
| 1356 | { |
| 1357 | int ret; |
| 1358 | struct btrfs_path *path; |
| 1359 | struct btrfs_key key; |
| 1360 | struct btrfs_file_extent_item *ei; |
| 1361 | u8 type; |
| 1362 | u8 compression; |
| 1363 | unsigned long off; |
| 1364 | int len; |
| 1365 | |
| 1366 | path = alloc_path_for_send(); |
| 1367 | if (!path) |
| 1368 | return -ENOMEM; |
| 1369 | |
| 1370 | key.objectid = ino; |
| 1371 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1372 | key.offset = 0; |
| 1373 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1374 | if (ret < 0) |
| 1375 | goto out; |
| 1376 | BUG_ON(ret); |
| 1377 | |
| 1378 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1379 | struct btrfs_file_extent_item); |
| 1380 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 1381 | compression = btrfs_file_extent_compression(path->nodes[0], ei); |
| 1382 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); |
| 1383 | BUG_ON(compression); |
| 1384 | |
| 1385 | off = btrfs_file_extent_inline_start(ei); |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 1386 | 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] | 1387 | |
| 1388 | 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] | 1389 | |
| 1390 | out: |
| 1391 | btrfs_free_path(path); |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Helper function to generate a file name that is unique in the root of |
| 1397 | * send_root and parent_root. This is used to generate names for orphan inodes. |
| 1398 | */ |
| 1399 | static int gen_unique_name(struct send_ctx *sctx, |
| 1400 | u64 ino, u64 gen, |
| 1401 | struct fs_path *dest) |
| 1402 | { |
| 1403 | int ret = 0; |
| 1404 | struct btrfs_path *path; |
| 1405 | struct btrfs_dir_item *di; |
| 1406 | char tmp[64]; |
| 1407 | int len; |
| 1408 | u64 idx = 0; |
| 1409 | |
| 1410 | path = alloc_path_for_send(); |
| 1411 | if (!path) |
| 1412 | return -ENOMEM; |
| 1413 | |
| 1414 | while (1) { |
Filipe David Borba Manana | f74b86d | 2014-01-21 23:36:38 +0000 | [diff] [blame] | 1415 | len = snprintf(tmp, sizeof(tmp), "o%llu-%llu-%llu", |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1416 | ino, gen, idx); |
David Sterba | 64792f2 | 2014-02-03 18:24:09 +0100 | [diff] [blame] | 1417 | ASSERT(len < sizeof(tmp)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1418 | |
| 1419 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, |
| 1420 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1421 | tmp, strlen(tmp), 0); |
| 1422 | btrfs_release_path(path); |
| 1423 | if (IS_ERR(di)) { |
| 1424 | ret = PTR_ERR(di); |
| 1425 | goto out; |
| 1426 | } |
| 1427 | if (di) { |
| 1428 | /* not unique, try again */ |
| 1429 | idx++; |
| 1430 | continue; |
| 1431 | } |
| 1432 | |
| 1433 | if (!sctx->parent_root) { |
| 1434 | /* unique */ |
| 1435 | ret = 0; |
| 1436 | break; |
| 1437 | } |
| 1438 | |
| 1439 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, |
| 1440 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1441 | tmp, strlen(tmp), 0); |
| 1442 | btrfs_release_path(path); |
| 1443 | if (IS_ERR(di)) { |
| 1444 | ret = PTR_ERR(di); |
| 1445 | goto out; |
| 1446 | } |
| 1447 | if (di) { |
| 1448 | /* not unique, try again */ |
| 1449 | idx++; |
| 1450 | continue; |
| 1451 | } |
| 1452 | /* unique */ |
| 1453 | break; |
| 1454 | } |
| 1455 | |
| 1456 | ret = fs_path_add(dest, tmp, strlen(tmp)); |
| 1457 | |
| 1458 | out: |
| 1459 | btrfs_free_path(path); |
| 1460 | return ret; |
| 1461 | } |
| 1462 | |
| 1463 | enum inode_state { |
| 1464 | inode_state_no_change, |
| 1465 | inode_state_will_create, |
| 1466 | inode_state_did_create, |
| 1467 | inode_state_will_delete, |
| 1468 | inode_state_did_delete, |
| 1469 | }; |
| 1470 | |
| 1471 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1472 | { |
| 1473 | int ret; |
| 1474 | int left_ret; |
| 1475 | int right_ret; |
| 1476 | u64 left_gen; |
| 1477 | u64 right_gen; |
| 1478 | |
| 1479 | 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] | 1480 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1481 | if (ret < 0 && ret != -ENOENT) |
| 1482 | goto out; |
| 1483 | left_ret = ret; |
| 1484 | |
| 1485 | if (!sctx->parent_root) { |
| 1486 | right_ret = -ENOENT; |
| 1487 | } else { |
| 1488 | ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1489 | NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1490 | if (ret < 0 && ret != -ENOENT) |
| 1491 | goto out; |
| 1492 | right_ret = ret; |
| 1493 | } |
| 1494 | |
| 1495 | if (!left_ret && !right_ret) { |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1496 | if (left_gen == gen && right_gen == gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1497 | ret = inode_state_no_change; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1498 | } else if (left_gen == gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1499 | if (ino < sctx->send_progress) |
| 1500 | ret = inode_state_did_create; |
| 1501 | else |
| 1502 | ret = inode_state_will_create; |
| 1503 | } else if (right_gen == gen) { |
| 1504 | if (ino < sctx->send_progress) |
| 1505 | ret = inode_state_did_delete; |
| 1506 | else |
| 1507 | ret = inode_state_will_delete; |
| 1508 | } else { |
| 1509 | ret = -ENOENT; |
| 1510 | } |
| 1511 | } else if (!left_ret) { |
| 1512 | if (left_gen == gen) { |
| 1513 | if (ino < sctx->send_progress) |
| 1514 | ret = inode_state_did_create; |
| 1515 | else |
| 1516 | ret = inode_state_will_create; |
| 1517 | } else { |
| 1518 | ret = -ENOENT; |
| 1519 | } |
| 1520 | } else if (!right_ret) { |
| 1521 | if (right_gen == gen) { |
| 1522 | if (ino < sctx->send_progress) |
| 1523 | ret = inode_state_did_delete; |
| 1524 | else |
| 1525 | ret = inode_state_will_delete; |
| 1526 | } else { |
| 1527 | ret = -ENOENT; |
| 1528 | } |
| 1529 | } else { |
| 1530 | ret = -ENOENT; |
| 1531 | } |
| 1532 | |
| 1533 | out: |
| 1534 | return ret; |
| 1535 | } |
| 1536 | |
| 1537 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1538 | { |
| 1539 | int ret; |
| 1540 | |
| 1541 | ret = get_cur_inode_state(sctx, ino, gen); |
| 1542 | if (ret < 0) |
| 1543 | goto out; |
| 1544 | |
| 1545 | if (ret == inode_state_no_change || |
| 1546 | ret == inode_state_did_create || |
| 1547 | ret == inode_state_will_delete) |
| 1548 | ret = 1; |
| 1549 | else |
| 1550 | ret = 0; |
| 1551 | |
| 1552 | out: |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | /* |
| 1557 | * Helper function to lookup a dir item in a dir. |
| 1558 | */ |
| 1559 | static int lookup_dir_item_inode(struct btrfs_root *root, |
| 1560 | u64 dir, const char *name, int name_len, |
| 1561 | u64 *found_inode, |
| 1562 | u8 *found_type) |
| 1563 | { |
| 1564 | int ret = 0; |
| 1565 | struct btrfs_dir_item *di; |
| 1566 | struct btrfs_key key; |
| 1567 | struct btrfs_path *path; |
| 1568 | |
| 1569 | path = alloc_path_for_send(); |
| 1570 | if (!path) |
| 1571 | return -ENOMEM; |
| 1572 | |
| 1573 | di = btrfs_lookup_dir_item(NULL, root, path, |
| 1574 | dir, name, name_len, 0); |
| 1575 | if (!di) { |
| 1576 | ret = -ENOENT; |
| 1577 | goto out; |
| 1578 | } |
| 1579 | if (IS_ERR(di)) { |
| 1580 | ret = PTR_ERR(di); |
| 1581 | goto out; |
| 1582 | } |
| 1583 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 1584 | *found_inode = key.objectid; |
| 1585 | *found_type = btrfs_dir_type(path->nodes[0], di); |
| 1586 | |
| 1587 | out: |
| 1588 | btrfs_free_path(path); |
| 1589 | return ret; |
| 1590 | } |
| 1591 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1592 | /* |
| 1593 | * Looks up the first btrfs_inode_ref of a given ino. It returns the parent dir, |
| 1594 | * generation of the parent dir and the name of the dir entry. |
| 1595 | */ |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1596 | static int get_first_ref(struct btrfs_root *root, u64 ino, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1597 | u64 *dir, u64 *dir_gen, struct fs_path *name) |
| 1598 | { |
| 1599 | int ret; |
| 1600 | struct btrfs_key key; |
| 1601 | struct btrfs_key found_key; |
| 1602 | struct btrfs_path *path; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1603 | int len; |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1604 | u64 parent_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1605 | |
| 1606 | path = alloc_path_for_send(); |
| 1607 | if (!path) |
| 1608 | return -ENOMEM; |
| 1609 | |
| 1610 | key.objectid = ino; |
| 1611 | key.type = BTRFS_INODE_REF_KEY; |
| 1612 | key.offset = 0; |
| 1613 | |
| 1614 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 1615 | if (ret < 0) |
| 1616 | goto out; |
| 1617 | if (!ret) |
| 1618 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1619 | path->slots[0]); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1620 | if (ret || found_key.objectid != ino || |
| 1621 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 1622 | found_key.type != BTRFS_INODE_EXTREF_KEY)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1623 | ret = -ENOENT; |
| 1624 | goto out; |
| 1625 | } |
| 1626 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1627 | if (key.type == BTRFS_INODE_REF_KEY) { |
| 1628 | struct btrfs_inode_ref *iref; |
| 1629 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1630 | struct btrfs_inode_ref); |
| 1631 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); |
| 1632 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1633 | (unsigned long)(iref + 1), |
| 1634 | len); |
| 1635 | parent_dir = found_key.offset; |
| 1636 | } else { |
| 1637 | struct btrfs_inode_extref *extref; |
| 1638 | extref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1639 | struct btrfs_inode_extref); |
| 1640 | len = btrfs_inode_extref_name_len(path->nodes[0], extref); |
| 1641 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1642 | (unsigned long)&extref->name, len); |
| 1643 | parent_dir = btrfs_inode_extref_parent(path->nodes[0], extref); |
| 1644 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1645 | if (ret < 0) |
| 1646 | goto out; |
| 1647 | btrfs_release_path(path); |
| 1648 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1649 | ret = get_inode_info(root, parent_dir, NULL, dir_gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1650 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1651 | if (ret < 0) |
| 1652 | goto out; |
| 1653 | |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 1654 | *dir = parent_dir; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1655 | |
| 1656 | out: |
| 1657 | btrfs_free_path(path); |
| 1658 | return ret; |
| 1659 | } |
| 1660 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1661 | static int is_first_ref(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1662 | u64 ino, u64 dir, |
| 1663 | const char *name, int name_len) |
| 1664 | { |
| 1665 | int ret; |
| 1666 | struct fs_path *tmp_name; |
| 1667 | u64 tmp_dir; |
| 1668 | u64 tmp_dir_gen; |
| 1669 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1670 | tmp_name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1671 | if (!tmp_name) |
| 1672 | return -ENOMEM; |
| 1673 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1674 | ret = get_first_ref(root, ino, &tmp_dir, &tmp_dir_gen, tmp_name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1675 | if (ret < 0) |
| 1676 | goto out; |
| 1677 | |
Alexander Block | b9291af | 2012-07-28 11:07:18 +0200 | [diff] [blame] | 1678 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1679 | ret = 0; |
| 1680 | goto out; |
| 1681 | } |
| 1682 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1683 | ret = !memcmp(tmp_name->start, name, name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1684 | |
| 1685 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1686 | fs_path_free(tmp_name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1687 | return ret; |
| 1688 | } |
| 1689 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1690 | /* |
| 1691 | * Used by process_recorded_refs to determine if a new ref would overwrite an |
| 1692 | * already existing ref. In case it detects an overwrite, it returns the |
| 1693 | * inode/gen in who_ino/who_gen. |
| 1694 | * When an overwrite is detected, process_recorded_refs does proper orphanizing |
| 1695 | * to make sure later references to the overwritten inode are possible. |
| 1696 | * Orphanizing is however only required for the first ref of an inode. |
| 1697 | * process_recorded_refs does an additional is_first_ref check to see if |
| 1698 | * orphanizing is really required. |
| 1699 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1700 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| 1701 | const char *name, int name_len, |
| 1702 | u64 *who_ino, u64 *who_gen) |
| 1703 | { |
| 1704 | int ret = 0; |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1705 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1706 | u64 other_inode = 0; |
| 1707 | u8 other_type = 0; |
| 1708 | |
| 1709 | if (!sctx->parent_root) |
| 1710 | goto out; |
| 1711 | |
| 1712 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1713 | if (ret <= 0) |
| 1714 | goto out; |
| 1715 | |
Josef Bacik | ebdad91 | 2013-08-06 16:47:48 -0400 | [diff] [blame] | 1716 | /* |
| 1717 | * If we have a parent root we need to verify that the parent dir was |
| 1718 | * not delted and then re-created, if it was then we have no overwrite |
| 1719 | * and we can just unlink this entry. |
| 1720 | */ |
| 1721 | if (sctx->parent_root) { |
| 1722 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, |
| 1723 | NULL, NULL, NULL); |
| 1724 | if (ret < 0 && ret != -ENOENT) |
| 1725 | goto out; |
| 1726 | if (ret) { |
| 1727 | ret = 0; |
| 1728 | goto out; |
| 1729 | } |
| 1730 | if (gen != dir_gen) |
| 1731 | goto out; |
| 1732 | } |
| 1733 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1734 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
| 1735 | &other_inode, &other_type); |
| 1736 | if (ret < 0 && ret != -ENOENT) |
| 1737 | goto out; |
| 1738 | if (ret) { |
| 1739 | ret = 0; |
| 1740 | goto out; |
| 1741 | } |
| 1742 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1743 | /* |
| 1744 | * Check if the overwritten ref was already processed. If yes, the ref |
| 1745 | * was already unlinked/moved, so we can safely assume that we will not |
| 1746 | * overwrite anything at this point in time. |
| 1747 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1748 | if (other_inode > sctx->send_progress) { |
| 1749 | ret = get_inode_info(sctx->parent_root, other_inode, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1750 | who_gen, NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1751 | if (ret < 0) |
| 1752 | goto out; |
| 1753 | |
| 1754 | ret = 1; |
| 1755 | *who_ino = other_inode; |
| 1756 | } else { |
| 1757 | ret = 0; |
| 1758 | } |
| 1759 | |
| 1760 | out: |
| 1761 | return ret; |
| 1762 | } |
| 1763 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1764 | /* |
| 1765 | * Checks if the ref was overwritten by an already processed inode. This is |
| 1766 | * used by __get_cur_name_and_parent to find out if the ref was orphanized and |
| 1767 | * thus the orphan name needs be used. |
| 1768 | * process_recorded_refs also uses it to avoid unlinking of refs that were |
| 1769 | * overwritten. |
| 1770 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1771 | static int did_overwrite_ref(struct send_ctx *sctx, |
| 1772 | u64 dir, u64 dir_gen, |
| 1773 | u64 ino, u64 ino_gen, |
| 1774 | const char *name, int name_len) |
| 1775 | { |
| 1776 | int ret = 0; |
| 1777 | u64 gen; |
| 1778 | u64 ow_inode; |
| 1779 | u8 other_type; |
| 1780 | |
| 1781 | if (!sctx->parent_root) |
| 1782 | goto out; |
| 1783 | |
| 1784 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1785 | if (ret <= 0) |
| 1786 | goto out; |
| 1787 | |
| 1788 | /* check if the ref was overwritten by another ref */ |
| 1789 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, |
| 1790 | &ow_inode, &other_type); |
| 1791 | if (ret < 0 && ret != -ENOENT) |
| 1792 | goto out; |
| 1793 | if (ret) { |
| 1794 | /* was never and will never be overwritten */ |
| 1795 | ret = 0; |
| 1796 | goto out; |
| 1797 | } |
| 1798 | |
| 1799 | 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] | 1800 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1801 | if (ret < 0) |
| 1802 | goto out; |
| 1803 | |
| 1804 | if (ow_inode == ino && gen == ino_gen) { |
| 1805 | ret = 0; |
| 1806 | goto out; |
| 1807 | } |
| 1808 | |
| 1809 | /* we know that it is or will be overwritten. check this now */ |
| 1810 | if (ow_inode < sctx->send_progress) |
| 1811 | ret = 1; |
| 1812 | else |
| 1813 | ret = 0; |
| 1814 | |
| 1815 | out: |
| 1816 | return ret; |
| 1817 | } |
| 1818 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1819 | /* |
| 1820 | * Same as did_overwrite_ref, but also checks if it is the first ref of an inode |
| 1821 | * that got overwritten. This is used by process_recorded_refs to determine |
| 1822 | * if it has to use the path as returned by get_cur_path or the orphan name. |
| 1823 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1824 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1825 | { |
| 1826 | int ret = 0; |
| 1827 | struct fs_path *name = NULL; |
| 1828 | u64 dir; |
| 1829 | u64 dir_gen; |
| 1830 | |
| 1831 | if (!sctx->parent_root) |
| 1832 | goto out; |
| 1833 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1834 | name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1835 | if (!name) |
| 1836 | return -ENOMEM; |
| 1837 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1838 | ret = get_first_ref(sctx->parent_root, ino, &dir, &dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1839 | if (ret < 0) |
| 1840 | goto out; |
| 1841 | |
| 1842 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, |
| 1843 | name->start, fs_path_len(name)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1844 | |
| 1845 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 1846 | fs_path_free(name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1847 | return ret; |
| 1848 | } |
| 1849 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1850 | /* |
| 1851 | * Insert a name cache entry. On 32bit kernels the radix tree index is 32bit, |
| 1852 | * so we need to do some special handling in case we have clashes. This function |
| 1853 | * 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] | 1854 | * In case of error, nce is kfreed. |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1855 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1856 | static int name_cache_insert(struct send_ctx *sctx, |
| 1857 | struct name_cache_entry *nce) |
| 1858 | { |
| 1859 | int ret = 0; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1860 | struct list_head *nce_head; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1861 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1862 | nce_head = radix_tree_lookup(&sctx->name_cache, |
| 1863 | (unsigned long)nce->ino); |
| 1864 | if (!nce_head) { |
| 1865 | nce_head = kmalloc(sizeof(*nce_head), GFP_NOFS); |
Tsutomu Itoh | cfa7a9c | 2012-12-17 06:38:51 +0000 | [diff] [blame] | 1866 | if (!nce_head) { |
| 1867 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1868 | return -ENOMEM; |
Tsutomu Itoh | cfa7a9c | 2012-12-17 06:38:51 +0000 | [diff] [blame] | 1869 | } |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1870 | INIT_LIST_HEAD(nce_head); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1871 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1872 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, nce_head); |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 1873 | if (ret < 0) { |
| 1874 | kfree(nce_head); |
| 1875 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1876 | return ret; |
Alexander Block | 5dc67d0 | 2012-08-01 12:07:43 +0200 | [diff] [blame] | 1877 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1878 | } |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1879 | list_add_tail(&nce->radix_list, nce_head); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1880 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 1881 | sctx->name_cache_size++; |
| 1882 | |
| 1883 | return ret; |
| 1884 | } |
| 1885 | |
| 1886 | static void name_cache_delete(struct send_ctx *sctx, |
| 1887 | struct name_cache_entry *nce) |
| 1888 | { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1889 | struct list_head *nce_head; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1890 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1891 | nce_head = radix_tree_lookup(&sctx->name_cache, |
| 1892 | (unsigned long)nce->ino); |
David Sterba | 57fb891 | 2014-02-03 19:24:40 +0100 | [diff] [blame^] | 1893 | if (!nce_head) { |
| 1894 | btrfs_err(sctx->send_root->fs_info, |
| 1895 | "name_cache_delete lookup failed ino %llu cache size %d, leaking memory", |
| 1896 | nce->ino, sctx->name_cache_size); |
| 1897 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1898 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1899 | list_del(&nce->radix_list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1900 | list_del(&nce->list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1901 | sctx->name_cache_size--; |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1902 | |
David Sterba | 57fb891 | 2014-02-03 19:24:40 +0100 | [diff] [blame^] | 1903 | /* |
| 1904 | * We may not get to the final release of nce_head if the lookup fails |
| 1905 | */ |
| 1906 | if (nce_head && list_empty(nce_head)) { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1907 | radix_tree_delete(&sctx->name_cache, (unsigned long)nce->ino); |
| 1908 | kfree(nce_head); |
| 1909 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, |
| 1913 | u64 ino, u64 gen) |
| 1914 | { |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1915 | struct list_head *nce_head; |
| 1916 | struct name_cache_entry *cur; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1917 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1918 | nce_head = radix_tree_lookup(&sctx->name_cache, (unsigned long)ino); |
| 1919 | if (!nce_head) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1920 | return NULL; |
| 1921 | |
Alexander Block | 7e0926f | 2012-07-28 14:20:58 +0200 | [diff] [blame] | 1922 | list_for_each_entry(cur, nce_head, radix_list) { |
| 1923 | if (cur->ino == ino && cur->gen == gen) |
| 1924 | return cur; |
| 1925 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1926 | return NULL; |
| 1927 | } |
| 1928 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1929 | /* |
| 1930 | * Removes the entry from the list and adds it back to the end. This marks the |
| 1931 | * entry as recently used so that name_cache_clean_unused does not remove it. |
| 1932 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1933 | static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
| 1934 | { |
| 1935 | list_del(&nce->list); |
| 1936 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 1937 | } |
| 1938 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1939 | /* |
| 1940 | * Remove some entries from the beginning of name_cache_list. |
| 1941 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1942 | static void name_cache_clean_unused(struct send_ctx *sctx) |
| 1943 | { |
| 1944 | struct name_cache_entry *nce; |
| 1945 | |
| 1946 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) |
| 1947 | return; |
| 1948 | |
| 1949 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { |
| 1950 | nce = list_entry(sctx->name_cache_list.next, |
| 1951 | struct name_cache_entry, list); |
| 1952 | name_cache_delete(sctx, nce); |
| 1953 | kfree(nce); |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | static void name_cache_free(struct send_ctx *sctx) |
| 1958 | { |
| 1959 | struct name_cache_entry *nce; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1960 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 1961 | while (!list_empty(&sctx->name_cache_list)) { |
| 1962 | nce = list_entry(sctx->name_cache_list.next, |
| 1963 | struct name_cache_entry, list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1964 | name_cache_delete(sctx, nce); |
Alexander Block | 17589bd | 2012-07-28 14:13:35 +0200 | [diff] [blame] | 1965 | kfree(nce); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1969 | /* |
| 1970 | * Used by get_cur_path for each ref up to the root. |
| 1971 | * Returns 0 if it succeeded. |
| 1972 | * Returns 1 if the inode is not existent or got overwritten. In that case, the |
| 1973 | * name is an orphan name. This instructs get_cur_path to stop iterating. If 1 |
| 1974 | * is returned, parent_ino/parent_gen are not guaranteed to be valid. |
| 1975 | * Returns <0 in case of error. |
| 1976 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1977 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
| 1978 | u64 ino, u64 gen, |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 1979 | int skip_name_cache, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1980 | u64 *parent_ino, |
| 1981 | u64 *parent_gen, |
| 1982 | struct fs_path *dest) |
| 1983 | { |
| 1984 | int ret; |
| 1985 | int nce_ret; |
| 1986 | struct btrfs_path *path = NULL; |
| 1987 | struct name_cache_entry *nce = NULL; |
| 1988 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 1989 | if (skip_name_cache) |
| 1990 | goto get_ref; |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 1991 | /* |
| 1992 | * First check if we already did a call to this function with the same |
| 1993 | * ino/gen. If yes, check if the cache entry is still up-to-date. If yes |
| 1994 | * return the cached result. |
| 1995 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1996 | nce = name_cache_search(sctx, ino, gen); |
| 1997 | if (nce) { |
| 1998 | if (ino < sctx->send_progress && nce->need_later_update) { |
| 1999 | name_cache_delete(sctx, nce); |
| 2000 | kfree(nce); |
| 2001 | nce = NULL; |
| 2002 | } else { |
| 2003 | name_cache_used(sctx, nce); |
| 2004 | *parent_ino = nce->parent_ino; |
| 2005 | *parent_gen = nce->parent_gen; |
| 2006 | ret = fs_path_add(dest, nce->name, nce->name_len); |
| 2007 | if (ret < 0) |
| 2008 | goto out; |
| 2009 | ret = nce->ret; |
| 2010 | goto out; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | path = alloc_path_for_send(); |
| 2015 | if (!path) |
| 2016 | return -ENOMEM; |
| 2017 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2018 | /* |
| 2019 | * If the inode is not existent yet, add the orphan name and return 1. |
| 2020 | * This should only happen for the parent dir that we determine in |
| 2021 | * __record_new_ref |
| 2022 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2023 | ret = is_inode_existent(sctx, ino, gen); |
| 2024 | if (ret < 0) |
| 2025 | goto out; |
| 2026 | |
| 2027 | if (!ret) { |
| 2028 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 2029 | if (ret < 0) |
| 2030 | goto out; |
| 2031 | ret = 1; |
| 2032 | goto out_cache; |
| 2033 | } |
| 2034 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2035 | get_ref: |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2036 | /* |
| 2037 | * Depending on whether the inode was already processed or not, use |
| 2038 | * send_root or parent_root for ref lookup. |
| 2039 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2040 | if (ino < sctx->send_progress && !skip_name_cache) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2041 | ret = get_first_ref(sctx->send_root, ino, |
| 2042 | parent_ino, parent_gen, dest); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2043 | else |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2044 | ret = get_first_ref(sctx->parent_root, ino, |
| 2045 | parent_ino, parent_gen, dest); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2046 | if (ret < 0) |
| 2047 | goto out; |
| 2048 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2049 | /* |
| 2050 | * Check if the ref was overwritten by an inode's ref that was processed |
| 2051 | * earlier. If yes, treat as orphan and return 1. |
| 2052 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2053 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
| 2054 | dest->start, dest->end - dest->start); |
| 2055 | if (ret < 0) |
| 2056 | goto out; |
| 2057 | if (ret) { |
| 2058 | fs_path_reset(dest); |
| 2059 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 2060 | if (ret < 0) |
| 2061 | goto out; |
| 2062 | ret = 1; |
| 2063 | } |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2064 | if (skip_name_cache) |
| 2065 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2066 | |
| 2067 | out_cache: |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2068 | /* |
| 2069 | * Store the result of the lookup in the name cache. |
| 2070 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2071 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS); |
| 2072 | if (!nce) { |
| 2073 | ret = -ENOMEM; |
| 2074 | goto out; |
| 2075 | } |
| 2076 | |
| 2077 | nce->ino = ino; |
| 2078 | nce->gen = gen; |
| 2079 | nce->parent_ino = *parent_ino; |
| 2080 | nce->parent_gen = *parent_gen; |
| 2081 | nce->name_len = fs_path_len(dest); |
| 2082 | nce->ret = ret; |
| 2083 | strcpy(nce->name, dest->start); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2084 | |
| 2085 | if (ino < sctx->send_progress) |
| 2086 | nce->need_later_update = 0; |
| 2087 | else |
| 2088 | nce->need_later_update = 1; |
| 2089 | |
| 2090 | nce_ret = name_cache_insert(sctx, nce); |
| 2091 | if (nce_ret < 0) |
| 2092 | ret = nce_ret; |
| 2093 | name_cache_clean_unused(sctx); |
| 2094 | |
| 2095 | out: |
| 2096 | btrfs_free_path(path); |
| 2097 | return ret; |
| 2098 | } |
| 2099 | |
| 2100 | /* |
| 2101 | * Magic happens here. This function returns the first ref to an inode as it |
| 2102 | * would look like while receiving the stream at this point in time. |
| 2103 | * We walk the path up to the root. For every inode in between, we check if it |
| 2104 | * was already processed/sent. If yes, we continue with the parent as found |
| 2105 | * in send_root. If not, we continue with the parent as found in parent_root. |
| 2106 | * If we encounter an inode that was deleted at this point in time, we use the |
| 2107 | * inodes "orphan" name instead of the real name and stop. Same with new inodes |
| 2108 | * that were not created yet and overwritten inodes/refs. |
| 2109 | * |
| 2110 | * When do we have have orphan inodes: |
| 2111 | * 1. When an inode is freshly created and thus no valid refs are available yet |
| 2112 | * 2. When a directory lost all it's refs (deleted) but still has dir items |
| 2113 | * inside which were not processed yet (pending for move/delete). If anyone |
| 2114 | * tried to get the path to the dir items, it would get a path inside that |
| 2115 | * orphan directory. |
| 2116 | * 3. When an inode is moved around or gets new links, it may overwrite the ref |
| 2117 | * of an unprocessed inode. If in that case the first ref would be |
| 2118 | * overwritten, the overwritten inode gets "orphanized". Later when we |
| 2119 | * process this overwritten inode, it is restored at a new place by moving |
| 2120 | * the orphan inode. |
| 2121 | * |
| 2122 | * sctx->send_progress tells this function at which point in time receiving |
| 2123 | * would be. |
| 2124 | */ |
| 2125 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2126 | struct fs_path *dest) |
| 2127 | { |
| 2128 | int ret = 0; |
| 2129 | struct fs_path *name = NULL; |
| 2130 | u64 parent_inode = 0; |
| 2131 | u64 parent_gen = 0; |
| 2132 | int stop = 0; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2133 | int skip_name_cache = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2134 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2135 | name = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2136 | if (!name) { |
| 2137 | ret = -ENOMEM; |
| 2138 | goto out; |
| 2139 | } |
| 2140 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2141 | if (is_waiting_for_move(sctx, ino)) |
| 2142 | skip_name_cache = 1; |
| 2143 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2144 | dest->reversed = 1; |
| 2145 | fs_path_reset(dest); |
| 2146 | |
| 2147 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 2148 | fs_path_reset(name); |
| 2149 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2150 | ret = __get_cur_name_and_parent(sctx, ino, gen, skip_name_cache, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2151 | &parent_inode, &parent_gen, name); |
| 2152 | if (ret < 0) |
| 2153 | goto out; |
| 2154 | if (ret) |
| 2155 | stop = 1; |
| 2156 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2157 | if (!skip_name_cache && |
Filipe David Borba Manana | 03cb4fb | 2014-02-01 02:00:15 +0000 | [diff] [blame] | 2158 | is_waiting_for_move(sctx, parent_inode)) |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2159 | skip_name_cache = 1; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2160 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2161 | ret = fs_path_add_path(dest, name); |
| 2162 | if (ret < 0) |
| 2163 | goto out; |
| 2164 | |
| 2165 | ino = parent_inode; |
| 2166 | gen = parent_gen; |
| 2167 | } |
| 2168 | |
| 2169 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2170 | fs_path_free(name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2171 | if (!ret) |
| 2172 | fs_path_unreverse(dest); |
| 2173 | return ret; |
| 2174 | } |
| 2175 | |
| 2176 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2177 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace |
| 2178 | */ |
| 2179 | static int send_subvol_begin(struct send_ctx *sctx) |
| 2180 | { |
| 2181 | int ret; |
| 2182 | struct btrfs_root *send_root = sctx->send_root; |
| 2183 | struct btrfs_root *parent_root = sctx->parent_root; |
| 2184 | struct btrfs_path *path; |
| 2185 | struct btrfs_key key; |
| 2186 | struct btrfs_root_ref *ref; |
| 2187 | struct extent_buffer *leaf; |
| 2188 | char *name = NULL; |
| 2189 | int namelen; |
| 2190 | |
Wang Shilong | ffcfaf8 | 2014-01-15 00:26:43 +0800 | [diff] [blame] | 2191 | path = btrfs_alloc_path(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2192 | if (!path) |
| 2193 | return -ENOMEM; |
| 2194 | |
| 2195 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS); |
| 2196 | if (!name) { |
| 2197 | btrfs_free_path(path); |
| 2198 | return -ENOMEM; |
| 2199 | } |
| 2200 | |
| 2201 | key.objectid = send_root->objectid; |
| 2202 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2203 | key.offset = 0; |
| 2204 | |
| 2205 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, |
| 2206 | &key, path, 1, 0); |
| 2207 | if (ret < 0) |
| 2208 | goto out; |
| 2209 | if (ret) { |
| 2210 | ret = -ENOENT; |
| 2211 | goto out; |
| 2212 | } |
| 2213 | |
| 2214 | leaf = path->nodes[0]; |
| 2215 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2216 | if (key.type != BTRFS_ROOT_BACKREF_KEY || |
| 2217 | key.objectid != send_root->objectid) { |
| 2218 | ret = -ENOENT; |
| 2219 | goto out; |
| 2220 | } |
| 2221 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 2222 | namelen = btrfs_root_ref_name_len(leaf, ref); |
| 2223 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); |
| 2224 | btrfs_release_path(path); |
| 2225 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2226 | if (parent_root) { |
| 2227 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); |
| 2228 | if (ret < 0) |
| 2229 | goto out; |
| 2230 | } else { |
| 2231 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); |
| 2232 | if (ret < 0) |
| 2233 | goto out; |
| 2234 | } |
| 2235 | |
| 2236 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); |
| 2237 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| 2238 | sctx->send_root->root_item.uuid); |
| 2239 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 2240 | le64_to_cpu(sctx->send_root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2241 | if (parent_root) { |
| 2242 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 2243 | sctx->parent_root->root_item.uuid); |
| 2244 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 2245 | le64_to_cpu(sctx->parent_root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | ret = send_cmd(sctx); |
| 2249 | |
| 2250 | tlv_put_failure: |
| 2251 | out: |
| 2252 | btrfs_free_path(path); |
| 2253 | kfree(name); |
| 2254 | return ret; |
| 2255 | } |
| 2256 | |
| 2257 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) |
| 2258 | { |
| 2259 | int ret = 0; |
| 2260 | struct fs_path *p; |
| 2261 | |
| 2262 | verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size); |
| 2263 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2264 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2265 | if (!p) |
| 2266 | return -ENOMEM; |
| 2267 | |
| 2268 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); |
| 2269 | if (ret < 0) |
| 2270 | goto out; |
| 2271 | |
| 2272 | ret = get_cur_path(sctx, ino, gen, p); |
| 2273 | if (ret < 0) |
| 2274 | goto out; |
| 2275 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2276 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); |
| 2277 | |
| 2278 | ret = send_cmd(sctx); |
| 2279 | |
| 2280 | tlv_put_failure: |
| 2281 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2282 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2283 | return ret; |
| 2284 | } |
| 2285 | |
| 2286 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) |
| 2287 | { |
| 2288 | int ret = 0; |
| 2289 | struct fs_path *p; |
| 2290 | |
| 2291 | verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode); |
| 2292 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2293 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2294 | if (!p) |
| 2295 | return -ENOMEM; |
| 2296 | |
| 2297 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); |
| 2298 | if (ret < 0) |
| 2299 | goto out; |
| 2300 | |
| 2301 | ret = get_cur_path(sctx, ino, gen, p); |
| 2302 | if (ret < 0) |
| 2303 | goto out; |
| 2304 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2305 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); |
| 2306 | |
| 2307 | ret = send_cmd(sctx); |
| 2308 | |
| 2309 | tlv_put_failure: |
| 2310 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2311 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2312 | return ret; |
| 2313 | } |
| 2314 | |
| 2315 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) |
| 2316 | { |
| 2317 | int ret = 0; |
| 2318 | struct fs_path *p; |
| 2319 | |
| 2320 | verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid); |
| 2321 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2322 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2323 | if (!p) |
| 2324 | return -ENOMEM; |
| 2325 | |
| 2326 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); |
| 2327 | if (ret < 0) |
| 2328 | goto out; |
| 2329 | |
| 2330 | ret = get_cur_path(sctx, ino, gen, p); |
| 2331 | if (ret < 0) |
| 2332 | goto out; |
| 2333 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2334 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); |
| 2335 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); |
| 2336 | |
| 2337 | ret = send_cmd(sctx); |
| 2338 | |
| 2339 | tlv_put_failure: |
| 2340 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2341 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2342 | return ret; |
| 2343 | } |
| 2344 | |
| 2345 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) |
| 2346 | { |
| 2347 | int ret = 0; |
| 2348 | struct fs_path *p = NULL; |
| 2349 | struct btrfs_inode_item *ii; |
| 2350 | struct btrfs_path *path = NULL; |
| 2351 | struct extent_buffer *eb; |
| 2352 | struct btrfs_key key; |
| 2353 | int slot; |
| 2354 | |
| 2355 | verbose_printk("btrfs: send_utimes %llu\n", ino); |
| 2356 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2357 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2358 | if (!p) |
| 2359 | return -ENOMEM; |
| 2360 | |
| 2361 | path = alloc_path_for_send(); |
| 2362 | if (!path) { |
| 2363 | ret = -ENOMEM; |
| 2364 | goto out; |
| 2365 | } |
| 2366 | |
| 2367 | key.objectid = ino; |
| 2368 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2369 | key.offset = 0; |
| 2370 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
| 2371 | if (ret < 0) |
| 2372 | goto out; |
| 2373 | |
| 2374 | eb = path->nodes[0]; |
| 2375 | slot = path->slots[0]; |
| 2376 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 2377 | |
| 2378 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); |
| 2379 | if (ret < 0) |
| 2380 | goto out; |
| 2381 | |
| 2382 | ret = get_cur_path(sctx, ino, gen, p); |
| 2383 | if (ret < 0) |
| 2384 | goto out; |
| 2385 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2386 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, |
| 2387 | btrfs_inode_atime(ii)); |
| 2388 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, |
| 2389 | btrfs_inode_mtime(ii)); |
| 2390 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, |
| 2391 | btrfs_inode_ctime(ii)); |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2392 | /* TODO Add otime support when the otime patches get into upstream */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2393 | |
| 2394 | ret = send_cmd(sctx); |
| 2395 | |
| 2396 | tlv_put_failure: |
| 2397 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2398 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2399 | btrfs_free_path(path); |
| 2400 | return ret; |
| 2401 | } |
| 2402 | |
| 2403 | /* |
| 2404 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have |
| 2405 | * a valid path yet because we did not process the refs yet. So, the inode |
| 2406 | * is created as orphan. |
| 2407 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2408 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2409 | { |
| 2410 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2411 | struct fs_path *p; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2412 | int cmd; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2413 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2414 | u64 mode; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2415 | u64 rdev; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2416 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2417 | verbose_printk("btrfs: send_create_inode %llu\n", ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2418 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2419 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2420 | if (!p) |
| 2421 | return -ENOMEM; |
| 2422 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2423 | ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL, |
| 2424 | NULL, &rdev); |
| 2425 | if (ret < 0) |
| 2426 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2427 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2428 | if (S_ISREG(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2429 | cmd = BTRFS_SEND_C_MKFILE; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2430 | } else if (S_ISDIR(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2431 | cmd = BTRFS_SEND_C_MKDIR; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2432 | } else if (S_ISLNK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2433 | cmd = BTRFS_SEND_C_SYMLINK; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2434 | } else if (S_ISCHR(mode) || S_ISBLK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2435 | cmd = BTRFS_SEND_C_MKNOD; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2436 | } else if (S_ISFIFO(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2437 | cmd = BTRFS_SEND_C_MKFIFO; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2438 | } else if (S_ISSOCK(mode)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2439 | cmd = BTRFS_SEND_C_MKSOCK; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2440 | } else { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2441 | printk(KERN_WARNING "btrfs: unexpected inode type %o", |
| 2442 | (int)(mode & S_IFMT)); |
| 2443 | ret = -ENOTSUPP; |
| 2444 | goto out; |
| 2445 | } |
| 2446 | |
| 2447 | ret = begin_cmd(sctx, cmd); |
| 2448 | if (ret < 0) |
| 2449 | goto out; |
| 2450 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2451 | ret = gen_unique_name(sctx, ino, gen, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2452 | if (ret < 0) |
| 2453 | goto out; |
| 2454 | |
| 2455 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2456 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2457 | |
| 2458 | if (S_ISLNK(mode)) { |
| 2459 | fs_path_reset(p); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2460 | ret = read_symlink(sctx->send_root, ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2461 | if (ret < 0) |
| 2462 | goto out; |
| 2463 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); |
| 2464 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || |
| 2465 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
Arne Jansen | d79e504 | 2012-10-15 18:28:46 +0000 | [diff] [blame] | 2466 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, new_encode_dev(rdev)); |
| 2467 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | ret = send_cmd(sctx); |
| 2471 | if (ret < 0) |
| 2472 | goto out; |
| 2473 | |
| 2474 | |
| 2475 | tlv_put_failure: |
| 2476 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2477 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2478 | return ret; |
| 2479 | } |
| 2480 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2481 | /* |
| 2482 | * We need some special handling for inodes that get processed before the parent |
| 2483 | * directory got created. See process_recorded_refs for details. |
| 2484 | * This function does the check if we already created the dir out of order. |
| 2485 | */ |
| 2486 | static int did_create_dir(struct send_ctx *sctx, u64 dir) |
| 2487 | { |
| 2488 | int ret = 0; |
| 2489 | struct btrfs_path *path = NULL; |
| 2490 | struct btrfs_key key; |
| 2491 | struct btrfs_key found_key; |
| 2492 | struct btrfs_key di_key; |
| 2493 | struct extent_buffer *eb; |
| 2494 | struct btrfs_dir_item *di; |
| 2495 | int slot; |
| 2496 | |
| 2497 | path = alloc_path_for_send(); |
| 2498 | if (!path) { |
| 2499 | ret = -ENOMEM; |
| 2500 | goto out; |
| 2501 | } |
| 2502 | |
| 2503 | key.objectid = dir; |
| 2504 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2505 | key.offset = 0; |
| 2506 | while (1) { |
| 2507 | ret = btrfs_search_slot_for_read(sctx->send_root, &key, path, |
| 2508 | 1, 0); |
| 2509 | if (ret < 0) |
| 2510 | goto out; |
| 2511 | if (!ret) { |
| 2512 | eb = path->nodes[0]; |
| 2513 | slot = path->slots[0]; |
| 2514 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 2515 | } |
| 2516 | if (ret || found_key.objectid != key.objectid || |
| 2517 | found_key.type != key.type) { |
| 2518 | ret = 0; |
| 2519 | goto out; |
| 2520 | } |
| 2521 | |
| 2522 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 2523 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 2524 | |
Josef Bacik | a052541 | 2013-08-12 10:56:14 -0400 | [diff] [blame] | 2525 | if (di_key.type != BTRFS_ROOT_ITEM_KEY && |
| 2526 | di_key.objectid < sctx->send_progress) { |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2527 | ret = 1; |
| 2528 | goto out; |
| 2529 | } |
| 2530 | |
| 2531 | key.offset = found_key.offset + 1; |
| 2532 | btrfs_release_path(path); |
| 2533 | } |
| 2534 | |
| 2535 | out: |
| 2536 | btrfs_free_path(path); |
| 2537 | return ret; |
| 2538 | } |
| 2539 | |
| 2540 | /* |
| 2541 | * Only creates the inode if it is: |
| 2542 | * 1. Not a directory |
| 2543 | * 2. Or a directory which was not created already due to out of order |
| 2544 | * directories. See did_create_dir and process_recorded_refs for details. |
| 2545 | */ |
| 2546 | static int send_create_inode_if_needed(struct send_ctx *sctx) |
| 2547 | { |
| 2548 | int ret; |
| 2549 | |
| 2550 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 2551 | ret = did_create_dir(sctx, sctx->cur_ino); |
| 2552 | if (ret < 0) |
| 2553 | goto out; |
| 2554 | if (ret) { |
| 2555 | ret = 0; |
| 2556 | goto out; |
| 2557 | } |
| 2558 | } |
| 2559 | |
| 2560 | ret = send_create_inode(sctx, sctx->cur_ino); |
| 2561 | if (ret < 0) |
| 2562 | goto out; |
| 2563 | |
| 2564 | out: |
| 2565 | return ret; |
| 2566 | } |
| 2567 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2568 | struct recorded_ref { |
| 2569 | struct list_head list; |
| 2570 | char *dir_path; |
| 2571 | char *name; |
| 2572 | struct fs_path *full_path; |
| 2573 | u64 dir; |
| 2574 | u64 dir_gen; |
| 2575 | int dir_path_len; |
| 2576 | int name_len; |
| 2577 | }; |
| 2578 | |
| 2579 | /* |
| 2580 | * We need to process new refs before deleted refs, but compare_tree gives us |
| 2581 | * everything mixed. So we first record all refs and later process them. |
| 2582 | * This function is a helper to record one ref. |
| 2583 | */ |
| 2584 | static int record_ref(struct list_head *head, u64 dir, |
| 2585 | u64 dir_gen, struct fs_path *path) |
| 2586 | { |
| 2587 | struct recorded_ref *ref; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2588 | |
| 2589 | ref = kmalloc(sizeof(*ref), GFP_NOFS); |
| 2590 | if (!ref) |
| 2591 | return -ENOMEM; |
| 2592 | |
| 2593 | ref->dir = dir; |
| 2594 | ref->dir_gen = dir_gen; |
| 2595 | ref->full_path = path; |
| 2596 | |
Andy Shevchenko | ed84885 | 2013-08-21 10:32:13 +0300 | [diff] [blame] | 2597 | ref->name = (char *)kbasename(ref->full_path->start); |
| 2598 | ref->name_len = ref->full_path->end - ref->name; |
| 2599 | ref->dir_path = ref->full_path->start; |
| 2600 | if (ref->name == ref->full_path->start) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2601 | ref->dir_path_len = 0; |
Andy Shevchenko | ed84885 | 2013-08-21 10:32:13 +0300 | [diff] [blame] | 2602 | else |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2603 | ref->dir_path_len = ref->full_path->end - |
| 2604 | ref->full_path->start - 1 - ref->name_len; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2605 | |
| 2606 | list_add_tail(&ref->list, head); |
| 2607 | return 0; |
| 2608 | } |
| 2609 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 2610 | static int dup_ref(struct recorded_ref *ref, struct list_head *list) |
| 2611 | { |
| 2612 | struct recorded_ref *new; |
| 2613 | |
| 2614 | new = kmalloc(sizeof(*ref), GFP_NOFS); |
| 2615 | if (!new) |
| 2616 | return -ENOMEM; |
| 2617 | |
| 2618 | new->dir = ref->dir; |
| 2619 | new->dir_gen = ref->dir_gen; |
| 2620 | new->full_path = NULL; |
| 2621 | INIT_LIST_HEAD(&new->list); |
| 2622 | list_add_tail(&new->list, list); |
| 2623 | return 0; |
| 2624 | } |
| 2625 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2626 | static void __free_recorded_refs(struct list_head *head) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2627 | { |
| 2628 | struct recorded_ref *cur; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2629 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2630 | while (!list_empty(head)) { |
| 2631 | cur = list_entry(head->next, struct recorded_ref, list); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2632 | fs_path_free(cur->full_path); |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 2633 | list_del(&cur->list); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2634 | kfree(cur); |
| 2635 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static void free_recorded_refs(struct send_ctx *sctx) |
| 2639 | { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2640 | __free_recorded_refs(&sctx->new_refs); |
| 2641 | __free_recorded_refs(&sctx->deleted_refs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 2645 | * 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] | 2646 | * ref of an unprocessed inode gets overwritten and for all non empty |
| 2647 | * directories. |
| 2648 | */ |
| 2649 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2650 | struct fs_path *path) |
| 2651 | { |
| 2652 | int ret; |
| 2653 | struct fs_path *orphan; |
| 2654 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2655 | orphan = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2656 | if (!orphan) |
| 2657 | return -ENOMEM; |
| 2658 | |
| 2659 | ret = gen_unique_name(sctx, ino, gen, orphan); |
| 2660 | if (ret < 0) |
| 2661 | goto out; |
| 2662 | |
| 2663 | ret = send_rename(sctx, path, orphan); |
| 2664 | |
| 2665 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 2666 | fs_path_free(orphan); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2667 | return ret; |
| 2668 | } |
| 2669 | |
| 2670 | /* |
| 2671 | * Returns 1 if a directory can be removed at this point in time. |
| 2672 | * We check this by iterating all dir items and checking if the inode behind |
| 2673 | * the dir item was already processed. |
| 2674 | */ |
| 2675 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress) |
| 2676 | { |
| 2677 | int ret = 0; |
| 2678 | struct btrfs_root *root = sctx->parent_root; |
| 2679 | struct btrfs_path *path; |
| 2680 | struct btrfs_key key; |
| 2681 | struct btrfs_key found_key; |
| 2682 | struct btrfs_key loc; |
| 2683 | struct btrfs_dir_item *di; |
| 2684 | |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 2685 | /* |
| 2686 | * Don't try to rmdir the top/root subvolume dir. |
| 2687 | */ |
| 2688 | if (dir == BTRFS_FIRST_FREE_OBJECTID) |
| 2689 | return 0; |
| 2690 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2691 | path = alloc_path_for_send(); |
| 2692 | if (!path) |
| 2693 | return -ENOMEM; |
| 2694 | |
| 2695 | key.objectid = dir; |
| 2696 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2697 | key.offset = 0; |
| 2698 | |
| 2699 | while (1) { |
| 2700 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 2701 | if (ret < 0) |
| 2702 | goto out; |
| 2703 | if (!ret) { |
| 2704 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2705 | path->slots[0]); |
| 2706 | } |
| 2707 | if (ret || found_key.objectid != key.objectid || |
| 2708 | found_key.type != key.type) { |
| 2709 | break; |
| 2710 | } |
| 2711 | |
| 2712 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2713 | struct btrfs_dir_item); |
| 2714 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); |
| 2715 | |
| 2716 | if (loc.objectid > send_progress) { |
| 2717 | ret = 0; |
| 2718 | goto out; |
| 2719 | } |
| 2720 | |
| 2721 | btrfs_release_path(path); |
| 2722 | key.offset = found_key.offset + 1; |
| 2723 | } |
| 2724 | |
| 2725 | ret = 1; |
| 2726 | |
| 2727 | out: |
| 2728 | btrfs_free_path(path); |
| 2729 | return ret; |
| 2730 | } |
| 2731 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2732 | static int is_waiting_for_move(struct send_ctx *sctx, u64 ino) |
| 2733 | { |
| 2734 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; |
| 2735 | struct waiting_dir_move *entry; |
| 2736 | |
| 2737 | while (n) { |
| 2738 | entry = rb_entry(n, struct waiting_dir_move, node); |
| 2739 | if (ino < entry->ino) |
| 2740 | n = n->rb_left; |
| 2741 | else if (ino > entry->ino) |
| 2742 | n = n->rb_right; |
| 2743 | else |
| 2744 | return 1; |
| 2745 | } |
| 2746 | return 0; |
| 2747 | } |
| 2748 | |
| 2749 | static int add_waiting_dir_move(struct send_ctx *sctx, u64 ino) |
| 2750 | { |
| 2751 | struct rb_node **p = &sctx->waiting_dir_moves.rb_node; |
| 2752 | struct rb_node *parent = NULL; |
| 2753 | struct waiting_dir_move *entry, *dm; |
| 2754 | |
| 2755 | dm = kmalloc(sizeof(*dm), GFP_NOFS); |
| 2756 | if (!dm) |
| 2757 | return -ENOMEM; |
| 2758 | dm->ino = ino; |
| 2759 | |
| 2760 | while (*p) { |
| 2761 | parent = *p; |
| 2762 | entry = rb_entry(parent, struct waiting_dir_move, node); |
| 2763 | if (ino < entry->ino) { |
| 2764 | p = &(*p)->rb_left; |
| 2765 | } else if (ino > entry->ino) { |
| 2766 | p = &(*p)->rb_right; |
| 2767 | } else { |
| 2768 | kfree(dm); |
| 2769 | return -EEXIST; |
| 2770 | } |
| 2771 | } |
| 2772 | |
| 2773 | rb_link_node(&dm->node, parent, p); |
| 2774 | rb_insert_color(&dm->node, &sctx->waiting_dir_moves); |
| 2775 | return 0; |
| 2776 | } |
| 2777 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2778 | static int del_waiting_dir_move(struct send_ctx *sctx, u64 ino) |
| 2779 | { |
| 2780 | struct rb_node *n = sctx->waiting_dir_moves.rb_node; |
| 2781 | struct waiting_dir_move *entry; |
| 2782 | |
| 2783 | while (n) { |
| 2784 | entry = rb_entry(n, struct waiting_dir_move, node); |
| 2785 | if (ino < entry->ino) { |
| 2786 | n = n->rb_left; |
| 2787 | } else if (ino > entry->ino) { |
| 2788 | n = n->rb_right; |
| 2789 | } else { |
| 2790 | rb_erase(&entry->node, &sctx->waiting_dir_moves); |
| 2791 | kfree(entry); |
| 2792 | return 0; |
| 2793 | } |
| 2794 | } |
| 2795 | return -ENOENT; |
| 2796 | } |
| 2797 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2798 | static int add_pending_dir_move(struct send_ctx *sctx, u64 parent_ino) |
| 2799 | { |
| 2800 | struct rb_node **p = &sctx->pending_dir_moves.rb_node; |
| 2801 | struct rb_node *parent = NULL; |
| 2802 | struct pending_dir_move *entry, *pm; |
| 2803 | struct recorded_ref *cur; |
| 2804 | int exists = 0; |
| 2805 | int ret; |
| 2806 | |
| 2807 | pm = kmalloc(sizeof(*pm), GFP_NOFS); |
| 2808 | if (!pm) |
| 2809 | return -ENOMEM; |
| 2810 | pm->parent_ino = parent_ino; |
| 2811 | pm->ino = sctx->cur_ino; |
| 2812 | pm->gen = sctx->cur_inode_gen; |
| 2813 | INIT_LIST_HEAD(&pm->list); |
| 2814 | INIT_LIST_HEAD(&pm->update_refs); |
| 2815 | RB_CLEAR_NODE(&pm->node); |
| 2816 | |
| 2817 | while (*p) { |
| 2818 | parent = *p; |
| 2819 | entry = rb_entry(parent, struct pending_dir_move, node); |
| 2820 | if (parent_ino < entry->parent_ino) { |
| 2821 | p = &(*p)->rb_left; |
| 2822 | } else if (parent_ino > entry->parent_ino) { |
| 2823 | p = &(*p)->rb_right; |
| 2824 | } else { |
| 2825 | exists = 1; |
| 2826 | break; |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
| 2831 | ret = dup_ref(cur, &pm->update_refs); |
| 2832 | if (ret < 0) |
| 2833 | goto out; |
| 2834 | } |
| 2835 | list_for_each_entry(cur, &sctx->new_refs, list) { |
| 2836 | ret = dup_ref(cur, &pm->update_refs); |
| 2837 | if (ret < 0) |
| 2838 | goto out; |
| 2839 | } |
| 2840 | |
| 2841 | ret = add_waiting_dir_move(sctx, pm->ino); |
| 2842 | if (ret) |
| 2843 | goto out; |
| 2844 | |
| 2845 | if (exists) { |
| 2846 | list_add_tail(&pm->list, &entry->list); |
| 2847 | } else { |
| 2848 | rb_link_node(&pm->node, parent, p); |
| 2849 | rb_insert_color(&pm->node, &sctx->pending_dir_moves); |
| 2850 | } |
| 2851 | ret = 0; |
| 2852 | out: |
| 2853 | if (ret) { |
| 2854 | __free_recorded_refs(&pm->update_refs); |
| 2855 | kfree(pm); |
| 2856 | } |
| 2857 | return ret; |
| 2858 | } |
| 2859 | |
| 2860 | static struct pending_dir_move *get_pending_dir_moves(struct send_ctx *sctx, |
| 2861 | u64 parent_ino) |
| 2862 | { |
| 2863 | struct rb_node *n = sctx->pending_dir_moves.rb_node; |
| 2864 | struct pending_dir_move *entry; |
| 2865 | |
| 2866 | while (n) { |
| 2867 | entry = rb_entry(n, struct pending_dir_move, node); |
| 2868 | if (parent_ino < entry->parent_ino) |
| 2869 | n = n->rb_left; |
| 2870 | else if (parent_ino > entry->parent_ino) |
| 2871 | n = n->rb_right; |
| 2872 | else |
| 2873 | return entry; |
| 2874 | } |
| 2875 | return NULL; |
| 2876 | } |
| 2877 | |
| 2878 | static int apply_dir_move(struct send_ctx *sctx, struct pending_dir_move *pm) |
| 2879 | { |
| 2880 | struct fs_path *from_path = NULL; |
| 2881 | struct fs_path *to_path = NULL; |
| 2882 | u64 orig_progress = sctx->send_progress; |
| 2883 | struct recorded_ref *cur; |
| 2884 | int ret; |
| 2885 | |
| 2886 | from_path = fs_path_alloc(); |
| 2887 | if (!from_path) |
| 2888 | return -ENOMEM; |
| 2889 | |
| 2890 | sctx->send_progress = pm->ino; |
| 2891 | ret = get_cur_path(sctx, pm->ino, pm->gen, from_path); |
| 2892 | if (ret < 0) |
| 2893 | goto out; |
| 2894 | |
| 2895 | to_path = fs_path_alloc(); |
| 2896 | if (!to_path) { |
| 2897 | ret = -ENOMEM; |
| 2898 | goto out; |
| 2899 | } |
| 2900 | |
| 2901 | sctx->send_progress = sctx->cur_ino + 1; |
Josef Bacik | 6cc98d9 | 2014-02-05 16:19:21 -0500 | [diff] [blame] | 2902 | ret = del_waiting_dir_move(sctx, pm->ino); |
| 2903 | ASSERT(ret == 0); |
| 2904 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 2905 | ret = get_cur_path(sctx, pm->ino, pm->gen, to_path); |
| 2906 | if (ret < 0) |
| 2907 | goto out; |
| 2908 | |
| 2909 | ret = send_rename(sctx, from_path, to_path); |
| 2910 | if (ret < 0) |
| 2911 | goto out; |
| 2912 | |
| 2913 | ret = send_utimes(sctx, pm->ino, pm->gen); |
| 2914 | if (ret < 0) |
| 2915 | goto out; |
| 2916 | |
| 2917 | /* |
| 2918 | * After rename/move, need to update the utimes of both new parent(s) |
| 2919 | * and old parent(s). |
| 2920 | */ |
| 2921 | list_for_each_entry(cur, &pm->update_refs, list) { |
| 2922 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
| 2923 | if (ret < 0) |
| 2924 | goto out; |
| 2925 | } |
| 2926 | |
| 2927 | out: |
| 2928 | fs_path_free(from_path); |
| 2929 | fs_path_free(to_path); |
| 2930 | sctx->send_progress = orig_progress; |
| 2931 | |
| 2932 | return ret; |
| 2933 | } |
| 2934 | |
| 2935 | static void free_pending_move(struct send_ctx *sctx, struct pending_dir_move *m) |
| 2936 | { |
| 2937 | if (!list_empty(&m->list)) |
| 2938 | list_del(&m->list); |
| 2939 | if (!RB_EMPTY_NODE(&m->node)) |
| 2940 | rb_erase(&m->node, &sctx->pending_dir_moves); |
| 2941 | __free_recorded_refs(&m->update_refs); |
| 2942 | kfree(m); |
| 2943 | } |
| 2944 | |
| 2945 | static void tail_append_pending_moves(struct pending_dir_move *moves, |
| 2946 | struct list_head *stack) |
| 2947 | { |
| 2948 | if (list_empty(&moves->list)) { |
| 2949 | list_add_tail(&moves->list, stack); |
| 2950 | } else { |
| 2951 | LIST_HEAD(list); |
| 2952 | list_splice_init(&moves->list, &list); |
| 2953 | list_add_tail(&moves->list, stack); |
| 2954 | list_splice_tail(&list, stack); |
| 2955 | } |
| 2956 | } |
| 2957 | |
| 2958 | static int apply_children_dir_moves(struct send_ctx *sctx) |
| 2959 | { |
| 2960 | struct pending_dir_move *pm; |
| 2961 | struct list_head stack; |
| 2962 | u64 parent_ino = sctx->cur_ino; |
| 2963 | int ret = 0; |
| 2964 | |
| 2965 | pm = get_pending_dir_moves(sctx, parent_ino); |
| 2966 | if (!pm) |
| 2967 | return 0; |
| 2968 | |
| 2969 | INIT_LIST_HEAD(&stack); |
| 2970 | tail_append_pending_moves(pm, &stack); |
| 2971 | |
| 2972 | while (!list_empty(&stack)) { |
| 2973 | pm = list_first_entry(&stack, struct pending_dir_move, list); |
| 2974 | parent_ino = pm->ino; |
| 2975 | ret = apply_dir_move(sctx, pm); |
| 2976 | free_pending_move(sctx, pm); |
| 2977 | if (ret) |
| 2978 | goto out; |
| 2979 | pm = get_pending_dir_moves(sctx, parent_ino); |
| 2980 | if (pm) |
| 2981 | tail_append_pending_moves(pm, &stack); |
| 2982 | } |
| 2983 | return 0; |
| 2984 | |
| 2985 | out: |
| 2986 | while (!list_empty(&stack)) { |
| 2987 | pm = list_first_entry(&stack, struct pending_dir_move, list); |
| 2988 | free_pending_move(sctx, pm); |
| 2989 | } |
| 2990 | return ret; |
| 2991 | } |
| 2992 | |
| 2993 | static int wait_for_parent_move(struct send_ctx *sctx, |
| 2994 | struct recorded_ref *parent_ref) |
| 2995 | { |
| 2996 | int ret; |
| 2997 | u64 ino = parent_ref->dir; |
| 2998 | u64 parent_ino_before, parent_ino_after; |
| 2999 | u64 new_gen, old_gen; |
| 3000 | struct fs_path *path_before = NULL; |
| 3001 | struct fs_path *path_after = NULL; |
| 3002 | int len1, len2; |
| 3003 | |
| 3004 | if (parent_ref->dir <= sctx->cur_ino) |
| 3005 | return 0; |
| 3006 | |
| 3007 | if (is_waiting_for_move(sctx, ino)) |
| 3008 | return 1; |
| 3009 | |
| 3010 | ret = get_inode_info(sctx->parent_root, ino, NULL, &old_gen, |
| 3011 | NULL, NULL, NULL, NULL); |
| 3012 | if (ret == -ENOENT) |
| 3013 | return 0; |
| 3014 | else if (ret < 0) |
| 3015 | return ret; |
| 3016 | |
| 3017 | ret = get_inode_info(sctx->send_root, ino, NULL, &new_gen, |
| 3018 | NULL, NULL, NULL, NULL); |
| 3019 | if (ret < 0) |
| 3020 | return ret; |
| 3021 | |
| 3022 | if (new_gen != old_gen) |
| 3023 | return 0; |
| 3024 | |
| 3025 | path_before = fs_path_alloc(); |
| 3026 | if (!path_before) |
| 3027 | return -ENOMEM; |
| 3028 | |
| 3029 | ret = get_first_ref(sctx->parent_root, ino, &parent_ino_before, |
| 3030 | NULL, path_before); |
| 3031 | if (ret == -ENOENT) { |
| 3032 | ret = 0; |
| 3033 | goto out; |
| 3034 | } else if (ret < 0) { |
| 3035 | goto out; |
| 3036 | } |
| 3037 | |
| 3038 | path_after = fs_path_alloc(); |
| 3039 | if (!path_after) { |
| 3040 | ret = -ENOMEM; |
| 3041 | goto out; |
| 3042 | } |
| 3043 | |
| 3044 | ret = get_first_ref(sctx->send_root, ino, &parent_ino_after, |
| 3045 | NULL, path_after); |
| 3046 | if (ret == -ENOENT) { |
| 3047 | ret = 0; |
| 3048 | goto out; |
| 3049 | } else if (ret < 0) { |
| 3050 | goto out; |
| 3051 | } |
| 3052 | |
| 3053 | len1 = fs_path_len(path_before); |
| 3054 | len2 = fs_path_len(path_after); |
Filipe David Borba Manana | 5ed7f9f | 2014-02-01 02:02:16 +0000 | [diff] [blame] | 3055 | if (parent_ino_before != parent_ino_after || len1 != len2 || |
| 3056 | memcmp(path_before->start, path_after->start, len1)) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3057 | ret = 1; |
| 3058 | goto out; |
| 3059 | } |
| 3060 | ret = 0; |
| 3061 | |
| 3062 | out: |
| 3063 | fs_path_free(path_before); |
| 3064 | fs_path_free(path_after); |
| 3065 | |
| 3066 | return ret; |
| 3067 | } |
| 3068 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3069 | /* |
| 3070 | * This does all the move/link/unlink/rmdir magic. |
| 3071 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3072 | static int process_recorded_refs(struct send_ctx *sctx, int *pending_move) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3073 | { |
| 3074 | int ret = 0; |
| 3075 | struct recorded_ref *cur; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3076 | struct recorded_ref *cur2; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3077 | struct list_head check_dirs; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3078 | struct fs_path *valid_path = NULL; |
Chris Mason | b24baf6 | 2012-07-25 19:21:10 -0400 | [diff] [blame] | 3079 | u64 ow_inode = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3080 | u64 ow_gen; |
| 3081 | int did_overwrite = 0; |
| 3082 | int is_orphan = 0; |
| 3083 | |
| 3084 | verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino); |
| 3085 | |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 3086 | /* |
| 3087 | * This should never happen as the root dir always has the same ref |
| 3088 | * which is always '..' |
| 3089 | */ |
| 3090 | BUG_ON(sctx->cur_ino <= BTRFS_FIRST_FREE_OBJECTID); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3091 | INIT_LIST_HEAD(&check_dirs); |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 3092 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3093 | valid_path = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3094 | if (!valid_path) { |
| 3095 | ret = -ENOMEM; |
| 3096 | goto out; |
| 3097 | } |
| 3098 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3099 | /* |
| 3100 | * First, check if the first ref of the current inode was overwritten |
| 3101 | * before. If yes, we know that the current inode was already orphanized |
| 3102 | * and thus use the orphan name. If not, we can use get_cur_path to |
| 3103 | * get the path of the first ref as it would like while receiving at |
| 3104 | * this point in time. |
| 3105 | * New inodes are always orphan at the beginning, so force to use the |
| 3106 | * orphan name in this case. |
| 3107 | * The first ref is stored in valid_path and will be updated if it |
| 3108 | * gets moved around. |
| 3109 | */ |
| 3110 | if (!sctx->cur_inode_new) { |
| 3111 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, |
| 3112 | sctx->cur_inode_gen); |
| 3113 | if (ret < 0) |
| 3114 | goto out; |
| 3115 | if (ret) |
| 3116 | did_overwrite = 1; |
| 3117 | } |
| 3118 | if (sctx->cur_inode_new || did_overwrite) { |
| 3119 | ret = gen_unique_name(sctx, sctx->cur_ino, |
| 3120 | sctx->cur_inode_gen, valid_path); |
| 3121 | if (ret < 0) |
| 3122 | goto out; |
| 3123 | is_orphan = 1; |
| 3124 | } else { |
| 3125 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 3126 | valid_path); |
| 3127 | if (ret < 0) |
| 3128 | goto out; |
| 3129 | } |
| 3130 | |
| 3131 | list_for_each_entry(cur, &sctx->new_refs, list) { |
| 3132 | /* |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3133 | * We may have refs where the parent directory does not exist |
| 3134 | * yet. This happens if the parent directories inum is higher |
| 3135 | * the the current inum. To handle this case, we create the |
| 3136 | * parent directory out of order. But we need to check if this |
| 3137 | * did already happen before due to other refs in the same dir. |
| 3138 | */ |
| 3139 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
| 3140 | if (ret < 0) |
| 3141 | goto out; |
| 3142 | if (ret == inode_state_will_create) { |
| 3143 | ret = 0; |
| 3144 | /* |
| 3145 | * First check if any of the current inodes refs did |
| 3146 | * already create the dir. |
| 3147 | */ |
| 3148 | list_for_each_entry(cur2, &sctx->new_refs, list) { |
| 3149 | if (cur == cur2) |
| 3150 | break; |
| 3151 | if (cur2->dir == cur->dir) { |
| 3152 | ret = 1; |
| 3153 | break; |
| 3154 | } |
| 3155 | } |
| 3156 | |
| 3157 | /* |
| 3158 | * If that did not happen, check if a previous inode |
| 3159 | * did already create the dir. |
| 3160 | */ |
| 3161 | if (!ret) |
| 3162 | ret = did_create_dir(sctx, cur->dir); |
| 3163 | if (ret < 0) |
| 3164 | goto out; |
| 3165 | if (!ret) { |
| 3166 | ret = send_create_inode(sctx, cur->dir); |
| 3167 | if (ret < 0) |
| 3168 | goto out; |
| 3169 | } |
| 3170 | } |
| 3171 | |
| 3172 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3173 | * Check if this new ref would overwrite the first ref of |
| 3174 | * another unprocessed inode. If yes, orphanize the |
| 3175 | * overwritten inode. If we find an overwritten ref that is |
| 3176 | * not the first ref, simply unlink it. |
| 3177 | */ |
| 3178 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 3179 | cur->name, cur->name_len, |
| 3180 | &ow_inode, &ow_gen); |
| 3181 | if (ret < 0) |
| 3182 | goto out; |
| 3183 | if (ret) { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3184 | ret = is_first_ref(sctx->parent_root, |
| 3185 | ow_inode, cur->dir, cur->name, |
| 3186 | cur->name_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3187 | if (ret < 0) |
| 3188 | goto out; |
| 3189 | if (ret) { |
| 3190 | ret = orphanize_inode(sctx, ow_inode, ow_gen, |
| 3191 | cur->full_path); |
| 3192 | if (ret < 0) |
| 3193 | goto out; |
| 3194 | } else { |
| 3195 | ret = send_unlink(sctx, cur->full_path); |
| 3196 | if (ret < 0) |
| 3197 | goto out; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | /* |
| 3202 | * link/move the ref to the new place. If we have an orphan |
| 3203 | * inode, move it and update valid_path. If not, link or move |
| 3204 | * it depending on the inode mode. |
| 3205 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3206 | if (is_orphan) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3207 | ret = send_rename(sctx, valid_path, cur->full_path); |
| 3208 | if (ret < 0) |
| 3209 | goto out; |
| 3210 | is_orphan = 0; |
| 3211 | ret = fs_path_copy(valid_path, cur->full_path); |
| 3212 | if (ret < 0) |
| 3213 | goto out; |
| 3214 | } else { |
| 3215 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 3216 | /* |
| 3217 | * Dirs can't be linked, so move it. For moved |
| 3218 | * dirs, we always have one new and one deleted |
| 3219 | * ref. The deleted ref is ignored later. |
| 3220 | */ |
Filipe David Borba Manana | d86477b | 2014-01-30 13:27:12 +0000 | [diff] [blame] | 3221 | ret = wait_for_parent_move(sctx, cur); |
| 3222 | if (ret < 0) |
| 3223 | goto out; |
| 3224 | if (ret) { |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3225 | ret = add_pending_dir_move(sctx, |
| 3226 | cur->dir); |
| 3227 | *pending_move = 1; |
| 3228 | } else { |
| 3229 | ret = send_rename(sctx, valid_path, |
| 3230 | cur->full_path); |
| 3231 | if (!ret) |
| 3232 | ret = fs_path_copy(valid_path, |
| 3233 | cur->full_path); |
| 3234 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3235 | if (ret < 0) |
| 3236 | goto out; |
| 3237 | } else { |
| 3238 | ret = send_link(sctx, cur->full_path, |
| 3239 | valid_path); |
| 3240 | if (ret < 0) |
| 3241 | goto out; |
| 3242 | } |
| 3243 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3244 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3245 | if (ret < 0) |
| 3246 | goto out; |
| 3247 | } |
| 3248 | |
| 3249 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { |
| 3250 | /* |
| 3251 | * Check if we can already rmdir the directory. If not, |
| 3252 | * orphanize it. For every dir item inside that gets deleted |
| 3253 | * later, we do this check again and rmdir it then if possible. |
| 3254 | * See the use of check_dirs for more details. |
| 3255 | */ |
| 3256 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino); |
| 3257 | if (ret < 0) |
| 3258 | goto out; |
| 3259 | if (ret) { |
| 3260 | ret = send_rmdir(sctx, valid_path); |
| 3261 | if (ret < 0) |
| 3262 | goto out; |
| 3263 | } else if (!is_orphan) { |
| 3264 | ret = orphanize_inode(sctx, sctx->cur_ino, |
| 3265 | sctx->cur_inode_gen, valid_path); |
| 3266 | if (ret < 0) |
| 3267 | goto out; |
| 3268 | is_orphan = 1; |
| 3269 | } |
| 3270 | |
| 3271 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3272 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3273 | if (ret < 0) |
| 3274 | goto out; |
| 3275 | } |
Alexander Block | ccf1626 | 2012-07-28 11:46:29 +0200 | [diff] [blame] | 3276 | } else if (S_ISDIR(sctx->cur_inode_mode) && |
| 3277 | !list_empty(&sctx->deleted_refs)) { |
| 3278 | /* |
| 3279 | * We have a moved dir. Add the old parent to check_dirs |
| 3280 | */ |
| 3281 | cur = list_entry(sctx->deleted_refs.next, struct recorded_ref, |
| 3282 | list); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3283 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | ccf1626 | 2012-07-28 11:46:29 +0200 | [diff] [blame] | 3284 | if (ret < 0) |
| 3285 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3286 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
| 3287 | /* |
| 3288 | * We have a non dir inode. Go through all deleted refs and |
| 3289 | * unlink them if they were not already overwritten by other |
| 3290 | * inodes. |
| 3291 | */ |
| 3292 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
| 3293 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 3294 | sctx->cur_ino, sctx->cur_inode_gen, |
| 3295 | cur->name, cur->name_len); |
| 3296 | if (ret < 0) |
| 3297 | goto out; |
| 3298 | if (!ret) { |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3299 | ret = send_unlink(sctx, cur->full_path); |
| 3300 | if (ret < 0) |
| 3301 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3302 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3303 | ret = dup_ref(cur, &check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3304 | if (ret < 0) |
| 3305 | goto out; |
| 3306 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3307 | /* |
| 3308 | * If the inode is still orphan, unlink the orphan. This may |
| 3309 | * happen when a previous inode did overwrite the first ref |
| 3310 | * of this inode and no new refs were added for the current |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 3311 | * inode. Unlinking does not mean that the inode is deleted in |
| 3312 | * all cases. There may still be links to this inode in other |
| 3313 | * places. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3314 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 3315 | if (is_orphan) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3316 | ret = send_unlink(sctx, valid_path); |
| 3317 | if (ret < 0) |
| 3318 | goto out; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | /* |
| 3323 | * We did collect all parent dirs where cur_inode was once located. We |
| 3324 | * now go through all these dirs and check if they are pending for |
| 3325 | * deletion and if it's finally possible to perform the rmdir now. |
| 3326 | * We also update the inode stats of the parent dirs here. |
| 3327 | */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3328 | list_for_each_entry(cur, &check_dirs, list) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 3329 | /* |
| 3330 | * In case we had refs into dirs that were not processed yet, |
| 3331 | * we don't need to do the utime and rmdir logic for these dirs. |
| 3332 | * The dir will be processed later. |
| 3333 | */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3334 | if (cur->dir > sctx->cur_ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3335 | continue; |
| 3336 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3337 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3338 | if (ret < 0) |
| 3339 | goto out; |
| 3340 | |
| 3341 | if (ret == inode_state_did_create || |
| 3342 | ret == inode_state_no_change) { |
| 3343 | /* TODO delayed utimes */ |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3344 | ret = send_utimes(sctx, cur->dir, cur->dir_gen); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3345 | if (ret < 0) |
| 3346 | goto out; |
| 3347 | } else if (ret == inode_state_did_delete) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3348 | ret = can_rmdir(sctx, cur->dir, sctx->cur_ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3349 | if (ret < 0) |
| 3350 | goto out; |
| 3351 | if (ret) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3352 | ret = get_cur_path(sctx, cur->dir, |
| 3353 | cur->dir_gen, valid_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3354 | if (ret < 0) |
| 3355 | goto out; |
| 3356 | ret = send_rmdir(sctx, valid_path); |
| 3357 | if (ret < 0) |
| 3358 | goto out; |
| 3359 | } |
| 3360 | } |
| 3361 | } |
| 3362 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3363 | ret = 0; |
| 3364 | |
| 3365 | out: |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3366 | __free_recorded_refs(&check_dirs); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3367 | free_recorded_refs(sctx); |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3368 | fs_path_free(valid_path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3369 | return ret; |
| 3370 | } |
| 3371 | |
| 3372 | static int __record_new_ref(int num, u64 dir, int index, |
| 3373 | struct fs_path *name, |
| 3374 | void *ctx) |
| 3375 | { |
| 3376 | int ret = 0; |
| 3377 | struct send_ctx *sctx = ctx; |
| 3378 | struct fs_path *p; |
| 3379 | u64 gen; |
| 3380 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3381 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3382 | if (!p) |
| 3383 | return -ENOMEM; |
| 3384 | |
| 3385 | ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 3386 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3387 | if (ret < 0) |
| 3388 | goto out; |
| 3389 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3390 | ret = get_cur_path(sctx, dir, gen, p); |
| 3391 | if (ret < 0) |
| 3392 | goto out; |
| 3393 | ret = fs_path_add_path(p, name); |
| 3394 | if (ret < 0) |
| 3395 | goto out; |
| 3396 | |
| 3397 | ret = record_ref(&sctx->new_refs, dir, gen, p); |
| 3398 | |
| 3399 | out: |
| 3400 | if (ret) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3401 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3402 | return ret; |
| 3403 | } |
| 3404 | |
| 3405 | static int __record_deleted_ref(int num, u64 dir, int index, |
| 3406 | struct fs_path *name, |
| 3407 | void *ctx) |
| 3408 | { |
| 3409 | int ret = 0; |
| 3410 | struct send_ctx *sctx = ctx; |
| 3411 | struct fs_path *p; |
| 3412 | u64 gen; |
| 3413 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3414 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3415 | if (!p) |
| 3416 | return -ENOMEM; |
| 3417 | |
| 3418 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 3419 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3420 | if (ret < 0) |
| 3421 | goto out; |
| 3422 | |
| 3423 | ret = get_cur_path(sctx, dir, gen, p); |
| 3424 | if (ret < 0) |
| 3425 | goto out; |
| 3426 | ret = fs_path_add_path(p, name); |
| 3427 | if (ret < 0) |
| 3428 | goto out; |
| 3429 | |
| 3430 | ret = record_ref(&sctx->deleted_refs, dir, gen, p); |
| 3431 | |
| 3432 | out: |
| 3433 | if (ret) |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3434 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3435 | return ret; |
| 3436 | } |
| 3437 | |
| 3438 | static int record_new_ref(struct send_ctx *sctx) |
| 3439 | { |
| 3440 | int ret; |
| 3441 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3442 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
| 3443 | sctx->cmp_key, 0, __record_new_ref, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3444 | if (ret < 0) |
| 3445 | goto out; |
| 3446 | ret = 0; |
| 3447 | |
| 3448 | out: |
| 3449 | return ret; |
| 3450 | } |
| 3451 | |
| 3452 | static int record_deleted_ref(struct send_ctx *sctx) |
| 3453 | { |
| 3454 | int ret; |
| 3455 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3456 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
| 3457 | sctx->cmp_key, 0, __record_deleted_ref, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3458 | if (ret < 0) |
| 3459 | goto out; |
| 3460 | ret = 0; |
| 3461 | |
| 3462 | out: |
| 3463 | return ret; |
| 3464 | } |
| 3465 | |
| 3466 | struct find_ref_ctx { |
| 3467 | u64 dir; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3468 | u64 dir_gen; |
| 3469 | struct btrfs_root *root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3470 | struct fs_path *name; |
| 3471 | int found_idx; |
| 3472 | }; |
| 3473 | |
| 3474 | static int __find_iref(int num, u64 dir, int index, |
| 3475 | struct fs_path *name, |
| 3476 | void *ctx_) |
| 3477 | { |
| 3478 | struct find_ref_ctx *ctx = ctx_; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3479 | u64 dir_gen; |
| 3480 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3481 | |
| 3482 | if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) && |
| 3483 | strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3484 | /* |
| 3485 | * To avoid doing extra lookups we'll only do this if everything |
| 3486 | * else matches. |
| 3487 | */ |
| 3488 | ret = get_inode_info(ctx->root, dir, NULL, &dir_gen, NULL, |
| 3489 | NULL, NULL, NULL); |
| 3490 | if (ret) |
| 3491 | return ret; |
| 3492 | if (dir_gen != ctx->dir_gen) |
| 3493 | return 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3494 | ctx->found_idx = num; |
| 3495 | return 1; |
| 3496 | } |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3500 | static int find_iref(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3501 | struct btrfs_path *path, |
| 3502 | struct btrfs_key *key, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3503 | u64 dir, u64 dir_gen, struct fs_path *name) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3504 | { |
| 3505 | int ret; |
| 3506 | struct find_ref_ctx ctx; |
| 3507 | |
| 3508 | ctx.dir = dir; |
| 3509 | ctx.name = name; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3510 | ctx.dir_gen = dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3511 | ctx.found_idx = -1; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3512 | ctx.root = root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3513 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3514 | ret = iterate_inode_ref(root, path, key, 0, __find_iref, &ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3515 | if (ret < 0) |
| 3516 | return ret; |
| 3517 | |
| 3518 | if (ctx.found_idx == -1) |
| 3519 | return -ENOENT; |
| 3520 | |
| 3521 | return ctx.found_idx; |
| 3522 | } |
| 3523 | |
| 3524 | static int __record_changed_new_ref(int num, u64 dir, int index, |
| 3525 | struct fs_path *name, |
| 3526 | void *ctx) |
| 3527 | { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3528 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3529 | int ret; |
| 3530 | struct send_ctx *sctx = ctx; |
| 3531 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3532 | ret = get_inode_info(sctx->send_root, dir, NULL, &dir_gen, NULL, |
| 3533 | NULL, NULL, NULL); |
| 3534 | if (ret) |
| 3535 | return ret; |
| 3536 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3537 | ret = find_iref(sctx->parent_root, sctx->right_path, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3538 | sctx->cmp_key, dir, dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3539 | if (ret == -ENOENT) |
| 3540 | ret = __record_new_ref(num, dir, index, name, sctx); |
| 3541 | else if (ret > 0) |
| 3542 | ret = 0; |
| 3543 | |
| 3544 | return ret; |
| 3545 | } |
| 3546 | |
| 3547 | static int __record_changed_deleted_ref(int num, u64 dir, int index, |
| 3548 | struct fs_path *name, |
| 3549 | void *ctx) |
| 3550 | { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3551 | u64 dir_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3552 | int ret; |
| 3553 | struct send_ctx *sctx = ctx; |
| 3554 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3555 | ret = get_inode_info(sctx->parent_root, dir, NULL, &dir_gen, NULL, |
| 3556 | NULL, NULL, NULL); |
| 3557 | if (ret) |
| 3558 | return ret; |
| 3559 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3560 | ret = find_iref(sctx->send_root, sctx->left_path, sctx->cmp_key, |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 3561 | dir, dir_gen, name); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3562 | if (ret == -ENOENT) |
| 3563 | ret = __record_deleted_ref(num, dir, index, name, sctx); |
| 3564 | else if (ret > 0) |
| 3565 | ret = 0; |
| 3566 | |
| 3567 | return ret; |
| 3568 | } |
| 3569 | |
| 3570 | static int record_changed_ref(struct send_ctx *sctx) |
| 3571 | { |
| 3572 | int ret = 0; |
| 3573 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3574 | ret = iterate_inode_ref(sctx->send_root, sctx->left_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3575 | sctx->cmp_key, 0, __record_changed_new_ref, sctx); |
| 3576 | if (ret < 0) |
| 3577 | goto out; |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3578 | ret = iterate_inode_ref(sctx->parent_root, sctx->right_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3579 | sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); |
| 3580 | if (ret < 0) |
| 3581 | goto out; |
| 3582 | ret = 0; |
| 3583 | |
| 3584 | out: |
| 3585 | return ret; |
| 3586 | } |
| 3587 | |
| 3588 | /* |
| 3589 | * Record and process all refs at once. Needed when an inode changes the |
| 3590 | * generation number, which means that it was deleted and recreated. |
| 3591 | */ |
| 3592 | static int process_all_refs(struct send_ctx *sctx, |
| 3593 | enum btrfs_compare_tree_result cmd) |
| 3594 | { |
| 3595 | int ret; |
| 3596 | struct btrfs_root *root; |
| 3597 | struct btrfs_path *path; |
| 3598 | struct btrfs_key key; |
| 3599 | struct btrfs_key found_key; |
| 3600 | struct extent_buffer *eb; |
| 3601 | int slot; |
| 3602 | iterate_inode_ref_t cb; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3603 | int pending_move = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3604 | |
| 3605 | path = alloc_path_for_send(); |
| 3606 | if (!path) |
| 3607 | return -ENOMEM; |
| 3608 | |
| 3609 | if (cmd == BTRFS_COMPARE_TREE_NEW) { |
| 3610 | root = sctx->send_root; |
| 3611 | cb = __record_new_ref; |
| 3612 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { |
| 3613 | root = sctx->parent_root; |
| 3614 | cb = __record_deleted_ref; |
| 3615 | } else { |
David Sterba | 4d1a63b | 2014-02-03 19:24:19 +0100 | [diff] [blame] | 3616 | btrfs_err(sctx->send_root->fs_info, |
| 3617 | "Wrong command %d in process_all_refs", cmd); |
| 3618 | ret = -EINVAL; |
| 3619 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | key.objectid = sctx->cmp_key->objectid; |
| 3623 | key.type = BTRFS_INODE_REF_KEY; |
| 3624 | key.offset = 0; |
| 3625 | while (1) { |
| 3626 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 3627 | if (ret < 0) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3628 | goto out; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 3629 | if (ret) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3630 | break; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3631 | |
| 3632 | eb = path->nodes[0]; |
| 3633 | slot = path->slots[0]; |
| 3634 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3635 | |
| 3636 | if (found_key.objectid != key.objectid || |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 3637 | (found_key.type != BTRFS_INODE_REF_KEY && |
| 3638 | found_key.type != BTRFS_INODE_EXTREF_KEY)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3639 | break; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3640 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3641 | ret = iterate_inode_ref(root, path, &found_key, 0, cb, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3642 | btrfs_release_path(path); |
| 3643 | if (ret < 0) |
| 3644 | goto out; |
| 3645 | |
| 3646 | key.offset = found_key.offset + 1; |
| 3647 | } |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 3648 | btrfs_release_path(path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3649 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 3650 | ret = process_recorded_refs(sctx, &pending_move); |
| 3651 | /* Only applicable to an incremental send. */ |
| 3652 | ASSERT(pending_move == 0); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3653 | |
| 3654 | out: |
| 3655 | btrfs_free_path(path); |
| 3656 | return ret; |
| 3657 | } |
| 3658 | |
| 3659 | static int send_set_xattr(struct send_ctx *sctx, |
| 3660 | struct fs_path *path, |
| 3661 | const char *name, int name_len, |
| 3662 | const char *data, int data_len) |
| 3663 | { |
| 3664 | int ret = 0; |
| 3665 | |
| 3666 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); |
| 3667 | if (ret < 0) |
| 3668 | goto out; |
| 3669 | |
| 3670 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 3671 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 3672 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); |
| 3673 | |
| 3674 | ret = send_cmd(sctx); |
| 3675 | |
| 3676 | tlv_put_failure: |
| 3677 | out: |
| 3678 | return ret; |
| 3679 | } |
| 3680 | |
| 3681 | static int send_remove_xattr(struct send_ctx *sctx, |
| 3682 | struct fs_path *path, |
| 3683 | const char *name, int name_len) |
| 3684 | { |
| 3685 | int ret = 0; |
| 3686 | |
| 3687 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); |
| 3688 | if (ret < 0) |
| 3689 | goto out; |
| 3690 | |
| 3691 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 3692 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 3693 | |
| 3694 | ret = send_cmd(sctx); |
| 3695 | |
| 3696 | tlv_put_failure: |
| 3697 | out: |
| 3698 | return ret; |
| 3699 | } |
| 3700 | |
| 3701 | static int __process_new_xattr(int num, struct btrfs_key *di_key, |
| 3702 | const char *name, int name_len, |
| 3703 | const char *data, int data_len, |
| 3704 | u8 type, void *ctx) |
| 3705 | { |
| 3706 | int ret; |
| 3707 | struct send_ctx *sctx = ctx; |
| 3708 | struct fs_path *p; |
| 3709 | posix_acl_xattr_header dummy_acl; |
| 3710 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3711 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3712 | if (!p) |
| 3713 | return -ENOMEM; |
| 3714 | |
| 3715 | /* |
| 3716 | * This hack is needed because empty acl's are stored as zero byte |
| 3717 | * data in xattrs. Problem with that is, that receiving these zero byte |
| 3718 | * acl's will fail later. To fix this, we send a dummy acl list that |
| 3719 | * only contains the version number and no entries. |
| 3720 | */ |
| 3721 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || |
| 3722 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { |
| 3723 | if (data_len == 0) { |
| 3724 | dummy_acl.a_version = |
| 3725 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
| 3726 | data = (char *)&dummy_acl; |
| 3727 | data_len = sizeof(dummy_acl); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 3732 | if (ret < 0) |
| 3733 | goto out; |
| 3734 | |
| 3735 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); |
| 3736 | |
| 3737 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3738 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3739 | return ret; |
| 3740 | } |
| 3741 | |
| 3742 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, |
| 3743 | const char *name, int name_len, |
| 3744 | const char *data, int data_len, |
| 3745 | u8 type, void *ctx) |
| 3746 | { |
| 3747 | int ret; |
| 3748 | struct send_ctx *sctx = ctx; |
| 3749 | struct fs_path *p; |
| 3750 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3751 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3752 | if (!p) |
| 3753 | return -ENOMEM; |
| 3754 | |
| 3755 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 3756 | if (ret < 0) |
| 3757 | goto out; |
| 3758 | |
| 3759 | ret = send_remove_xattr(sctx, p, name, name_len); |
| 3760 | |
| 3761 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3762 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3763 | return ret; |
| 3764 | } |
| 3765 | |
| 3766 | static int process_new_xattr(struct send_ctx *sctx) |
| 3767 | { |
| 3768 | int ret = 0; |
| 3769 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3770 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
| 3771 | sctx->cmp_key, __process_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3772 | |
| 3773 | return ret; |
| 3774 | } |
| 3775 | |
| 3776 | static int process_deleted_xattr(struct send_ctx *sctx) |
| 3777 | { |
| 3778 | int ret; |
| 3779 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3780 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
| 3781 | sctx->cmp_key, __process_deleted_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3782 | |
| 3783 | return ret; |
| 3784 | } |
| 3785 | |
| 3786 | struct find_xattr_ctx { |
| 3787 | const char *name; |
| 3788 | int name_len; |
| 3789 | int found_idx; |
| 3790 | char *found_data; |
| 3791 | int found_data_len; |
| 3792 | }; |
| 3793 | |
| 3794 | static int __find_xattr(int num, struct btrfs_key *di_key, |
| 3795 | const char *name, int name_len, |
| 3796 | const char *data, int data_len, |
| 3797 | u8 type, void *vctx) |
| 3798 | { |
| 3799 | struct find_xattr_ctx *ctx = vctx; |
| 3800 | |
| 3801 | if (name_len == ctx->name_len && |
| 3802 | strncmp(name, ctx->name, name_len) == 0) { |
| 3803 | ctx->found_idx = num; |
| 3804 | ctx->found_data_len = data_len; |
Thomas Meyer | a5959bc | 2013-06-01 09:37:50 +0000 | [diff] [blame] | 3805 | ctx->found_data = kmemdup(data, data_len, GFP_NOFS); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3806 | if (!ctx->found_data) |
| 3807 | return -ENOMEM; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3808 | return 1; |
| 3809 | } |
| 3810 | return 0; |
| 3811 | } |
| 3812 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3813 | static int find_xattr(struct btrfs_root *root, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3814 | struct btrfs_path *path, |
| 3815 | struct btrfs_key *key, |
| 3816 | const char *name, int name_len, |
| 3817 | char **data, int *data_len) |
| 3818 | { |
| 3819 | int ret; |
| 3820 | struct find_xattr_ctx ctx; |
| 3821 | |
| 3822 | ctx.name = name; |
| 3823 | ctx.name_len = name_len; |
| 3824 | ctx.found_idx = -1; |
| 3825 | ctx.found_data = NULL; |
| 3826 | ctx.found_data_len = 0; |
| 3827 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3828 | ret = iterate_dir_item(root, path, key, __find_xattr, &ctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3829 | if (ret < 0) |
| 3830 | return ret; |
| 3831 | |
| 3832 | if (ctx.found_idx == -1) |
| 3833 | return -ENOENT; |
| 3834 | if (data) { |
| 3835 | *data = ctx.found_data; |
| 3836 | *data_len = ctx.found_data_len; |
| 3837 | } else { |
| 3838 | kfree(ctx.found_data); |
| 3839 | } |
| 3840 | return ctx.found_idx; |
| 3841 | } |
| 3842 | |
| 3843 | |
| 3844 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, |
| 3845 | const char *name, int name_len, |
| 3846 | const char *data, int data_len, |
| 3847 | u8 type, void *ctx) |
| 3848 | { |
| 3849 | int ret; |
| 3850 | struct send_ctx *sctx = ctx; |
| 3851 | char *found_data = NULL; |
| 3852 | int found_data_len = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3853 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3854 | ret = find_xattr(sctx->parent_root, sctx->right_path, |
| 3855 | sctx->cmp_key, name, name_len, &found_data, |
| 3856 | &found_data_len); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3857 | if (ret == -ENOENT) { |
| 3858 | ret = __process_new_xattr(num, di_key, name, name_len, data, |
| 3859 | data_len, type, ctx); |
| 3860 | } else if (ret >= 0) { |
| 3861 | if (data_len != found_data_len || |
| 3862 | memcmp(data, found_data, data_len)) { |
| 3863 | ret = __process_new_xattr(num, di_key, name, name_len, |
| 3864 | data, data_len, type, ctx); |
| 3865 | } else { |
| 3866 | ret = 0; |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | kfree(found_data); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3871 | return ret; |
| 3872 | } |
| 3873 | |
| 3874 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, |
| 3875 | const char *name, int name_len, |
| 3876 | const char *data, int data_len, |
| 3877 | u8 type, void *ctx) |
| 3878 | { |
| 3879 | int ret; |
| 3880 | struct send_ctx *sctx = ctx; |
| 3881 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3882 | ret = find_xattr(sctx->send_root, sctx->left_path, sctx->cmp_key, |
| 3883 | name, name_len, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3884 | if (ret == -ENOENT) |
| 3885 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, |
| 3886 | data_len, type, ctx); |
| 3887 | else if (ret >= 0) |
| 3888 | ret = 0; |
| 3889 | |
| 3890 | return ret; |
| 3891 | } |
| 3892 | |
| 3893 | static int process_changed_xattr(struct send_ctx *sctx) |
| 3894 | { |
| 3895 | int ret = 0; |
| 3896 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3897 | ret = iterate_dir_item(sctx->send_root, sctx->left_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3898 | sctx->cmp_key, __process_changed_new_xattr, sctx); |
| 3899 | if (ret < 0) |
| 3900 | goto out; |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3901 | ret = iterate_dir_item(sctx->parent_root, sctx->right_path, |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3902 | sctx->cmp_key, __process_changed_deleted_xattr, sctx); |
| 3903 | |
| 3904 | out: |
| 3905 | return ret; |
| 3906 | } |
| 3907 | |
| 3908 | static int process_all_new_xattrs(struct send_ctx *sctx) |
| 3909 | { |
| 3910 | int ret; |
| 3911 | struct btrfs_root *root; |
| 3912 | struct btrfs_path *path; |
| 3913 | struct btrfs_key key; |
| 3914 | struct btrfs_key found_key; |
| 3915 | struct extent_buffer *eb; |
| 3916 | int slot; |
| 3917 | |
| 3918 | path = alloc_path_for_send(); |
| 3919 | if (!path) |
| 3920 | return -ENOMEM; |
| 3921 | |
| 3922 | root = sctx->send_root; |
| 3923 | |
| 3924 | key.objectid = sctx->cmp_key->objectid; |
| 3925 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 3926 | key.offset = 0; |
| 3927 | while (1) { |
| 3928 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 3929 | if (ret < 0) |
| 3930 | goto out; |
| 3931 | if (ret) { |
| 3932 | ret = 0; |
| 3933 | goto out; |
| 3934 | } |
| 3935 | |
| 3936 | eb = path->nodes[0]; |
| 3937 | slot = path->slots[0]; |
| 3938 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3939 | |
| 3940 | if (found_key.objectid != key.objectid || |
| 3941 | found_key.type != key.type) { |
| 3942 | ret = 0; |
| 3943 | goto out; |
| 3944 | } |
| 3945 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 3946 | ret = iterate_dir_item(root, path, &found_key, |
| 3947 | __process_new_xattr, sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3948 | if (ret < 0) |
| 3949 | goto out; |
| 3950 | |
| 3951 | btrfs_release_path(path); |
| 3952 | key.offset = found_key.offset + 1; |
| 3953 | } |
| 3954 | |
| 3955 | out: |
| 3956 | btrfs_free_path(path); |
| 3957 | return ret; |
| 3958 | } |
| 3959 | |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 3960 | static ssize_t fill_read_buf(struct send_ctx *sctx, u64 offset, u32 len) |
| 3961 | { |
| 3962 | struct btrfs_root *root = sctx->send_root; |
| 3963 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 3964 | struct inode *inode; |
| 3965 | struct page *page; |
| 3966 | char *addr; |
| 3967 | struct btrfs_key key; |
| 3968 | pgoff_t index = offset >> PAGE_CACHE_SHIFT; |
| 3969 | pgoff_t last_index; |
| 3970 | unsigned pg_offset = offset & ~PAGE_CACHE_MASK; |
| 3971 | ssize_t ret = 0; |
| 3972 | |
| 3973 | key.objectid = sctx->cur_ino; |
| 3974 | key.type = BTRFS_INODE_ITEM_KEY; |
| 3975 | key.offset = 0; |
| 3976 | |
| 3977 | inode = btrfs_iget(fs_info->sb, &key, root, NULL); |
| 3978 | if (IS_ERR(inode)) |
| 3979 | return PTR_ERR(inode); |
| 3980 | |
| 3981 | if (offset + len > i_size_read(inode)) { |
| 3982 | if (offset > i_size_read(inode)) |
| 3983 | len = 0; |
| 3984 | else |
| 3985 | len = offset - i_size_read(inode); |
| 3986 | } |
| 3987 | if (len == 0) |
| 3988 | goto out; |
| 3989 | |
| 3990 | last_index = (offset + len - 1) >> PAGE_CACHE_SHIFT; |
| 3991 | while (index <= last_index) { |
| 3992 | unsigned cur_len = min_t(unsigned, len, |
| 3993 | PAGE_CACHE_SIZE - pg_offset); |
| 3994 | page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); |
| 3995 | if (!page) { |
| 3996 | ret = -ENOMEM; |
| 3997 | break; |
| 3998 | } |
| 3999 | |
| 4000 | if (!PageUptodate(page)) { |
| 4001 | btrfs_readpage(NULL, page); |
| 4002 | lock_page(page); |
| 4003 | if (!PageUptodate(page)) { |
| 4004 | unlock_page(page); |
| 4005 | page_cache_release(page); |
| 4006 | ret = -EIO; |
| 4007 | break; |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | addr = kmap(page); |
| 4012 | memcpy(sctx->read_buf + ret, addr + pg_offset, cur_len); |
| 4013 | kunmap(page); |
| 4014 | unlock_page(page); |
| 4015 | page_cache_release(page); |
| 4016 | index++; |
| 4017 | pg_offset = 0; |
| 4018 | len -= cur_len; |
| 4019 | ret += cur_len; |
| 4020 | } |
| 4021 | out: |
| 4022 | iput(inode); |
| 4023 | return ret; |
| 4024 | } |
| 4025 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4026 | /* |
| 4027 | * Read some bytes from the current inode/file and send a write command to |
| 4028 | * user space. |
| 4029 | */ |
| 4030 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) |
| 4031 | { |
| 4032 | int ret = 0; |
| 4033 | struct fs_path *p; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4034 | ssize_t num_read = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4035 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4036 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4037 | if (!p) |
| 4038 | return -ENOMEM; |
| 4039 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4040 | verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len); |
| 4041 | |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4042 | num_read = fill_read_buf(sctx, offset, len); |
| 4043 | if (num_read <= 0) { |
| 4044 | if (num_read < 0) |
| 4045 | ret = num_read; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4046 | goto out; |
Josef Bacik | ed25909 | 2013-10-25 11:36:01 -0400 | [diff] [blame] | 4047 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4048 | |
| 4049 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 4050 | if (ret < 0) |
| 4051 | goto out; |
| 4052 | |
| 4053 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4054 | if (ret < 0) |
| 4055 | goto out; |
| 4056 | |
| 4057 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4058 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4059 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, num_read); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4060 | |
| 4061 | ret = send_cmd(sctx); |
| 4062 | |
| 4063 | tlv_put_failure: |
| 4064 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4065 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4066 | if (ret < 0) |
| 4067 | return ret; |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4068 | return num_read; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4069 | } |
| 4070 | |
| 4071 | /* |
| 4072 | * Send a clone command to user space. |
| 4073 | */ |
| 4074 | static int send_clone(struct send_ctx *sctx, |
| 4075 | u64 offset, u32 len, |
| 4076 | struct clone_root *clone_root) |
| 4077 | { |
| 4078 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4079 | struct fs_path *p; |
| 4080 | u64 gen; |
| 4081 | |
| 4082 | verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, " |
| 4083 | "clone_inode=%llu, clone_offset=%llu\n", offset, len, |
| 4084 | clone_root->root->objectid, clone_root->ino, |
| 4085 | clone_root->offset); |
| 4086 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4087 | p = fs_path_alloc(); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4088 | if (!p) |
| 4089 | return -ENOMEM; |
| 4090 | |
| 4091 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); |
| 4092 | if (ret < 0) |
| 4093 | goto out; |
| 4094 | |
| 4095 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4096 | if (ret < 0) |
| 4097 | goto out; |
| 4098 | |
| 4099 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 4100 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); |
| 4101 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4102 | |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4103 | if (clone_root->root == sctx->send_root) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4104 | ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 4105 | &gen, NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4106 | if (ret < 0) |
| 4107 | goto out; |
| 4108 | ret = get_cur_path(sctx, clone_root->ino, gen, p); |
| 4109 | } else { |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4110 | ret = get_inode_path(clone_root->root, clone_root->ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4111 | } |
| 4112 | if (ret < 0) |
| 4113 | goto out; |
| 4114 | |
| 4115 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
Alexander Block | e938c8a | 2012-07-28 16:33:49 +0200 | [diff] [blame] | 4116 | clone_root->root->root_item.uuid); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4117 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
Filipe David Borba Manana | 5a0f4e2 | 2013-12-03 15:55:48 +0000 | [diff] [blame] | 4118 | le64_to_cpu(clone_root->root->root_item.ctransid)); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4119 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
| 4120 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, |
| 4121 | clone_root->offset); |
| 4122 | |
| 4123 | ret = send_cmd(sctx); |
| 4124 | |
| 4125 | tlv_put_failure: |
| 4126 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4127 | fs_path_free(p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4128 | return ret; |
| 4129 | } |
| 4130 | |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4131 | /* |
| 4132 | * Send an update extent command to user space. |
| 4133 | */ |
| 4134 | static int send_update_extent(struct send_ctx *sctx, |
| 4135 | u64 offset, u32 len) |
| 4136 | { |
| 4137 | int ret = 0; |
| 4138 | struct fs_path *p; |
| 4139 | |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4140 | p = fs_path_alloc(); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4141 | if (!p) |
| 4142 | return -ENOMEM; |
| 4143 | |
| 4144 | ret = begin_cmd(sctx, BTRFS_SEND_C_UPDATE_EXTENT); |
| 4145 | if (ret < 0) |
| 4146 | goto out; |
| 4147 | |
| 4148 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4149 | if (ret < 0) |
| 4150 | goto out; |
| 4151 | |
| 4152 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4153 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 4154 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, len); |
| 4155 | |
| 4156 | ret = send_cmd(sctx); |
| 4157 | |
| 4158 | tlv_put_failure: |
| 4159 | out: |
Tsutomu Itoh | 924794c | 2013-05-08 07:51:52 +0000 | [diff] [blame] | 4160 | fs_path_free(p); |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4161 | return ret; |
| 4162 | } |
| 4163 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4164 | static int send_hole(struct send_ctx *sctx, u64 end) |
| 4165 | { |
| 4166 | struct fs_path *p = NULL; |
| 4167 | u64 offset = sctx->cur_inode_last_extent; |
| 4168 | u64 len; |
| 4169 | int ret = 0; |
| 4170 | |
| 4171 | p = fs_path_alloc(); |
| 4172 | if (!p) |
| 4173 | return -ENOMEM; |
| 4174 | memset(sctx->read_buf, 0, BTRFS_SEND_READ_SIZE); |
| 4175 | while (offset < end) { |
| 4176 | len = min_t(u64, end - offset, BTRFS_SEND_READ_SIZE); |
| 4177 | |
| 4178 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 4179 | if (ret < 0) |
| 4180 | break; |
| 4181 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 4182 | if (ret < 0) |
| 4183 | break; |
| 4184 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 4185 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 4186 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, len); |
| 4187 | ret = send_cmd(sctx); |
| 4188 | if (ret < 0) |
| 4189 | break; |
| 4190 | offset += len; |
| 4191 | } |
| 4192 | tlv_put_failure: |
| 4193 | fs_path_free(p); |
| 4194 | return ret; |
| 4195 | } |
| 4196 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4197 | static int send_write_or_clone(struct send_ctx *sctx, |
| 4198 | struct btrfs_path *path, |
| 4199 | struct btrfs_key *key, |
| 4200 | struct clone_root *clone_root) |
| 4201 | { |
| 4202 | int ret = 0; |
| 4203 | struct btrfs_file_extent_item *ei; |
| 4204 | u64 offset = key->offset; |
| 4205 | u64 pos = 0; |
| 4206 | u64 len; |
| 4207 | u32 l; |
| 4208 | u8 type; |
Filipe David Borba Manana | 28e5dd8 | 2014-01-12 02:26:28 +0000 | [diff] [blame] | 4209 | u64 bs = sctx->send_root->fs_info->sb->s_blocksize; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4210 | |
| 4211 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4212 | struct btrfs_file_extent_item); |
| 4213 | type = btrfs_file_extent_type(path->nodes[0], ei); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4214 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4215 | len = btrfs_file_extent_inline_len(path->nodes[0], |
| 4216 | path->slots[0], ei); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4217 | /* |
| 4218 | * it is possible the inline item won't cover the whole page, |
| 4219 | * but there may be items after this page. Make |
| 4220 | * sure to send the whole thing |
| 4221 | */ |
| 4222 | len = PAGE_CACHE_ALIGN(len); |
| 4223 | } else { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4224 | len = btrfs_file_extent_num_bytes(path->nodes[0], ei); |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4225 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4226 | |
| 4227 | if (offset + len > sctx->cur_inode_size) |
| 4228 | len = sctx->cur_inode_size - offset; |
| 4229 | if (len == 0) { |
| 4230 | ret = 0; |
| 4231 | goto out; |
| 4232 | } |
| 4233 | |
Filipe David Borba Manana | 28e5dd8 | 2014-01-12 02:26:28 +0000 | [diff] [blame] | 4234 | if (clone_root && IS_ALIGNED(offset + len, bs)) { |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 4235 | ret = send_clone(sctx, offset, len, clone_root); |
| 4236 | } else if (sctx->flags & BTRFS_SEND_FLAG_NO_FILE_DATA) { |
| 4237 | ret = send_update_extent(sctx, offset, len); |
| 4238 | } else { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4239 | while (pos < len) { |
| 4240 | l = len - pos; |
| 4241 | if (l > BTRFS_SEND_READ_SIZE) |
| 4242 | l = BTRFS_SEND_READ_SIZE; |
| 4243 | ret = send_write(sctx, pos + offset, l); |
| 4244 | if (ret < 0) |
| 4245 | goto out; |
| 4246 | if (!ret) |
| 4247 | break; |
| 4248 | pos += ret; |
| 4249 | } |
| 4250 | ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4251 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4252 | out: |
| 4253 | return ret; |
| 4254 | } |
| 4255 | |
| 4256 | static int is_extent_unchanged(struct send_ctx *sctx, |
| 4257 | struct btrfs_path *left_path, |
| 4258 | struct btrfs_key *ekey) |
| 4259 | { |
| 4260 | int ret = 0; |
| 4261 | struct btrfs_key key; |
| 4262 | struct btrfs_path *path = NULL; |
| 4263 | struct extent_buffer *eb; |
| 4264 | int slot; |
| 4265 | struct btrfs_key found_key; |
| 4266 | struct btrfs_file_extent_item *ei; |
| 4267 | u64 left_disknr; |
| 4268 | u64 right_disknr; |
| 4269 | u64 left_offset; |
| 4270 | u64 right_offset; |
| 4271 | u64 left_offset_fixed; |
| 4272 | u64 left_len; |
| 4273 | u64 right_len; |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4274 | u64 left_gen; |
| 4275 | u64 right_gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4276 | u8 left_type; |
| 4277 | u8 right_type; |
| 4278 | |
| 4279 | path = alloc_path_for_send(); |
| 4280 | if (!path) |
| 4281 | return -ENOMEM; |
| 4282 | |
| 4283 | eb = left_path->nodes[0]; |
| 4284 | slot = left_path->slots[0]; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4285 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 4286 | left_type = btrfs_file_extent_type(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4287 | |
| 4288 | if (left_type != BTRFS_FILE_EXTENT_REG) { |
| 4289 | ret = 0; |
| 4290 | goto out; |
| 4291 | } |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4292 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 4293 | left_len = btrfs_file_extent_num_bytes(eb, ei); |
| 4294 | left_offset = btrfs_file_extent_offset(eb, ei); |
| 4295 | left_gen = btrfs_file_extent_generation(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4296 | |
| 4297 | /* |
| 4298 | * Following comments will refer to these graphics. L is the left |
| 4299 | * extents which we are checking at the moment. 1-8 are the right |
| 4300 | * extents that we iterate. |
| 4301 | * |
| 4302 | * |-----L-----| |
| 4303 | * |-1-|-2a-|-3-|-4-|-5-|-6-| |
| 4304 | * |
| 4305 | * |-----L-----| |
| 4306 | * |--1--|-2b-|...(same as above) |
| 4307 | * |
| 4308 | * Alternative situation. Happens on files where extents got split. |
| 4309 | * |-----L-----| |
| 4310 | * |-----------7-----------|-6-| |
| 4311 | * |
| 4312 | * Alternative situation. Happens on files which got larger. |
| 4313 | * |-----L-----| |
| 4314 | * |-8-| |
| 4315 | * Nothing follows after 8. |
| 4316 | */ |
| 4317 | |
| 4318 | key.objectid = ekey->objectid; |
| 4319 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4320 | key.offset = ekey->offset; |
| 4321 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); |
| 4322 | if (ret < 0) |
| 4323 | goto out; |
| 4324 | if (ret) { |
| 4325 | ret = 0; |
| 4326 | goto out; |
| 4327 | } |
| 4328 | |
| 4329 | /* |
| 4330 | * Handle special case where the right side has no extents at all. |
| 4331 | */ |
| 4332 | eb = path->nodes[0]; |
| 4333 | slot = path->slots[0]; |
| 4334 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4335 | if (found_key.objectid != key.objectid || |
| 4336 | found_key.type != key.type) { |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 4337 | /* If we're a hole then just pretend nothing changed */ |
| 4338 | ret = (left_disknr) ? 0 : 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4339 | goto out; |
| 4340 | } |
| 4341 | |
| 4342 | /* |
| 4343 | * We're now on 2a, 2b or 7. |
| 4344 | */ |
| 4345 | key = found_key; |
| 4346 | while (key.offset < ekey->offset + left_len) { |
| 4347 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 4348 | right_type = btrfs_file_extent_type(eb, ei); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4349 | if (right_type != BTRFS_FILE_EXTENT_REG) { |
| 4350 | ret = 0; |
| 4351 | goto out; |
| 4352 | } |
| 4353 | |
Josef Bacik | 007d31f | 2013-10-31 16:49:02 -0400 | [diff] [blame] | 4354 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 4355 | right_len = btrfs_file_extent_num_bytes(eb, ei); |
| 4356 | right_offset = btrfs_file_extent_offset(eb, ei); |
| 4357 | right_gen = btrfs_file_extent_generation(eb, ei); |
| 4358 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4359 | /* |
| 4360 | * Are we at extent 8? If yes, we know the extent is changed. |
| 4361 | * This may only happen on the first iteration. |
| 4362 | */ |
Alexander Block | d8347fa | 2012-08-01 12:49:15 +0200 | [diff] [blame] | 4363 | if (found_key.offset + right_len <= ekey->offset) { |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 4364 | /* If we're a hole just pretend nothing changed */ |
| 4365 | ret = (left_disknr) ? 0 : 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4366 | goto out; |
| 4367 | } |
| 4368 | |
| 4369 | left_offset_fixed = left_offset; |
| 4370 | if (key.offset < ekey->offset) { |
| 4371 | /* Fix the right offset for 2a and 7. */ |
| 4372 | right_offset += ekey->offset - key.offset; |
| 4373 | } else { |
| 4374 | /* Fix the left offset for all behind 2a and 2b */ |
| 4375 | left_offset_fixed += key.offset - ekey->offset; |
| 4376 | } |
| 4377 | |
| 4378 | /* |
| 4379 | * Check if we have the same extent. |
| 4380 | */ |
Alexander Block | 3954096 | 2012-08-01 12:46:05 +0200 | [diff] [blame] | 4381 | if (left_disknr != right_disknr || |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 4382 | left_offset_fixed != right_offset || |
| 4383 | left_gen != right_gen) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4384 | ret = 0; |
| 4385 | goto out; |
| 4386 | } |
| 4387 | |
| 4388 | /* |
| 4389 | * Go to the next extent. |
| 4390 | */ |
| 4391 | ret = btrfs_next_item(sctx->parent_root, path); |
| 4392 | if (ret < 0) |
| 4393 | goto out; |
| 4394 | if (!ret) { |
| 4395 | eb = path->nodes[0]; |
| 4396 | slot = path->slots[0]; |
| 4397 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4398 | } |
| 4399 | if (ret || found_key.objectid != key.objectid || |
| 4400 | found_key.type != key.type) { |
| 4401 | key.offset += right_len; |
| 4402 | break; |
Jan Schmidt | adaa4b8 | 2013-03-21 14:30:23 +0000 | [diff] [blame] | 4403 | } |
| 4404 | if (found_key.offset != key.offset + right_len) { |
| 4405 | ret = 0; |
| 4406 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4407 | } |
| 4408 | key = found_key; |
| 4409 | } |
| 4410 | |
| 4411 | /* |
| 4412 | * We're now behind the left extent (treat as unchanged) or at the end |
| 4413 | * of the right side (treat as changed). |
| 4414 | */ |
| 4415 | if (key.offset >= ekey->offset + left_len) |
| 4416 | ret = 1; |
| 4417 | else |
| 4418 | ret = 0; |
| 4419 | |
| 4420 | |
| 4421 | out: |
| 4422 | btrfs_free_path(path); |
| 4423 | return ret; |
| 4424 | } |
| 4425 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4426 | static int get_last_extent(struct send_ctx *sctx, u64 offset) |
| 4427 | { |
| 4428 | struct btrfs_path *path; |
| 4429 | struct btrfs_root *root = sctx->send_root; |
| 4430 | struct btrfs_file_extent_item *fi; |
| 4431 | struct btrfs_key key; |
| 4432 | u64 extent_end; |
| 4433 | u8 type; |
| 4434 | int ret; |
| 4435 | |
| 4436 | path = alloc_path_for_send(); |
| 4437 | if (!path) |
| 4438 | return -ENOMEM; |
| 4439 | |
| 4440 | sctx->cur_inode_last_extent = 0; |
| 4441 | |
| 4442 | key.objectid = sctx->cur_ino; |
| 4443 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4444 | key.offset = offset; |
| 4445 | ret = btrfs_search_slot_for_read(root, &key, path, 0, 1); |
| 4446 | if (ret < 0) |
| 4447 | goto out; |
| 4448 | ret = 0; |
| 4449 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
| 4450 | if (key.objectid != sctx->cur_ino || key.type != BTRFS_EXTENT_DATA_KEY) |
| 4451 | goto out; |
| 4452 | |
| 4453 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4454 | struct btrfs_file_extent_item); |
| 4455 | type = btrfs_file_extent_type(path->nodes[0], fi); |
| 4456 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4457 | u64 size = btrfs_file_extent_inline_len(path->nodes[0], |
| 4458 | path->slots[0], fi); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4459 | extent_end = ALIGN(key.offset + size, |
| 4460 | sctx->send_root->sectorsize); |
| 4461 | } else { |
| 4462 | extent_end = key.offset + |
| 4463 | btrfs_file_extent_num_bytes(path->nodes[0], fi); |
| 4464 | } |
| 4465 | sctx->cur_inode_last_extent = extent_end; |
| 4466 | out: |
| 4467 | btrfs_free_path(path); |
| 4468 | return ret; |
| 4469 | } |
| 4470 | |
| 4471 | static int maybe_send_hole(struct send_ctx *sctx, struct btrfs_path *path, |
| 4472 | struct btrfs_key *key) |
| 4473 | { |
| 4474 | struct btrfs_file_extent_item *fi; |
| 4475 | u64 extent_end; |
| 4476 | u8 type; |
| 4477 | int ret = 0; |
| 4478 | |
| 4479 | if (sctx->cur_ino != key->objectid || !need_send_hole(sctx)) |
| 4480 | return 0; |
| 4481 | |
| 4482 | if (sctx->cur_inode_last_extent == (u64)-1) { |
| 4483 | ret = get_last_extent(sctx, key->offset - 1); |
| 4484 | if (ret) |
| 4485 | return ret; |
| 4486 | } |
| 4487 | |
| 4488 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4489 | struct btrfs_file_extent_item); |
| 4490 | type = btrfs_file_extent_type(path->nodes[0], fi); |
| 4491 | if (type == BTRFS_FILE_EXTENT_INLINE) { |
Chris Mason | 514ac8a | 2014-01-03 21:07:00 -0800 | [diff] [blame] | 4492 | u64 size = btrfs_file_extent_inline_len(path->nodes[0], |
| 4493 | path->slots[0], fi); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4494 | extent_end = ALIGN(key->offset + size, |
| 4495 | sctx->send_root->sectorsize); |
| 4496 | } else { |
| 4497 | extent_end = key->offset + |
| 4498 | btrfs_file_extent_num_bytes(path->nodes[0], fi); |
| 4499 | } |
Filipe David Borba Manana | bf54f41 | 2014-01-28 01:38:06 +0000 | [diff] [blame] | 4500 | |
| 4501 | if (path->slots[0] == 0 && |
| 4502 | sctx->cur_inode_last_extent < key->offset) { |
| 4503 | /* |
| 4504 | * We might have skipped entire leafs that contained only |
| 4505 | * file extent items for our current inode. These leafs have |
| 4506 | * a generation number smaller (older) than the one in the |
| 4507 | * current leaf and the leaf our last extent came from, and |
| 4508 | * are located between these 2 leafs. |
| 4509 | */ |
| 4510 | ret = get_last_extent(sctx, key->offset - 1); |
| 4511 | if (ret) |
| 4512 | return ret; |
| 4513 | } |
| 4514 | |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4515 | if (sctx->cur_inode_last_extent < key->offset) |
| 4516 | ret = send_hole(sctx, key->offset); |
| 4517 | sctx->cur_inode_last_extent = extent_end; |
| 4518 | return ret; |
| 4519 | } |
| 4520 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4521 | static int process_extent(struct send_ctx *sctx, |
| 4522 | struct btrfs_path *path, |
| 4523 | struct btrfs_key *key) |
| 4524 | { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4525 | struct clone_root *found_clone = NULL; |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 4526 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4527 | |
| 4528 | if (S_ISLNK(sctx->cur_inode_mode)) |
| 4529 | return 0; |
| 4530 | |
| 4531 | if (sctx->parent_root && !sctx->cur_inode_new) { |
| 4532 | ret = is_extent_unchanged(sctx, path, key); |
| 4533 | if (ret < 0) |
| 4534 | goto out; |
| 4535 | if (ret) { |
| 4536 | ret = 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4537 | goto out_hole; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4538 | } |
Josef Bacik | 57cfd46 | 2013-08-20 15:55:39 -0400 | [diff] [blame] | 4539 | } else { |
| 4540 | struct btrfs_file_extent_item *ei; |
| 4541 | u8 type; |
| 4542 | |
| 4543 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 4544 | struct btrfs_file_extent_item); |
| 4545 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 4546 | if (type == BTRFS_FILE_EXTENT_PREALLOC || |
| 4547 | type == BTRFS_FILE_EXTENT_REG) { |
| 4548 | /* |
| 4549 | * The send spec does not have a prealloc command yet, |
| 4550 | * so just leave a hole for prealloc'ed extents until |
| 4551 | * we have enough commands queued up to justify rev'ing |
| 4552 | * the send spec. |
| 4553 | */ |
| 4554 | if (type == BTRFS_FILE_EXTENT_PREALLOC) { |
| 4555 | ret = 0; |
| 4556 | goto out; |
| 4557 | } |
| 4558 | |
| 4559 | /* Have a hole, just skip it. */ |
| 4560 | if (btrfs_file_extent_disk_bytenr(path->nodes[0], ei) == 0) { |
| 4561 | ret = 0; |
| 4562 | goto out; |
| 4563 | } |
| 4564 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, |
| 4568 | sctx->cur_inode_size, &found_clone); |
| 4569 | if (ret != -ENOENT && ret < 0) |
| 4570 | goto out; |
| 4571 | |
| 4572 | ret = send_write_or_clone(sctx, path, key, found_clone); |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4573 | if (ret) |
| 4574 | goto out; |
| 4575 | out_hole: |
| 4576 | ret = maybe_send_hole(sctx, path, key); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4577 | out: |
| 4578 | return ret; |
| 4579 | } |
| 4580 | |
| 4581 | static int process_all_extents(struct send_ctx *sctx) |
| 4582 | { |
| 4583 | int ret; |
| 4584 | struct btrfs_root *root; |
| 4585 | struct btrfs_path *path; |
| 4586 | struct btrfs_key key; |
| 4587 | struct btrfs_key found_key; |
| 4588 | struct extent_buffer *eb; |
| 4589 | int slot; |
| 4590 | |
| 4591 | root = sctx->send_root; |
| 4592 | path = alloc_path_for_send(); |
| 4593 | if (!path) |
| 4594 | return -ENOMEM; |
| 4595 | |
| 4596 | key.objectid = sctx->cmp_key->objectid; |
| 4597 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 4598 | key.offset = 0; |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 4599 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 4600 | if (ret < 0) |
| 4601 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4602 | |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 4603 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4604 | eb = path->nodes[0]; |
| 4605 | slot = path->slots[0]; |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 4606 | |
| 4607 | if (slot >= btrfs_header_nritems(eb)) { |
| 4608 | ret = btrfs_next_leaf(root, path); |
| 4609 | if (ret < 0) { |
| 4610 | goto out; |
| 4611 | } else if (ret > 0) { |
| 4612 | ret = 0; |
| 4613 | break; |
| 4614 | } |
| 4615 | continue; |
| 4616 | } |
| 4617 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4618 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4619 | |
| 4620 | if (found_key.objectid != key.objectid || |
| 4621 | found_key.type != key.type) { |
| 4622 | ret = 0; |
| 4623 | goto out; |
| 4624 | } |
| 4625 | |
| 4626 | ret = process_extent(sctx, path, &found_key); |
| 4627 | if (ret < 0) |
| 4628 | goto out; |
| 4629 | |
Filipe David Borba Manana | 7fdd29d | 2014-01-24 17:42:09 +0000 | [diff] [blame] | 4630 | path->slots[0]++; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | out: |
| 4634 | btrfs_free_path(path); |
| 4635 | return ret; |
| 4636 | } |
| 4637 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4638 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end, |
| 4639 | int *pending_move, |
| 4640 | int *refs_processed) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4641 | { |
| 4642 | int ret = 0; |
| 4643 | |
| 4644 | if (sctx->cur_ino == 0) |
| 4645 | goto out; |
| 4646 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 4647 | sctx->cmp_key->type <= BTRFS_INODE_EXTREF_KEY) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4648 | goto out; |
| 4649 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) |
| 4650 | goto out; |
| 4651 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4652 | ret = process_recorded_refs(sctx, pending_move); |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 4653 | if (ret < 0) |
| 4654 | goto out; |
| 4655 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4656 | *refs_processed = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4657 | out: |
| 4658 | return ret; |
| 4659 | } |
| 4660 | |
| 4661 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) |
| 4662 | { |
| 4663 | int ret = 0; |
| 4664 | u64 left_mode; |
| 4665 | u64 left_uid; |
| 4666 | u64 left_gid; |
| 4667 | u64 right_mode; |
| 4668 | u64 right_uid; |
| 4669 | u64 right_gid; |
| 4670 | int need_chmod = 0; |
| 4671 | int need_chown = 0; |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4672 | int pending_move = 0; |
| 4673 | int refs_processed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4674 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4675 | ret = process_recorded_refs_if_needed(sctx, at_end, &pending_move, |
| 4676 | &refs_processed); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4677 | if (ret < 0) |
| 4678 | goto out; |
| 4679 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4680 | /* |
| 4681 | * We have processed the refs and thus need to advance send_progress. |
| 4682 | * Now, calls to get_cur_xxx will take the updated refs of the current |
| 4683 | * inode into account. |
| 4684 | * |
| 4685 | * On the other hand, if our current inode is a directory and couldn't |
| 4686 | * be moved/renamed because its parent was renamed/moved too and it has |
| 4687 | * a higher inode number, we can only move/rename our current inode |
| 4688 | * after we moved/renamed its parent. Therefore in this case operate on |
| 4689 | * the old path (pre move/rename) of our current inode, and the |
| 4690 | * move/rename will be performed later. |
| 4691 | */ |
| 4692 | if (refs_processed && !pending_move) |
| 4693 | sctx->send_progress = sctx->cur_ino + 1; |
| 4694 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4695 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) |
| 4696 | goto out; |
| 4697 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) |
| 4698 | goto out; |
| 4699 | |
| 4700 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 4701 | &left_mode, &left_uid, &left_gid, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4702 | if (ret < 0) |
| 4703 | goto out; |
| 4704 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 4705 | if (!sctx->parent_root || sctx->cur_inode_new) { |
| 4706 | need_chown = 1; |
| 4707 | if (!S_ISLNK(sctx->cur_inode_mode)) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4708 | need_chmod = 1; |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 4709 | } else { |
| 4710 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, |
| 4711 | NULL, NULL, &right_mode, &right_uid, |
| 4712 | &right_gid, NULL); |
| 4713 | if (ret < 0) |
| 4714 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4715 | |
Alex Lyakas | e2d044f | 2012-10-17 13:52:47 +0000 | [diff] [blame] | 4716 | if (left_uid != right_uid || left_gid != right_gid) |
| 4717 | need_chown = 1; |
| 4718 | if (!S_ISLNK(sctx->cur_inode_mode) && left_mode != right_mode) |
| 4719 | need_chmod = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | if (S_ISREG(sctx->cur_inode_mode)) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4723 | if (need_send_hole(sctx)) { |
| 4724 | if (sctx->cur_inode_last_extent == (u64)-1) { |
| 4725 | ret = get_last_extent(sctx, (u64)-1); |
| 4726 | if (ret) |
| 4727 | goto out; |
| 4728 | } |
| 4729 | if (sctx->cur_inode_last_extent < |
| 4730 | sctx->cur_inode_size) { |
| 4731 | ret = send_hole(sctx, sctx->cur_inode_size); |
| 4732 | if (ret) |
| 4733 | goto out; |
| 4734 | } |
| 4735 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4736 | ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 4737 | sctx->cur_inode_size); |
| 4738 | if (ret < 0) |
| 4739 | goto out; |
| 4740 | } |
| 4741 | |
| 4742 | if (need_chown) { |
| 4743 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 4744 | left_uid, left_gid); |
| 4745 | if (ret < 0) |
| 4746 | goto out; |
| 4747 | } |
| 4748 | if (need_chmod) { |
| 4749 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 4750 | left_mode); |
| 4751 | if (ret < 0) |
| 4752 | goto out; |
| 4753 | } |
| 4754 | |
| 4755 | /* |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4756 | * If other directory inodes depended on our current directory |
| 4757 | * inode's move/rename, now do their move/rename operations. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4758 | */ |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 4759 | if (!is_waiting_for_move(sctx, sctx->cur_ino)) { |
| 4760 | ret = apply_children_dir_moves(sctx); |
| 4761 | if (ret) |
| 4762 | goto out; |
| 4763 | } |
| 4764 | |
| 4765 | /* |
| 4766 | * Need to send that every time, no matter if it actually |
| 4767 | * changed between the two trees as we have done changes to |
| 4768 | * the inode before. |
| 4769 | */ |
| 4770 | sctx->send_progress = sctx->cur_ino + 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4771 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); |
| 4772 | if (ret < 0) |
| 4773 | goto out; |
| 4774 | |
| 4775 | out: |
| 4776 | return ret; |
| 4777 | } |
| 4778 | |
| 4779 | static int changed_inode(struct send_ctx *sctx, |
| 4780 | enum btrfs_compare_tree_result result) |
| 4781 | { |
| 4782 | int ret = 0; |
| 4783 | struct btrfs_key *key = sctx->cmp_key; |
| 4784 | struct btrfs_inode_item *left_ii = NULL; |
| 4785 | struct btrfs_inode_item *right_ii = NULL; |
| 4786 | u64 left_gen = 0; |
| 4787 | u64 right_gen = 0; |
| 4788 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4789 | sctx->cur_ino = key->objectid; |
| 4790 | sctx->cur_inode_new_gen = 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 4791 | sctx->cur_inode_last_extent = (u64)-1; |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 4792 | |
| 4793 | /* |
| 4794 | * Set send_progress to current inode. This will tell all get_cur_xxx |
| 4795 | * functions that the current inode's refs are not updated yet. Later, |
| 4796 | * when process_recorded_refs is finished, it is set to cur_ino + 1. |
| 4797 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4798 | sctx->send_progress = sctx->cur_ino; |
| 4799 | |
| 4800 | if (result == BTRFS_COMPARE_TREE_NEW || |
| 4801 | result == BTRFS_COMPARE_TREE_CHANGED) { |
| 4802 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], |
| 4803 | sctx->left_path->slots[0], |
| 4804 | struct btrfs_inode_item); |
| 4805 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], |
| 4806 | left_ii); |
| 4807 | } else { |
| 4808 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 4809 | sctx->right_path->slots[0], |
| 4810 | struct btrfs_inode_item); |
| 4811 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 4812 | right_ii); |
| 4813 | } |
| 4814 | if (result == BTRFS_COMPARE_TREE_CHANGED) { |
| 4815 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 4816 | sctx->right_path->slots[0], |
| 4817 | struct btrfs_inode_item); |
| 4818 | |
| 4819 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 4820 | right_ii); |
Alexander Block | 6d85ed05 | 2012-08-01 14:48:59 +0200 | [diff] [blame] | 4821 | |
| 4822 | /* |
| 4823 | * The cur_ino = root dir case is special here. We can't treat |
| 4824 | * the inode as deleted+reused because it would generate a |
| 4825 | * stream that tries to delete/mkdir the root dir. |
| 4826 | */ |
| 4827 | if (left_gen != right_gen && |
| 4828 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4829 | sctx->cur_inode_new_gen = 1; |
| 4830 | } |
| 4831 | |
| 4832 | if (result == BTRFS_COMPARE_TREE_NEW) { |
| 4833 | sctx->cur_inode_gen = left_gen; |
| 4834 | sctx->cur_inode_new = 1; |
| 4835 | sctx->cur_inode_deleted = 0; |
| 4836 | sctx->cur_inode_size = btrfs_inode_size( |
| 4837 | sctx->left_path->nodes[0], left_ii); |
| 4838 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4839 | sctx->left_path->nodes[0], left_ii); |
| 4840 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4841 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4842 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
| 4843 | sctx->cur_inode_gen = right_gen; |
| 4844 | sctx->cur_inode_new = 0; |
| 4845 | sctx->cur_inode_deleted = 1; |
| 4846 | sctx->cur_inode_size = btrfs_inode_size( |
| 4847 | sctx->right_path->nodes[0], right_ii); |
| 4848 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4849 | sctx->right_path->nodes[0], right_ii); |
| 4850 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4851 | /* |
| 4852 | * We need to do some special handling in case the inode was |
| 4853 | * reported as changed with a changed generation number. This |
| 4854 | * means that the original inode was deleted and new inode |
| 4855 | * reused the same inum. So we have to treat the old inode as |
| 4856 | * deleted and the new one as new. |
| 4857 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4858 | if (sctx->cur_inode_new_gen) { |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4859 | /* |
| 4860 | * First, process the inode as if it was deleted. |
| 4861 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4862 | sctx->cur_inode_gen = right_gen; |
| 4863 | sctx->cur_inode_new = 0; |
| 4864 | sctx->cur_inode_deleted = 1; |
| 4865 | sctx->cur_inode_size = btrfs_inode_size( |
| 4866 | sctx->right_path->nodes[0], right_ii); |
| 4867 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4868 | sctx->right_path->nodes[0], right_ii); |
| 4869 | ret = process_all_refs(sctx, |
| 4870 | BTRFS_COMPARE_TREE_DELETED); |
| 4871 | if (ret < 0) |
| 4872 | goto out; |
| 4873 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4874 | /* |
| 4875 | * Now process the inode as if it was new. |
| 4876 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4877 | sctx->cur_inode_gen = left_gen; |
| 4878 | sctx->cur_inode_new = 1; |
| 4879 | sctx->cur_inode_deleted = 0; |
| 4880 | sctx->cur_inode_size = btrfs_inode_size( |
| 4881 | sctx->left_path->nodes[0], left_ii); |
| 4882 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4883 | sctx->left_path->nodes[0], left_ii); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4884 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4885 | if (ret < 0) |
| 4886 | goto out; |
| 4887 | |
| 4888 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); |
| 4889 | if (ret < 0) |
| 4890 | goto out; |
Alexander Block | e479d9b | 2012-07-28 16:09:35 +0200 | [diff] [blame] | 4891 | /* |
| 4892 | * Advance send_progress now as we did not get into |
| 4893 | * process_recorded_refs_if_needed in the new_gen case. |
| 4894 | */ |
| 4895 | sctx->send_progress = sctx->cur_ino + 1; |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4896 | |
| 4897 | /* |
| 4898 | * Now process all extents and xattrs of the inode as if |
| 4899 | * they were all new. |
| 4900 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4901 | ret = process_all_extents(sctx); |
| 4902 | if (ret < 0) |
| 4903 | goto out; |
| 4904 | ret = process_all_new_xattrs(sctx); |
| 4905 | if (ret < 0) |
| 4906 | goto out; |
| 4907 | } else { |
| 4908 | sctx->cur_inode_gen = left_gen; |
| 4909 | sctx->cur_inode_new = 0; |
| 4910 | sctx->cur_inode_new_gen = 0; |
| 4911 | sctx->cur_inode_deleted = 0; |
| 4912 | sctx->cur_inode_size = btrfs_inode_size( |
| 4913 | sctx->left_path->nodes[0], left_ii); |
| 4914 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4915 | sctx->left_path->nodes[0], left_ii); |
| 4916 | } |
| 4917 | } |
| 4918 | |
| 4919 | out: |
| 4920 | return ret; |
| 4921 | } |
| 4922 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4923 | /* |
| 4924 | * We have to process new refs before deleted refs, but compare_trees gives us |
| 4925 | * the new and deleted refs mixed. To fix this, we record the new/deleted refs |
| 4926 | * first and later process them in process_recorded_refs. |
| 4927 | * For the cur_inode_new_gen case, we skip recording completely because |
| 4928 | * changed_inode did already initiate processing of refs. The reason for this is |
| 4929 | * that in this case, compare_tree actually compares the refs of 2 different |
| 4930 | * inodes. To fix this, process_all_refs is used in changed_inode to handle all |
| 4931 | * refs of the right tree as deleted and all refs of the left tree as new. |
| 4932 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4933 | static int changed_ref(struct send_ctx *sctx, |
| 4934 | enum btrfs_compare_tree_result result) |
| 4935 | { |
| 4936 | int ret = 0; |
| 4937 | |
| 4938 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); |
| 4939 | |
| 4940 | if (!sctx->cur_inode_new_gen && |
| 4941 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 4942 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 4943 | ret = record_new_ref(sctx); |
| 4944 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 4945 | ret = record_deleted_ref(sctx); |
| 4946 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 4947 | ret = record_changed_ref(sctx); |
| 4948 | } |
| 4949 | |
| 4950 | return ret; |
| 4951 | } |
| 4952 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4953 | /* |
| 4954 | * Process new/deleted/changed xattrs. We skip processing in the |
| 4955 | * cur_inode_new_gen case because changed_inode did already initiate processing |
| 4956 | * of xattrs. The reason is the same as in changed_ref |
| 4957 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4958 | static int changed_xattr(struct send_ctx *sctx, |
| 4959 | enum btrfs_compare_tree_result result) |
| 4960 | { |
| 4961 | int ret = 0; |
| 4962 | |
| 4963 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); |
| 4964 | |
| 4965 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 4966 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 4967 | ret = process_new_xattr(sctx); |
| 4968 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 4969 | ret = process_deleted_xattr(sctx); |
| 4970 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 4971 | ret = process_changed_xattr(sctx); |
| 4972 | } |
| 4973 | |
| 4974 | return ret; |
| 4975 | } |
| 4976 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 4977 | /* |
| 4978 | * Process new/deleted/changed extents. We skip processing in the |
| 4979 | * cur_inode_new_gen case because changed_inode did already initiate processing |
| 4980 | * of extents. The reason is the same as in changed_ref |
| 4981 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4982 | static int changed_extent(struct send_ctx *sctx, |
| 4983 | enum btrfs_compare_tree_result result) |
| 4984 | { |
| 4985 | int ret = 0; |
| 4986 | |
| 4987 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); |
| 4988 | |
| 4989 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 4990 | if (result != BTRFS_COMPARE_TREE_DELETED) |
| 4991 | ret = process_extent(sctx, sctx->left_path, |
| 4992 | sctx->cmp_key); |
| 4993 | } |
| 4994 | |
| 4995 | return ret; |
| 4996 | } |
| 4997 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 4998 | static int dir_changed(struct send_ctx *sctx, u64 dir) |
| 4999 | { |
| 5000 | u64 orig_gen, new_gen; |
| 5001 | int ret; |
| 5002 | |
| 5003 | ret = get_inode_info(sctx->send_root, dir, NULL, &new_gen, NULL, NULL, |
| 5004 | NULL, NULL); |
| 5005 | if (ret) |
| 5006 | return ret; |
| 5007 | |
| 5008 | ret = get_inode_info(sctx->parent_root, dir, NULL, &orig_gen, NULL, |
| 5009 | NULL, NULL, NULL); |
| 5010 | if (ret) |
| 5011 | return ret; |
| 5012 | |
| 5013 | return (orig_gen != new_gen) ? 1 : 0; |
| 5014 | } |
| 5015 | |
| 5016 | static int compare_refs(struct send_ctx *sctx, struct btrfs_path *path, |
| 5017 | struct btrfs_key *key) |
| 5018 | { |
| 5019 | struct btrfs_inode_extref *extref; |
| 5020 | struct extent_buffer *leaf; |
| 5021 | u64 dirid = 0, last_dirid = 0; |
| 5022 | unsigned long ptr; |
| 5023 | u32 item_size; |
| 5024 | u32 cur_offset = 0; |
| 5025 | int ref_name_len; |
| 5026 | int ret = 0; |
| 5027 | |
| 5028 | /* Easy case, just check this one dirid */ |
| 5029 | if (key->type == BTRFS_INODE_REF_KEY) { |
| 5030 | dirid = key->offset; |
| 5031 | |
| 5032 | ret = dir_changed(sctx, dirid); |
| 5033 | goto out; |
| 5034 | } |
| 5035 | |
| 5036 | leaf = path->nodes[0]; |
| 5037 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 5038 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 5039 | while (cur_offset < item_size) { |
| 5040 | extref = (struct btrfs_inode_extref *)(ptr + |
| 5041 | cur_offset); |
| 5042 | dirid = btrfs_inode_extref_parent(leaf, extref); |
| 5043 | ref_name_len = btrfs_inode_extref_name_len(leaf, extref); |
| 5044 | cur_offset += ref_name_len + sizeof(*extref); |
| 5045 | if (dirid == last_dirid) |
| 5046 | continue; |
| 5047 | ret = dir_changed(sctx, dirid); |
| 5048 | if (ret) |
| 5049 | break; |
| 5050 | last_dirid = dirid; |
| 5051 | } |
| 5052 | out: |
| 5053 | return ret; |
| 5054 | } |
| 5055 | |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 5056 | /* |
| 5057 | * Updates compare related fields in sctx and simply forwards to the actual |
| 5058 | * changed_xxx functions. |
| 5059 | */ |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5060 | static int changed_cb(struct btrfs_root *left_root, |
| 5061 | struct btrfs_root *right_root, |
| 5062 | struct btrfs_path *left_path, |
| 5063 | struct btrfs_path *right_path, |
| 5064 | struct btrfs_key *key, |
| 5065 | enum btrfs_compare_tree_result result, |
| 5066 | void *ctx) |
| 5067 | { |
| 5068 | int ret = 0; |
| 5069 | struct send_ctx *sctx = ctx; |
| 5070 | |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5071 | if (result == BTRFS_COMPARE_TREE_SAME) { |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5072 | if (key->type == BTRFS_INODE_REF_KEY || |
| 5073 | key->type == BTRFS_INODE_EXTREF_KEY) { |
| 5074 | ret = compare_refs(sctx, left_path, key); |
| 5075 | if (!ret) |
| 5076 | return 0; |
| 5077 | if (ret < 0) |
| 5078 | return ret; |
| 5079 | } else if (key->type == BTRFS_EXTENT_DATA_KEY) { |
| 5080 | return maybe_send_hole(sctx, left_path, key); |
| 5081 | } else { |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5082 | return 0; |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5083 | } |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5084 | result = BTRFS_COMPARE_TREE_CHANGED; |
| 5085 | ret = 0; |
| 5086 | } |
| 5087 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5088 | sctx->left_path = left_path; |
| 5089 | sctx->right_path = right_path; |
| 5090 | sctx->cmp_key = key; |
| 5091 | |
| 5092 | ret = finish_inode_if_needed(sctx, 0); |
| 5093 | if (ret < 0) |
| 5094 | goto out; |
| 5095 | |
Alexander Block | 2981e22 | 2012-08-01 14:47:03 +0200 | [diff] [blame] | 5096 | /* Ignore non-FS objects */ |
| 5097 | if (key->objectid == BTRFS_FREE_INO_OBJECTID || |
| 5098 | key->objectid == BTRFS_FREE_SPACE_OBJECTID) |
| 5099 | goto out; |
| 5100 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5101 | if (key->type == BTRFS_INODE_ITEM_KEY) |
| 5102 | ret = changed_inode(sctx, result); |
Jan Schmidt | 96b5bd7 | 2012-10-15 08:30:45 +0000 | [diff] [blame] | 5103 | else if (key->type == BTRFS_INODE_REF_KEY || |
| 5104 | key->type == BTRFS_INODE_EXTREF_KEY) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5105 | ret = changed_ref(sctx, result); |
| 5106 | else if (key->type == BTRFS_XATTR_ITEM_KEY) |
| 5107 | ret = changed_xattr(sctx, result); |
| 5108 | else if (key->type == BTRFS_EXTENT_DATA_KEY) |
| 5109 | ret = changed_extent(sctx, result); |
| 5110 | |
| 5111 | out: |
| 5112 | return ret; |
| 5113 | } |
| 5114 | |
| 5115 | static int full_send_tree(struct send_ctx *sctx) |
| 5116 | { |
| 5117 | int ret; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5118 | struct btrfs_root *send_root = sctx->send_root; |
| 5119 | struct btrfs_key key; |
| 5120 | struct btrfs_key found_key; |
| 5121 | struct btrfs_path *path; |
| 5122 | struct extent_buffer *eb; |
| 5123 | int slot; |
| 5124 | u64 start_ctransid; |
| 5125 | u64 ctransid; |
| 5126 | |
| 5127 | path = alloc_path_for_send(); |
| 5128 | if (!path) |
| 5129 | return -ENOMEM; |
| 5130 | |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5131 | spin_lock(&send_root->root_item_lock); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5132 | start_ctransid = btrfs_root_ctransid(&send_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5133 | spin_unlock(&send_root->root_item_lock); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5134 | |
| 5135 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 5136 | key.type = BTRFS_INODE_ITEM_KEY; |
| 5137 | key.offset = 0; |
| 5138 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5139 | /* |
Alexander Block | 766702e | 2012-07-28 14:11:31 +0200 | [diff] [blame] | 5140 | * Make sure the tree has not changed after re-joining. We detect this |
| 5141 | * by comparing start_ctransid and ctransid. They should always match. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5142 | */ |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5143 | spin_lock(&send_root->root_item_lock); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5144 | ctransid = btrfs_root_ctransid(&send_root->root_item); |
Anand Jain | 5f3ab90 | 2012-12-07 09:28:54 +0000 | [diff] [blame] | 5145 | spin_unlock(&send_root->root_item_lock); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5146 | |
| 5147 | if (ctransid != start_ctransid) { |
Frank Holton | efe120a | 2013-12-20 11:37:06 -0500 | [diff] [blame] | 5148 | WARN(1, KERN_WARNING "BTRFS: the root that you're trying to " |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5149 | "send was modified in between. This is " |
| 5150 | "probably a bug.\n"); |
| 5151 | ret = -EIO; |
| 5152 | goto out; |
| 5153 | } |
| 5154 | |
| 5155 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); |
| 5156 | if (ret < 0) |
| 5157 | goto out; |
| 5158 | if (ret) |
| 5159 | goto out_finish; |
| 5160 | |
| 5161 | while (1) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5162 | eb = path->nodes[0]; |
| 5163 | slot = path->slots[0]; |
| 5164 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 5165 | |
| 5166 | ret = changed_cb(send_root, NULL, path, NULL, |
| 5167 | &found_key, BTRFS_COMPARE_TREE_NEW, sctx); |
| 5168 | if (ret < 0) |
| 5169 | goto out; |
| 5170 | |
| 5171 | key.objectid = found_key.objectid; |
| 5172 | key.type = found_key.type; |
| 5173 | key.offset = found_key.offset + 1; |
| 5174 | |
| 5175 | ret = btrfs_next_item(send_root, path); |
| 5176 | if (ret < 0) |
| 5177 | goto out; |
| 5178 | if (ret) { |
| 5179 | ret = 0; |
| 5180 | break; |
| 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | out_finish: |
| 5185 | ret = finish_inode_if_needed(sctx, 1); |
| 5186 | |
| 5187 | out: |
| 5188 | btrfs_free_path(path); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5189 | return ret; |
| 5190 | } |
| 5191 | |
| 5192 | static int send_subvol(struct send_ctx *sctx) |
| 5193 | { |
| 5194 | int ret; |
| 5195 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 5196 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_STREAM_HEADER)) { |
| 5197 | ret = send_header(sctx); |
| 5198 | if (ret < 0) |
| 5199 | goto out; |
| 5200 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5201 | |
| 5202 | ret = send_subvol_begin(sctx); |
| 5203 | if (ret < 0) |
| 5204 | goto out; |
| 5205 | |
| 5206 | if (sctx->parent_root) { |
| 5207 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, |
| 5208 | changed_cb, sctx); |
| 5209 | if (ret < 0) |
| 5210 | goto out; |
| 5211 | ret = finish_inode_if_needed(sctx, 1); |
| 5212 | if (ret < 0) |
| 5213 | goto out; |
| 5214 | } else { |
| 5215 | ret = full_send_tree(sctx); |
| 5216 | if (ret < 0) |
| 5217 | goto out; |
| 5218 | } |
| 5219 | |
| 5220 | out: |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5221 | free_recorded_refs(sctx); |
| 5222 | return ret; |
| 5223 | } |
| 5224 | |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 5225 | static void btrfs_root_dec_send_in_progress(struct btrfs_root* root) |
| 5226 | { |
| 5227 | spin_lock(&root->root_item_lock); |
| 5228 | root->send_in_progress--; |
| 5229 | /* |
| 5230 | * Not much left to do, we don't know why it's unbalanced and |
| 5231 | * can't blindly reset it to 0. |
| 5232 | */ |
| 5233 | if (root->send_in_progress < 0) |
| 5234 | btrfs_err(root->fs_info, |
| 5235 | "send_in_progres unbalanced %d root %llu\n", |
| 5236 | root->send_in_progress, root->root_key.objectid); |
| 5237 | spin_unlock(&root->root_item_lock); |
| 5238 | } |
| 5239 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5240 | long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_) |
| 5241 | { |
| 5242 | int ret = 0; |
| 5243 | struct btrfs_root *send_root; |
| 5244 | struct btrfs_root *clone_root; |
| 5245 | struct btrfs_fs_info *fs_info; |
| 5246 | struct btrfs_ioctl_send_args *arg = NULL; |
| 5247 | struct btrfs_key key; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5248 | struct send_ctx *sctx = NULL; |
| 5249 | u32 i; |
| 5250 | u64 *clone_sources_tmp = NULL; |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5251 | int clone_sources_to_rollback = 0; |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 5252 | int sort_clone_roots = 0; |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5253 | int index; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5254 | |
| 5255 | if (!capable(CAP_SYS_ADMIN)) |
| 5256 | return -EPERM; |
| 5257 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 5258 | send_root = BTRFS_I(file_inode(mnt_file))->root; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5259 | fs_info = send_root->fs_info; |
| 5260 | |
Josef Bacik | 139f807 | 2013-05-20 11:26:50 -0400 | [diff] [blame] | 5261 | /* |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5262 | * The subvolume must remain read-only during send, protect against |
| 5263 | * making it RW. |
| 5264 | */ |
| 5265 | spin_lock(&send_root->root_item_lock); |
| 5266 | send_root->send_in_progress++; |
| 5267 | spin_unlock(&send_root->root_item_lock); |
| 5268 | |
| 5269 | /* |
Josef Bacik | 139f807 | 2013-05-20 11:26:50 -0400 | [diff] [blame] | 5270 | * This is done when we lookup the root, it should already be complete |
| 5271 | * by the time we get here. |
| 5272 | */ |
| 5273 | WARN_ON(send_root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE); |
| 5274 | |
| 5275 | /* |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5276 | * Userspace tools do the checks and warn the user if it's |
| 5277 | * not RO. |
| 5278 | */ |
| 5279 | if (!btrfs_root_readonly(send_root)) { |
| 5280 | ret = -EPERM; |
| 5281 | goto out; |
| 5282 | } |
| 5283 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5284 | arg = memdup_user(arg_, sizeof(*arg)); |
| 5285 | if (IS_ERR(arg)) { |
| 5286 | ret = PTR_ERR(arg); |
| 5287 | arg = NULL; |
| 5288 | goto out; |
| 5289 | } |
| 5290 | |
| 5291 | if (!access_ok(VERIFY_READ, arg->clone_sources, |
Dan Carpenter | 700ff4f | 2013-01-10 03:57:25 -0500 | [diff] [blame] | 5292 | sizeof(*arg->clone_sources) * |
| 5293 | arg->clone_sources_count)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5294 | ret = -EFAULT; |
| 5295 | goto out; |
| 5296 | } |
| 5297 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 5298 | if (arg->flags & ~BTRFS_SEND_FLAG_MASK) { |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5299 | ret = -EINVAL; |
| 5300 | goto out; |
| 5301 | } |
| 5302 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5303 | sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS); |
| 5304 | if (!sctx) { |
| 5305 | ret = -ENOMEM; |
| 5306 | goto out; |
| 5307 | } |
| 5308 | |
| 5309 | INIT_LIST_HEAD(&sctx->new_refs); |
| 5310 | INIT_LIST_HEAD(&sctx->deleted_refs); |
| 5311 | INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS); |
| 5312 | INIT_LIST_HEAD(&sctx->name_cache_list); |
| 5313 | |
Mark Fasheh | cb95e7b | 2013-02-04 20:54:57 +0000 | [diff] [blame] | 5314 | sctx->flags = arg->flags; |
| 5315 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5316 | sctx->send_filp = fget(arg->send_fd); |
Tsutomu Itoh | ecc7ada | 2013-04-19 01:04:46 +0000 | [diff] [blame] | 5317 | if (!sctx->send_filp) { |
| 5318 | ret = -EBADF; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5319 | goto out; |
| 5320 | } |
| 5321 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5322 | sctx->send_root = send_root; |
| 5323 | sctx->clone_roots_cnt = arg->clone_sources_count; |
| 5324 | |
| 5325 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE; |
| 5326 | sctx->send_buf = vmalloc(sctx->send_max_size); |
| 5327 | if (!sctx->send_buf) { |
| 5328 | ret = -ENOMEM; |
| 5329 | goto out; |
| 5330 | } |
| 5331 | |
| 5332 | sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE); |
| 5333 | if (!sctx->read_buf) { |
| 5334 | ret = -ENOMEM; |
| 5335 | goto out; |
| 5336 | } |
| 5337 | |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5338 | sctx->pending_dir_moves = RB_ROOT; |
| 5339 | sctx->waiting_dir_moves = RB_ROOT; |
| 5340 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5341 | sctx->clone_roots = vzalloc(sizeof(struct clone_root) * |
| 5342 | (arg->clone_sources_count + 1)); |
| 5343 | if (!sctx->clone_roots) { |
| 5344 | ret = -ENOMEM; |
| 5345 | goto out; |
| 5346 | } |
| 5347 | |
| 5348 | if (arg->clone_sources_count) { |
| 5349 | clone_sources_tmp = vmalloc(arg->clone_sources_count * |
| 5350 | sizeof(*arg->clone_sources)); |
| 5351 | if (!clone_sources_tmp) { |
| 5352 | ret = -ENOMEM; |
| 5353 | goto out; |
| 5354 | } |
| 5355 | |
| 5356 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, |
| 5357 | arg->clone_sources_count * |
| 5358 | sizeof(*arg->clone_sources)); |
| 5359 | if (ret) { |
| 5360 | ret = -EFAULT; |
| 5361 | goto out; |
| 5362 | } |
| 5363 | |
| 5364 | for (i = 0; i < arg->clone_sources_count; i++) { |
| 5365 | key.objectid = clone_sources_tmp[i]; |
| 5366 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 5367 | key.offset = (u64)-1; |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5368 | |
| 5369 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 5370 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5371 | clone_root = btrfs_read_fs_root_no_name(fs_info, &key); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5372 | if (IS_ERR(clone_root)) { |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5373 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5374 | ret = PTR_ERR(clone_root); |
| 5375 | goto out; |
| 5376 | } |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5377 | clone_sources_to_rollback = i + 1; |
| 5378 | spin_lock(&clone_root->root_item_lock); |
| 5379 | clone_root->send_in_progress++; |
| 5380 | if (!btrfs_root_readonly(clone_root)) { |
| 5381 | spin_unlock(&clone_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5382 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5383 | ret = -EPERM; |
| 5384 | goto out; |
| 5385 | } |
| 5386 | spin_unlock(&clone_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5387 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
| 5388 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5389 | sctx->clone_roots[i].root = clone_root; |
| 5390 | } |
| 5391 | vfree(clone_sources_tmp); |
| 5392 | clone_sources_tmp = NULL; |
| 5393 | } |
| 5394 | |
| 5395 | if (arg->parent_root) { |
| 5396 | key.objectid = arg->parent_root; |
| 5397 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 5398 | key.offset = (u64)-1; |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5399 | |
| 5400 | index = srcu_read_lock(&fs_info->subvol_srcu); |
| 5401 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5402 | sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key); |
Stefan Behrens | b1b1959 | 2013-05-13 14:42:57 +0000 | [diff] [blame] | 5403 | if (IS_ERR(sctx->parent_root)) { |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5404 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Stefan Behrens | b1b1959 | 2013-05-13 14:42:57 +0000 | [diff] [blame] | 5405 | ret = PTR_ERR(sctx->parent_root); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5406 | goto out; |
| 5407 | } |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5408 | |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5409 | spin_lock(&sctx->parent_root->root_item_lock); |
| 5410 | sctx->parent_root->send_in_progress++; |
| 5411 | if (!btrfs_root_readonly(sctx->parent_root)) { |
| 5412 | spin_unlock(&sctx->parent_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5413 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5414 | ret = -EPERM; |
| 5415 | goto out; |
| 5416 | } |
| 5417 | spin_unlock(&sctx->parent_root->root_item_lock); |
Wang Shilong | 18f687d | 2014-01-07 17:25:19 +0800 | [diff] [blame] | 5418 | |
| 5419 | srcu_read_unlock(&fs_info->subvol_srcu, index); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | /* |
| 5423 | * Clones from send_root are allowed, but only if the clone source |
| 5424 | * is behind the current send position. This is checked while searching |
| 5425 | * for possible clone sources. |
| 5426 | */ |
| 5427 | sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root; |
| 5428 | |
| 5429 | /* We do a bsearch later */ |
| 5430 | sort(sctx->clone_roots, sctx->clone_roots_cnt, |
| 5431 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, |
| 5432 | NULL); |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 5433 | sort_clone_roots = 1; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5434 | |
| 5435 | ret = send_subvol(sctx); |
| 5436 | if (ret < 0) |
| 5437 | goto out; |
| 5438 | |
Stefan Behrens | c2c7132 | 2013-04-10 17:10:52 +0000 | [diff] [blame] | 5439 | if (!(sctx->flags & BTRFS_SEND_FLAG_OMIT_END_CMD)) { |
| 5440 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); |
| 5441 | if (ret < 0) |
| 5442 | goto out; |
| 5443 | ret = send_cmd(sctx); |
| 5444 | if (ret < 0) |
| 5445 | goto out; |
| 5446 | } |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5447 | |
| 5448 | out: |
Filipe David Borba Manana | 9f03740 | 2014-01-22 10:00:53 +0000 | [diff] [blame] | 5449 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)); |
| 5450 | while (sctx && !RB_EMPTY_ROOT(&sctx->pending_dir_moves)) { |
| 5451 | struct rb_node *n; |
| 5452 | struct pending_dir_move *pm; |
| 5453 | |
| 5454 | n = rb_first(&sctx->pending_dir_moves); |
| 5455 | pm = rb_entry(n, struct pending_dir_move, node); |
| 5456 | while (!list_empty(&pm->list)) { |
| 5457 | struct pending_dir_move *pm2; |
| 5458 | |
| 5459 | pm2 = list_first_entry(&pm->list, |
| 5460 | struct pending_dir_move, list); |
| 5461 | free_pending_move(sctx, pm2); |
| 5462 | } |
| 5463 | free_pending_move(sctx, pm); |
| 5464 | } |
| 5465 | |
| 5466 | WARN_ON(sctx && !ret && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)); |
| 5467 | while (sctx && !RB_EMPTY_ROOT(&sctx->waiting_dir_moves)) { |
| 5468 | struct rb_node *n; |
| 5469 | struct waiting_dir_move *dm; |
| 5470 | |
| 5471 | n = rb_first(&sctx->waiting_dir_moves); |
| 5472 | dm = rb_entry(n, struct waiting_dir_move, node); |
| 5473 | rb_erase(&dm->node, &sctx->waiting_dir_moves); |
| 5474 | kfree(dm); |
| 5475 | } |
| 5476 | |
Wang Shilong | 896c14f | 2014-01-07 17:25:18 +0800 | [diff] [blame] | 5477 | if (sort_clone_roots) { |
| 5478 | for (i = 0; i < sctx->clone_roots_cnt; i++) |
| 5479 | btrfs_root_dec_send_in_progress( |
| 5480 | sctx->clone_roots[i].root); |
| 5481 | } else { |
| 5482 | for (i = 0; sctx && i < clone_sources_to_rollback; i++) |
| 5483 | btrfs_root_dec_send_in_progress( |
| 5484 | sctx->clone_roots[i].root); |
| 5485 | |
| 5486 | btrfs_root_dec_send_in_progress(send_root); |
| 5487 | } |
David Sterba | 66ef7d6 | 2013-12-17 15:07:20 +0100 | [diff] [blame] | 5488 | if (sctx && !IS_ERR_OR_NULL(sctx->parent_root)) |
| 5489 | btrfs_root_dec_send_in_progress(sctx->parent_root); |
David Sterba | 2c68653 | 2013-12-16 17:34:17 +0100 | [diff] [blame] | 5490 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 5491 | kfree(arg); |
| 5492 | vfree(clone_sources_tmp); |
| 5493 | |
| 5494 | if (sctx) { |
| 5495 | if (sctx->send_filp) |
| 5496 | fput(sctx->send_filp); |
| 5497 | |
| 5498 | vfree(sctx->clone_roots); |
| 5499 | vfree(sctx->send_buf); |
| 5500 | vfree(sctx->read_buf); |
| 5501 | |
| 5502 | name_cache_free(sctx); |
| 5503 | |
| 5504 | kfree(sctx); |
| 5505 | } |
| 5506 | |
| 5507 | return ret; |
| 5508 | } |