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