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> |
| 27 | #include <linux/crc32c.h> |
Stephen Rothwell | a1857eb | 2012-07-27 10:11:13 +1000 | [diff] [blame] | 28 | #include <linux/vmalloc.h> |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 29 | |
| 30 | #include "send.h" |
| 31 | #include "backref.h" |
| 32 | #include "locking.h" |
| 33 | #include "disk-io.h" |
| 34 | #include "btrfs_inode.h" |
| 35 | #include "transaction.h" |
| 36 | |
| 37 | static int g_verbose = 0; |
| 38 | |
| 39 | #define verbose_printk(...) if (g_verbose) printk(__VA_ARGS__) |
| 40 | |
| 41 | /* |
| 42 | * A fs_path is a helper to dynamically build path names with unknown size. |
| 43 | * It reallocates the internal buffer on demand. |
| 44 | * It allows fast adding of path elements on the right side (normal path) and |
| 45 | * fast adding to the left side (reversed path). A reversed path can also be |
| 46 | * unreversed if needed. |
| 47 | */ |
| 48 | struct fs_path { |
| 49 | union { |
| 50 | struct { |
| 51 | char *start; |
| 52 | char *end; |
| 53 | char *prepared; |
| 54 | |
| 55 | char *buf; |
| 56 | int buf_len; |
| 57 | int reversed:1; |
| 58 | int virtual_mem:1; |
| 59 | char inline_buf[]; |
| 60 | }; |
| 61 | char pad[PAGE_SIZE]; |
| 62 | }; |
| 63 | }; |
| 64 | #define FS_PATH_INLINE_SIZE \ |
| 65 | (sizeof(struct fs_path) - offsetof(struct fs_path, inline_buf)) |
| 66 | |
| 67 | |
| 68 | /* reused for each extent */ |
| 69 | struct clone_root { |
| 70 | struct btrfs_root *root; |
| 71 | u64 ino; |
| 72 | u64 offset; |
| 73 | |
| 74 | u64 found_refs; |
| 75 | }; |
| 76 | |
| 77 | #define SEND_CTX_MAX_NAME_CACHE_SIZE 128 |
| 78 | #define SEND_CTX_NAME_CACHE_CLEAN_SIZE (SEND_CTX_MAX_NAME_CACHE_SIZE * 2) |
| 79 | |
| 80 | struct send_ctx { |
| 81 | struct file *send_filp; |
| 82 | loff_t send_off; |
| 83 | char *send_buf; |
| 84 | u32 send_size; |
| 85 | u32 send_max_size; |
| 86 | u64 total_send_size; |
| 87 | u64 cmd_send_size[BTRFS_SEND_C_MAX + 1]; |
| 88 | |
| 89 | struct vfsmount *mnt; |
| 90 | |
| 91 | struct btrfs_root *send_root; |
| 92 | struct btrfs_root *parent_root; |
| 93 | struct clone_root *clone_roots; |
| 94 | int clone_roots_cnt; |
| 95 | |
| 96 | /* current state of the compare_tree call */ |
| 97 | struct btrfs_path *left_path; |
| 98 | struct btrfs_path *right_path; |
| 99 | struct btrfs_key *cmp_key; |
| 100 | |
| 101 | /* |
| 102 | * infos of the currently processed inode. In case of deleted inodes, |
| 103 | * these are the values from the deleted inode. |
| 104 | */ |
| 105 | u64 cur_ino; |
| 106 | u64 cur_inode_gen; |
| 107 | int cur_inode_new; |
| 108 | int cur_inode_new_gen; |
| 109 | int cur_inode_deleted; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 110 | u64 cur_inode_size; |
| 111 | u64 cur_inode_mode; |
| 112 | |
| 113 | u64 send_progress; |
| 114 | |
| 115 | struct list_head new_refs; |
| 116 | struct list_head deleted_refs; |
| 117 | |
| 118 | struct radix_tree_root name_cache; |
| 119 | struct list_head name_cache_list; |
| 120 | int name_cache_size; |
| 121 | |
| 122 | struct file *cur_inode_filp; |
| 123 | char *read_buf; |
| 124 | }; |
| 125 | |
| 126 | struct name_cache_entry { |
| 127 | struct list_head list; |
| 128 | struct list_head use_list; |
| 129 | u64 ino; |
| 130 | u64 gen; |
| 131 | u64 parent_ino; |
| 132 | u64 parent_gen; |
| 133 | int ret; |
| 134 | int need_later_update; |
| 135 | int name_len; |
| 136 | char name[]; |
| 137 | }; |
| 138 | |
| 139 | static void fs_path_reset(struct fs_path *p) |
| 140 | { |
| 141 | if (p->reversed) { |
| 142 | p->start = p->buf + p->buf_len - 1; |
| 143 | p->end = p->start; |
| 144 | *p->start = 0; |
| 145 | } else { |
| 146 | p->start = p->buf; |
| 147 | p->end = p->start; |
| 148 | *p->start = 0; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static struct fs_path *fs_path_alloc(struct send_ctx *sctx) |
| 153 | { |
| 154 | struct fs_path *p; |
| 155 | |
| 156 | p = kmalloc(sizeof(*p), GFP_NOFS); |
| 157 | if (!p) |
| 158 | return NULL; |
| 159 | p->reversed = 0; |
| 160 | p->virtual_mem = 0; |
| 161 | p->buf = p->inline_buf; |
| 162 | p->buf_len = FS_PATH_INLINE_SIZE; |
| 163 | fs_path_reset(p); |
| 164 | return p; |
| 165 | } |
| 166 | |
| 167 | static struct fs_path *fs_path_alloc_reversed(struct send_ctx *sctx) |
| 168 | { |
| 169 | struct fs_path *p; |
| 170 | |
| 171 | p = fs_path_alloc(sctx); |
| 172 | if (!p) |
| 173 | return NULL; |
| 174 | p->reversed = 1; |
| 175 | fs_path_reset(p); |
| 176 | return p; |
| 177 | } |
| 178 | |
| 179 | static void fs_path_free(struct send_ctx *sctx, struct fs_path *p) |
| 180 | { |
| 181 | if (!p) |
| 182 | return; |
| 183 | if (p->buf != p->inline_buf) { |
| 184 | if (p->virtual_mem) |
| 185 | vfree(p->buf); |
| 186 | else |
| 187 | kfree(p->buf); |
| 188 | } |
| 189 | kfree(p); |
| 190 | } |
| 191 | |
| 192 | static int fs_path_len(struct fs_path *p) |
| 193 | { |
| 194 | return p->end - p->start; |
| 195 | } |
| 196 | |
| 197 | static int fs_path_ensure_buf(struct fs_path *p, int len) |
| 198 | { |
| 199 | char *tmp_buf; |
| 200 | int path_len; |
| 201 | int old_buf_len; |
| 202 | |
| 203 | len++; |
| 204 | |
| 205 | if (p->buf_len >= len) |
| 206 | return 0; |
| 207 | |
| 208 | path_len = p->end - p->start; |
| 209 | old_buf_len = p->buf_len; |
| 210 | len = PAGE_ALIGN(len); |
| 211 | |
| 212 | if (p->buf == p->inline_buf) { |
| 213 | tmp_buf = kmalloc(len, GFP_NOFS); |
| 214 | if (!tmp_buf) { |
| 215 | tmp_buf = vmalloc(len); |
| 216 | if (!tmp_buf) |
| 217 | return -ENOMEM; |
| 218 | p->virtual_mem = 1; |
| 219 | } |
| 220 | memcpy(tmp_buf, p->buf, p->buf_len); |
| 221 | p->buf = tmp_buf; |
| 222 | p->buf_len = len; |
| 223 | } else { |
| 224 | if (p->virtual_mem) { |
| 225 | tmp_buf = vmalloc(len); |
| 226 | if (!tmp_buf) |
| 227 | return -ENOMEM; |
| 228 | memcpy(tmp_buf, p->buf, p->buf_len); |
| 229 | vfree(p->buf); |
| 230 | } else { |
| 231 | tmp_buf = krealloc(p->buf, len, GFP_NOFS); |
| 232 | if (!tmp_buf) { |
| 233 | tmp_buf = vmalloc(len); |
| 234 | if (!tmp_buf) |
| 235 | return -ENOMEM; |
| 236 | memcpy(tmp_buf, p->buf, p->buf_len); |
| 237 | kfree(p->buf); |
| 238 | p->virtual_mem = 1; |
| 239 | } |
| 240 | } |
| 241 | p->buf = tmp_buf; |
| 242 | p->buf_len = len; |
| 243 | } |
| 244 | if (p->reversed) { |
| 245 | tmp_buf = p->buf + old_buf_len - path_len - 1; |
| 246 | p->end = p->buf + p->buf_len - 1; |
| 247 | p->start = p->end - path_len; |
| 248 | memmove(p->start, tmp_buf, path_len + 1); |
| 249 | } else { |
| 250 | p->start = p->buf; |
| 251 | p->end = p->start + path_len; |
| 252 | } |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int fs_path_prepare_for_add(struct fs_path *p, int name_len) |
| 257 | { |
| 258 | int ret; |
| 259 | int new_len; |
| 260 | |
| 261 | new_len = p->end - p->start + name_len; |
| 262 | if (p->start != p->end) |
| 263 | new_len++; |
| 264 | ret = fs_path_ensure_buf(p, new_len); |
| 265 | if (ret < 0) |
| 266 | goto out; |
| 267 | |
| 268 | if (p->reversed) { |
| 269 | if (p->start != p->end) |
| 270 | *--p->start = '/'; |
| 271 | p->start -= name_len; |
| 272 | p->prepared = p->start; |
| 273 | } else { |
| 274 | if (p->start != p->end) |
| 275 | *p->end++ = '/'; |
| 276 | p->prepared = p->end; |
| 277 | p->end += name_len; |
| 278 | *p->end = 0; |
| 279 | } |
| 280 | |
| 281 | out: |
| 282 | return ret; |
| 283 | } |
| 284 | |
| 285 | static int fs_path_add(struct fs_path *p, const char *name, int name_len) |
| 286 | { |
| 287 | int ret; |
| 288 | |
| 289 | ret = fs_path_prepare_for_add(p, name_len); |
| 290 | if (ret < 0) |
| 291 | goto out; |
| 292 | memcpy(p->prepared, name, name_len); |
| 293 | p->prepared = NULL; |
| 294 | |
| 295 | out: |
| 296 | return ret; |
| 297 | } |
| 298 | |
| 299 | static int fs_path_add_path(struct fs_path *p, struct fs_path *p2) |
| 300 | { |
| 301 | int ret; |
| 302 | |
| 303 | ret = fs_path_prepare_for_add(p, p2->end - p2->start); |
| 304 | if (ret < 0) |
| 305 | goto out; |
| 306 | memcpy(p->prepared, p2->start, p2->end - p2->start); |
| 307 | p->prepared = NULL; |
| 308 | |
| 309 | out: |
| 310 | return ret; |
| 311 | } |
| 312 | |
| 313 | static int fs_path_add_from_extent_buffer(struct fs_path *p, |
| 314 | struct extent_buffer *eb, |
| 315 | unsigned long off, int len) |
| 316 | { |
| 317 | int ret; |
| 318 | |
| 319 | ret = fs_path_prepare_for_add(p, len); |
| 320 | if (ret < 0) |
| 321 | goto out; |
| 322 | |
| 323 | read_extent_buffer(eb, p->prepared, off, len); |
| 324 | p->prepared = NULL; |
| 325 | |
| 326 | out: |
| 327 | return ret; |
| 328 | } |
| 329 | |
Alexander Block | 9ea3ef5 | 2012-07-28 11:08:09 +0200 | [diff] [blame^] | 330 | #if 0 |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 331 | static void fs_path_remove(struct fs_path *p) |
| 332 | { |
| 333 | BUG_ON(p->reversed); |
| 334 | while (p->start != p->end && *p->end != '/') |
| 335 | p->end--; |
| 336 | *p->end = 0; |
| 337 | } |
Alexander Block | 9ea3ef5 | 2012-07-28 11:08:09 +0200 | [diff] [blame^] | 338 | #endif |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 339 | |
| 340 | static int fs_path_copy(struct fs_path *p, struct fs_path *from) |
| 341 | { |
| 342 | int ret; |
| 343 | |
| 344 | p->reversed = from->reversed; |
| 345 | fs_path_reset(p); |
| 346 | |
| 347 | ret = fs_path_add_path(p, from); |
| 348 | |
| 349 | return ret; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | static void fs_path_unreverse(struct fs_path *p) |
| 354 | { |
| 355 | char *tmp; |
| 356 | int len; |
| 357 | |
| 358 | if (!p->reversed) |
| 359 | return; |
| 360 | |
| 361 | tmp = p->start; |
| 362 | len = p->end - p->start; |
| 363 | p->start = p->buf; |
| 364 | p->end = p->start + len; |
| 365 | memmove(p->start, tmp, len + 1); |
| 366 | p->reversed = 0; |
| 367 | } |
| 368 | |
| 369 | static struct btrfs_path *alloc_path_for_send(void) |
| 370 | { |
| 371 | struct btrfs_path *path; |
| 372 | |
| 373 | path = btrfs_alloc_path(); |
| 374 | if (!path) |
| 375 | return NULL; |
| 376 | path->search_commit_root = 1; |
| 377 | path->skip_locking = 1; |
| 378 | return path; |
| 379 | } |
| 380 | |
| 381 | static int write_buf(struct send_ctx *sctx, const void *buf, u32 len) |
| 382 | { |
| 383 | int ret; |
| 384 | mm_segment_t old_fs; |
| 385 | u32 pos = 0; |
| 386 | |
| 387 | old_fs = get_fs(); |
| 388 | set_fs(KERNEL_DS); |
| 389 | |
| 390 | while (pos < len) { |
| 391 | ret = vfs_write(sctx->send_filp, (char *)buf + pos, len - pos, |
| 392 | &sctx->send_off); |
| 393 | /* TODO handle that correctly */ |
| 394 | /*if (ret == -ERESTARTSYS) { |
| 395 | continue; |
| 396 | }*/ |
| 397 | if (ret < 0) |
| 398 | goto out; |
| 399 | if (ret == 0) { |
| 400 | ret = -EIO; |
| 401 | goto out; |
| 402 | } |
| 403 | pos += ret; |
| 404 | } |
| 405 | |
| 406 | ret = 0; |
| 407 | |
| 408 | out: |
| 409 | set_fs(old_fs); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static int tlv_put(struct send_ctx *sctx, u16 attr, const void *data, int len) |
| 414 | { |
| 415 | struct btrfs_tlv_header *hdr; |
| 416 | int total_len = sizeof(*hdr) + len; |
| 417 | int left = sctx->send_max_size - sctx->send_size; |
| 418 | |
| 419 | if (unlikely(left < total_len)) |
| 420 | return -EOVERFLOW; |
| 421 | |
| 422 | hdr = (struct btrfs_tlv_header *) (sctx->send_buf + sctx->send_size); |
| 423 | hdr->tlv_type = cpu_to_le16(attr); |
| 424 | hdr->tlv_len = cpu_to_le16(len); |
| 425 | memcpy(hdr + 1, data, len); |
| 426 | sctx->send_size += total_len; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | #if 0 |
| 432 | static int tlv_put_u8(struct send_ctx *sctx, u16 attr, u8 value) |
| 433 | { |
| 434 | return tlv_put(sctx, attr, &value, sizeof(value)); |
| 435 | } |
| 436 | |
| 437 | static int tlv_put_u16(struct send_ctx *sctx, u16 attr, u16 value) |
| 438 | { |
| 439 | __le16 tmp = cpu_to_le16(value); |
| 440 | return tlv_put(sctx, attr, &tmp, sizeof(tmp)); |
| 441 | } |
| 442 | |
| 443 | static int tlv_put_u32(struct send_ctx *sctx, u16 attr, u32 value) |
| 444 | { |
| 445 | __le32 tmp = cpu_to_le32(value); |
| 446 | return tlv_put(sctx, attr, &tmp, sizeof(tmp)); |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | static int tlv_put_u64(struct send_ctx *sctx, u16 attr, u64 value) |
| 451 | { |
| 452 | __le64 tmp = cpu_to_le64(value); |
| 453 | return tlv_put(sctx, attr, &tmp, sizeof(tmp)); |
| 454 | } |
| 455 | |
| 456 | static int tlv_put_string(struct send_ctx *sctx, u16 attr, |
| 457 | const char *str, int len) |
| 458 | { |
| 459 | if (len == -1) |
| 460 | len = strlen(str); |
| 461 | return tlv_put(sctx, attr, str, len); |
| 462 | } |
| 463 | |
| 464 | static int tlv_put_uuid(struct send_ctx *sctx, u16 attr, |
| 465 | const u8 *uuid) |
| 466 | { |
| 467 | return tlv_put(sctx, attr, uuid, BTRFS_UUID_SIZE); |
| 468 | } |
| 469 | |
| 470 | #if 0 |
| 471 | static int tlv_put_timespec(struct send_ctx *sctx, u16 attr, |
| 472 | struct timespec *ts) |
| 473 | { |
| 474 | struct btrfs_timespec bts; |
| 475 | bts.sec = cpu_to_le64(ts->tv_sec); |
| 476 | bts.nsec = cpu_to_le32(ts->tv_nsec); |
| 477 | return tlv_put(sctx, attr, &bts, sizeof(bts)); |
| 478 | } |
| 479 | #endif |
| 480 | |
| 481 | static int tlv_put_btrfs_timespec(struct send_ctx *sctx, u16 attr, |
| 482 | struct extent_buffer *eb, |
| 483 | struct btrfs_timespec *ts) |
| 484 | { |
| 485 | struct btrfs_timespec bts; |
| 486 | read_extent_buffer(eb, &bts, (unsigned long)ts, sizeof(bts)); |
| 487 | return tlv_put(sctx, attr, &bts, sizeof(bts)); |
| 488 | } |
| 489 | |
| 490 | |
| 491 | #define TLV_PUT(sctx, attrtype, attrlen, data) \ |
| 492 | do { \ |
| 493 | ret = tlv_put(sctx, attrtype, attrlen, data); \ |
| 494 | if (ret < 0) \ |
| 495 | goto tlv_put_failure; \ |
| 496 | } while (0) |
| 497 | |
| 498 | #define TLV_PUT_INT(sctx, attrtype, bits, value) \ |
| 499 | do { \ |
| 500 | ret = tlv_put_u##bits(sctx, attrtype, value); \ |
| 501 | if (ret < 0) \ |
| 502 | goto tlv_put_failure; \ |
| 503 | } while (0) |
| 504 | |
| 505 | #define TLV_PUT_U8(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 8, data) |
| 506 | #define TLV_PUT_U16(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 16, data) |
| 507 | #define TLV_PUT_U32(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 32, data) |
| 508 | #define TLV_PUT_U64(sctx, attrtype, data) TLV_PUT_INT(sctx, attrtype, 64, data) |
| 509 | #define TLV_PUT_STRING(sctx, attrtype, str, len) \ |
| 510 | do { \ |
| 511 | ret = tlv_put_string(sctx, attrtype, str, len); \ |
| 512 | if (ret < 0) \ |
| 513 | goto tlv_put_failure; \ |
| 514 | } while (0) |
| 515 | #define TLV_PUT_PATH(sctx, attrtype, p) \ |
| 516 | do { \ |
| 517 | ret = tlv_put_string(sctx, attrtype, p->start, \ |
| 518 | p->end - p->start); \ |
| 519 | if (ret < 0) \ |
| 520 | goto tlv_put_failure; \ |
| 521 | } while(0) |
| 522 | #define TLV_PUT_UUID(sctx, attrtype, uuid) \ |
| 523 | do { \ |
| 524 | ret = tlv_put_uuid(sctx, attrtype, uuid); \ |
| 525 | if (ret < 0) \ |
| 526 | goto tlv_put_failure; \ |
| 527 | } while (0) |
| 528 | #define TLV_PUT_TIMESPEC(sctx, attrtype, ts) \ |
| 529 | do { \ |
| 530 | ret = tlv_put_timespec(sctx, attrtype, ts); \ |
| 531 | if (ret < 0) \ |
| 532 | goto tlv_put_failure; \ |
| 533 | } while (0) |
| 534 | #define TLV_PUT_BTRFS_TIMESPEC(sctx, attrtype, eb, ts) \ |
| 535 | do { \ |
| 536 | ret = tlv_put_btrfs_timespec(sctx, attrtype, eb, ts); \ |
| 537 | if (ret < 0) \ |
| 538 | goto tlv_put_failure; \ |
| 539 | } while (0) |
| 540 | |
| 541 | static int send_header(struct send_ctx *sctx) |
| 542 | { |
| 543 | struct btrfs_stream_header hdr; |
| 544 | |
| 545 | strcpy(hdr.magic, BTRFS_SEND_STREAM_MAGIC); |
| 546 | hdr.version = cpu_to_le32(BTRFS_SEND_STREAM_VERSION); |
| 547 | |
| 548 | return write_buf(sctx, &hdr, sizeof(hdr)); |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * For each command/item we want to send to userspace, we call this function. |
| 553 | */ |
| 554 | static int begin_cmd(struct send_ctx *sctx, int cmd) |
| 555 | { |
| 556 | struct btrfs_cmd_header *hdr; |
| 557 | |
| 558 | if (!sctx->send_buf) { |
| 559 | WARN_ON(1); |
| 560 | return -EINVAL; |
| 561 | } |
| 562 | |
| 563 | BUG_ON(sctx->send_size); |
| 564 | |
| 565 | sctx->send_size += sizeof(*hdr); |
| 566 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 567 | hdr->cmd = cpu_to_le16(cmd); |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int send_cmd(struct send_ctx *sctx) |
| 573 | { |
| 574 | int ret; |
| 575 | struct btrfs_cmd_header *hdr; |
| 576 | u32 crc; |
| 577 | |
| 578 | hdr = (struct btrfs_cmd_header *)sctx->send_buf; |
| 579 | hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr)); |
| 580 | hdr->crc = 0; |
| 581 | |
| 582 | crc = crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size); |
| 583 | hdr->crc = cpu_to_le32(crc); |
| 584 | |
| 585 | ret = write_buf(sctx, sctx->send_buf, sctx->send_size); |
| 586 | |
| 587 | sctx->total_send_size += sctx->send_size; |
| 588 | sctx->cmd_send_size[le16_to_cpu(hdr->cmd)] += sctx->send_size; |
| 589 | sctx->send_size = 0; |
| 590 | |
| 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Sends a move instruction to user space |
| 596 | */ |
| 597 | static int send_rename(struct send_ctx *sctx, |
| 598 | struct fs_path *from, struct fs_path *to) |
| 599 | { |
| 600 | int ret; |
| 601 | |
| 602 | verbose_printk("btrfs: send_rename %s -> %s\n", from->start, to->start); |
| 603 | |
| 604 | ret = begin_cmd(sctx, BTRFS_SEND_C_RENAME); |
| 605 | if (ret < 0) |
| 606 | goto out; |
| 607 | |
| 608 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, from); |
| 609 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_TO, to); |
| 610 | |
| 611 | ret = send_cmd(sctx); |
| 612 | |
| 613 | tlv_put_failure: |
| 614 | out: |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Sends a link instruction to user space |
| 620 | */ |
| 621 | static int send_link(struct send_ctx *sctx, |
| 622 | struct fs_path *path, struct fs_path *lnk) |
| 623 | { |
| 624 | int ret; |
| 625 | |
| 626 | verbose_printk("btrfs: send_link %s -> %s\n", path->start, lnk->start); |
| 627 | |
| 628 | ret = begin_cmd(sctx, BTRFS_SEND_C_LINK); |
| 629 | if (ret < 0) |
| 630 | goto out; |
| 631 | |
| 632 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 633 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, lnk); |
| 634 | |
| 635 | ret = send_cmd(sctx); |
| 636 | |
| 637 | tlv_put_failure: |
| 638 | out: |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | /* |
| 643 | * Sends an unlink instruction to user space |
| 644 | */ |
| 645 | static int send_unlink(struct send_ctx *sctx, struct fs_path *path) |
| 646 | { |
| 647 | int ret; |
| 648 | |
| 649 | verbose_printk("btrfs: send_unlink %s\n", path->start); |
| 650 | |
| 651 | ret = begin_cmd(sctx, BTRFS_SEND_C_UNLINK); |
| 652 | if (ret < 0) |
| 653 | goto out; |
| 654 | |
| 655 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 656 | |
| 657 | ret = send_cmd(sctx); |
| 658 | |
| 659 | tlv_put_failure: |
| 660 | out: |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Sends a rmdir instruction to user space |
| 666 | */ |
| 667 | static int send_rmdir(struct send_ctx *sctx, struct fs_path *path) |
| 668 | { |
| 669 | int ret; |
| 670 | |
| 671 | verbose_printk("btrfs: send_rmdir %s\n", path->start); |
| 672 | |
| 673 | ret = begin_cmd(sctx, BTRFS_SEND_C_RMDIR); |
| 674 | if (ret < 0) |
| 675 | goto out; |
| 676 | |
| 677 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 678 | |
| 679 | ret = send_cmd(sctx); |
| 680 | |
| 681 | tlv_put_failure: |
| 682 | out: |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | /* |
| 687 | * Helper function to retrieve some fields from an inode item. |
| 688 | */ |
| 689 | static int get_inode_info(struct btrfs_root *root, |
| 690 | u64 ino, u64 *size, u64 *gen, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 691 | u64 *mode, u64 *uid, u64 *gid, |
| 692 | u64 *rdev) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 693 | { |
| 694 | int ret; |
| 695 | struct btrfs_inode_item *ii; |
| 696 | struct btrfs_key key; |
| 697 | struct btrfs_path *path; |
| 698 | |
| 699 | path = alloc_path_for_send(); |
| 700 | if (!path) |
| 701 | return -ENOMEM; |
| 702 | |
| 703 | key.objectid = ino; |
| 704 | key.type = BTRFS_INODE_ITEM_KEY; |
| 705 | key.offset = 0; |
| 706 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 707 | if (ret < 0) |
| 708 | goto out; |
| 709 | if (ret) { |
| 710 | ret = -ENOENT; |
| 711 | goto out; |
| 712 | } |
| 713 | |
| 714 | ii = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 715 | struct btrfs_inode_item); |
| 716 | if (size) |
| 717 | *size = btrfs_inode_size(path->nodes[0], ii); |
| 718 | if (gen) |
| 719 | *gen = btrfs_inode_generation(path->nodes[0], ii); |
| 720 | if (mode) |
| 721 | *mode = btrfs_inode_mode(path->nodes[0], ii); |
| 722 | if (uid) |
| 723 | *uid = btrfs_inode_uid(path->nodes[0], ii); |
| 724 | if (gid) |
| 725 | *gid = btrfs_inode_gid(path->nodes[0], ii); |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 726 | if (rdev) |
| 727 | *rdev = btrfs_inode_rdev(path->nodes[0], ii); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 728 | |
| 729 | out: |
| 730 | btrfs_free_path(path); |
| 731 | return ret; |
| 732 | } |
| 733 | |
| 734 | typedef int (*iterate_inode_ref_t)(int num, u64 dir, int index, |
| 735 | struct fs_path *p, |
| 736 | void *ctx); |
| 737 | |
| 738 | /* |
| 739 | * Helper function to iterate the entries in ONE btrfs_inode_ref. |
| 740 | * The iterate callback may return a non zero value to stop iteration. This can |
| 741 | * be a negative value for error codes or 1 to simply stop it. |
| 742 | * |
| 743 | * path must point to the INODE_REF when called. |
| 744 | */ |
| 745 | static int iterate_inode_ref(struct send_ctx *sctx, |
| 746 | struct btrfs_root *root, struct btrfs_path *path, |
| 747 | struct btrfs_key *found_key, int resolve, |
| 748 | iterate_inode_ref_t iterate, void *ctx) |
| 749 | { |
| 750 | struct extent_buffer *eb; |
| 751 | struct btrfs_item *item; |
| 752 | struct btrfs_inode_ref *iref; |
| 753 | struct btrfs_path *tmp_path; |
| 754 | struct fs_path *p; |
| 755 | u32 cur; |
| 756 | u32 len; |
| 757 | u32 total; |
| 758 | int slot; |
| 759 | u32 name_len; |
| 760 | char *start; |
| 761 | int ret = 0; |
| 762 | int num; |
| 763 | int index; |
| 764 | |
| 765 | p = fs_path_alloc_reversed(sctx); |
| 766 | if (!p) |
| 767 | return -ENOMEM; |
| 768 | |
| 769 | tmp_path = alloc_path_for_send(); |
| 770 | if (!tmp_path) { |
| 771 | fs_path_free(sctx, p); |
| 772 | return -ENOMEM; |
| 773 | } |
| 774 | |
| 775 | eb = path->nodes[0]; |
| 776 | slot = path->slots[0]; |
| 777 | item = btrfs_item_nr(eb, slot); |
| 778 | iref = btrfs_item_ptr(eb, slot, struct btrfs_inode_ref); |
| 779 | cur = 0; |
| 780 | len = 0; |
| 781 | total = btrfs_item_size(eb, item); |
| 782 | |
| 783 | num = 0; |
| 784 | while (cur < total) { |
| 785 | fs_path_reset(p); |
| 786 | |
| 787 | name_len = btrfs_inode_ref_name_len(eb, iref); |
| 788 | index = btrfs_inode_ref_index(eb, iref); |
| 789 | if (resolve) { |
| 790 | start = btrfs_iref_to_path(root, tmp_path, iref, eb, |
| 791 | found_key->offset, p->buf, |
| 792 | p->buf_len); |
| 793 | if (IS_ERR(start)) { |
| 794 | ret = PTR_ERR(start); |
| 795 | goto out; |
| 796 | } |
| 797 | if (start < p->buf) { |
| 798 | /* overflow , try again with larger buffer */ |
| 799 | ret = fs_path_ensure_buf(p, |
| 800 | p->buf_len + p->buf - start); |
| 801 | if (ret < 0) |
| 802 | goto out; |
| 803 | start = btrfs_iref_to_path(root, tmp_path, iref, |
| 804 | eb, found_key->offset, p->buf, |
| 805 | p->buf_len); |
| 806 | if (IS_ERR(start)) { |
| 807 | ret = PTR_ERR(start); |
| 808 | goto out; |
| 809 | } |
| 810 | BUG_ON(start < p->buf); |
| 811 | } |
| 812 | p->start = start; |
| 813 | } else { |
| 814 | ret = fs_path_add_from_extent_buffer(p, eb, |
| 815 | (unsigned long)(iref + 1), name_len); |
| 816 | if (ret < 0) |
| 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | len = sizeof(*iref) + name_len; |
| 822 | iref = (struct btrfs_inode_ref *)((char *)iref + len); |
| 823 | cur += len; |
| 824 | |
| 825 | ret = iterate(num, found_key->offset, index, p, ctx); |
| 826 | if (ret) |
| 827 | goto out; |
| 828 | |
| 829 | num++; |
| 830 | } |
| 831 | |
| 832 | out: |
| 833 | btrfs_free_path(tmp_path); |
| 834 | fs_path_free(sctx, p); |
| 835 | return ret; |
| 836 | } |
| 837 | |
| 838 | typedef int (*iterate_dir_item_t)(int num, struct btrfs_key *di_key, |
| 839 | const char *name, int name_len, |
| 840 | const char *data, int data_len, |
| 841 | u8 type, void *ctx); |
| 842 | |
| 843 | /* |
| 844 | * Helper function to iterate the entries in ONE btrfs_dir_item. |
| 845 | * The iterate callback may return a non zero value to stop iteration. This can |
| 846 | * be a negative value for error codes or 1 to simply stop it. |
| 847 | * |
| 848 | * path must point to the dir item when called. |
| 849 | */ |
| 850 | static int iterate_dir_item(struct send_ctx *sctx, |
| 851 | struct btrfs_root *root, struct btrfs_path *path, |
| 852 | struct btrfs_key *found_key, |
| 853 | iterate_dir_item_t iterate, void *ctx) |
| 854 | { |
| 855 | int ret = 0; |
| 856 | struct extent_buffer *eb; |
| 857 | struct btrfs_item *item; |
| 858 | struct btrfs_dir_item *di; |
| 859 | struct btrfs_path *tmp_path = NULL; |
| 860 | struct btrfs_key di_key; |
| 861 | char *buf = NULL; |
| 862 | char *buf2 = NULL; |
| 863 | int buf_len; |
| 864 | int buf_virtual = 0; |
| 865 | u32 name_len; |
| 866 | u32 data_len; |
| 867 | u32 cur; |
| 868 | u32 len; |
| 869 | u32 total; |
| 870 | int slot; |
| 871 | int num; |
| 872 | u8 type; |
| 873 | |
| 874 | buf_len = PAGE_SIZE; |
| 875 | buf = kmalloc(buf_len, GFP_NOFS); |
| 876 | if (!buf) { |
| 877 | ret = -ENOMEM; |
| 878 | goto out; |
| 879 | } |
| 880 | |
| 881 | tmp_path = alloc_path_for_send(); |
| 882 | if (!tmp_path) { |
| 883 | ret = -ENOMEM; |
| 884 | goto out; |
| 885 | } |
| 886 | |
| 887 | eb = path->nodes[0]; |
| 888 | slot = path->slots[0]; |
| 889 | item = btrfs_item_nr(eb, slot); |
| 890 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 891 | cur = 0; |
| 892 | len = 0; |
| 893 | total = btrfs_item_size(eb, item); |
| 894 | |
| 895 | num = 0; |
| 896 | while (cur < total) { |
| 897 | name_len = btrfs_dir_name_len(eb, di); |
| 898 | data_len = btrfs_dir_data_len(eb, di); |
| 899 | type = btrfs_dir_type(eb, di); |
| 900 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 901 | |
| 902 | if (name_len + data_len > buf_len) { |
| 903 | buf_len = PAGE_ALIGN(name_len + data_len); |
| 904 | if (buf_virtual) { |
| 905 | buf2 = vmalloc(buf_len); |
| 906 | if (!buf2) { |
| 907 | ret = -ENOMEM; |
| 908 | goto out; |
| 909 | } |
| 910 | vfree(buf); |
| 911 | } else { |
| 912 | buf2 = krealloc(buf, buf_len, GFP_NOFS); |
| 913 | if (!buf2) { |
| 914 | buf2 = vmalloc(buf_len); |
| 915 | if (!buf2) { |
| 916 | ret = -ENOMEM; |
| 917 | goto out; |
| 918 | } |
| 919 | kfree(buf); |
| 920 | buf_virtual = 1; |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | buf = buf2; |
| 925 | buf2 = NULL; |
| 926 | } |
| 927 | |
| 928 | read_extent_buffer(eb, buf, (unsigned long)(di + 1), |
| 929 | name_len + data_len); |
| 930 | |
| 931 | len = sizeof(*di) + name_len + data_len; |
| 932 | di = (struct btrfs_dir_item *)((char *)di + len); |
| 933 | cur += len; |
| 934 | |
| 935 | ret = iterate(num, &di_key, buf, name_len, buf + name_len, |
| 936 | data_len, type, ctx); |
| 937 | if (ret < 0) |
| 938 | goto out; |
| 939 | if (ret) { |
| 940 | ret = 0; |
| 941 | goto out; |
| 942 | } |
| 943 | |
| 944 | num++; |
| 945 | } |
| 946 | |
| 947 | out: |
| 948 | btrfs_free_path(tmp_path); |
| 949 | if (buf_virtual) |
| 950 | vfree(buf); |
| 951 | else |
| 952 | kfree(buf); |
| 953 | return ret; |
| 954 | } |
| 955 | |
| 956 | static int __copy_first_ref(int num, u64 dir, int index, |
| 957 | struct fs_path *p, void *ctx) |
| 958 | { |
| 959 | int ret; |
| 960 | struct fs_path *pt = ctx; |
| 961 | |
| 962 | ret = fs_path_copy(pt, p); |
| 963 | if (ret < 0) |
| 964 | return ret; |
| 965 | |
| 966 | /* we want the first only */ |
| 967 | return 1; |
| 968 | } |
| 969 | |
| 970 | /* |
| 971 | * Retrieve the first path of an inode. If an inode has more then one |
| 972 | * ref/hardlink, this is ignored. |
| 973 | */ |
| 974 | static int get_inode_path(struct send_ctx *sctx, struct btrfs_root *root, |
| 975 | u64 ino, struct fs_path *path) |
| 976 | { |
| 977 | int ret; |
| 978 | struct btrfs_key key, found_key; |
| 979 | struct btrfs_path *p; |
| 980 | |
| 981 | p = alloc_path_for_send(); |
| 982 | if (!p) |
| 983 | return -ENOMEM; |
| 984 | |
| 985 | fs_path_reset(path); |
| 986 | |
| 987 | key.objectid = ino; |
| 988 | key.type = BTRFS_INODE_REF_KEY; |
| 989 | key.offset = 0; |
| 990 | |
| 991 | ret = btrfs_search_slot_for_read(root, &key, p, 1, 0); |
| 992 | if (ret < 0) |
| 993 | goto out; |
| 994 | if (ret) { |
| 995 | ret = 1; |
| 996 | goto out; |
| 997 | } |
| 998 | btrfs_item_key_to_cpu(p->nodes[0], &found_key, p->slots[0]); |
| 999 | if (found_key.objectid != ino || |
| 1000 | found_key.type != BTRFS_INODE_REF_KEY) { |
| 1001 | ret = -ENOENT; |
| 1002 | goto out; |
| 1003 | } |
| 1004 | |
| 1005 | ret = iterate_inode_ref(sctx, root, p, &found_key, 1, |
| 1006 | __copy_first_ref, path); |
| 1007 | if (ret < 0) |
| 1008 | goto out; |
| 1009 | ret = 0; |
| 1010 | |
| 1011 | out: |
| 1012 | btrfs_free_path(p); |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
| 1016 | struct backref_ctx { |
| 1017 | struct send_ctx *sctx; |
| 1018 | |
| 1019 | /* number of total found references */ |
| 1020 | u64 found; |
| 1021 | |
| 1022 | /* |
| 1023 | * used for clones found in send_root. clones found behind cur_objectid |
| 1024 | * and cur_offset are not considered as allowed clones. |
| 1025 | */ |
| 1026 | u64 cur_objectid; |
| 1027 | u64 cur_offset; |
| 1028 | |
| 1029 | /* may be truncated in case it's the last extent in a file */ |
| 1030 | u64 extent_len; |
| 1031 | |
| 1032 | /* Just to check for bugs in backref resolving */ |
| 1033 | int found_in_send_root; |
| 1034 | }; |
| 1035 | |
| 1036 | static int __clone_root_cmp_bsearch(const void *key, const void *elt) |
| 1037 | { |
| 1038 | u64 root = (u64)key; |
| 1039 | struct clone_root *cr = (struct clone_root *)elt; |
| 1040 | |
| 1041 | if (root < cr->root->objectid) |
| 1042 | return -1; |
| 1043 | if (root > cr->root->objectid) |
| 1044 | return 1; |
| 1045 | return 0; |
| 1046 | } |
| 1047 | |
| 1048 | static int __clone_root_cmp_sort(const void *e1, const void *e2) |
| 1049 | { |
| 1050 | struct clone_root *cr1 = (struct clone_root *)e1; |
| 1051 | struct clone_root *cr2 = (struct clone_root *)e2; |
| 1052 | |
| 1053 | if (cr1->root->objectid < cr2->root->objectid) |
| 1054 | return -1; |
| 1055 | if (cr1->root->objectid > cr2->root->objectid) |
| 1056 | return 1; |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * Called for every backref that is found for the current extent. |
| 1062 | */ |
| 1063 | static int __iterate_backrefs(u64 ino, u64 offset, u64 root, void *ctx_) |
| 1064 | { |
| 1065 | struct backref_ctx *bctx = ctx_; |
| 1066 | struct clone_root *found; |
| 1067 | int ret; |
| 1068 | u64 i_size; |
| 1069 | |
| 1070 | /* First check if the root is in the list of accepted clone sources */ |
| 1071 | found = bsearch((void *)root, bctx->sctx->clone_roots, |
| 1072 | bctx->sctx->clone_roots_cnt, |
| 1073 | sizeof(struct clone_root), |
| 1074 | __clone_root_cmp_bsearch); |
| 1075 | if (!found) |
| 1076 | return 0; |
| 1077 | |
| 1078 | if (found->root == bctx->sctx->send_root && |
| 1079 | ino == bctx->cur_objectid && |
| 1080 | offset == bctx->cur_offset) { |
| 1081 | bctx->found_in_send_root = 1; |
| 1082 | } |
| 1083 | |
| 1084 | /* |
| 1085 | * There are inodes that have extents that lie behind it's i_size. Don't |
| 1086 | * accept clones from these extents. |
| 1087 | */ |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1088 | ret = get_inode_info(found->root, ino, &i_size, NULL, NULL, NULL, NULL, |
| 1089 | NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1090 | if (ret < 0) |
| 1091 | return ret; |
| 1092 | |
| 1093 | if (offset + bctx->extent_len > i_size) |
| 1094 | return 0; |
| 1095 | |
| 1096 | /* |
| 1097 | * Make sure we don't consider clones from send_root that are |
| 1098 | * behind the current inode/offset. |
| 1099 | */ |
| 1100 | if (found->root == bctx->sctx->send_root) { |
| 1101 | /* |
| 1102 | * TODO for the moment we don't accept clones from the inode |
| 1103 | * that is currently send. We may change this when |
| 1104 | * BTRFS_IOC_CLONE_RANGE supports cloning from and to the same |
| 1105 | * file. |
| 1106 | */ |
| 1107 | if (ino >= bctx->cur_objectid) |
| 1108 | return 0; |
| 1109 | /*if (ino > ctx->cur_objectid) |
| 1110 | return 0; |
| 1111 | if (offset + ctx->extent_len > ctx->cur_offset) |
| 1112 | return 0;*/ |
| 1113 | |
| 1114 | bctx->found++; |
| 1115 | found->found_refs++; |
| 1116 | found->ino = ino; |
| 1117 | found->offset = offset; |
| 1118 | return 0; |
| 1119 | } |
| 1120 | |
| 1121 | bctx->found++; |
| 1122 | found->found_refs++; |
| 1123 | if (ino < found->ino) { |
| 1124 | found->ino = ino; |
| 1125 | found->offset = offset; |
| 1126 | } else if (found->ino == ino) { |
| 1127 | /* |
| 1128 | * same extent found more then once in the same file. |
| 1129 | */ |
| 1130 | if (found->offset > offset + bctx->extent_len) |
| 1131 | found->offset = offset; |
| 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | /* |
| 1138 | * path must point to the extent item when called. |
| 1139 | */ |
| 1140 | static int find_extent_clone(struct send_ctx *sctx, |
| 1141 | struct btrfs_path *path, |
| 1142 | u64 ino, u64 data_offset, |
| 1143 | u64 ino_size, |
| 1144 | struct clone_root **found) |
| 1145 | { |
| 1146 | int ret; |
| 1147 | int extent_type; |
| 1148 | u64 logical; |
| 1149 | u64 num_bytes; |
| 1150 | u64 extent_item_pos; |
| 1151 | struct btrfs_file_extent_item *fi; |
| 1152 | struct extent_buffer *eb = path->nodes[0]; |
| 1153 | struct backref_ctx backref_ctx; |
| 1154 | struct clone_root *cur_clone_root; |
| 1155 | struct btrfs_key found_key; |
| 1156 | struct btrfs_path *tmp_path; |
| 1157 | u32 i; |
| 1158 | |
| 1159 | tmp_path = alloc_path_for_send(); |
| 1160 | if (!tmp_path) |
| 1161 | return -ENOMEM; |
| 1162 | |
| 1163 | if (data_offset >= ino_size) { |
| 1164 | /* |
| 1165 | * There may be extents that lie behind the file's size. |
| 1166 | * I at least had this in combination with snapshotting while |
| 1167 | * writing large files. |
| 1168 | */ |
| 1169 | ret = 0; |
| 1170 | goto out; |
| 1171 | } |
| 1172 | |
| 1173 | fi = btrfs_item_ptr(eb, path->slots[0], |
| 1174 | struct btrfs_file_extent_item); |
| 1175 | extent_type = btrfs_file_extent_type(eb, fi); |
| 1176 | if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1177 | ret = -ENOENT; |
| 1178 | goto out; |
| 1179 | } |
| 1180 | |
| 1181 | num_bytes = btrfs_file_extent_num_bytes(eb, fi); |
| 1182 | logical = btrfs_file_extent_disk_bytenr(eb, fi); |
| 1183 | if (logical == 0) { |
| 1184 | ret = -ENOENT; |
| 1185 | goto out; |
| 1186 | } |
| 1187 | logical += btrfs_file_extent_offset(eb, fi); |
| 1188 | |
| 1189 | ret = extent_from_logical(sctx->send_root->fs_info, |
| 1190 | logical, tmp_path, &found_key); |
| 1191 | btrfs_release_path(tmp_path); |
| 1192 | |
| 1193 | if (ret < 0) |
| 1194 | goto out; |
| 1195 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) { |
| 1196 | ret = -EIO; |
| 1197 | goto out; |
| 1198 | } |
| 1199 | |
| 1200 | /* |
| 1201 | * Setup the clone roots. |
| 1202 | */ |
| 1203 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1204 | cur_clone_root = sctx->clone_roots + i; |
| 1205 | cur_clone_root->ino = (u64)-1; |
| 1206 | cur_clone_root->offset = 0; |
| 1207 | cur_clone_root->found_refs = 0; |
| 1208 | } |
| 1209 | |
| 1210 | backref_ctx.sctx = sctx; |
| 1211 | backref_ctx.found = 0; |
| 1212 | backref_ctx.cur_objectid = ino; |
| 1213 | backref_ctx.cur_offset = data_offset; |
| 1214 | backref_ctx.found_in_send_root = 0; |
| 1215 | backref_ctx.extent_len = num_bytes; |
| 1216 | |
| 1217 | /* |
| 1218 | * The last extent of a file may be too large due to page alignment. |
| 1219 | * We need to adjust extent_len in this case so that the checks in |
| 1220 | * __iterate_backrefs work. |
| 1221 | */ |
| 1222 | if (data_offset + num_bytes >= ino_size) |
| 1223 | backref_ctx.extent_len = ino_size - data_offset; |
| 1224 | |
| 1225 | /* |
| 1226 | * Now collect all backrefs. |
| 1227 | */ |
| 1228 | extent_item_pos = logical - found_key.objectid; |
| 1229 | ret = iterate_extent_inodes(sctx->send_root->fs_info, |
| 1230 | found_key.objectid, extent_item_pos, 1, |
| 1231 | __iterate_backrefs, &backref_ctx); |
| 1232 | if (ret < 0) |
| 1233 | goto out; |
| 1234 | |
| 1235 | if (!backref_ctx.found_in_send_root) { |
| 1236 | /* found a bug in backref code? */ |
| 1237 | ret = -EIO; |
| 1238 | printk(KERN_ERR "btrfs: ERROR did not find backref in " |
| 1239 | "send_root. inode=%llu, offset=%llu, " |
| 1240 | "logical=%llu\n", |
| 1241 | ino, data_offset, logical); |
| 1242 | goto out; |
| 1243 | } |
| 1244 | |
| 1245 | verbose_printk(KERN_DEBUG "btrfs: find_extent_clone: data_offset=%llu, " |
| 1246 | "ino=%llu, " |
| 1247 | "num_bytes=%llu, logical=%llu\n", |
| 1248 | data_offset, ino, num_bytes, logical); |
| 1249 | |
| 1250 | if (!backref_ctx.found) |
| 1251 | verbose_printk("btrfs: no clones found\n"); |
| 1252 | |
| 1253 | cur_clone_root = NULL; |
| 1254 | for (i = 0; i < sctx->clone_roots_cnt; i++) { |
| 1255 | if (sctx->clone_roots[i].found_refs) { |
| 1256 | if (!cur_clone_root) |
| 1257 | cur_clone_root = sctx->clone_roots + i; |
| 1258 | else if (sctx->clone_roots[i].root == sctx->send_root) |
| 1259 | /* prefer clones from send_root over others */ |
| 1260 | cur_clone_root = sctx->clone_roots + i; |
| 1261 | break; |
| 1262 | } |
| 1263 | |
| 1264 | } |
| 1265 | |
| 1266 | if (cur_clone_root) { |
| 1267 | *found = cur_clone_root; |
| 1268 | ret = 0; |
| 1269 | } else { |
| 1270 | ret = -ENOENT; |
| 1271 | } |
| 1272 | |
| 1273 | out: |
| 1274 | btrfs_free_path(tmp_path); |
| 1275 | return ret; |
| 1276 | } |
| 1277 | |
| 1278 | static int read_symlink(struct send_ctx *sctx, |
| 1279 | struct btrfs_root *root, |
| 1280 | u64 ino, |
| 1281 | struct fs_path *dest) |
| 1282 | { |
| 1283 | int ret; |
| 1284 | struct btrfs_path *path; |
| 1285 | struct btrfs_key key; |
| 1286 | struct btrfs_file_extent_item *ei; |
| 1287 | u8 type; |
| 1288 | u8 compression; |
| 1289 | unsigned long off; |
| 1290 | int len; |
| 1291 | |
| 1292 | path = alloc_path_for_send(); |
| 1293 | if (!path) |
| 1294 | return -ENOMEM; |
| 1295 | |
| 1296 | key.objectid = ino; |
| 1297 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 1298 | key.offset = 0; |
| 1299 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 1300 | if (ret < 0) |
| 1301 | goto out; |
| 1302 | BUG_ON(ret); |
| 1303 | |
| 1304 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1305 | struct btrfs_file_extent_item); |
| 1306 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 1307 | compression = btrfs_file_extent_compression(path->nodes[0], ei); |
| 1308 | BUG_ON(type != BTRFS_FILE_EXTENT_INLINE); |
| 1309 | BUG_ON(compression); |
| 1310 | |
| 1311 | off = btrfs_file_extent_inline_start(ei); |
| 1312 | len = btrfs_file_extent_inline_len(path->nodes[0], ei); |
| 1313 | |
| 1314 | ret = fs_path_add_from_extent_buffer(dest, path->nodes[0], off, len); |
| 1315 | if (ret < 0) |
| 1316 | goto out; |
| 1317 | |
| 1318 | out: |
| 1319 | btrfs_free_path(path); |
| 1320 | return ret; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * Helper function to generate a file name that is unique in the root of |
| 1325 | * send_root and parent_root. This is used to generate names for orphan inodes. |
| 1326 | */ |
| 1327 | static int gen_unique_name(struct send_ctx *sctx, |
| 1328 | u64 ino, u64 gen, |
| 1329 | struct fs_path *dest) |
| 1330 | { |
| 1331 | int ret = 0; |
| 1332 | struct btrfs_path *path; |
| 1333 | struct btrfs_dir_item *di; |
| 1334 | char tmp[64]; |
| 1335 | int len; |
| 1336 | u64 idx = 0; |
| 1337 | |
| 1338 | path = alloc_path_for_send(); |
| 1339 | if (!path) |
| 1340 | return -ENOMEM; |
| 1341 | |
| 1342 | while (1) { |
| 1343 | len = snprintf(tmp, sizeof(tmp) - 1, "o%llu-%llu-%llu", |
| 1344 | ino, gen, idx); |
| 1345 | if (len >= sizeof(tmp)) { |
| 1346 | /* should really not happen */ |
| 1347 | ret = -EOVERFLOW; |
| 1348 | goto out; |
| 1349 | } |
| 1350 | |
| 1351 | di = btrfs_lookup_dir_item(NULL, sctx->send_root, |
| 1352 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1353 | tmp, strlen(tmp), 0); |
| 1354 | btrfs_release_path(path); |
| 1355 | if (IS_ERR(di)) { |
| 1356 | ret = PTR_ERR(di); |
| 1357 | goto out; |
| 1358 | } |
| 1359 | if (di) { |
| 1360 | /* not unique, try again */ |
| 1361 | idx++; |
| 1362 | continue; |
| 1363 | } |
| 1364 | |
| 1365 | if (!sctx->parent_root) { |
| 1366 | /* unique */ |
| 1367 | ret = 0; |
| 1368 | break; |
| 1369 | } |
| 1370 | |
| 1371 | di = btrfs_lookup_dir_item(NULL, sctx->parent_root, |
| 1372 | path, BTRFS_FIRST_FREE_OBJECTID, |
| 1373 | tmp, strlen(tmp), 0); |
| 1374 | btrfs_release_path(path); |
| 1375 | if (IS_ERR(di)) { |
| 1376 | ret = PTR_ERR(di); |
| 1377 | goto out; |
| 1378 | } |
| 1379 | if (di) { |
| 1380 | /* not unique, try again */ |
| 1381 | idx++; |
| 1382 | continue; |
| 1383 | } |
| 1384 | /* unique */ |
| 1385 | break; |
| 1386 | } |
| 1387 | |
| 1388 | ret = fs_path_add(dest, tmp, strlen(tmp)); |
| 1389 | |
| 1390 | out: |
| 1391 | btrfs_free_path(path); |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | enum inode_state { |
| 1396 | inode_state_no_change, |
| 1397 | inode_state_will_create, |
| 1398 | inode_state_did_create, |
| 1399 | inode_state_will_delete, |
| 1400 | inode_state_did_delete, |
| 1401 | }; |
| 1402 | |
| 1403 | static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1404 | { |
| 1405 | int ret; |
| 1406 | int left_ret; |
| 1407 | int right_ret; |
| 1408 | u64 left_gen; |
| 1409 | u64 right_gen; |
| 1410 | |
| 1411 | 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] | 1412 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1413 | if (ret < 0 && ret != -ENOENT) |
| 1414 | goto out; |
| 1415 | left_ret = ret; |
| 1416 | |
| 1417 | if (!sctx->parent_root) { |
| 1418 | right_ret = -ENOENT; |
| 1419 | } else { |
| 1420 | ret = get_inode_info(sctx->parent_root, ino, NULL, &right_gen, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1421 | NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1422 | if (ret < 0 && ret != -ENOENT) |
| 1423 | goto out; |
| 1424 | right_ret = ret; |
| 1425 | } |
| 1426 | |
| 1427 | if (!left_ret && !right_ret) { |
| 1428 | if (left_gen == gen && right_gen == gen) |
| 1429 | ret = inode_state_no_change; |
| 1430 | else if (left_gen == gen) { |
| 1431 | if (ino < sctx->send_progress) |
| 1432 | ret = inode_state_did_create; |
| 1433 | else |
| 1434 | ret = inode_state_will_create; |
| 1435 | } else if (right_gen == gen) { |
| 1436 | if (ino < sctx->send_progress) |
| 1437 | ret = inode_state_did_delete; |
| 1438 | else |
| 1439 | ret = inode_state_will_delete; |
| 1440 | } else { |
| 1441 | ret = -ENOENT; |
| 1442 | } |
| 1443 | } else if (!left_ret) { |
| 1444 | if (left_gen == gen) { |
| 1445 | if (ino < sctx->send_progress) |
| 1446 | ret = inode_state_did_create; |
| 1447 | else |
| 1448 | ret = inode_state_will_create; |
| 1449 | } else { |
| 1450 | ret = -ENOENT; |
| 1451 | } |
| 1452 | } else if (!right_ret) { |
| 1453 | if (right_gen == gen) { |
| 1454 | if (ino < sctx->send_progress) |
| 1455 | ret = inode_state_did_delete; |
| 1456 | else |
| 1457 | ret = inode_state_will_delete; |
| 1458 | } else { |
| 1459 | ret = -ENOENT; |
| 1460 | } |
| 1461 | } else { |
| 1462 | ret = -ENOENT; |
| 1463 | } |
| 1464 | |
| 1465 | out: |
| 1466 | return ret; |
| 1467 | } |
| 1468 | |
| 1469 | static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1470 | { |
| 1471 | int ret; |
| 1472 | |
| 1473 | ret = get_cur_inode_state(sctx, ino, gen); |
| 1474 | if (ret < 0) |
| 1475 | goto out; |
| 1476 | |
| 1477 | if (ret == inode_state_no_change || |
| 1478 | ret == inode_state_did_create || |
| 1479 | ret == inode_state_will_delete) |
| 1480 | ret = 1; |
| 1481 | else |
| 1482 | ret = 0; |
| 1483 | |
| 1484 | out: |
| 1485 | return ret; |
| 1486 | } |
| 1487 | |
| 1488 | /* |
| 1489 | * Helper function to lookup a dir item in a dir. |
| 1490 | */ |
| 1491 | static int lookup_dir_item_inode(struct btrfs_root *root, |
| 1492 | u64 dir, const char *name, int name_len, |
| 1493 | u64 *found_inode, |
| 1494 | u8 *found_type) |
| 1495 | { |
| 1496 | int ret = 0; |
| 1497 | struct btrfs_dir_item *di; |
| 1498 | struct btrfs_key key; |
| 1499 | struct btrfs_path *path; |
| 1500 | |
| 1501 | path = alloc_path_for_send(); |
| 1502 | if (!path) |
| 1503 | return -ENOMEM; |
| 1504 | |
| 1505 | di = btrfs_lookup_dir_item(NULL, root, path, |
| 1506 | dir, name, name_len, 0); |
| 1507 | if (!di) { |
| 1508 | ret = -ENOENT; |
| 1509 | goto out; |
| 1510 | } |
| 1511 | if (IS_ERR(di)) { |
| 1512 | ret = PTR_ERR(di); |
| 1513 | goto out; |
| 1514 | } |
| 1515 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); |
| 1516 | *found_inode = key.objectid; |
| 1517 | *found_type = btrfs_dir_type(path->nodes[0], di); |
| 1518 | |
| 1519 | out: |
| 1520 | btrfs_free_path(path); |
| 1521 | return ret; |
| 1522 | } |
| 1523 | |
| 1524 | static int get_first_ref(struct send_ctx *sctx, |
| 1525 | struct btrfs_root *root, u64 ino, |
| 1526 | u64 *dir, u64 *dir_gen, struct fs_path *name) |
| 1527 | { |
| 1528 | int ret; |
| 1529 | struct btrfs_key key; |
| 1530 | struct btrfs_key found_key; |
| 1531 | struct btrfs_path *path; |
| 1532 | struct btrfs_inode_ref *iref; |
| 1533 | int len; |
| 1534 | |
| 1535 | path = alloc_path_for_send(); |
| 1536 | if (!path) |
| 1537 | return -ENOMEM; |
| 1538 | |
| 1539 | key.objectid = ino; |
| 1540 | key.type = BTRFS_INODE_REF_KEY; |
| 1541 | key.offset = 0; |
| 1542 | |
| 1543 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 1544 | if (ret < 0) |
| 1545 | goto out; |
| 1546 | if (!ret) |
| 1547 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1548 | path->slots[0]); |
| 1549 | if (ret || found_key.objectid != key.objectid || |
| 1550 | found_key.type != key.type) { |
| 1551 | ret = -ENOENT; |
| 1552 | goto out; |
| 1553 | } |
| 1554 | |
| 1555 | iref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1556 | struct btrfs_inode_ref); |
| 1557 | len = btrfs_inode_ref_name_len(path->nodes[0], iref); |
| 1558 | ret = fs_path_add_from_extent_buffer(name, path->nodes[0], |
| 1559 | (unsigned long)(iref + 1), len); |
| 1560 | if (ret < 0) |
| 1561 | goto out; |
| 1562 | btrfs_release_path(path); |
| 1563 | |
| 1564 | ret = get_inode_info(root, found_key.offset, NULL, dir_gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1565 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1566 | if (ret < 0) |
| 1567 | goto out; |
| 1568 | |
| 1569 | *dir = found_key.offset; |
| 1570 | |
| 1571 | out: |
| 1572 | btrfs_free_path(path); |
| 1573 | return ret; |
| 1574 | } |
| 1575 | |
| 1576 | static int is_first_ref(struct send_ctx *sctx, |
| 1577 | struct btrfs_root *root, |
| 1578 | u64 ino, u64 dir, |
| 1579 | const char *name, int name_len) |
| 1580 | { |
| 1581 | int ret; |
| 1582 | struct fs_path *tmp_name; |
| 1583 | u64 tmp_dir; |
| 1584 | u64 tmp_dir_gen; |
| 1585 | |
| 1586 | tmp_name = fs_path_alloc(sctx); |
| 1587 | if (!tmp_name) |
| 1588 | return -ENOMEM; |
| 1589 | |
| 1590 | ret = get_first_ref(sctx, root, ino, &tmp_dir, &tmp_dir_gen, tmp_name); |
| 1591 | if (ret < 0) |
| 1592 | goto out; |
| 1593 | |
Alexander Block | b9291af | 2012-07-28 11:07:18 +0200 | [diff] [blame] | 1594 | if (dir != tmp_dir || name_len != fs_path_len(tmp_name)) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1595 | ret = 0; |
| 1596 | goto out; |
| 1597 | } |
| 1598 | |
| 1599 | ret = memcmp(tmp_name->start, name, name_len); |
| 1600 | if (ret) |
| 1601 | ret = 0; |
| 1602 | else |
| 1603 | ret = 1; |
| 1604 | |
| 1605 | out: |
| 1606 | fs_path_free(sctx, tmp_name); |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
| 1610 | static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen, |
| 1611 | const char *name, int name_len, |
| 1612 | u64 *who_ino, u64 *who_gen) |
| 1613 | { |
| 1614 | int ret = 0; |
| 1615 | u64 other_inode = 0; |
| 1616 | u8 other_type = 0; |
| 1617 | |
| 1618 | if (!sctx->parent_root) |
| 1619 | goto out; |
| 1620 | |
| 1621 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1622 | if (ret <= 0) |
| 1623 | goto out; |
| 1624 | |
| 1625 | ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len, |
| 1626 | &other_inode, &other_type); |
| 1627 | if (ret < 0 && ret != -ENOENT) |
| 1628 | goto out; |
| 1629 | if (ret) { |
| 1630 | ret = 0; |
| 1631 | goto out; |
| 1632 | } |
| 1633 | |
| 1634 | if (other_inode > sctx->send_progress) { |
| 1635 | ret = get_inode_info(sctx->parent_root, other_inode, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 1636 | who_gen, NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1637 | if (ret < 0) |
| 1638 | goto out; |
| 1639 | |
| 1640 | ret = 1; |
| 1641 | *who_ino = other_inode; |
| 1642 | } else { |
| 1643 | ret = 0; |
| 1644 | } |
| 1645 | |
| 1646 | out: |
| 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | static int did_overwrite_ref(struct send_ctx *sctx, |
| 1651 | u64 dir, u64 dir_gen, |
| 1652 | u64 ino, u64 ino_gen, |
| 1653 | const char *name, int name_len) |
| 1654 | { |
| 1655 | int ret = 0; |
| 1656 | u64 gen; |
| 1657 | u64 ow_inode; |
| 1658 | u8 other_type; |
| 1659 | |
| 1660 | if (!sctx->parent_root) |
| 1661 | goto out; |
| 1662 | |
| 1663 | ret = is_inode_existent(sctx, dir, dir_gen); |
| 1664 | if (ret <= 0) |
| 1665 | goto out; |
| 1666 | |
| 1667 | /* check if the ref was overwritten by another ref */ |
| 1668 | ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len, |
| 1669 | &ow_inode, &other_type); |
| 1670 | if (ret < 0 && ret != -ENOENT) |
| 1671 | goto out; |
| 1672 | if (ret) { |
| 1673 | /* was never and will never be overwritten */ |
| 1674 | ret = 0; |
| 1675 | goto out; |
| 1676 | } |
| 1677 | |
| 1678 | 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] | 1679 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 1680 | if (ret < 0) |
| 1681 | goto out; |
| 1682 | |
| 1683 | if (ow_inode == ino && gen == ino_gen) { |
| 1684 | ret = 0; |
| 1685 | goto out; |
| 1686 | } |
| 1687 | |
| 1688 | /* we know that it is or will be overwritten. check this now */ |
| 1689 | if (ow_inode < sctx->send_progress) |
| 1690 | ret = 1; |
| 1691 | else |
| 1692 | ret = 0; |
| 1693 | |
| 1694 | out: |
| 1695 | return ret; |
| 1696 | } |
| 1697 | |
| 1698 | static int did_overwrite_first_ref(struct send_ctx *sctx, u64 ino, u64 gen) |
| 1699 | { |
| 1700 | int ret = 0; |
| 1701 | struct fs_path *name = NULL; |
| 1702 | u64 dir; |
| 1703 | u64 dir_gen; |
| 1704 | |
| 1705 | if (!sctx->parent_root) |
| 1706 | goto out; |
| 1707 | |
| 1708 | name = fs_path_alloc(sctx); |
| 1709 | if (!name) |
| 1710 | return -ENOMEM; |
| 1711 | |
| 1712 | ret = get_first_ref(sctx, sctx->parent_root, ino, &dir, &dir_gen, name); |
| 1713 | if (ret < 0) |
| 1714 | goto out; |
| 1715 | |
| 1716 | ret = did_overwrite_ref(sctx, dir, dir_gen, ino, gen, |
| 1717 | name->start, fs_path_len(name)); |
| 1718 | if (ret < 0) |
| 1719 | goto out; |
| 1720 | |
| 1721 | out: |
| 1722 | fs_path_free(sctx, name); |
| 1723 | return ret; |
| 1724 | } |
| 1725 | |
| 1726 | static int name_cache_insert(struct send_ctx *sctx, |
| 1727 | struct name_cache_entry *nce) |
| 1728 | { |
| 1729 | int ret = 0; |
| 1730 | struct name_cache_entry **ncea; |
| 1731 | |
| 1732 | ncea = radix_tree_lookup(&sctx->name_cache, nce->ino); |
| 1733 | if (ncea) { |
| 1734 | if (!ncea[0]) |
| 1735 | ncea[0] = nce; |
| 1736 | else if (!ncea[1]) |
| 1737 | ncea[1] = nce; |
| 1738 | else |
| 1739 | BUG(); |
| 1740 | } else { |
| 1741 | ncea = kmalloc(sizeof(void *) * 2, GFP_NOFS); |
| 1742 | if (!ncea) |
| 1743 | return -ENOMEM; |
| 1744 | |
| 1745 | ncea[0] = nce; |
| 1746 | ncea[1] = NULL; |
| 1747 | ret = radix_tree_insert(&sctx->name_cache, nce->ino, ncea); |
| 1748 | if (ret < 0) |
| 1749 | return ret; |
| 1750 | } |
| 1751 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 1752 | sctx->name_cache_size++; |
| 1753 | |
| 1754 | return ret; |
| 1755 | } |
| 1756 | |
| 1757 | static void name_cache_delete(struct send_ctx *sctx, |
| 1758 | struct name_cache_entry *nce) |
| 1759 | { |
| 1760 | struct name_cache_entry **ncea; |
| 1761 | |
| 1762 | ncea = radix_tree_lookup(&sctx->name_cache, nce->ino); |
| 1763 | BUG_ON(!ncea); |
| 1764 | |
| 1765 | if (ncea[0] == nce) |
| 1766 | ncea[0] = NULL; |
| 1767 | else if (ncea[1] == nce) |
| 1768 | ncea[1] = NULL; |
| 1769 | else |
| 1770 | BUG(); |
| 1771 | |
| 1772 | if (!ncea[0] && !ncea[1]) { |
| 1773 | radix_tree_delete(&sctx->name_cache, nce->ino); |
| 1774 | kfree(ncea); |
| 1775 | } |
| 1776 | |
| 1777 | list_del(&nce->list); |
| 1778 | |
| 1779 | sctx->name_cache_size--; |
| 1780 | } |
| 1781 | |
| 1782 | static struct name_cache_entry *name_cache_search(struct send_ctx *sctx, |
| 1783 | u64 ino, u64 gen) |
| 1784 | { |
| 1785 | struct name_cache_entry **ncea; |
| 1786 | |
| 1787 | ncea = radix_tree_lookup(&sctx->name_cache, ino); |
| 1788 | if (!ncea) |
| 1789 | return NULL; |
| 1790 | |
| 1791 | if (ncea[0] && ncea[0]->gen == gen) |
| 1792 | return ncea[0]; |
| 1793 | else if (ncea[1] && ncea[1]->gen == gen) |
| 1794 | return ncea[1]; |
| 1795 | return NULL; |
| 1796 | } |
| 1797 | |
| 1798 | static void name_cache_used(struct send_ctx *sctx, struct name_cache_entry *nce) |
| 1799 | { |
| 1800 | list_del(&nce->list); |
| 1801 | list_add_tail(&nce->list, &sctx->name_cache_list); |
| 1802 | } |
| 1803 | |
| 1804 | static void name_cache_clean_unused(struct send_ctx *sctx) |
| 1805 | { |
| 1806 | struct name_cache_entry *nce; |
| 1807 | |
| 1808 | if (sctx->name_cache_size < SEND_CTX_NAME_CACHE_CLEAN_SIZE) |
| 1809 | return; |
| 1810 | |
| 1811 | while (sctx->name_cache_size > SEND_CTX_MAX_NAME_CACHE_SIZE) { |
| 1812 | nce = list_entry(sctx->name_cache_list.next, |
| 1813 | struct name_cache_entry, list); |
| 1814 | name_cache_delete(sctx, nce); |
| 1815 | kfree(nce); |
| 1816 | } |
| 1817 | } |
| 1818 | |
| 1819 | static void name_cache_free(struct send_ctx *sctx) |
| 1820 | { |
| 1821 | struct name_cache_entry *nce; |
| 1822 | struct name_cache_entry *tmp; |
| 1823 | |
| 1824 | list_for_each_entry_safe(nce, tmp, &sctx->name_cache_list, list) { |
| 1825 | name_cache_delete(sctx, nce); |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | static int __get_cur_name_and_parent(struct send_ctx *sctx, |
| 1830 | u64 ino, u64 gen, |
| 1831 | u64 *parent_ino, |
| 1832 | u64 *parent_gen, |
| 1833 | struct fs_path *dest) |
| 1834 | { |
| 1835 | int ret; |
| 1836 | int nce_ret; |
| 1837 | struct btrfs_path *path = NULL; |
| 1838 | struct name_cache_entry *nce = NULL; |
| 1839 | |
| 1840 | nce = name_cache_search(sctx, ino, gen); |
| 1841 | if (nce) { |
| 1842 | if (ino < sctx->send_progress && nce->need_later_update) { |
| 1843 | name_cache_delete(sctx, nce); |
| 1844 | kfree(nce); |
| 1845 | nce = NULL; |
| 1846 | } else { |
| 1847 | name_cache_used(sctx, nce); |
| 1848 | *parent_ino = nce->parent_ino; |
| 1849 | *parent_gen = nce->parent_gen; |
| 1850 | ret = fs_path_add(dest, nce->name, nce->name_len); |
| 1851 | if (ret < 0) |
| 1852 | goto out; |
| 1853 | ret = nce->ret; |
| 1854 | goto out; |
| 1855 | } |
| 1856 | } |
| 1857 | |
| 1858 | path = alloc_path_for_send(); |
| 1859 | if (!path) |
| 1860 | return -ENOMEM; |
| 1861 | |
| 1862 | ret = is_inode_existent(sctx, ino, gen); |
| 1863 | if (ret < 0) |
| 1864 | goto out; |
| 1865 | |
| 1866 | if (!ret) { |
| 1867 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 1868 | if (ret < 0) |
| 1869 | goto out; |
| 1870 | ret = 1; |
| 1871 | goto out_cache; |
| 1872 | } |
| 1873 | |
| 1874 | if (ino < sctx->send_progress) |
| 1875 | ret = get_first_ref(sctx, sctx->send_root, ino, |
| 1876 | parent_ino, parent_gen, dest); |
| 1877 | else |
| 1878 | ret = get_first_ref(sctx, sctx->parent_root, ino, |
| 1879 | parent_ino, parent_gen, dest); |
| 1880 | if (ret < 0) |
| 1881 | goto out; |
| 1882 | |
| 1883 | ret = did_overwrite_ref(sctx, *parent_ino, *parent_gen, ino, gen, |
| 1884 | dest->start, dest->end - dest->start); |
| 1885 | if (ret < 0) |
| 1886 | goto out; |
| 1887 | if (ret) { |
| 1888 | fs_path_reset(dest); |
| 1889 | ret = gen_unique_name(sctx, ino, gen, dest); |
| 1890 | if (ret < 0) |
| 1891 | goto out; |
| 1892 | ret = 1; |
| 1893 | } |
| 1894 | |
| 1895 | out_cache: |
| 1896 | nce = kmalloc(sizeof(*nce) + fs_path_len(dest) + 1, GFP_NOFS); |
| 1897 | if (!nce) { |
| 1898 | ret = -ENOMEM; |
| 1899 | goto out; |
| 1900 | } |
| 1901 | |
| 1902 | nce->ino = ino; |
| 1903 | nce->gen = gen; |
| 1904 | nce->parent_ino = *parent_ino; |
| 1905 | nce->parent_gen = *parent_gen; |
| 1906 | nce->name_len = fs_path_len(dest); |
| 1907 | nce->ret = ret; |
| 1908 | strcpy(nce->name, dest->start); |
| 1909 | memset(&nce->use_list, 0, sizeof(nce->use_list)); |
| 1910 | |
| 1911 | if (ino < sctx->send_progress) |
| 1912 | nce->need_later_update = 0; |
| 1913 | else |
| 1914 | nce->need_later_update = 1; |
| 1915 | |
| 1916 | nce_ret = name_cache_insert(sctx, nce); |
| 1917 | if (nce_ret < 0) |
| 1918 | ret = nce_ret; |
| 1919 | name_cache_clean_unused(sctx); |
| 1920 | |
| 1921 | out: |
| 1922 | btrfs_free_path(path); |
| 1923 | return ret; |
| 1924 | } |
| 1925 | |
| 1926 | /* |
| 1927 | * Magic happens here. This function returns the first ref to an inode as it |
| 1928 | * would look like while receiving the stream at this point in time. |
| 1929 | * We walk the path up to the root. For every inode in between, we check if it |
| 1930 | * was already processed/sent. If yes, we continue with the parent as found |
| 1931 | * in send_root. If not, we continue with the parent as found in parent_root. |
| 1932 | * If we encounter an inode that was deleted at this point in time, we use the |
| 1933 | * inodes "orphan" name instead of the real name and stop. Same with new inodes |
| 1934 | * that were not created yet and overwritten inodes/refs. |
| 1935 | * |
| 1936 | * When do we have have orphan inodes: |
| 1937 | * 1. When an inode is freshly created and thus no valid refs are available yet |
| 1938 | * 2. When a directory lost all it's refs (deleted) but still has dir items |
| 1939 | * inside which were not processed yet (pending for move/delete). If anyone |
| 1940 | * tried to get the path to the dir items, it would get a path inside that |
| 1941 | * orphan directory. |
| 1942 | * 3. When an inode is moved around or gets new links, it may overwrite the ref |
| 1943 | * of an unprocessed inode. If in that case the first ref would be |
| 1944 | * overwritten, the overwritten inode gets "orphanized". Later when we |
| 1945 | * process this overwritten inode, it is restored at a new place by moving |
| 1946 | * the orphan inode. |
| 1947 | * |
| 1948 | * sctx->send_progress tells this function at which point in time receiving |
| 1949 | * would be. |
| 1950 | */ |
| 1951 | static int get_cur_path(struct send_ctx *sctx, u64 ino, u64 gen, |
| 1952 | struct fs_path *dest) |
| 1953 | { |
| 1954 | int ret = 0; |
| 1955 | struct fs_path *name = NULL; |
| 1956 | u64 parent_inode = 0; |
| 1957 | u64 parent_gen = 0; |
| 1958 | int stop = 0; |
| 1959 | |
| 1960 | name = fs_path_alloc(sctx); |
| 1961 | if (!name) { |
| 1962 | ret = -ENOMEM; |
| 1963 | goto out; |
| 1964 | } |
| 1965 | |
| 1966 | dest->reversed = 1; |
| 1967 | fs_path_reset(dest); |
| 1968 | |
| 1969 | while (!stop && ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 1970 | fs_path_reset(name); |
| 1971 | |
| 1972 | ret = __get_cur_name_and_parent(sctx, ino, gen, |
| 1973 | &parent_inode, &parent_gen, name); |
| 1974 | if (ret < 0) |
| 1975 | goto out; |
| 1976 | if (ret) |
| 1977 | stop = 1; |
| 1978 | |
| 1979 | ret = fs_path_add_path(dest, name); |
| 1980 | if (ret < 0) |
| 1981 | goto out; |
| 1982 | |
| 1983 | ino = parent_inode; |
| 1984 | gen = parent_gen; |
| 1985 | } |
| 1986 | |
| 1987 | out: |
| 1988 | fs_path_free(sctx, name); |
| 1989 | if (!ret) |
| 1990 | fs_path_unreverse(dest); |
| 1991 | return ret; |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | * Called for regular files when sending extents data. Opens a struct file |
| 1996 | * to read from the file. |
| 1997 | */ |
| 1998 | static int open_cur_inode_file(struct send_ctx *sctx) |
| 1999 | { |
| 2000 | int ret = 0; |
| 2001 | struct btrfs_key key; |
Linus Torvalds | e2aed8d | 2012-07-26 14:48:55 -0700 | [diff] [blame] | 2002 | struct path path; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2003 | struct inode *inode; |
| 2004 | struct dentry *dentry; |
| 2005 | struct file *filp; |
| 2006 | int new = 0; |
| 2007 | |
| 2008 | if (sctx->cur_inode_filp) |
| 2009 | goto out; |
| 2010 | |
| 2011 | key.objectid = sctx->cur_ino; |
| 2012 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2013 | key.offset = 0; |
| 2014 | |
| 2015 | inode = btrfs_iget(sctx->send_root->fs_info->sb, &key, sctx->send_root, |
| 2016 | &new); |
| 2017 | if (IS_ERR(inode)) { |
| 2018 | ret = PTR_ERR(inode); |
| 2019 | goto out; |
| 2020 | } |
| 2021 | |
| 2022 | dentry = d_obtain_alias(inode); |
| 2023 | inode = NULL; |
| 2024 | if (IS_ERR(dentry)) { |
| 2025 | ret = PTR_ERR(dentry); |
| 2026 | goto out; |
| 2027 | } |
| 2028 | |
Linus Torvalds | e2aed8d | 2012-07-26 14:48:55 -0700 | [diff] [blame] | 2029 | path.mnt = sctx->mnt; |
| 2030 | path.dentry = dentry; |
| 2031 | filp = dentry_open(&path, O_RDONLY | O_LARGEFILE, current_cred()); |
| 2032 | dput(dentry); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2033 | dentry = NULL; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2034 | if (IS_ERR(filp)) { |
| 2035 | ret = PTR_ERR(filp); |
| 2036 | goto out; |
| 2037 | } |
| 2038 | sctx->cur_inode_filp = filp; |
| 2039 | |
| 2040 | out: |
| 2041 | /* |
| 2042 | * no xxxput required here as every vfs op |
| 2043 | * does it by itself on failure |
| 2044 | */ |
| 2045 | return ret; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * Closes the struct file that was created in open_cur_inode_file |
| 2050 | */ |
| 2051 | static int close_cur_inode_file(struct send_ctx *sctx) |
| 2052 | { |
| 2053 | int ret = 0; |
| 2054 | |
| 2055 | if (!sctx->cur_inode_filp) |
| 2056 | goto out; |
| 2057 | |
| 2058 | ret = filp_close(sctx->cur_inode_filp, NULL); |
| 2059 | sctx->cur_inode_filp = NULL; |
| 2060 | |
| 2061 | out: |
| 2062 | return ret; |
| 2063 | } |
| 2064 | |
| 2065 | /* |
| 2066 | * Sends a BTRFS_SEND_C_SUBVOL command/item to userspace |
| 2067 | */ |
| 2068 | static int send_subvol_begin(struct send_ctx *sctx) |
| 2069 | { |
| 2070 | int ret; |
| 2071 | struct btrfs_root *send_root = sctx->send_root; |
| 2072 | struct btrfs_root *parent_root = sctx->parent_root; |
| 2073 | struct btrfs_path *path; |
| 2074 | struct btrfs_key key; |
| 2075 | struct btrfs_root_ref *ref; |
| 2076 | struct extent_buffer *leaf; |
| 2077 | char *name = NULL; |
| 2078 | int namelen; |
| 2079 | |
| 2080 | path = alloc_path_for_send(); |
| 2081 | if (!path) |
| 2082 | return -ENOMEM; |
| 2083 | |
| 2084 | name = kmalloc(BTRFS_PATH_NAME_MAX, GFP_NOFS); |
| 2085 | if (!name) { |
| 2086 | btrfs_free_path(path); |
| 2087 | return -ENOMEM; |
| 2088 | } |
| 2089 | |
| 2090 | key.objectid = send_root->objectid; |
| 2091 | key.type = BTRFS_ROOT_BACKREF_KEY; |
| 2092 | key.offset = 0; |
| 2093 | |
| 2094 | ret = btrfs_search_slot_for_read(send_root->fs_info->tree_root, |
| 2095 | &key, path, 1, 0); |
| 2096 | if (ret < 0) |
| 2097 | goto out; |
| 2098 | if (ret) { |
| 2099 | ret = -ENOENT; |
| 2100 | goto out; |
| 2101 | } |
| 2102 | |
| 2103 | leaf = path->nodes[0]; |
| 2104 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 2105 | if (key.type != BTRFS_ROOT_BACKREF_KEY || |
| 2106 | key.objectid != send_root->objectid) { |
| 2107 | ret = -ENOENT; |
| 2108 | goto out; |
| 2109 | } |
| 2110 | ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref); |
| 2111 | namelen = btrfs_root_ref_name_len(leaf, ref); |
| 2112 | read_extent_buffer(leaf, name, (unsigned long)(ref + 1), namelen); |
| 2113 | btrfs_release_path(path); |
| 2114 | |
| 2115 | if (ret < 0) |
| 2116 | goto out; |
| 2117 | |
| 2118 | if (parent_root) { |
| 2119 | ret = begin_cmd(sctx, BTRFS_SEND_C_SNAPSHOT); |
| 2120 | if (ret < 0) |
| 2121 | goto out; |
| 2122 | } else { |
| 2123 | ret = begin_cmd(sctx, BTRFS_SEND_C_SUBVOL); |
| 2124 | if (ret < 0) |
| 2125 | goto out; |
| 2126 | } |
| 2127 | |
| 2128 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_PATH, name, namelen); |
| 2129 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_UUID, |
| 2130 | sctx->send_root->root_item.uuid); |
| 2131 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CTRANSID, |
| 2132 | sctx->send_root->root_item.ctransid); |
| 2133 | if (parent_root) { |
| 2134 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 2135 | sctx->parent_root->root_item.uuid); |
| 2136 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
| 2137 | sctx->parent_root->root_item.ctransid); |
| 2138 | } |
| 2139 | |
| 2140 | ret = send_cmd(sctx); |
| 2141 | |
| 2142 | tlv_put_failure: |
| 2143 | out: |
| 2144 | btrfs_free_path(path); |
| 2145 | kfree(name); |
| 2146 | return ret; |
| 2147 | } |
| 2148 | |
| 2149 | static int send_truncate(struct send_ctx *sctx, u64 ino, u64 gen, u64 size) |
| 2150 | { |
| 2151 | int ret = 0; |
| 2152 | struct fs_path *p; |
| 2153 | |
| 2154 | verbose_printk("btrfs: send_truncate %llu size=%llu\n", ino, size); |
| 2155 | |
| 2156 | p = fs_path_alloc(sctx); |
| 2157 | if (!p) |
| 2158 | return -ENOMEM; |
| 2159 | |
| 2160 | ret = begin_cmd(sctx, BTRFS_SEND_C_TRUNCATE); |
| 2161 | if (ret < 0) |
| 2162 | goto out; |
| 2163 | |
| 2164 | ret = get_cur_path(sctx, ino, gen, p); |
| 2165 | if (ret < 0) |
| 2166 | goto out; |
| 2167 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2168 | TLV_PUT_U64(sctx, BTRFS_SEND_A_SIZE, size); |
| 2169 | |
| 2170 | ret = send_cmd(sctx); |
| 2171 | |
| 2172 | tlv_put_failure: |
| 2173 | out: |
| 2174 | fs_path_free(sctx, p); |
| 2175 | return ret; |
| 2176 | } |
| 2177 | |
| 2178 | static int send_chmod(struct send_ctx *sctx, u64 ino, u64 gen, u64 mode) |
| 2179 | { |
| 2180 | int ret = 0; |
| 2181 | struct fs_path *p; |
| 2182 | |
| 2183 | verbose_printk("btrfs: send_chmod %llu mode=%llu\n", ino, mode); |
| 2184 | |
| 2185 | p = fs_path_alloc(sctx); |
| 2186 | if (!p) |
| 2187 | return -ENOMEM; |
| 2188 | |
| 2189 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHMOD); |
| 2190 | if (ret < 0) |
| 2191 | goto out; |
| 2192 | |
| 2193 | ret = get_cur_path(sctx, ino, gen, p); |
| 2194 | if (ret < 0) |
| 2195 | goto out; |
| 2196 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2197 | TLV_PUT_U64(sctx, BTRFS_SEND_A_MODE, mode & 07777); |
| 2198 | |
| 2199 | ret = send_cmd(sctx); |
| 2200 | |
| 2201 | tlv_put_failure: |
| 2202 | out: |
| 2203 | fs_path_free(sctx, p); |
| 2204 | return ret; |
| 2205 | } |
| 2206 | |
| 2207 | static int send_chown(struct send_ctx *sctx, u64 ino, u64 gen, u64 uid, u64 gid) |
| 2208 | { |
| 2209 | int ret = 0; |
| 2210 | struct fs_path *p; |
| 2211 | |
| 2212 | verbose_printk("btrfs: send_chown %llu uid=%llu, gid=%llu\n", ino, uid, gid); |
| 2213 | |
| 2214 | p = fs_path_alloc(sctx); |
| 2215 | if (!p) |
| 2216 | return -ENOMEM; |
| 2217 | |
| 2218 | ret = begin_cmd(sctx, BTRFS_SEND_C_CHOWN); |
| 2219 | if (ret < 0) |
| 2220 | goto out; |
| 2221 | |
| 2222 | ret = get_cur_path(sctx, ino, gen, p); |
| 2223 | if (ret < 0) |
| 2224 | goto out; |
| 2225 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2226 | TLV_PUT_U64(sctx, BTRFS_SEND_A_UID, uid); |
| 2227 | TLV_PUT_U64(sctx, BTRFS_SEND_A_GID, gid); |
| 2228 | |
| 2229 | ret = send_cmd(sctx); |
| 2230 | |
| 2231 | tlv_put_failure: |
| 2232 | out: |
| 2233 | fs_path_free(sctx, p); |
| 2234 | return ret; |
| 2235 | } |
| 2236 | |
| 2237 | static int send_utimes(struct send_ctx *sctx, u64 ino, u64 gen) |
| 2238 | { |
| 2239 | int ret = 0; |
| 2240 | struct fs_path *p = NULL; |
| 2241 | struct btrfs_inode_item *ii; |
| 2242 | struct btrfs_path *path = NULL; |
| 2243 | struct extent_buffer *eb; |
| 2244 | struct btrfs_key key; |
| 2245 | int slot; |
| 2246 | |
| 2247 | verbose_printk("btrfs: send_utimes %llu\n", ino); |
| 2248 | |
| 2249 | p = fs_path_alloc(sctx); |
| 2250 | if (!p) |
| 2251 | return -ENOMEM; |
| 2252 | |
| 2253 | path = alloc_path_for_send(); |
| 2254 | if (!path) { |
| 2255 | ret = -ENOMEM; |
| 2256 | goto out; |
| 2257 | } |
| 2258 | |
| 2259 | key.objectid = ino; |
| 2260 | key.type = BTRFS_INODE_ITEM_KEY; |
| 2261 | key.offset = 0; |
| 2262 | ret = btrfs_search_slot(NULL, sctx->send_root, &key, path, 0, 0); |
| 2263 | if (ret < 0) |
| 2264 | goto out; |
| 2265 | |
| 2266 | eb = path->nodes[0]; |
| 2267 | slot = path->slots[0]; |
| 2268 | ii = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); |
| 2269 | |
| 2270 | ret = begin_cmd(sctx, BTRFS_SEND_C_UTIMES); |
| 2271 | if (ret < 0) |
| 2272 | goto out; |
| 2273 | |
| 2274 | ret = get_cur_path(sctx, ino, gen, p); |
| 2275 | if (ret < 0) |
| 2276 | goto out; |
| 2277 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 2278 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_ATIME, eb, |
| 2279 | btrfs_inode_atime(ii)); |
| 2280 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_MTIME, eb, |
| 2281 | btrfs_inode_mtime(ii)); |
| 2282 | TLV_PUT_BTRFS_TIMESPEC(sctx, BTRFS_SEND_A_CTIME, eb, |
| 2283 | btrfs_inode_ctime(ii)); |
| 2284 | /* TODO otime? */ |
| 2285 | |
| 2286 | ret = send_cmd(sctx); |
| 2287 | |
| 2288 | tlv_put_failure: |
| 2289 | out: |
| 2290 | fs_path_free(sctx, p); |
| 2291 | btrfs_free_path(path); |
| 2292 | return ret; |
| 2293 | } |
| 2294 | |
| 2295 | /* |
| 2296 | * Sends a BTRFS_SEND_C_MKXXX or SYMLINK command to user space. We don't have |
| 2297 | * a valid path yet because we did not process the refs yet. So, the inode |
| 2298 | * is created as orphan. |
| 2299 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2300 | static int send_create_inode(struct send_ctx *sctx, u64 ino) |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2301 | { |
| 2302 | int ret = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2303 | struct fs_path *p; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2304 | int cmd; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2305 | u64 gen; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2306 | u64 mode; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2307 | u64 rdev; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2308 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2309 | verbose_printk("btrfs: send_create_inode %llu\n", ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2310 | |
| 2311 | p = fs_path_alloc(sctx); |
| 2312 | if (!p) |
| 2313 | return -ENOMEM; |
| 2314 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2315 | ret = get_inode_info(sctx->send_root, ino, NULL, &gen, &mode, NULL, |
| 2316 | NULL, &rdev); |
| 2317 | if (ret < 0) |
| 2318 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2319 | |
| 2320 | if (S_ISREG(mode)) |
| 2321 | cmd = BTRFS_SEND_C_MKFILE; |
| 2322 | else if (S_ISDIR(mode)) |
| 2323 | cmd = BTRFS_SEND_C_MKDIR; |
| 2324 | else if (S_ISLNK(mode)) |
| 2325 | cmd = BTRFS_SEND_C_SYMLINK; |
| 2326 | else if (S_ISCHR(mode) || S_ISBLK(mode)) |
| 2327 | cmd = BTRFS_SEND_C_MKNOD; |
| 2328 | else if (S_ISFIFO(mode)) |
| 2329 | cmd = BTRFS_SEND_C_MKFIFO; |
| 2330 | else if (S_ISSOCK(mode)) |
| 2331 | cmd = BTRFS_SEND_C_MKSOCK; |
| 2332 | else { |
| 2333 | printk(KERN_WARNING "btrfs: unexpected inode type %o", |
| 2334 | (int)(mode & S_IFMT)); |
| 2335 | ret = -ENOTSUPP; |
| 2336 | goto out; |
| 2337 | } |
| 2338 | |
| 2339 | ret = begin_cmd(sctx, cmd); |
| 2340 | if (ret < 0) |
| 2341 | goto out; |
| 2342 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2343 | ret = gen_unique_name(sctx, ino, gen, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2344 | if (ret < 0) |
| 2345 | goto out; |
| 2346 | |
| 2347 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2348 | TLV_PUT_U64(sctx, BTRFS_SEND_A_INO, ino); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2349 | |
| 2350 | if (S_ISLNK(mode)) { |
| 2351 | fs_path_reset(p); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2352 | ret = read_symlink(sctx, sctx->send_root, ino, p); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2353 | if (ret < 0) |
| 2354 | goto out; |
| 2355 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH_LINK, p); |
| 2356 | } else if (S_ISCHR(mode) || S_ISBLK(mode) || |
| 2357 | S_ISFIFO(mode) || S_ISSOCK(mode)) { |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2358 | TLV_PUT_U64(sctx, BTRFS_SEND_A_RDEV, rdev); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | ret = send_cmd(sctx); |
| 2362 | if (ret < 0) |
| 2363 | goto out; |
| 2364 | |
| 2365 | |
| 2366 | tlv_put_failure: |
| 2367 | out: |
| 2368 | fs_path_free(sctx, p); |
| 2369 | return ret; |
| 2370 | } |
| 2371 | |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2372 | /* |
| 2373 | * We need some special handling for inodes that get processed before the parent |
| 2374 | * directory got created. See process_recorded_refs for details. |
| 2375 | * This function does the check if we already created the dir out of order. |
| 2376 | */ |
| 2377 | static int did_create_dir(struct send_ctx *sctx, u64 dir) |
| 2378 | { |
| 2379 | int ret = 0; |
| 2380 | struct btrfs_path *path = NULL; |
| 2381 | struct btrfs_key key; |
| 2382 | struct btrfs_key found_key; |
| 2383 | struct btrfs_key di_key; |
| 2384 | struct extent_buffer *eb; |
| 2385 | struct btrfs_dir_item *di; |
| 2386 | int slot; |
| 2387 | |
| 2388 | path = alloc_path_for_send(); |
| 2389 | if (!path) { |
| 2390 | ret = -ENOMEM; |
| 2391 | goto out; |
| 2392 | } |
| 2393 | |
| 2394 | key.objectid = dir; |
| 2395 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2396 | key.offset = 0; |
| 2397 | while (1) { |
| 2398 | ret = btrfs_search_slot_for_read(sctx->send_root, &key, path, |
| 2399 | 1, 0); |
| 2400 | if (ret < 0) |
| 2401 | goto out; |
| 2402 | if (!ret) { |
| 2403 | eb = path->nodes[0]; |
| 2404 | slot = path->slots[0]; |
| 2405 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 2406 | } |
| 2407 | if (ret || found_key.objectid != key.objectid || |
| 2408 | found_key.type != key.type) { |
| 2409 | ret = 0; |
| 2410 | goto out; |
| 2411 | } |
| 2412 | |
| 2413 | di = btrfs_item_ptr(eb, slot, struct btrfs_dir_item); |
| 2414 | btrfs_dir_item_key_to_cpu(eb, di, &di_key); |
| 2415 | |
| 2416 | if (di_key.objectid < sctx->send_progress) { |
| 2417 | ret = 1; |
| 2418 | goto out; |
| 2419 | } |
| 2420 | |
| 2421 | key.offset = found_key.offset + 1; |
| 2422 | btrfs_release_path(path); |
| 2423 | } |
| 2424 | |
| 2425 | out: |
| 2426 | btrfs_free_path(path); |
| 2427 | return ret; |
| 2428 | } |
| 2429 | |
| 2430 | /* |
| 2431 | * Only creates the inode if it is: |
| 2432 | * 1. Not a directory |
| 2433 | * 2. Or a directory which was not created already due to out of order |
| 2434 | * directories. See did_create_dir and process_recorded_refs for details. |
| 2435 | */ |
| 2436 | static int send_create_inode_if_needed(struct send_ctx *sctx) |
| 2437 | { |
| 2438 | int ret; |
| 2439 | |
| 2440 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 2441 | ret = did_create_dir(sctx, sctx->cur_ino); |
| 2442 | if (ret < 0) |
| 2443 | goto out; |
| 2444 | if (ret) { |
| 2445 | ret = 0; |
| 2446 | goto out; |
| 2447 | } |
| 2448 | } |
| 2449 | |
| 2450 | ret = send_create_inode(sctx, sctx->cur_ino); |
| 2451 | if (ret < 0) |
| 2452 | goto out; |
| 2453 | |
| 2454 | out: |
| 2455 | return ret; |
| 2456 | } |
| 2457 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2458 | struct recorded_ref { |
| 2459 | struct list_head list; |
| 2460 | char *dir_path; |
| 2461 | char *name; |
| 2462 | struct fs_path *full_path; |
| 2463 | u64 dir; |
| 2464 | u64 dir_gen; |
| 2465 | int dir_path_len; |
| 2466 | int name_len; |
| 2467 | }; |
| 2468 | |
| 2469 | /* |
| 2470 | * We need to process new refs before deleted refs, but compare_tree gives us |
| 2471 | * everything mixed. So we first record all refs and later process them. |
| 2472 | * This function is a helper to record one ref. |
| 2473 | */ |
| 2474 | static int record_ref(struct list_head *head, u64 dir, |
| 2475 | u64 dir_gen, struct fs_path *path) |
| 2476 | { |
| 2477 | struct recorded_ref *ref; |
| 2478 | char *tmp; |
| 2479 | |
| 2480 | ref = kmalloc(sizeof(*ref), GFP_NOFS); |
| 2481 | if (!ref) |
| 2482 | return -ENOMEM; |
| 2483 | |
| 2484 | ref->dir = dir; |
| 2485 | ref->dir_gen = dir_gen; |
| 2486 | ref->full_path = path; |
| 2487 | |
| 2488 | tmp = strrchr(ref->full_path->start, '/'); |
| 2489 | if (!tmp) { |
| 2490 | ref->name_len = ref->full_path->end - ref->full_path->start; |
| 2491 | ref->name = ref->full_path->start; |
| 2492 | ref->dir_path_len = 0; |
| 2493 | ref->dir_path = ref->full_path->start; |
| 2494 | } else { |
| 2495 | tmp++; |
| 2496 | ref->name_len = ref->full_path->end - tmp; |
| 2497 | ref->name = tmp; |
| 2498 | ref->dir_path = ref->full_path->start; |
| 2499 | ref->dir_path_len = ref->full_path->end - |
| 2500 | ref->full_path->start - 1 - ref->name_len; |
| 2501 | } |
| 2502 | |
| 2503 | list_add_tail(&ref->list, head); |
| 2504 | return 0; |
| 2505 | } |
| 2506 | |
| 2507 | static void __free_recorded_refs(struct send_ctx *sctx, struct list_head *head) |
| 2508 | { |
| 2509 | struct recorded_ref *cur; |
| 2510 | struct recorded_ref *tmp; |
| 2511 | |
| 2512 | list_for_each_entry_safe(cur, tmp, head, list) { |
| 2513 | fs_path_free(sctx, cur->full_path); |
| 2514 | kfree(cur); |
| 2515 | } |
| 2516 | INIT_LIST_HEAD(head); |
| 2517 | } |
| 2518 | |
| 2519 | static void free_recorded_refs(struct send_ctx *sctx) |
| 2520 | { |
| 2521 | __free_recorded_refs(sctx, &sctx->new_refs); |
| 2522 | __free_recorded_refs(sctx, &sctx->deleted_refs); |
| 2523 | } |
| 2524 | |
| 2525 | /* |
| 2526 | * Renames/moves a file/dir to it's orphan name. Used when the first |
| 2527 | * ref of an unprocessed inode gets overwritten and for all non empty |
| 2528 | * directories. |
| 2529 | */ |
| 2530 | static int orphanize_inode(struct send_ctx *sctx, u64 ino, u64 gen, |
| 2531 | struct fs_path *path) |
| 2532 | { |
| 2533 | int ret; |
| 2534 | struct fs_path *orphan; |
| 2535 | |
| 2536 | orphan = fs_path_alloc(sctx); |
| 2537 | if (!orphan) |
| 2538 | return -ENOMEM; |
| 2539 | |
| 2540 | ret = gen_unique_name(sctx, ino, gen, orphan); |
| 2541 | if (ret < 0) |
| 2542 | goto out; |
| 2543 | |
| 2544 | ret = send_rename(sctx, path, orphan); |
| 2545 | |
| 2546 | out: |
| 2547 | fs_path_free(sctx, orphan); |
| 2548 | return ret; |
| 2549 | } |
| 2550 | |
| 2551 | /* |
| 2552 | * Returns 1 if a directory can be removed at this point in time. |
| 2553 | * We check this by iterating all dir items and checking if the inode behind |
| 2554 | * the dir item was already processed. |
| 2555 | */ |
| 2556 | static int can_rmdir(struct send_ctx *sctx, u64 dir, u64 send_progress) |
| 2557 | { |
| 2558 | int ret = 0; |
| 2559 | struct btrfs_root *root = sctx->parent_root; |
| 2560 | struct btrfs_path *path; |
| 2561 | struct btrfs_key key; |
| 2562 | struct btrfs_key found_key; |
| 2563 | struct btrfs_key loc; |
| 2564 | struct btrfs_dir_item *di; |
| 2565 | |
| 2566 | path = alloc_path_for_send(); |
| 2567 | if (!path) |
| 2568 | return -ENOMEM; |
| 2569 | |
| 2570 | key.objectid = dir; |
| 2571 | key.type = BTRFS_DIR_INDEX_KEY; |
| 2572 | key.offset = 0; |
| 2573 | |
| 2574 | while (1) { |
| 2575 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 2576 | if (ret < 0) |
| 2577 | goto out; |
| 2578 | if (!ret) { |
| 2579 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 2580 | path->slots[0]); |
| 2581 | } |
| 2582 | if (ret || found_key.objectid != key.objectid || |
| 2583 | found_key.type != key.type) { |
| 2584 | break; |
| 2585 | } |
| 2586 | |
| 2587 | di = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2588 | struct btrfs_dir_item); |
| 2589 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &loc); |
| 2590 | |
| 2591 | if (loc.objectid > send_progress) { |
| 2592 | ret = 0; |
| 2593 | goto out; |
| 2594 | } |
| 2595 | |
| 2596 | btrfs_release_path(path); |
| 2597 | key.offset = found_key.offset + 1; |
| 2598 | } |
| 2599 | |
| 2600 | ret = 1; |
| 2601 | |
| 2602 | out: |
| 2603 | btrfs_free_path(path); |
| 2604 | return ret; |
| 2605 | } |
| 2606 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2607 | /* |
| 2608 | * This does all the move/link/unlink/rmdir magic. |
| 2609 | */ |
| 2610 | static int process_recorded_refs(struct send_ctx *sctx) |
| 2611 | { |
| 2612 | int ret = 0; |
| 2613 | struct recorded_ref *cur; |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2614 | struct recorded_ref *cur2; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2615 | struct ulist *check_dirs = NULL; |
| 2616 | struct ulist_iterator uit; |
| 2617 | struct ulist_node *un; |
| 2618 | struct fs_path *valid_path = NULL; |
Chris Mason | b24baf6 | 2012-07-25 19:21:10 -0400 | [diff] [blame] | 2619 | u64 ow_inode = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2620 | u64 ow_gen; |
| 2621 | int did_overwrite = 0; |
| 2622 | int is_orphan = 0; |
| 2623 | |
| 2624 | verbose_printk("btrfs: process_recorded_refs %llu\n", sctx->cur_ino); |
| 2625 | |
| 2626 | valid_path = fs_path_alloc(sctx); |
| 2627 | if (!valid_path) { |
| 2628 | ret = -ENOMEM; |
| 2629 | goto out; |
| 2630 | } |
| 2631 | |
| 2632 | check_dirs = ulist_alloc(GFP_NOFS); |
| 2633 | if (!check_dirs) { |
| 2634 | ret = -ENOMEM; |
| 2635 | goto out; |
| 2636 | } |
| 2637 | |
| 2638 | /* |
| 2639 | * First, check if the first ref of the current inode was overwritten |
| 2640 | * before. If yes, we know that the current inode was already orphanized |
| 2641 | * and thus use the orphan name. If not, we can use get_cur_path to |
| 2642 | * get the path of the first ref as it would like while receiving at |
| 2643 | * this point in time. |
| 2644 | * New inodes are always orphan at the beginning, so force to use the |
| 2645 | * orphan name in this case. |
| 2646 | * The first ref is stored in valid_path and will be updated if it |
| 2647 | * gets moved around. |
| 2648 | */ |
| 2649 | if (!sctx->cur_inode_new) { |
| 2650 | ret = did_overwrite_first_ref(sctx, sctx->cur_ino, |
| 2651 | sctx->cur_inode_gen); |
| 2652 | if (ret < 0) |
| 2653 | goto out; |
| 2654 | if (ret) |
| 2655 | did_overwrite = 1; |
| 2656 | } |
| 2657 | if (sctx->cur_inode_new || did_overwrite) { |
| 2658 | ret = gen_unique_name(sctx, sctx->cur_ino, |
| 2659 | sctx->cur_inode_gen, valid_path); |
| 2660 | if (ret < 0) |
| 2661 | goto out; |
| 2662 | is_orphan = 1; |
| 2663 | } else { |
| 2664 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 2665 | valid_path); |
| 2666 | if (ret < 0) |
| 2667 | goto out; |
| 2668 | } |
| 2669 | |
| 2670 | list_for_each_entry(cur, &sctx->new_refs, list) { |
| 2671 | /* |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2672 | * We may have refs where the parent directory does not exist |
| 2673 | * yet. This happens if the parent directories inum is higher |
| 2674 | * the the current inum. To handle this case, we create the |
| 2675 | * parent directory out of order. But we need to check if this |
| 2676 | * did already happen before due to other refs in the same dir. |
| 2677 | */ |
| 2678 | ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen); |
| 2679 | if (ret < 0) |
| 2680 | goto out; |
| 2681 | if (ret == inode_state_will_create) { |
| 2682 | ret = 0; |
| 2683 | /* |
| 2684 | * First check if any of the current inodes refs did |
| 2685 | * already create the dir. |
| 2686 | */ |
| 2687 | list_for_each_entry(cur2, &sctx->new_refs, list) { |
| 2688 | if (cur == cur2) |
| 2689 | break; |
| 2690 | if (cur2->dir == cur->dir) { |
| 2691 | ret = 1; |
| 2692 | break; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | /* |
| 2697 | * If that did not happen, check if a previous inode |
| 2698 | * did already create the dir. |
| 2699 | */ |
| 2700 | if (!ret) |
| 2701 | ret = did_create_dir(sctx, cur->dir); |
| 2702 | if (ret < 0) |
| 2703 | goto out; |
| 2704 | if (!ret) { |
| 2705 | ret = send_create_inode(sctx, cur->dir); |
| 2706 | if (ret < 0) |
| 2707 | goto out; |
| 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | /* |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2712 | * Check if this new ref would overwrite the first ref of |
| 2713 | * another unprocessed inode. If yes, orphanize the |
| 2714 | * overwritten inode. If we find an overwritten ref that is |
| 2715 | * not the first ref, simply unlink it. |
| 2716 | */ |
| 2717 | ret = will_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 2718 | cur->name, cur->name_len, |
| 2719 | &ow_inode, &ow_gen); |
| 2720 | if (ret < 0) |
| 2721 | goto out; |
| 2722 | if (ret) { |
| 2723 | ret = is_first_ref(sctx, sctx->parent_root, |
| 2724 | ow_inode, cur->dir, cur->name, |
| 2725 | cur->name_len); |
| 2726 | if (ret < 0) |
| 2727 | goto out; |
| 2728 | if (ret) { |
| 2729 | ret = orphanize_inode(sctx, ow_inode, ow_gen, |
| 2730 | cur->full_path); |
| 2731 | if (ret < 0) |
| 2732 | goto out; |
| 2733 | } else { |
| 2734 | ret = send_unlink(sctx, cur->full_path); |
| 2735 | if (ret < 0) |
| 2736 | goto out; |
| 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | /* |
| 2741 | * link/move the ref to the new place. If we have an orphan |
| 2742 | * inode, move it and update valid_path. If not, link or move |
| 2743 | * it depending on the inode mode. |
| 2744 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2745 | if (is_orphan) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2746 | ret = send_rename(sctx, valid_path, cur->full_path); |
| 2747 | if (ret < 0) |
| 2748 | goto out; |
| 2749 | is_orphan = 0; |
| 2750 | ret = fs_path_copy(valid_path, cur->full_path); |
| 2751 | if (ret < 0) |
| 2752 | goto out; |
| 2753 | } else { |
| 2754 | if (S_ISDIR(sctx->cur_inode_mode)) { |
| 2755 | /* |
| 2756 | * Dirs can't be linked, so move it. For moved |
| 2757 | * dirs, we always have one new and one deleted |
| 2758 | * ref. The deleted ref is ignored later. |
| 2759 | */ |
| 2760 | ret = send_rename(sctx, valid_path, |
| 2761 | cur->full_path); |
| 2762 | if (ret < 0) |
| 2763 | goto out; |
| 2764 | ret = fs_path_copy(valid_path, cur->full_path); |
| 2765 | if (ret < 0) |
| 2766 | goto out; |
| 2767 | } else { |
| 2768 | ret = send_link(sctx, cur->full_path, |
| 2769 | valid_path); |
| 2770 | if (ret < 0) |
| 2771 | goto out; |
| 2772 | } |
| 2773 | } |
| 2774 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, |
| 2775 | GFP_NOFS); |
| 2776 | if (ret < 0) |
| 2777 | goto out; |
| 2778 | } |
| 2779 | |
| 2780 | if (S_ISDIR(sctx->cur_inode_mode) && sctx->cur_inode_deleted) { |
| 2781 | /* |
| 2782 | * Check if we can already rmdir the directory. If not, |
| 2783 | * orphanize it. For every dir item inside that gets deleted |
| 2784 | * later, we do this check again and rmdir it then if possible. |
| 2785 | * See the use of check_dirs for more details. |
| 2786 | */ |
| 2787 | ret = can_rmdir(sctx, sctx->cur_ino, sctx->cur_ino); |
| 2788 | if (ret < 0) |
| 2789 | goto out; |
| 2790 | if (ret) { |
| 2791 | ret = send_rmdir(sctx, valid_path); |
| 2792 | if (ret < 0) |
| 2793 | goto out; |
| 2794 | } else if (!is_orphan) { |
| 2795 | ret = orphanize_inode(sctx, sctx->cur_ino, |
| 2796 | sctx->cur_inode_gen, valid_path); |
| 2797 | if (ret < 0) |
| 2798 | goto out; |
| 2799 | is_orphan = 1; |
| 2800 | } |
| 2801 | |
| 2802 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
| 2803 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, |
| 2804 | GFP_NOFS); |
| 2805 | if (ret < 0) |
| 2806 | goto out; |
| 2807 | } |
| 2808 | } else if (!S_ISDIR(sctx->cur_inode_mode)) { |
| 2809 | /* |
| 2810 | * We have a non dir inode. Go through all deleted refs and |
| 2811 | * unlink them if they were not already overwritten by other |
| 2812 | * inodes. |
| 2813 | */ |
| 2814 | list_for_each_entry(cur, &sctx->deleted_refs, list) { |
| 2815 | ret = did_overwrite_ref(sctx, cur->dir, cur->dir_gen, |
| 2816 | sctx->cur_ino, sctx->cur_inode_gen, |
| 2817 | cur->name, cur->name_len); |
| 2818 | if (ret < 0) |
| 2819 | goto out; |
| 2820 | if (!ret) { |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2821 | ret = send_unlink(sctx, cur->full_path); |
| 2822 | if (ret < 0) |
| 2823 | goto out; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2824 | } |
| 2825 | ret = ulist_add(check_dirs, cur->dir, cur->dir_gen, |
| 2826 | GFP_NOFS); |
| 2827 | if (ret < 0) |
| 2828 | goto out; |
| 2829 | } |
| 2830 | |
| 2831 | /* |
| 2832 | * If the inode is still orphan, unlink the orphan. This may |
| 2833 | * happen when a previous inode did overwrite the first ref |
| 2834 | * of this inode and no new refs were added for the current |
| 2835 | * inode. |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2836 | */ |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 2837 | if (is_orphan) { |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2838 | ret = send_unlink(sctx, valid_path); |
| 2839 | if (ret < 0) |
| 2840 | goto out; |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | /* |
| 2845 | * We did collect all parent dirs where cur_inode was once located. We |
| 2846 | * now go through all these dirs and check if they are pending for |
| 2847 | * deletion and if it's finally possible to perform the rmdir now. |
| 2848 | * We also update the inode stats of the parent dirs here. |
| 2849 | */ |
| 2850 | ULIST_ITER_INIT(&uit); |
| 2851 | while ((un = ulist_next(check_dirs, &uit))) { |
| 2852 | if (un->val > sctx->cur_ino) |
| 2853 | continue; |
| 2854 | |
| 2855 | ret = get_cur_inode_state(sctx, un->val, un->aux); |
| 2856 | if (ret < 0) |
| 2857 | goto out; |
| 2858 | |
| 2859 | if (ret == inode_state_did_create || |
| 2860 | ret == inode_state_no_change) { |
| 2861 | /* TODO delayed utimes */ |
| 2862 | ret = send_utimes(sctx, un->val, un->aux); |
| 2863 | if (ret < 0) |
| 2864 | goto out; |
| 2865 | } else if (ret == inode_state_did_delete) { |
| 2866 | ret = can_rmdir(sctx, un->val, sctx->cur_ino); |
| 2867 | if (ret < 0) |
| 2868 | goto out; |
| 2869 | if (ret) { |
| 2870 | ret = get_cur_path(sctx, un->val, un->aux, |
| 2871 | valid_path); |
| 2872 | if (ret < 0) |
| 2873 | goto out; |
| 2874 | ret = send_rmdir(sctx, valid_path); |
| 2875 | if (ret < 0) |
| 2876 | goto out; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | /* |
| 2882 | * Current inode is now at it's new position, so we must increase |
| 2883 | * send_progress |
| 2884 | */ |
| 2885 | sctx->send_progress = sctx->cur_ino + 1; |
| 2886 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2887 | ret = 0; |
| 2888 | |
| 2889 | out: |
| 2890 | free_recorded_refs(sctx); |
| 2891 | ulist_free(check_dirs); |
| 2892 | fs_path_free(sctx, valid_path); |
| 2893 | return ret; |
| 2894 | } |
| 2895 | |
| 2896 | static int __record_new_ref(int num, u64 dir, int index, |
| 2897 | struct fs_path *name, |
| 2898 | void *ctx) |
| 2899 | { |
| 2900 | int ret = 0; |
| 2901 | struct send_ctx *sctx = ctx; |
| 2902 | struct fs_path *p; |
| 2903 | u64 gen; |
| 2904 | |
| 2905 | p = fs_path_alloc(sctx); |
| 2906 | if (!p) |
| 2907 | return -ENOMEM; |
| 2908 | |
| 2909 | ret = get_inode_info(sctx->send_root, dir, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 2910 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2911 | if (ret < 0) |
| 2912 | goto out; |
| 2913 | |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2914 | ret = get_cur_path(sctx, dir, gen, p); |
| 2915 | if (ret < 0) |
| 2916 | goto out; |
| 2917 | ret = fs_path_add_path(p, name); |
| 2918 | if (ret < 0) |
| 2919 | goto out; |
| 2920 | |
| 2921 | ret = record_ref(&sctx->new_refs, dir, gen, p); |
| 2922 | |
| 2923 | out: |
| 2924 | if (ret) |
| 2925 | fs_path_free(sctx, p); |
| 2926 | return ret; |
| 2927 | } |
| 2928 | |
| 2929 | static int __record_deleted_ref(int num, u64 dir, int index, |
| 2930 | struct fs_path *name, |
| 2931 | void *ctx) |
| 2932 | { |
| 2933 | int ret = 0; |
| 2934 | struct send_ctx *sctx = ctx; |
| 2935 | struct fs_path *p; |
| 2936 | u64 gen; |
| 2937 | |
| 2938 | p = fs_path_alloc(sctx); |
| 2939 | if (!p) |
| 2940 | return -ENOMEM; |
| 2941 | |
| 2942 | ret = get_inode_info(sctx->parent_root, dir, NULL, &gen, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 2943 | NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 2944 | if (ret < 0) |
| 2945 | goto out; |
| 2946 | |
| 2947 | ret = get_cur_path(sctx, dir, gen, p); |
| 2948 | if (ret < 0) |
| 2949 | goto out; |
| 2950 | ret = fs_path_add_path(p, name); |
| 2951 | if (ret < 0) |
| 2952 | goto out; |
| 2953 | |
| 2954 | ret = record_ref(&sctx->deleted_refs, dir, gen, p); |
| 2955 | |
| 2956 | out: |
| 2957 | if (ret) |
| 2958 | fs_path_free(sctx, p); |
| 2959 | return ret; |
| 2960 | } |
| 2961 | |
| 2962 | static int record_new_ref(struct send_ctx *sctx) |
| 2963 | { |
| 2964 | int ret; |
| 2965 | |
| 2966 | ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path, |
| 2967 | sctx->cmp_key, 0, __record_new_ref, sctx); |
| 2968 | if (ret < 0) |
| 2969 | goto out; |
| 2970 | ret = 0; |
| 2971 | |
| 2972 | out: |
| 2973 | return ret; |
| 2974 | } |
| 2975 | |
| 2976 | static int record_deleted_ref(struct send_ctx *sctx) |
| 2977 | { |
| 2978 | int ret; |
| 2979 | |
| 2980 | ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path, |
| 2981 | sctx->cmp_key, 0, __record_deleted_ref, sctx); |
| 2982 | if (ret < 0) |
| 2983 | goto out; |
| 2984 | ret = 0; |
| 2985 | |
| 2986 | out: |
| 2987 | return ret; |
| 2988 | } |
| 2989 | |
| 2990 | struct find_ref_ctx { |
| 2991 | u64 dir; |
| 2992 | struct fs_path *name; |
| 2993 | int found_idx; |
| 2994 | }; |
| 2995 | |
| 2996 | static int __find_iref(int num, u64 dir, int index, |
| 2997 | struct fs_path *name, |
| 2998 | void *ctx_) |
| 2999 | { |
| 3000 | struct find_ref_ctx *ctx = ctx_; |
| 3001 | |
| 3002 | if (dir == ctx->dir && fs_path_len(name) == fs_path_len(ctx->name) && |
| 3003 | strncmp(name->start, ctx->name->start, fs_path_len(name)) == 0) { |
| 3004 | ctx->found_idx = num; |
| 3005 | return 1; |
| 3006 | } |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
| 3010 | static int find_iref(struct send_ctx *sctx, |
| 3011 | struct btrfs_root *root, |
| 3012 | struct btrfs_path *path, |
| 3013 | struct btrfs_key *key, |
| 3014 | u64 dir, struct fs_path *name) |
| 3015 | { |
| 3016 | int ret; |
| 3017 | struct find_ref_ctx ctx; |
| 3018 | |
| 3019 | ctx.dir = dir; |
| 3020 | ctx.name = name; |
| 3021 | ctx.found_idx = -1; |
| 3022 | |
| 3023 | ret = iterate_inode_ref(sctx, root, path, key, 0, __find_iref, &ctx); |
| 3024 | if (ret < 0) |
| 3025 | return ret; |
| 3026 | |
| 3027 | if (ctx.found_idx == -1) |
| 3028 | return -ENOENT; |
| 3029 | |
| 3030 | return ctx.found_idx; |
| 3031 | } |
| 3032 | |
| 3033 | static int __record_changed_new_ref(int num, u64 dir, int index, |
| 3034 | struct fs_path *name, |
| 3035 | void *ctx) |
| 3036 | { |
| 3037 | int ret; |
| 3038 | struct send_ctx *sctx = ctx; |
| 3039 | |
| 3040 | ret = find_iref(sctx, sctx->parent_root, sctx->right_path, |
| 3041 | sctx->cmp_key, dir, name); |
| 3042 | if (ret == -ENOENT) |
| 3043 | ret = __record_new_ref(num, dir, index, name, sctx); |
| 3044 | else if (ret > 0) |
| 3045 | ret = 0; |
| 3046 | |
| 3047 | return ret; |
| 3048 | } |
| 3049 | |
| 3050 | static int __record_changed_deleted_ref(int num, u64 dir, int index, |
| 3051 | struct fs_path *name, |
| 3052 | void *ctx) |
| 3053 | { |
| 3054 | int ret; |
| 3055 | struct send_ctx *sctx = ctx; |
| 3056 | |
| 3057 | ret = find_iref(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key, |
| 3058 | dir, name); |
| 3059 | if (ret == -ENOENT) |
| 3060 | ret = __record_deleted_ref(num, dir, index, name, sctx); |
| 3061 | else if (ret > 0) |
| 3062 | ret = 0; |
| 3063 | |
| 3064 | return ret; |
| 3065 | } |
| 3066 | |
| 3067 | static int record_changed_ref(struct send_ctx *sctx) |
| 3068 | { |
| 3069 | int ret = 0; |
| 3070 | |
| 3071 | ret = iterate_inode_ref(sctx, sctx->send_root, sctx->left_path, |
| 3072 | sctx->cmp_key, 0, __record_changed_new_ref, sctx); |
| 3073 | if (ret < 0) |
| 3074 | goto out; |
| 3075 | ret = iterate_inode_ref(sctx, sctx->parent_root, sctx->right_path, |
| 3076 | sctx->cmp_key, 0, __record_changed_deleted_ref, sctx); |
| 3077 | if (ret < 0) |
| 3078 | goto out; |
| 3079 | ret = 0; |
| 3080 | |
| 3081 | out: |
| 3082 | return ret; |
| 3083 | } |
| 3084 | |
| 3085 | /* |
| 3086 | * Record and process all refs at once. Needed when an inode changes the |
| 3087 | * generation number, which means that it was deleted and recreated. |
| 3088 | */ |
| 3089 | static int process_all_refs(struct send_ctx *sctx, |
| 3090 | enum btrfs_compare_tree_result cmd) |
| 3091 | { |
| 3092 | int ret; |
| 3093 | struct btrfs_root *root; |
| 3094 | struct btrfs_path *path; |
| 3095 | struct btrfs_key key; |
| 3096 | struct btrfs_key found_key; |
| 3097 | struct extent_buffer *eb; |
| 3098 | int slot; |
| 3099 | iterate_inode_ref_t cb; |
| 3100 | |
| 3101 | path = alloc_path_for_send(); |
| 3102 | if (!path) |
| 3103 | return -ENOMEM; |
| 3104 | |
| 3105 | if (cmd == BTRFS_COMPARE_TREE_NEW) { |
| 3106 | root = sctx->send_root; |
| 3107 | cb = __record_new_ref; |
| 3108 | } else if (cmd == BTRFS_COMPARE_TREE_DELETED) { |
| 3109 | root = sctx->parent_root; |
| 3110 | cb = __record_deleted_ref; |
| 3111 | } else { |
| 3112 | BUG(); |
| 3113 | } |
| 3114 | |
| 3115 | key.objectid = sctx->cmp_key->objectid; |
| 3116 | key.type = BTRFS_INODE_REF_KEY; |
| 3117 | key.offset = 0; |
| 3118 | while (1) { |
| 3119 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 3120 | if (ret < 0) { |
| 3121 | btrfs_release_path(path); |
| 3122 | goto out; |
| 3123 | } |
| 3124 | if (ret) { |
| 3125 | btrfs_release_path(path); |
| 3126 | break; |
| 3127 | } |
| 3128 | |
| 3129 | eb = path->nodes[0]; |
| 3130 | slot = path->slots[0]; |
| 3131 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3132 | |
| 3133 | if (found_key.objectid != key.objectid || |
| 3134 | found_key.type != key.type) { |
| 3135 | btrfs_release_path(path); |
| 3136 | break; |
| 3137 | } |
| 3138 | |
| 3139 | ret = iterate_inode_ref(sctx, sctx->parent_root, path, |
| 3140 | &found_key, 0, cb, sctx); |
| 3141 | btrfs_release_path(path); |
| 3142 | if (ret < 0) |
| 3143 | goto out; |
| 3144 | |
| 3145 | key.offset = found_key.offset + 1; |
| 3146 | } |
| 3147 | |
| 3148 | ret = process_recorded_refs(sctx); |
| 3149 | |
| 3150 | out: |
| 3151 | btrfs_free_path(path); |
| 3152 | return ret; |
| 3153 | } |
| 3154 | |
| 3155 | static int send_set_xattr(struct send_ctx *sctx, |
| 3156 | struct fs_path *path, |
| 3157 | const char *name, int name_len, |
| 3158 | const char *data, int data_len) |
| 3159 | { |
| 3160 | int ret = 0; |
| 3161 | |
| 3162 | ret = begin_cmd(sctx, BTRFS_SEND_C_SET_XATTR); |
| 3163 | if (ret < 0) |
| 3164 | goto out; |
| 3165 | |
| 3166 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 3167 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 3168 | TLV_PUT(sctx, BTRFS_SEND_A_XATTR_DATA, data, data_len); |
| 3169 | |
| 3170 | ret = send_cmd(sctx); |
| 3171 | |
| 3172 | tlv_put_failure: |
| 3173 | out: |
| 3174 | return ret; |
| 3175 | } |
| 3176 | |
| 3177 | static int send_remove_xattr(struct send_ctx *sctx, |
| 3178 | struct fs_path *path, |
| 3179 | const char *name, int name_len) |
| 3180 | { |
| 3181 | int ret = 0; |
| 3182 | |
| 3183 | ret = begin_cmd(sctx, BTRFS_SEND_C_REMOVE_XATTR); |
| 3184 | if (ret < 0) |
| 3185 | goto out; |
| 3186 | |
| 3187 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, path); |
| 3188 | TLV_PUT_STRING(sctx, BTRFS_SEND_A_XATTR_NAME, name, name_len); |
| 3189 | |
| 3190 | ret = send_cmd(sctx); |
| 3191 | |
| 3192 | tlv_put_failure: |
| 3193 | out: |
| 3194 | return ret; |
| 3195 | } |
| 3196 | |
| 3197 | static int __process_new_xattr(int num, struct btrfs_key *di_key, |
| 3198 | const char *name, int name_len, |
| 3199 | const char *data, int data_len, |
| 3200 | u8 type, void *ctx) |
| 3201 | { |
| 3202 | int ret; |
| 3203 | struct send_ctx *sctx = ctx; |
| 3204 | struct fs_path *p; |
| 3205 | posix_acl_xattr_header dummy_acl; |
| 3206 | |
| 3207 | p = fs_path_alloc(sctx); |
| 3208 | if (!p) |
| 3209 | return -ENOMEM; |
| 3210 | |
| 3211 | /* |
| 3212 | * This hack is needed because empty acl's are stored as zero byte |
| 3213 | * data in xattrs. Problem with that is, that receiving these zero byte |
| 3214 | * acl's will fail later. To fix this, we send a dummy acl list that |
| 3215 | * only contains the version number and no entries. |
| 3216 | */ |
| 3217 | if (!strncmp(name, XATTR_NAME_POSIX_ACL_ACCESS, name_len) || |
| 3218 | !strncmp(name, XATTR_NAME_POSIX_ACL_DEFAULT, name_len)) { |
| 3219 | if (data_len == 0) { |
| 3220 | dummy_acl.a_version = |
| 3221 | cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
| 3222 | data = (char *)&dummy_acl; |
| 3223 | data_len = sizeof(dummy_acl); |
| 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 3228 | if (ret < 0) |
| 3229 | goto out; |
| 3230 | |
| 3231 | ret = send_set_xattr(sctx, p, name, name_len, data, data_len); |
| 3232 | |
| 3233 | out: |
| 3234 | fs_path_free(sctx, p); |
| 3235 | return ret; |
| 3236 | } |
| 3237 | |
| 3238 | static int __process_deleted_xattr(int num, struct btrfs_key *di_key, |
| 3239 | const char *name, int name_len, |
| 3240 | const char *data, int data_len, |
| 3241 | u8 type, void *ctx) |
| 3242 | { |
| 3243 | int ret; |
| 3244 | struct send_ctx *sctx = ctx; |
| 3245 | struct fs_path *p; |
| 3246 | |
| 3247 | p = fs_path_alloc(sctx); |
| 3248 | if (!p) |
| 3249 | return -ENOMEM; |
| 3250 | |
| 3251 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 3252 | if (ret < 0) |
| 3253 | goto out; |
| 3254 | |
| 3255 | ret = send_remove_xattr(sctx, p, name, name_len); |
| 3256 | |
| 3257 | out: |
| 3258 | fs_path_free(sctx, p); |
| 3259 | return ret; |
| 3260 | } |
| 3261 | |
| 3262 | static int process_new_xattr(struct send_ctx *sctx) |
| 3263 | { |
| 3264 | int ret = 0; |
| 3265 | |
| 3266 | ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path, |
| 3267 | sctx->cmp_key, __process_new_xattr, sctx); |
| 3268 | |
| 3269 | return ret; |
| 3270 | } |
| 3271 | |
| 3272 | static int process_deleted_xattr(struct send_ctx *sctx) |
| 3273 | { |
| 3274 | int ret; |
| 3275 | |
| 3276 | ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path, |
| 3277 | sctx->cmp_key, __process_deleted_xattr, sctx); |
| 3278 | |
| 3279 | return ret; |
| 3280 | } |
| 3281 | |
| 3282 | struct find_xattr_ctx { |
| 3283 | const char *name; |
| 3284 | int name_len; |
| 3285 | int found_idx; |
| 3286 | char *found_data; |
| 3287 | int found_data_len; |
| 3288 | }; |
| 3289 | |
| 3290 | static int __find_xattr(int num, struct btrfs_key *di_key, |
| 3291 | const char *name, int name_len, |
| 3292 | const char *data, int data_len, |
| 3293 | u8 type, void *vctx) |
| 3294 | { |
| 3295 | struct find_xattr_ctx *ctx = vctx; |
| 3296 | |
| 3297 | if (name_len == ctx->name_len && |
| 3298 | strncmp(name, ctx->name, name_len) == 0) { |
| 3299 | ctx->found_idx = num; |
| 3300 | ctx->found_data_len = data_len; |
| 3301 | ctx->found_data = kmalloc(data_len, GFP_NOFS); |
| 3302 | if (!ctx->found_data) |
| 3303 | return -ENOMEM; |
| 3304 | memcpy(ctx->found_data, data, data_len); |
| 3305 | return 1; |
| 3306 | } |
| 3307 | return 0; |
| 3308 | } |
| 3309 | |
| 3310 | static int find_xattr(struct send_ctx *sctx, |
| 3311 | struct btrfs_root *root, |
| 3312 | struct btrfs_path *path, |
| 3313 | struct btrfs_key *key, |
| 3314 | const char *name, int name_len, |
| 3315 | char **data, int *data_len) |
| 3316 | { |
| 3317 | int ret; |
| 3318 | struct find_xattr_ctx ctx; |
| 3319 | |
| 3320 | ctx.name = name; |
| 3321 | ctx.name_len = name_len; |
| 3322 | ctx.found_idx = -1; |
| 3323 | ctx.found_data = NULL; |
| 3324 | ctx.found_data_len = 0; |
| 3325 | |
| 3326 | ret = iterate_dir_item(sctx, root, path, key, __find_xattr, &ctx); |
| 3327 | if (ret < 0) |
| 3328 | return ret; |
| 3329 | |
| 3330 | if (ctx.found_idx == -1) |
| 3331 | return -ENOENT; |
| 3332 | if (data) { |
| 3333 | *data = ctx.found_data; |
| 3334 | *data_len = ctx.found_data_len; |
| 3335 | } else { |
| 3336 | kfree(ctx.found_data); |
| 3337 | } |
| 3338 | return ctx.found_idx; |
| 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | static int __process_changed_new_xattr(int num, struct btrfs_key *di_key, |
| 3343 | const char *name, int name_len, |
| 3344 | const char *data, int data_len, |
| 3345 | u8 type, void *ctx) |
| 3346 | { |
| 3347 | int ret; |
| 3348 | struct send_ctx *sctx = ctx; |
| 3349 | char *found_data = NULL; |
| 3350 | int found_data_len = 0; |
| 3351 | struct fs_path *p = NULL; |
| 3352 | |
| 3353 | ret = find_xattr(sctx, sctx->parent_root, sctx->right_path, |
| 3354 | sctx->cmp_key, name, name_len, &found_data, |
| 3355 | &found_data_len); |
| 3356 | if (ret == -ENOENT) { |
| 3357 | ret = __process_new_xattr(num, di_key, name, name_len, data, |
| 3358 | data_len, type, ctx); |
| 3359 | } else if (ret >= 0) { |
| 3360 | if (data_len != found_data_len || |
| 3361 | memcmp(data, found_data, data_len)) { |
| 3362 | ret = __process_new_xattr(num, di_key, name, name_len, |
| 3363 | data, data_len, type, ctx); |
| 3364 | } else { |
| 3365 | ret = 0; |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | kfree(found_data); |
| 3370 | fs_path_free(sctx, p); |
| 3371 | return ret; |
| 3372 | } |
| 3373 | |
| 3374 | static int __process_changed_deleted_xattr(int num, struct btrfs_key *di_key, |
| 3375 | const char *name, int name_len, |
| 3376 | const char *data, int data_len, |
| 3377 | u8 type, void *ctx) |
| 3378 | { |
| 3379 | int ret; |
| 3380 | struct send_ctx *sctx = ctx; |
| 3381 | |
| 3382 | ret = find_xattr(sctx, sctx->send_root, sctx->left_path, sctx->cmp_key, |
| 3383 | name, name_len, NULL, NULL); |
| 3384 | if (ret == -ENOENT) |
| 3385 | ret = __process_deleted_xattr(num, di_key, name, name_len, data, |
| 3386 | data_len, type, ctx); |
| 3387 | else if (ret >= 0) |
| 3388 | ret = 0; |
| 3389 | |
| 3390 | return ret; |
| 3391 | } |
| 3392 | |
| 3393 | static int process_changed_xattr(struct send_ctx *sctx) |
| 3394 | { |
| 3395 | int ret = 0; |
| 3396 | |
| 3397 | ret = iterate_dir_item(sctx, sctx->send_root, sctx->left_path, |
| 3398 | sctx->cmp_key, __process_changed_new_xattr, sctx); |
| 3399 | if (ret < 0) |
| 3400 | goto out; |
| 3401 | ret = iterate_dir_item(sctx, sctx->parent_root, sctx->right_path, |
| 3402 | sctx->cmp_key, __process_changed_deleted_xattr, sctx); |
| 3403 | |
| 3404 | out: |
| 3405 | return ret; |
| 3406 | } |
| 3407 | |
| 3408 | static int process_all_new_xattrs(struct send_ctx *sctx) |
| 3409 | { |
| 3410 | int ret; |
| 3411 | struct btrfs_root *root; |
| 3412 | struct btrfs_path *path; |
| 3413 | struct btrfs_key key; |
| 3414 | struct btrfs_key found_key; |
| 3415 | struct extent_buffer *eb; |
| 3416 | int slot; |
| 3417 | |
| 3418 | path = alloc_path_for_send(); |
| 3419 | if (!path) |
| 3420 | return -ENOMEM; |
| 3421 | |
| 3422 | root = sctx->send_root; |
| 3423 | |
| 3424 | key.objectid = sctx->cmp_key->objectid; |
| 3425 | key.type = BTRFS_XATTR_ITEM_KEY; |
| 3426 | key.offset = 0; |
| 3427 | while (1) { |
| 3428 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 3429 | if (ret < 0) |
| 3430 | goto out; |
| 3431 | if (ret) { |
| 3432 | ret = 0; |
| 3433 | goto out; |
| 3434 | } |
| 3435 | |
| 3436 | eb = path->nodes[0]; |
| 3437 | slot = path->slots[0]; |
| 3438 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3439 | |
| 3440 | if (found_key.objectid != key.objectid || |
| 3441 | found_key.type != key.type) { |
| 3442 | ret = 0; |
| 3443 | goto out; |
| 3444 | } |
| 3445 | |
| 3446 | ret = iterate_dir_item(sctx, root, path, &found_key, |
| 3447 | __process_new_xattr, sctx); |
| 3448 | if (ret < 0) |
| 3449 | goto out; |
| 3450 | |
| 3451 | btrfs_release_path(path); |
| 3452 | key.offset = found_key.offset + 1; |
| 3453 | } |
| 3454 | |
| 3455 | out: |
| 3456 | btrfs_free_path(path); |
| 3457 | return ret; |
| 3458 | } |
| 3459 | |
| 3460 | /* |
| 3461 | * Read some bytes from the current inode/file and send a write command to |
| 3462 | * user space. |
| 3463 | */ |
| 3464 | static int send_write(struct send_ctx *sctx, u64 offset, u32 len) |
| 3465 | { |
| 3466 | int ret = 0; |
| 3467 | struct fs_path *p; |
| 3468 | loff_t pos = offset; |
Chris Mason | b24baf6 | 2012-07-25 19:21:10 -0400 | [diff] [blame] | 3469 | int readed = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3470 | mm_segment_t old_fs; |
| 3471 | |
| 3472 | p = fs_path_alloc(sctx); |
| 3473 | if (!p) |
| 3474 | return -ENOMEM; |
| 3475 | |
| 3476 | /* |
| 3477 | * vfs normally only accepts user space buffers for security reasons. |
| 3478 | * we only read from the file and also only provide the read_buf buffer |
| 3479 | * to vfs. As this buffer does not come from a user space call, it's |
| 3480 | * ok to temporary allow kernel space buffers. |
| 3481 | */ |
| 3482 | old_fs = get_fs(); |
| 3483 | set_fs(KERNEL_DS); |
| 3484 | |
| 3485 | verbose_printk("btrfs: send_write offset=%llu, len=%d\n", offset, len); |
| 3486 | |
| 3487 | ret = open_cur_inode_file(sctx); |
| 3488 | if (ret < 0) |
| 3489 | goto out; |
| 3490 | |
| 3491 | ret = vfs_read(sctx->cur_inode_filp, sctx->read_buf, len, &pos); |
| 3492 | if (ret < 0) |
| 3493 | goto out; |
| 3494 | readed = ret; |
| 3495 | if (!readed) |
| 3496 | goto out; |
| 3497 | |
| 3498 | ret = begin_cmd(sctx, BTRFS_SEND_C_WRITE); |
| 3499 | if (ret < 0) |
| 3500 | goto out; |
| 3501 | |
| 3502 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 3503 | if (ret < 0) |
| 3504 | goto out; |
| 3505 | |
| 3506 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 3507 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 3508 | TLV_PUT(sctx, BTRFS_SEND_A_DATA, sctx->read_buf, readed); |
| 3509 | |
| 3510 | ret = send_cmd(sctx); |
| 3511 | |
| 3512 | tlv_put_failure: |
| 3513 | out: |
| 3514 | fs_path_free(sctx, p); |
| 3515 | set_fs(old_fs); |
| 3516 | if (ret < 0) |
| 3517 | return ret; |
| 3518 | return readed; |
| 3519 | } |
| 3520 | |
| 3521 | /* |
| 3522 | * Send a clone command to user space. |
| 3523 | */ |
| 3524 | static int send_clone(struct send_ctx *sctx, |
| 3525 | u64 offset, u32 len, |
| 3526 | struct clone_root *clone_root) |
| 3527 | { |
| 3528 | int ret = 0; |
| 3529 | struct btrfs_root *clone_root2 = clone_root->root; |
| 3530 | struct fs_path *p; |
| 3531 | u64 gen; |
| 3532 | |
| 3533 | verbose_printk("btrfs: send_clone offset=%llu, len=%d, clone_root=%llu, " |
| 3534 | "clone_inode=%llu, clone_offset=%llu\n", offset, len, |
| 3535 | clone_root->root->objectid, clone_root->ino, |
| 3536 | clone_root->offset); |
| 3537 | |
| 3538 | p = fs_path_alloc(sctx); |
| 3539 | if (!p) |
| 3540 | return -ENOMEM; |
| 3541 | |
| 3542 | ret = begin_cmd(sctx, BTRFS_SEND_C_CLONE); |
| 3543 | if (ret < 0) |
| 3544 | goto out; |
| 3545 | |
| 3546 | ret = get_cur_path(sctx, sctx->cur_ino, sctx->cur_inode_gen, p); |
| 3547 | if (ret < 0) |
| 3548 | goto out; |
| 3549 | |
| 3550 | TLV_PUT_U64(sctx, BTRFS_SEND_A_FILE_OFFSET, offset); |
| 3551 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_LEN, len); |
| 3552 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_PATH, p); |
| 3553 | |
| 3554 | if (clone_root2 == sctx->send_root) { |
| 3555 | ret = get_inode_info(sctx->send_root, clone_root->ino, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 3556 | &gen, NULL, NULL, NULL, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3557 | if (ret < 0) |
| 3558 | goto out; |
| 3559 | ret = get_cur_path(sctx, clone_root->ino, gen, p); |
| 3560 | } else { |
| 3561 | ret = get_inode_path(sctx, clone_root2, clone_root->ino, p); |
| 3562 | } |
| 3563 | if (ret < 0) |
| 3564 | goto out; |
| 3565 | |
| 3566 | TLV_PUT_UUID(sctx, BTRFS_SEND_A_CLONE_UUID, |
| 3567 | clone_root2->root_item.uuid); |
| 3568 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_CTRANSID, |
| 3569 | clone_root2->root_item.ctransid); |
| 3570 | TLV_PUT_PATH(sctx, BTRFS_SEND_A_CLONE_PATH, p); |
| 3571 | TLV_PUT_U64(sctx, BTRFS_SEND_A_CLONE_OFFSET, |
| 3572 | clone_root->offset); |
| 3573 | |
| 3574 | ret = send_cmd(sctx); |
| 3575 | |
| 3576 | tlv_put_failure: |
| 3577 | out: |
| 3578 | fs_path_free(sctx, p); |
| 3579 | return ret; |
| 3580 | } |
| 3581 | |
| 3582 | static int send_write_or_clone(struct send_ctx *sctx, |
| 3583 | struct btrfs_path *path, |
| 3584 | struct btrfs_key *key, |
| 3585 | struct clone_root *clone_root) |
| 3586 | { |
| 3587 | int ret = 0; |
| 3588 | struct btrfs_file_extent_item *ei; |
| 3589 | u64 offset = key->offset; |
| 3590 | u64 pos = 0; |
| 3591 | u64 len; |
| 3592 | u32 l; |
| 3593 | u8 type; |
| 3594 | |
| 3595 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 3596 | struct btrfs_file_extent_item); |
| 3597 | type = btrfs_file_extent_type(path->nodes[0], ei); |
| 3598 | if (type == BTRFS_FILE_EXTENT_INLINE) |
| 3599 | len = btrfs_file_extent_inline_len(path->nodes[0], ei); |
| 3600 | else |
| 3601 | len = btrfs_file_extent_num_bytes(path->nodes[0], ei); |
| 3602 | |
| 3603 | if (offset + len > sctx->cur_inode_size) |
| 3604 | len = sctx->cur_inode_size - offset; |
| 3605 | if (len == 0) { |
| 3606 | ret = 0; |
| 3607 | goto out; |
| 3608 | } |
| 3609 | |
| 3610 | if (!clone_root) { |
| 3611 | while (pos < len) { |
| 3612 | l = len - pos; |
| 3613 | if (l > BTRFS_SEND_READ_SIZE) |
| 3614 | l = BTRFS_SEND_READ_SIZE; |
| 3615 | ret = send_write(sctx, pos + offset, l); |
| 3616 | if (ret < 0) |
| 3617 | goto out; |
| 3618 | if (!ret) |
| 3619 | break; |
| 3620 | pos += ret; |
| 3621 | } |
| 3622 | ret = 0; |
| 3623 | } else { |
| 3624 | ret = send_clone(sctx, offset, len, clone_root); |
| 3625 | } |
| 3626 | |
| 3627 | out: |
| 3628 | return ret; |
| 3629 | } |
| 3630 | |
| 3631 | static int is_extent_unchanged(struct send_ctx *sctx, |
| 3632 | struct btrfs_path *left_path, |
| 3633 | struct btrfs_key *ekey) |
| 3634 | { |
| 3635 | int ret = 0; |
| 3636 | struct btrfs_key key; |
| 3637 | struct btrfs_path *path = NULL; |
| 3638 | struct extent_buffer *eb; |
| 3639 | int slot; |
| 3640 | struct btrfs_key found_key; |
| 3641 | struct btrfs_file_extent_item *ei; |
| 3642 | u64 left_disknr; |
| 3643 | u64 right_disknr; |
| 3644 | u64 left_offset; |
| 3645 | u64 right_offset; |
| 3646 | u64 left_offset_fixed; |
| 3647 | u64 left_len; |
| 3648 | u64 right_len; |
| 3649 | u8 left_type; |
| 3650 | u8 right_type; |
| 3651 | |
| 3652 | path = alloc_path_for_send(); |
| 3653 | if (!path) |
| 3654 | return -ENOMEM; |
| 3655 | |
| 3656 | eb = left_path->nodes[0]; |
| 3657 | slot = left_path->slots[0]; |
| 3658 | |
| 3659 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 3660 | left_type = btrfs_file_extent_type(eb, ei); |
| 3661 | left_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 3662 | left_len = btrfs_file_extent_num_bytes(eb, ei); |
| 3663 | left_offset = btrfs_file_extent_offset(eb, ei); |
| 3664 | |
| 3665 | if (left_type != BTRFS_FILE_EXTENT_REG) { |
| 3666 | ret = 0; |
| 3667 | goto out; |
| 3668 | } |
| 3669 | |
| 3670 | /* |
| 3671 | * Following comments will refer to these graphics. L is the left |
| 3672 | * extents which we are checking at the moment. 1-8 are the right |
| 3673 | * extents that we iterate. |
| 3674 | * |
| 3675 | * |-----L-----| |
| 3676 | * |-1-|-2a-|-3-|-4-|-5-|-6-| |
| 3677 | * |
| 3678 | * |-----L-----| |
| 3679 | * |--1--|-2b-|...(same as above) |
| 3680 | * |
| 3681 | * Alternative situation. Happens on files where extents got split. |
| 3682 | * |-----L-----| |
| 3683 | * |-----------7-----------|-6-| |
| 3684 | * |
| 3685 | * Alternative situation. Happens on files which got larger. |
| 3686 | * |-----L-----| |
| 3687 | * |-8-| |
| 3688 | * Nothing follows after 8. |
| 3689 | */ |
| 3690 | |
| 3691 | key.objectid = ekey->objectid; |
| 3692 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 3693 | key.offset = ekey->offset; |
| 3694 | ret = btrfs_search_slot_for_read(sctx->parent_root, &key, path, 0, 0); |
| 3695 | if (ret < 0) |
| 3696 | goto out; |
| 3697 | if (ret) { |
| 3698 | ret = 0; |
| 3699 | goto out; |
| 3700 | } |
| 3701 | |
| 3702 | /* |
| 3703 | * Handle special case where the right side has no extents at all. |
| 3704 | */ |
| 3705 | eb = path->nodes[0]; |
| 3706 | slot = path->slots[0]; |
| 3707 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3708 | if (found_key.objectid != key.objectid || |
| 3709 | found_key.type != key.type) { |
| 3710 | ret = 0; |
| 3711 | goto out; |
| 3712 | } |
| 3713 | |
| 3714 | /* |
| 3715 | * We're now on 2a, 2b or 7. |
| 3716 | */ |
| 3717 | key = found_key; |
| 3718 | while (key.offset < ekey->offset + left_len) { |
| 3719 | ei = btrfs_item_ptr(eb, slot, struct btrfs_file_extent_item); |
| 3720 | right_type = btrfs_file_extent_type(eb, ei); |
| 3721 | right_disknr = btrfs_file_extent_disk_bytenr(eb, ei); |
| 3722 | right_len = btrfs_file_extent_num_bytes(eb, ei); |
| 3723 | right_offset = btrfs_file_extent_offset(eb, ei); |
| 3724 | |
| 3725 | if (right_type != BTRFS_FILE_EXTENT_REG) { |
| 3726 | ret = 0; |
| 3727 | goto out; |
| 3728 | } |
| 3729 | |
| 3730 | /* |
| 3731 | * Are we at extent 8? If yes, we know the extent is changed. |
| 3732 | * This may only happen on the first iteration. |
| 3733 | */ |
| 3734 | if (found_key.offset + right_len < ekey->offset) { |
| 3735 | ret = 0; |
| 3736 | goto out; |
| 3737 | } |
| 3738 | |
| 3739 | left_offset_fixed = left_offset; |
| 3740 | if (key.offset < ekey->offset) { |
| 3741 | /* Fix the right offset for 2a and 7. */ |
| 3742 | right_offset += ekey->offset - key.offset; |
| 3743 | } else { |
| 3744 | /* Fix the left offset for all behind 2a and 2b */ |
| 3745 | left_offset_fixed += key.offset - ekey->offset; |
| 3746 | } |
| 3747 | |
| 3748 | /* |
| 3749 | * Check if we have the same extent. |
| 3750 | */ |
| 3751 | if (left_disknr + left_offset_fixed != |
| 3752 | right_disknr + right_offset) { |
| 3753 | ret = 0; |
| 3754 | goto out; |
| 3755 | } |
| 3756 | |
| 3757 | /* |
| 3758 | * Go to the next extent. |
| 3759 | */ |
| 3760 | ret = btrfs_next_item(sctx->parent_root, path); |
| 3761 | if (ret < 0) |
| 3762 | goto out; |
| 3763 | if (!ret) { |
| 3764 | eb = path->nodes[0]; |
| 3765 | slot = path->slots[0]; |
| 3766 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3767 | } |
| 3768 | if (ret || found_key.objectid != key.objectid || |
| 3769 | found_key.type != key.type) { |
| 3770 | key.offset += right_len; |
| 3771 | break; |
| 3772 | } else { |
| 3773 | if (found_key.offset != key.offset + right_len) { |
| 3774 | /* Should really not happen */ |
| 3775 | ret = -EIO; |
| 3776 | goto out; |
| 3777 | } |
| 3778 | } |
| 3779 | key = found_key; |
| 3780 | } |
| 3781 | |
| 3782 | /* |
| 3783 | * We're now behind the left extent (treat as unchanged) or at the end |
| 3784 | * of the right side (treat as changed). |
| 3785 | */ |
| 3786 | if (key.offset >= ekey->offset + left_len) |
| 3787 | ret = 1; |
| 3788 | else |
| 3789 | ret = 0; |
| 3790 | |
| 3791 | |
| 3792 | out: |
| 3793 | btrfs_free_path(path); |
| 3794 | return ret; |
| 3795 | } |
| 3796 | |
| 3797 | static int process_extent(struct send_ctx *sctx, |
| 3798 | struct btrfs_path *path, |
| 3799 | struct btrfs_key *key) |
| 3800 | { |
| 3801 | int ret = 0; |
| 3802 | struct clone_root *found_clone = NULL; |
| 3803 | |
| 3804 | if (S_ISLNK(sctx->cur_inode_mode)) |
| 3805 | return 0; |
| 3806 | |
| 3807 | if (sctx->parent_root && !sctx->cur_inode_new) { |
| 3808 | ret = is_extent_unchanged(sctx, path, key); |
| 3809 | if (ret < 0) |
| 3810 | goto out; |
| 3811 | if (ret) { |
| 3812 | ret = 0; |
| 3813 | goto out; |
| 3814 | } |
| 3815 | } |
| 3816 | |
| 3817 | ret = find_extent_clone(sctx, path, key->objectid, key->offset, |
| 3818 | sctx->cur_inode_size, &found_clone); |
| 3819 | if (ret != -ENOENT && ret < 0) |
| 3820 | goto out; |
| 3821 | |
| 3822 | ret = send_write_or_clone(sctx, path, key, found_clone); |
| 3823 | |
| 3824 | out: |
| 3825 | return ret; |
| 3826 | } |
| 3827 | |
| 3828 | static int process_all_extents(struct send_ctx *sctx) |
| 3829 | { |
| 3830 | int ret; |
| 3831 | struct btrfs_root *root; |
| 3832 | struct btrfs_path *path; |
| 3833 | struct btrfs_key key; |
| 3834 | struct btrfs_key found_key; |
| 3835 | struct extent_buffer *eb; |
| 3836 | int slot; |
| 3837 | |
| 3838 | root = sctx->send_root; |
| 3839 | path = alloc_path_for_send(); |
| 3840 | if (!path) |
| 3841 | return -ENOMEM; |
| 3842 | |
| 3843 | key.objectid = sctx->cmp_key->objectid; |
| 3844 | key.type = BTRFS_EXTENT_DATA_KEY; |
| 3845 | key.offset = 0; |
| 3846 | while (1) { |
| 3847 | ret = btrfs_search_slot_for_read(root, &key, path, 1, 0); |
| 3848 | if (ret < 0) |
| 3849 | goto out; |
| 3850 | if (ret) { |
| 3851 | ret = 0; |
| 3852 | goto out; |
| 3853 | } |
| 3854 | |
| 3855 | eb = path->nodes[0]; |
| 3856 | slot = path->slots[0]; |
| 3857 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 3858 | |
| 3859 | if (found_key.objectid != key.objectid || |
| 3860 | found_key.type != key.type) { |
| 3861 | ret = 0; |
| 3862 | goto out; |
| 3863 | } |
| 3864 | |
| 3865 | ret = process_extent(sctx, path, &found_key); |
| 3866 | if (ret < 0) |
| 3867 | goto out; |
| 3868 | |
| 3869 | btrfs_release_path(path); |
| 3870 | key.offset = found_key.offset + 1; |
| 3871 | } |
| 3872 | |
| 3873 | out: |
| 3874 | btrfs_free_path(path); |
| 3875 | return ret; |
| 3876 | } |
| 3877 | |
| 3878 | static int process_recorded_refs_if_needed(struct send_ctx *sctx, int at_end) |
| 3879 | { |
| 3880 | int ret = 0; |
| 3881 | |
| 3882 | if (sctx->cur_ino == 0) |
| 3883 | goto out; |
| 3884 | if (!at_end && sctx->cur_ino == sctx->cmp_key->objectid && |
| 3885 | sctx->cmp_key->type <= BTRFS_INODE_REF_KEY) |
| 3886 | goto out; |
| 3887 | if (list_empty(&sctx->new_refs) && list_empty(&sctx->deleted_refs)) |
| 3888 | goto out; |
| 3889 | |
| 3890 | ret = process_recorded_refs(sctx); |
| 3891 | |
| 3892 | out: |
| 3893 | return ret; |
| 3894 | } |
| 3895 | |
| 3896 | static int finish_inode_if_needed(struct send_ctx *sctx, int at_end) |
| 3897 | { |
| 3898 | int ret = 0; |
| 3899 | u64 left_mode; |
| 3900 | u64 left_uid; |
| 3901 | u64 left_gid; |
| 3902 | u64 right_mode; |
| 3903 | u64 right_uid; |
| 3904 | u64 right_gid; |
| 3905 | int need_chmod = 0; |
| 3906 | int need_chown = 0; |
| 3907 | |
| 3908 | ret = process_recorded_refs_if_needed(sctx, at_end); |
| 3909 | if (ret < 0) |
| 3910 | goto out; |
| 3911 | |
| 3912 | if (sctx->cur_ino == 0 || sctx->cur_inode_deleted) |
| 3913 | goto out; |
| 3914 | if (!at_end && sctx->cmp_key->objectid == sctx->cur_ino) |
| 3915 | goto out; |
| 3916 | |
| 3917 | ret = get_inode_info(sctx->send_root, sctx->cur_ino, NULL, NULL, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 3918 | &left_mode, &left_uid, &left_gid, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3919 | if (ret < 0) |
| 3920 | goto out; |
| 3921 | |
| 3922 | if (!S_ISLNK(sctx->cur_inode_mode)) { |
| 3923 | if (!sctx->parent_root || sctx->cur_inode_new) { |
| 3924 | need_chmod = 1; |
| 3925 | need_chown = 1; |
| 3926 | } else { |
| 3927 | ret = get_inode_info(sctx->parent_root, sctx->cur_ino, |
| 3928 | NULL, NULL, &right_mode, &right_uid, |
Alexander Block | 85a7b33 | 2012-07-26 23:39:10 +0200 | [diff] [blame] | 3929 | &right_gid, NULL); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3930 | if (ret < 0) |
| 3931 | goto out; |
| 3932 | |
| 3933 | if (left_uid != right_uid || left_gid != right_gid) |
| 3934 | need_chown = 1; |
| 3935 | if (left_mode != right_mode) |
| 3936 | need_chmod = 1; |
| 3937 | } |
| 3938 | } |
| 3939 | |
| 3940 | if (S_ISREG(sctx->cur_inode_mode)) { |
| 3941 | ret = send_truncate(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 3942 | sctx->cur_inode_size); |
| 3943 | if (ret < 0) |
| 3944 | goto out; |
| 3945 | } |
| 3946 | |
| 3947 | if (need_chown) { |
| 3948 | ret = send_chown(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 3949 | left_uid, left_gid); |
| 3950 | if (ret < 0) |
| 3951 | goto out; |
| 3952 | } |
| 3953 | if (need_chmod) { |
| 3954 | ret = send_chmod(sctx, sctx->cur_ino, sctx->cur_inode_gen, |
| 3955 | left_mode); |
| 3956 | if (ret < 0) |
| 3957 | goto out; |
| 3958 | } |
| 3959 | |
| 3960 | /* |
| 3961 | * Need to send that every time, no matter if it actually changed |
| 3962 | * between the two trees as we have done changes to the inode before. |
| 3963 | */ |
| 3964 | ret = send_utimes(sctx, sctx->cur_ino, sctx->cur_inode_gen); |
| 3965 | if (ret < 0) |
| 3966 | goto out; |
| 3967 | |
| 3968 | out: |
| 3969 | return ret; |
| 3970 | } |
| 3971 | |
| 3972 | static int changed_inode(struct send_ctx *sctx, |
| 3973 | enum btrfs_compare_tree_result result) |
| 3974 | { |
| 3975 | int ret = 0; |
| 3976 | struct btrfs_key *key = sctx->cmp_key; |
| 3977 | struct btrfs_inode_item *left_ii = NULL; |
| 3978 | struct btrfs_inode_item *right_ii = NULL; |
| 3979 | u64 left_gen = 0; |
| 3980 | u64 right_gen = 0; |
| 3981 | |
| 3982 | ret = close_cur_inode_file(sctx); |
| 3983 | if (ret < 0) |
| 3984 | goto out; |
| 3985 | |
| 3986 | sctx->cur_ino = key->objectid; |
| 3987 | sctx->cur_inode_new_gen = 0; |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 3988 | sctx->send_progress = sctx->cur_ino; |
| 3989 | |
| 3990 | if (result == BTRFS_COMPARE_TREE_NEW || |
| 3991 | result == BTRFS_COMPARE_TREE_CHANGED) { |
| 3992 | left_ii = btrfs_item_ptr(sctx->left_path->nodes[0], |
| 3993 | sctx->left_path->slots[0], |
| 3994 | struct btrfs_inode_item); |
| 3995 | left_gen = btrfs_inode_generation(sctx->left_path->nodes[0], |
| 3996 | left_ii); |
| 3997 | } else { |
| 3998 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 3999 | sctx->right_path->slots[0], |
| 4000 | struct btrfs_inode_item); |
| 4001 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 4002 | right_ii); |
| 4003 | } |
| 4004 | if (result == BTRFS_COMPARE_TREE_CHANGED) { |
| 4005 | right_ii = btrfs_item_ptr(sctx->right_path->nodes[0], |
| 4006 | sctx->right_path->slots[0], |
| 4007 | struct btrfs_inode_item); |
| 4008 | |
| 4009 | right_gen = btrfs_inode_generation(sctx->right_path->nodes[0], |
| 4010 | right_ii); |
| 4011 | if (left_gen != right_gen) |
| 4012 | sctx->cur_inode_new_gen = 1; |
| 4013 | } |
| 4014 | |
| 4015 | if (result == BTRFS_COMPARE_TREE_NEW) { |
| 4016 | sctx->cur_inode_gen = left_gen; |
| 4017 | sctx->cur_inode_new = 1; |
| 4018 | sctx->cur_inode_deleted = 0; |
| 4019 | sctx->cur_inode_size = btrfs_inode_size( |
| 4020 | sctx->left_path->nodes[0], left_ii); |
| 4021 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4022 | sctx->left_path->nodes[0], left_ii); |
| 4023 | if (sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4024 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4025 | } else if (result == BTRFS_COMPARE_TREE_DELETED) { |
| 4026 | sctx->cur_inode_gen = right_gen; |
| 4027 | sctx->cur_inode_new = 0; |
| 4028 | sctx->cur_inode_deleted = 1; |
| 4029 | sctx->cur_inode_size = btrfs_inode_size( |
| 4030 | sctx->right_path->nodes[0], right_ii); |
| 4031 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4032 | sctx->right_path->nodes[0], right_ii); |
| 4033 | } else if (result == BTRFS_COMPARE_TREE_CHANGED) { |
| 4034 | if (sctx->cur_inode_new_gen) { |
| 4035 | sctx->cur_inode_gen = right_gen; |
| 4036 | sctx->cur_inode_new = 0; |
| 4037 | sctx->cur_inode_deleted = 1; |
| 4038 | sctx->cur_inode_size = btrfs_inode_size( |
| 4039 | sctx->right_path->nodes[0], right_ii); |
| 4040 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4041 | sctx->right_path->nodes[0], right_ii); |
| 4042 | ret = process_all_refs(sctx, |
| 4043 | BTRFS_COMPARE_TREE_DELETED); |
| 4044 | if (ret < 0) |
| 4045 | goto out; |
| 4046 | |
| 4047 | sctx->cur_inode_gen = left_gen; |
| 4048 | sctx->cur_inode_new = 1; |
| 4049 | sctx->cur_inode_deleted = 0; |
| 4050 | sctx->cur_inode_size = btrfs_inode_size( |
| 4051 | sctx->left_path->nodes[0], left_ii); |
| 4052 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4053 | sctx->left_path->nodes[0], left_ii); |
Alexander Block | 1f4692d | 2012-07-28 10:42:24 +0200 | [diff] [blame] | 4054 | ret = send_create_inode_if_needed(sctx); |
Alexander Block | 31db9f7 | 2012-07-25 23:19:24 +0200 | [diff] [blame] | 4055 | if (ret < 0) |
| 4056 | goto out; |
| 4057 | |
| 4058 | ret = process_all_refs(sctx, BTRFS_COMPARE_TREE_NEW); |
| 4059 | if (ret < 0) |
| 4060 | goto out; |
| 4061 | ret = process_all_extents(sctx); |
| 4062 | if (ret < 0) |
| 4063 | goto out; |
| 4064 | ret = process_all_new_xattrs(sctx); |
| 4065 | if (ret < 0) |
| 4066 | goto out; |
| 4067 | } else { |
| 4068 | sctx->cur_inode_gen = left_gen; |
| 4069 | sctx->cur_inode_new = 0; |
| 4070 | sctx->cur_inode_new_gen = 0; |
| 4071 | sctx->cur_inode_deleted = 0; |
| 4072 | sctx->cur_inode_size = btrfs_inode_size( |
| 4073 | sctx->left_path->nodes[0], left_ii); |
| 4074 | sctx->cur_inode_mode = btrfs_inode_mode( |
| 4075 | sctx->left_path->nodes[0], left_ii); |
| 4076 | } |
| 4077 | } |
| 4078 | |
| 4079 | out: |
| 4080 | return ret; |
| 4081 | } |
| 4082 | |
| 4083 | static int changed_ref(struct send_ctx *sctx, |
| 4084 | enum btrfs_compare_tree_result result) |
| 4085 | { |
| 4086 | int ret = 0; |
| 4087 | |
| 4088 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); |
| 4089 | |
| 4090 | if (!sctx->cur_inode_new_gen && |
| 4091 | sctx->cur_ino != BTRFS_FIRST_FREE_OBJECTID) { |
| 4092 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 4093 | ret = record_new_ref(sctx); |
| 4094 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 4095 | ret = record_deleted_ref(sctx); |
| 4096 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 4097 | ret = record_changed_ref(sctx); |
| 4098 | } |
| 4099 | |
| 4100 | return ret; |
| 4101 | } |
| 4102 | |
| 4103 | static int changed_xattr(struct send_ctx *sctx, |
| 4104 | enum btrfs_compare_tree_result result) |
| 4105 | { |
| 4106 | int ret = 0; |
| 4107 | |
| 4108 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); |
| 4109 | |
| 4110 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 4111 | if (result == BTRFS_COMPARE_TREE_NEW) |
| 4112 | ret = process_new_xattr(sctx); |
| 4113 | else if (result == BTRFS_COMPARE_TREE_DELETED) |
| 4114 | ret = process_deleted_xattr(sctx); |
| 4115 | else if (result == BTRFS_COMPARE_TREE_CHANGED) |
| 4116 | ret = process_changed_xattr(sctx); |
| 4117 | } |
| 4118 | |
| 4119 | return ret; |
| 4120 | } |
| 4121 | |
| 4122 | static int changed_extent(struct send_ctx *sctx, |
| 4123 | enum btrfs_compare_tree_result result) |
| 4124 | { |
| 4125 | int ret = 0; |
| 4126 | |
| 4127 | BUG_ON(sctx->cur_ino != sctx->cmp_key->objectid); |
| 4128 | |
| 4129 | if (!sctx->cur_inode_new_gen && !sctx->cur_inode_deleted) { |
| 4130 | if (result != BTRFS_COMPARE_TREE_DELETED) |
| 4131 | ret = process_extent(sctx, sctx->left_path, |
| 4132 | sctx->cmp_key); |
| 4133 | } |
| 4134 | |
| 4135 | return ret; |
| 4136 | } |
| 4137 | |
| 4138 | |
| 4139 | static int changed_cb(struct btrfs_root *left_root, |
| 4140 | struct btrfs_root *right_root, |
| 4141 | struct btrfs_path *left_path, |
| 4142 | struct btrfs_path *right_path, |
| 4143 | struct btrfs_key *key, |
| 4144 | enum btrfs_compare_tree_result result, |
| 4145 | void *ctx) |
| 4146 | { |
| 4147 | int ret = 0; |
| 4148 | struct send_ctx *sctx = ctx; |
| 4149 | |
| 4150 | sctx->left_path = left_path; |
| 4151 | sctx->right_path = right_path; |
| 4152 | sctx->cmp_key = key; |
| 4153 | |
| 4154 | ret = finish_inode_if_needed(sctx, 0); |
| 4155 | if (ret < 0) |
| 4156 | goto out; |
| 4157 | |
| 4158 | if (key->type == BTRFS_INODE_ITEM_KEY) |
| 4159 | ret = changed_inode(sctx, result); |
| 4160 | else if (key->type == BTRFS_INODE_REF_KEY) |
| 4161 | ret = changed_ref(sctx, result); |
| 4162 | else if (key->type == BTRFS_XATTR_ITEM_KEY) |
| 4163 | ret = changed_xattr(sctx, result); |
| 4164 | else if (key->type == BTRFS_EXTENT_DATA_KEY) |
| 4165 | ret = changed_extent(sctx, result); |
| 4166 | |
| 4167 | out: |
| 4168 | return ret; |
| 4169 | } |
| 4170 | |
| 4171 | static int full_send_tree(struct send_ctx *sctx) |
| 4172 | { |
| 4173 | int ret; |
| 4174 | struct btrfs_trans_handle *trans = NULL; |
| 4175 | struct btrfs_root *send_root = sctx->send_root; |
| 4176 | struct btrfs_key key; |
| 4177 | struct btrfs_key found_key; |
| 4178 | struct btrfs_path *path; |
| 4179 | struct extent_buffer *eb; |
| 4180 | int slot; |
| 4181 | u64 start_ctransid; |
| 4182 | u64 ctransid; |
| 4183 | |
| 4184 | path = alloc_path_for_send(); |
| 4185 | if (!path) |
| 4186 | return -ENOMEM; |
| 4187 | |
| 4188 | spin_lock(&send_root->root_times_lock); |
| 4189 | start_ctransid = btrfs_root_ctransid(&send_root->root_item); |
| 4190 | spin_unlock(&send_root->root_times_lock); |
| 4191 | |
| 4192 | key.objectid = BTRFS_FIRST_FREE_OBJECTID; |
| 4193 | key.type = BTRFS_INODE_ITEM_KEY; |
| 4194 | key.offset = 0; |
| 4195 | |
| 4196 | join_trans: |
| 4197 | /* |
| 4198 | * We need to make sure the transaction does not get committed |
| 4199 | * while we do anything on commit roots. Join a transaction to prevent |
| 4200 | * this. |
| 4201 | */ |
| 4202 | trans = btrfs_join_transaction(send_root); |
| 4203 | if (IS_ERR(trans)) { |
| 4204 | ret = PTR_ERR(trans); |
| 4205 | trans = NULL; |
| 4206 | goto out; |
| 4207 | } |
| 4208 | |
| 4209 | /* |
| 4210 | * Make sure the tree has not changed |
| 4211 | */ |
| 4212 | spin_lock(&send_root->root_times_lock); |
| 4213 | ctransid = btrfs_root_ctransid(&send_root->root_item); |
| 4214 | spin_unlock(&send_root->root_times_lock); |
| 4215 | |
| 4216 | if (ctransid != start_ctransid) { |
| 4217 | WARN(1, KERN_WARNING "btrfs: the root that you're trying to " |
| 4218 | "send was modified in between. This is " |
| 4219 | "probably a bug.\n"); |
| 4220 | ret = -EIO; |
| 4221 | goto out; |
| 4222 | } |
| 4223 | |
| 4224 | ret = btrfs_search_slot_for_read(send_root, &key, path, 1, 0); |
| 4225 | if (ret < 0) |
| 4226 | goto out; |
| 4227 | if (ret) |
| 4228 | goto out_finish; |
| 4229 | |
| 4230 | while (1) { |
| 4231 | /* |
| 4232 | * When someone want to commit while we iterate, end the |
| 4233 | * joined transaction and rejoin. |
| 4234 | */ |
| 4235 | if (btrfs_should_end_transaction(trans, send_root)) { |
| 4236 | ret = btrfs_end_transaction(trans, send_root); |
| 4237 | trans = NULL; |
| 4238 | if (ret < 0) |
| 4239 | goto out; |
| 4240 | btrfs_release_path(path); |
| 4241 | goto join_trans; |
| 4242 | } |
| 4243 | |
| 4244 | eb = path->nodes[0]; |
| 4245 | slot = path->slots[0]; |
| 4246 | btrfs_item_key_to_cpu(eb, &found_key, slot); |
| 4247 | |
| 4248 | ret = changed_cb(send_root, NULL, path, NULL, |
| 4249 | &found_key, BTRFS_COMPARE_TREE_NEW, sctx); |
| 4250 | if (ret < 0) |
| 4251 | goto out; |
| 4252 | |
| 4253 | key.objectid = found_key.objectid; |
| 4254 | key.type = found_key.type; |
| 4255 | key.offset = found_key.offset + 1; |
| 4256 | |
| 4257 | ret = btrfs_next_item(send_root, path); |
| 4258 | if (ret < 0) |
| 4259 | goto out; |
| 4260 | if (ret) { |
| 4261 | ret = 0; |
| 4262 | break; |
| 4263 | } |
| 4264 | } |
| 4265 | |
| 4266 | out_finish: |
| 4267 | ret = finish_inode_if_needed(sctx, 1); |
| 4268 | |
| 4269 | out: |
| 4270 | btrfs_free_path(path); |
| 4271 | if (trans) { |
| 4272 | if (!ret) |
| 4273 | ret = btrfs_end_transaction(trans, send_root); |
| 4274 | else |
| 4275 | btrfs_end_transaction(trans, send_root); |
| 4276 | } |
| 4277 | return ret; |
| 4278 | } |
| 4279 | |
| 4280 | static int send_subvol(struct send_ctx *sctx) |
| 4281 | { |
| 4282 | int ret; |
| 4283 | |
| 4284 | ret = send_header(sctx); |
| 4285 | if (ret < 0) |
| 4286 | goto out; |
| 4287 | |
| 4288 | ret = send_subvol_begin(sctx); |
| 4289 | if (ret < 0) |
| 4290 | goto out; |
| 4291 | |
| 4292 | if (sctx->parent_root) { |
| 4293 | ret = btrfs_compare_trees(sctx->send_root, sctx->parent_root, |
| 4294 | changed_cb, sctx); |
| 4295 | if (ret < 0) |
| 4296 | goto out; |
| 4297 | ret = finish_inode_if_needed(sctx, 1); |
| 4298 | if (ret < 0) |
| 4299 | goto out; |
| 4300 | } else { |
| 4301 | ret = full_send_tree(sctx); |
| 4302 | if (ret < 0) |
| 4303 | goto out; |
| 4304 | } |
| 4305 | |
| 4306 | out: |
| 4307 | if (!ret) |
| 4308 | ret = close_cur_inode_file(sctx); |
| 4309 | else |
| 4310 | close_cur_inode_file(sctx); |
| 4311 | |
| 4312 | free_recorded_refs(sctx); |
| 4313 | return ret; |
| 4314 | } |
| 4315 | |
| 4316 | long btrfs_ioctl_send(struct file *mnt_file, void __user *arg_) |
| 4317 | { |
| 4318 | int ret = 0; |
| 4319 | struct btrfs_root *send_root; |
| 4320 | struct btrfs_root *clone_root; |
| 4321 | struct btrfs_fs_info *fs_info; |
| 4322 | struct btrfs_ioctl_send_args *arg = NULL; |
| 4323 | struct btrfs_key key; |
| 4324 | struct file *filp = NULL; |
| 4325 | struct send_ctx *sctx = NULL; |
| 4326 | u32 i; |
| 4327 | u64 *clone_sources_tmp = NULL; |
| 4328 | |
| 4329 | if (!capable(CAP_SYS_ADMIN)) |
| 4330 | return -EPERM; |
| 4331 | |
| 4332 | send_root = BTRFS_I(fdentry(mnt_file)->d_inode)->root; |
| 4333 | fs_info = send_root->fs_info; |
| 4334 | |
| 4335 | arg = memdup_user(arg_, sizeof(*arg)); |
| 4336 | if (IS_ERR(arg)) { |
| 4337 | ret = PTR_ERR(arg); |
| 4338 | arg = NULL; |
| 4339 | goto out; |
| 4340 | } |
| 4341 | |
| 4342 | if (!access_ok(VERIFY_READ, arg->clone_sources, |
| 4343 | sizeof(*arg->clone_sources * |
| 4344 | arg->clone_sources_count))) { |
| 4345 | ret = -EFAULT; |
| 4346 | goto out; |
| 4347 | } |
| 4348 | |
| 4349 | sctx = kzalloc(sizeof(struct send_ctx), GFP_NOFS); |
| 4350 | if (!sctx) { |
| 4351 | ret = -ENOMEM; |
| 4352 | goto out; |
| 4353 | } |
| 4354 | |
| 4355 | INIT_LIST_HEAD(&sctx->new_refs); |
| 4356 | INIT_LIST_HEAD(&sctx->deleted_refs); |
| 4357 | INIT_RADIX_TREE(&sctx->name_cache, GFP_NOFS); |
| 4358 | INIT_LIST_HEAD(&sctx->name_cache_list); |
| 4359 | |
| 4360 | sctx->send_filp = fget(arg->send_fd); |
| 4361 | if (IS_ERR(sctx->send_filp)) { |
| 4362 | ret = PTR_ERR(sctx->send_filp); |
| 4363 | goto out; |
| 4364 | } |
| 4365 | |
| 4366 | sctx->mnt = mnt_file->f_path.mnt; |
| 4367 | |
| 4368 | sctx->send_root = send_root; |
| 4369 | sctx->clone_roots_cnt = arg->clone_sources_count; |
| 4370 | |
| 4371 | sctx->send_max_size = BTRFS_SEND_BUF_SIZE; |
| 4372 | sctx->send_buf = vmalloc(sctx->send_max_size); |
| 4373 | if (!sctx->send_buf) { |
| 4374 | ret = -ENOMEM; |
| 4375 | goto out; |
| 4376 | } |
| 4377 | |
| 4378 | sctx->read_buf = vmalloc(BTRFS_SEND_READ_SIZE); |
| 4379 | if (!sctx->read_buf) { |
| 4380 | ret = -ENOMEM; |
| 4381 | goto out; |
| 4382 | } |
| 4383 | |
| 4384 | sctx->clone_roots = vzalloc(sizeof(struct clone_root) * |
| 4385 | (arg->clone_sources_count + 1)); |
| 4386 | if (!sctx->clone_roots) { |
| 4387 | ret = -ENOMEM; |
| 4388 | goto out; |
| 4389 | } |
| 4390 | |
| 4391 | if (arg->clone_sources_count) { |
| 4392 | clone_sources_tmp = vmalloc(arg->clone_sources_count * |
| 4393 | sizeof(*arg->clone_sources)); |
| 4394 | if (!clone_sources_tmp) { |
| 4395 | ret = -ENOMEM; |
| 4396 | goto out; |
| 4397 | } |
| 4398 | |
| 4399 | ret = copy_from_user(clone_sources_tmp, arg->clone_sources, |
| 4400 | arg->clone_sources_count * |
| 4401 | sizeof(*arg->clone_sources)); |
| 4402 | if (ret) { |
| 4403 | ret = -EFAULT; |
| 4404 | goto out; |
| 4405 | } |
| 4406 | |
| 4407 | for (i = 0; i < arg->clone_sources_count; i++) { |
| 4408 | key.objectid = clone_sources_tmp[i]; |
| 4409 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4410 | key.offset = (u64)-1; |
| 4411 | clone_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4412 | if (!clone_root) { |
| 4413 | ret = -EINVAL; |
| 4414 | goto out; |
| 4415 | } |
| 4416 | if (IS_ERR(clone_root)) { |
| 4417 | ret = PTR_ERR(clone_root); |
| 4418 | goto out; |
| 4419 | } |
| 4420 | sctx->clone_roots[i].root = clone_root; |
| 4421 | } |
| 4422 | vfree(clone_sources_tmp); |
| 4423 | clone_sources_tmp = NULL; |
| 4424 | } |
| 4425 | |
| 4426 | if (arg->parent_root) { |
| 4427 | key.objectid = arg->parent_root; |
| 4428 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 4429 | key.offset = (u64)-1; |
| 4430 | sctx->parent_root = btrfs_read_fs_root_no_name(fs_info, &key); |
| 4431 | if (!sctx->parent_root) { |
| 4432 | ret = -EINVAL; |
| 4433 | goto out; |
| 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | /* |
| 4438 | * Clones from send_root are allowed, but only if the clone source |
| 4439 | * is behind the current send position. This is checked while searching |
| 4440 | * for possible clone sources. |
| 4441 | */ |
| 4442 | sctx->clone_roots[sctx->clone_roots_cnt++].root = sctx->send_root; |
| 4443 | |
| 4444 | /* We do a bsearch later */ |
| 4445 | sort(sctx->clone_roots, sctx->clone_roots_cnt, |
| 4446 | sizeof(*sctx->clone_roots), __clone_root_cmp_sort, |
| 4447 | NULL); |
| 4448 | |
| 4449 | ret = send_subvol(sctx); |
| 4450 | if (ret < 0) |
| 4451 | goto out; |
| 4452 | |
| 4453 | ret = begin_cmd(sctx, BTRFS_SEND_C_END); |
| 4454 | if (ret < 0) |
| 4455 | goto out; |
| 4456 | ret = send_cmd(sctx); |
| 4457 | if (ret < 0) |
| 4458 | goto out; |
| 4459 | |
| 4460 | out: |
| 4461 | if (filp) |
| 4462 | fput(filp); |
| 4463 | kfree(arg); |
| 4464 | vfree(clone_sources_tmp); |
| 4465 | |
| 4466 | if (sctx) { |
| 4467 | if (sctx->send_filp) |
| 4468 | fput(sctx->send_filp); |
| 4469 | |
| 4470 | vfree(sctx->clone_roots); |
| 4471 | vfree(sctx->send_buf); |
| 4472 | vfree(sctx->read_buf); |
| 4473 | |
| 4474 | name_cache_free(sctx); |
| 4475 | |
| 4476 | kfree(sctx); |
| 4477 | } |
| 4478 | |
| 4479 | return ret; |
| 4480 | } |