sys/vfs/hammer/hammer_mirror.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 | /* * Copyright (c) 2008 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Matthew Dillon <dillon@backplane.com> * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * HAMMER mirroring ioctls - serialize and deserialize modifications made * to a filesystem. */ #include "hammer.h" static int hammer_mirror_check(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec); static int hammer_mirror_update(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec); static int hammer_ioc_mirror_write_rec(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec, struct hammer_ioc_mirror_rw *mirror, uint32_t localization, char *uptr); static int hammer_ioc_mirror_write_pass(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec, struct hammer_ioc_mirror_rw *mirror, uint32_t localization); static int hammer_ioc_mirror_write_skip(hammer_cursor_t cursor, struct hammer_ioc_mrecord_skip *mrec, struct hammer_ioc_mirror_rw *mirror, uint32_t localization); static int hammer_mirror_delete_to(hammer_cursor_t cursor, struct hammer_ioc_mirror_rw *mirror); static int hammer_mirror_nomirror(hammer_base_elm_t base); /* * All B-Tree records within the specified key range which also conform * to the transaction id range are returned. Mirroring code keeps track * of the last transaction id fully scanned and can efficiently pick up * where it left off if interrupted. * * The PFS is identified in the mirror structure. The passed ip is just * some directory in the overall HAMMER filesystem and has nothing to * do with the PFS. */ int hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip, struct hammer_ioc_mirror_rw *mirror) { struct hammer_cmirror cmirror; struct hammer_cursor cursor; union hammer_ioc_mrecord_any mrec; hammer_btree_leaf_elm_t elm; char *uptr; int error; int data_len; int bytes; int eatdisk; int mrec_flags; uint32_t localization; hammer_crc_t rec_crc; localization = pfs_to_lo(mirror->pfs_id); if ((mirror->key_beg.localization | mirror->key_end.localization) & HAMMER_LOCALIZE_PSEUDOFS_MASK) { return(EINVAL); } if (hammer_btree_cmp(&mirror->key_beg, &mirror->key_end) > 0) return(EINVAL); mirror->key_cur = mirror->key_beg; mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK; mirror->key_cur.localization |= localization; bzero(&mrec, sizeof(mrec)); bzero(&cmirror, sizeof(cmirror)); /* * Make CRC errors non-fatal (at least on data), causing an EDOM * error instead of EIO. */ trans->flags |= HAMMER_TRANSF_CRCDOM; retry: error = hammer_init_cursor(trans, &cursor, NULL, NULL); if (error) { hammer_done_cursor(&cursor); goto failed; } cursor.key_beg = mirror->key_cur; cursor.key_end = mirror->key_end; cursor.key_end.localization &= HAMMER_LOCALIZE_MASK; cursor.key_end.localization |= localization; cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE; cursor.flags |= HAMMER_CURSOR_BACKEND; /* * This flag filters the search to only return elements whos create * or delete TID is >= mirror_tid. The B-Tree uses the mirror_tid * field stored with internal and leaf nodes to shortcut the scan. */ cursor.flags |= HAMMER_CURSOR_MIRROR_FILTERED; cursor.cmirror = &cmirror; cmirror.mirror_tid = mirror->tid_beg; error = hammer_btree_first(&cursor); while (error == 0) { /* * Yield to more important tasks */ if (error == 0) { error = hammer_signal_check(trans->hmp); if (error) break; } /* * An internal node can be returned in mirror-filtered * mode and indicates that the scan is returning a skip * range in the cursor->cmirror structure. */ uptr = (char *)mirror->ubuf + mirror->count; if (cursor.node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) { /* * Check space */ mirror->key_cur = cmirror.skip_beg; bytes = sizeof(mrec.skip); if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) > mirror->size) { break; } /* * Fill mrec */ mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE; mrec.head.type = HAMMER_MREC_TYPE_SKIP; mrec.head.rec_size = bytes; mrec.skip.skip_beg = cmirror.skip_beg; mrec.skip.skip_end = cmirror.skip_end; hammer_crc_set_mrec_head(&mrec.head, bytes); error = copyout(&mrec, uptr, bytes); eatdisk = 0; goto didwrite; } /* * Leaf node. In full-history mode we could filter out * elements modified outside the user-requested TID range. * * However, such elements must be returned so the writer * can compare them against the target to determine what * needs to be deleted on the target, particular for * no-history mirrors. */ KKASSERT(cursor.node->ondisk->type == HAMMER_BTREE_TYPE_LEAF); elm = &cursor.node->ondisk->elms[cursor.index].leaf; mirror->key_cur = elm->base; /* * If the record was created after our end point we just * ignore it. */ if (elm->base.create_tid > mirror->tid_end) { error = 0; bytes = 0; eatdisk = 1; goto didwrite; } /* * Determine if we should generate a PASS or a REC. PASS * records are records without any data payload. Such * records will be generated if the target is already expected * to have the record, allowing it to delete the gaps. * * A PASS record is also used to perform deletions on the * target. * * Such deletions are needed if the master or files on the * master are no-history, or if the slave is so far behind * the master has already been pruned. */ if (elm->base.create_tid < mirror->tid_beg) { bytes = sizeof(mrec.rec); if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) > mirror->size) { break; } /* * Fill mrec. */ mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE; mrec.head.type = HAMMER_MREC_TYPE_PASS; mrec.head.rec_size = bytes; mrec.rec.leaf = *elm; hammer_crc_set_mrec_head(&mrec.head, bytes); error = copyout(&mrec, uptr, bytes); eatdisk = 1; goto didwrite; } /* * The core code exports the data to userland. * * CRC errors on data are reported but passed through, * but the data must be washed by the user program. * * If userland just wants the btree records it can * request that bulk data not be returned. This is * use during mirror-stream histogram generation. */ mrec_flags = 0; data_len = (elm->data_offset) ? elm->data_len : 0; if (data_len && (mirror->head.flags & HAMMER_IOC_MIRROR_NODATA)) { data_len = 0; mrec_flags |= HAMMER_MRECF_NODATA; } if (data_len) { error = hammer_btree_extract_data(&cursor); if (error) { if (error != EDOM) break; mrec_flags |= HAMMER_MRECF_CRC_ERROR | HAMMER_MRECF_DATA_CRC_BAD; } } bytes = sizeof(mrec.rec) + data_len; if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) > mirror->size) break; /* * Construct the record for userland and copyout. * * The user is asking for a snapshot, if the record was * deleted beyond the user-requested ending tid, the record * is not considered deleted from the point of view of * userland and delete_tid is cleared. */ mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE; mrec.head.type = HAMMER_MREC_TYPE_REC | mrec_flags; mrec.head.rec_size = bytes; mrec.rec.leaf = *elm; if (elm->base.delete_tid > mirror->tid_end) mrec.rec.leaf.base.delete_tid = 0; rec_crc = hammer_crc_get_mrec_head(&mrec.head, sizeof(mrec.rec)); if (data_len) rec_crc = crc32_ext(cursor.data, data_len, rec_crc); mrec.head.rec_crc = rec_crc; error = copyout(&mrec, uptr, sizeof(mrec.rec)); if (data_len && error == 0) { error = copyout(cursor.data, uptr + sizeof(mrec.rec), data_len); } eatdisk = 1; /* * eatdisk controls whether we skip the current cursor * position on the next scan or not. If doing a SKIP * the cursor is already positioned properly for the next * scan and eatdisk will be 0. */ didwrite: if (error == 0) { mirror->count += HAMMER_HEAD_DOALIGN(bytes); if (eatdisk) cursor.flags |= HAMMER_CURSOR_ATEDISK; else cursor.flags &= ~HAMMER_CURSOR_ATEDISK; error = hammer_btree_iterate(&cursor); } } if (error == ENOENT) { mirror->key_cur = mirror->key_end; error = 0; } hammer_done_cursor(&cursor); if (error == EDEADLK) goto retry; if (error == EINTR) { mirror->head.flags |= HAMMER_IOC_HEAD_INTR; error = 0; } failed: mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK; return(error); } /* * Copy records from userland to the target mirror. * * The PFS is identified in the mirror structure. The passed ip is just * some directory in the overall HAMMER filesystem and has nothing to * do with the PFS. In fact, there might not even be a root directory for * the PFS yet! */ int hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip, struct hammer_ioc_mirror_rw *mirror) { union hammer_ioc_mrecord_any mrec; struct hammer_cursor cursor; uint32_t localization; int checkspace_count = 0; int error; int bytes; char *uptr; int seq; localization = pfs_to_lo(mirror->pfs_id); seq = trans->hmp->flusher.done; /* * Validate the mirror structure and relocalize the tracking keys. */ if (mirror->size < 0 || mirror->size > 0x70000000) return(EINVAL); mirror->key_beg.localization &= HAMMER_LOCALIZE_MASK; mirror->key_beg.localization |= localization; mirror->key_end.localization &= HAMMER_LOCALIZE_MASK; mirror->key_end.localization |= localization; mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK; mirror->key_cur.localization |= localization; /* * Set up our tracking cursor for the loop. The tracking cursor * is used to delete records that are no longer present on the * master. The last handled record at key_cur must be skipped. */ error = hammer_init_cursor(trans, &cursor, NULL, NULL); cursor.key_beg = mirror->key_cur; cursor.key_end = mirror->key_end; cursor.flags |= HAMMER_CURSOR_BACKEND; error = hammer_btree_first(&cursor); if (error == 0) cursor.flags |= HAMMER_CURSOR_ATEDISK; if (error == ENOENT) error = 0; /* * Loop until our input buffer has been exhausted. */ while (error == 0 && mirror->count + sizeof(mrec.head) <= mirror->size) { /* * Don't blow out the buffer cache. Leave room for frontend * cache as well. * * WARNING: See warnings in hammer_unlock_cursor() function. */ while (hammer_flusher_meta_halflimit(trans->hmp) || hammer_flusher_undo_exhausted(trans, 2)) { hammer_unlock_cursor(&cursor); hammer_flusher_wait(trans->hmp, seq); hammer_lock_cursor(&cursor); seq = hammer_flusher_async_one(trans->hmp); } /* * If there is insufficient free space it may be due to * reserved big-blocks, which flushing might fix. */ if (hammer_checkspace(trans->hmp, HAMMER_CHKSPC_MIRROR)) { if (++checkspace_count == 10) { error = ENOSPC; break; } hammer_unlock_cursor(&cursor); hammer_flusher_wait(trans->hmp, seq); hammer_lock_cursor(&cursor); seq = hammer_flusher_async(trans->hmp, NULL); } /* * Acquire and validate header */ if ((bytes = mirror->size - mirror->count) > sizeof(mrec)) bytes = sizeof(mrec); uptr = (char *)mirror->ubuf + mirror->count; error = copyin(uptr, &mrec, bytes); if (error) break; if (mrec.head.signature != HAMMER_IOC_MIRROR_SIGNATURE) { error = EINVAL; break; } if (mrec.head.rec_size < sizeof(mrec.head) || mrec.head.rec_size > sizeof(mrec) + HAMMER_XBUFSIZE || mirror->count + mrec.head.rec_size > mirror->size) { error = EINVAL; break; } switch(mrec.head.type & HAMMER_MRECF_TYPE_MASK) { case HAMMER_MREC_TYPE_SKIP: if (mrec.head.rec_size != sizeof(mrec.skip)) error = EINVAL; if (error == 0) error = hammer_ioc_mirror_write_skip(&cursor, &mrec.skip, mirror, localization); break; case HAMMER_MREC_TYPE_REC: if (mrec.head.rec_size < sizeof(mrec.rec)) error = EINVAL; if (error == 0) error = hammer_ioc_mirror_write_rec(&cursor, &mrec.rec, mirror, localization, uptr + sizeof(mrec.rec)); break; case HAMMER_MREC_TYPE_REC_NODATA: case HAMMER_MREC_TYPE_REC_BADCRC: /* * Records with bad data payloads are ignored XXX. * Records with no data payload have to be skipped * (they shouldn't have been written in the first * place). */ if (mrec.head.rec_size < sizeof(mrec.rec)) error = EINVAL; break; case HAMMER_MREC_TYPE_PASS: if (mrec.head.rec_size != sizeof(mrec.rec)) error = EINVAL; if (error == 0) error = hammer_ioc_mirror_write_pass(&cursor, &mrec.rec, mirror, localization); break; default: error = EINVAL; break; } /* * Retry the current record on deadlock, otherwise setup * for the next loop. */ if (error == EDEADLK) { while (error == EDEADLK) { hammer_sync_lock_sh(trans); hammer_recover_cursor(&cursor); error = hammer_cursor_upgrade(&cursor); hammer_sync_unlock(trans); } } else { if (error == EALREADY) error = 0; if (error == 0) { mirror->count += HAMMER_HEAD_DOALIGN(mrec.head.rec_size); } } } hammer_done_cursor(&cursor); /* * cumulative error */ if (error) { mirror->head.flags |= HAMMER_IOC_HEAD_ERROR; mirror->head.error = error; } /* * ioctls don't update the RW data structure if an error is returned, * always return 0. */ return(0); } /* * Handle skip records. * * We must iterate from the last resolved record position at mirror->key_cur * to skip_beg non-inclusive and delete any records encountered. * * mirror->key_cur must be carefully set when we succeed in processing * this mrec. */ static int hammer_ioc_mirror_write_skip(hammer_cursor_t cursor, struct hammer_ioc_mrecord_skip *mrec, struct hammer_ioc_mirror_rw *mirror, uint32_t localization) { int error; /* * Relocalize the skip range */ mrec->skip_beg.localization &= HAMMER_LOCALIZE_MASK; mrec->skip_beg.localization |= localization; mrec->skip_end.localization &= HAMMER_LOCALIZE_MASK; mrec->skip_end.localization |= localization; /* * Iterate from current position to skip_beg, deleting any records * we encounter. The record at skip_beg is not included (it is * skipped). */ cursor->key_end = mrec->skip_beg; cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE; cursor->flags |= HAMMER_CURSOR_BACKEND; error = hammer_mirror_delete_to(cursor, mirror); /* * Now skip past the skip (which is the whole point point of * having a skip record). The sender has not sent us any records * for the skip area so we wouldn't know what to keep and what * to delete anyway. * * Clear ATEDISK because skip_end is non-inclusive, so we can't * count an exact match if we happened to get one. */ if (error == 0) { mirror->key_cur = mrec->skip_end; cursor->key_beg = mrec->skip_end; error = hammer_btree_lookup(cursor); cursor->flags &= ~HAMMER_CURSOR_ATEDISK; if (error == ENOENT) error = 0; } return(error); } /* * Handle B-Tree records. * * We must iterate to mrec->base.key (non-inclusively), and then process * the record. We are allowed to write a new record or delete an existing * record, but cannot replace an existing record. * * mirror->key_cur must be carefully set when we succeed in processing * this mrec. */ static int hammer_ioc_mirror_write_rec(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec, struct hammer_ioc_mirror_rw *mirror, uint32_t localization, char *uptr) { int error; if (mrec->leaf.data_len < 0 || mrec->leaf.data_len > HAMMER_XBUFSIZE || mrec->leaf.data_len + sizeof(*mrec) > mrec->head.rec_size) { return(EINVAL); } /* * Re-localize for target. relocalization of data is handled * by hammer_create_at_cursor(). */ mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK; mrec->leaf.base.localization |= localization; /* * Delete records through until we reach (non-inclusively) the * target record. */ cursor->key_end = mrec->leaf.base; cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE; cursor->flags |= HAMMER_CURSOR_BACKEND; error = hammer_mirror_delete_to(cursor, mirror); /* * Certain records are not part of the mirroring operation */ if (error == 0 && hammer_mirror_nomirror(&mrec->leaf.base)) return(0); /* * Locate the record. * * If the record exists only the delete_tid may be updated. * * If the record does not exist we can create it only if the * create_tid is not too old. If the create_tid is too old * it may have already been destroyed on the slave from pruning. * * Note that mirror operations are effectively as-of operations * and delete_tid can be 0 for mirroring purposes even if it is * not actually 0 at the originator. * * These functions can return EDEADLK */ if (error == 0) { cursor->key_beg = mrec->leaf.base; cursor->flags |= HAMMER_CURSOR_BACKEND; cursor->flags &= ~HAMMER_CURSOR_INSERT; error = hammer_btree_lookup(cursor); } if (error == 0 && hammer_mirror_check(cursor, mrec)) { error = hammer_mirror_update(cursor, mrec); } else if (error == ENOENT) { if (mrec->leaf.base.create_tid >= mirror->tid_beg) { error = hammer_create_at_cursor( cursor, &mrec->leaf, uptr, HAMMER_CREATE_MODE_UMIRROR); } else { error = 0; } } if (error == 0 || error == EALREADY) mirror->key_cur = mrec->leaf.base; return(error); } /* * This works like write_rec but no write or update is necessary, * and no data payload is included so we couldn't do a write even * if we wanted to. * * We must still iterate for deletions, and we can validate the * record header which is a good way to test for corrupted mirror * targets XXX. * * mirror->key_cur must be carefully set when we succeed in processing * this mrec. */ static int hammer_ioc_mirror_write_pass(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec, struct hammer_ioc_mirror_rw *mirror, uint32_t localization) { int error; /* * Re-localize for target. Relocalization of data is handled * by hammer_create_at_cursor(). */ mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK; mrec->leaf.base.localization |= localization; /* * Delete records through until we reach (non-inclusively) the * target record. */ cursor->key_end = mrec->leaf.base; cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE; cursor->flags |= HAMMER_CURSOR_BACKEND; error = hammer_mirror_delete_to(cursor, mirror); /* * Certain records are not part of the mirroring operation */ if (hammer_mirror_nomirror(&mrec->leaf.base)) return(0); /* * Locate the record and get past it by setting ATEDISK. Perform * any necessary deletions. We have no data payload and cannot * create a new record. */ if (error == 0) { mirror->key_cur = mrec->leaf.base; cursor->key_beg = mrec->leaf.base; cursor->flags |= HAMMER_CURSOR_BACKEND; cursor->flags &= ~HAMMER_CURSOR_INSERT; error = hammer_btree_lookup(cursor); if (error == 0) { if (hammer_mirror_check(cursor, mrec)) error = hammer_mirror_update(cursor, mrec); cursor->flags |= HAMMER_CURSOR_ATEDISK; } else { cursor->flags &= ~HAMMER_CURSOR_ATEDISK; } if (error == ENOENT) error = 0; } return(error); } /* * As part of the mirror write we iterate across swaths of records * on the target which no longer exist on the source, and mark them * deleted. * * The caller has indexed the cursor and set up key_end. We iterate * through to key_end. * * There is an edge case where the master has deleted a record whos * create_tid exactly matches our end_tid. We cannot delete this * record on the slave yet because we cannot assign delete_tid == create_tid. * The deletion should be picked up on the next sequence since in order * to have been deleted on the master a transaction must have occured with * a TID greater then the create_tid of the record. * * To support incremental re-mirroring, just for robustness, we do not * touch any records created beyond (or equal to) mirror->tid_end. */ static int hammer_mirror_delete_to(hammer_cursor_t cursor, struct hammer_ioc_mirror_rw *mirror) { hammer_btree_leaf_elm_t elm; int error; error = hammer_btree_iterate(cursor); while (error == 0) { elm = &cursor->node->ondisk->elms[cursor->index].leaf; KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD); cursor->flags |= HAMMER_CURSOR_ATEDISK; /* * Certain records are not part of the mirroring operation */ if (hammer_mirror_nomirror(&elm->base)) { error = hammer_btree_iterate(cursor); continue; } /* * Note: Must still delete records with create_tid < tid_beg, * as record may have been pruned-away on source. */ if (elm->base.delete_tid == 0 && elm->base.create_tid < mirror->tid_end) { error = hammer_delete_at_cursor(cursor, HAMMER_DELETE_ADJUST, mirror->tid_end, time_second, 1, NULL); } if (error == 0) error = hammer_btree_iterate(cursor); } if (error == ENOENT) error = 0; return(error); } /* * Check whether an update is needed in the case where a match already * exists on the target. The only type of update allowed in this case * is an update of the delete_tid. * * Return non-zero if the update should proceed. */ static int hammer_mirror_check(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec) { hammer_btree_leaf_elm_t leaf = cursor->leaf; if (leaf->base.delete_tid != mrec->leaf.base.delete_tid) { if (mrec->leaf.base.delete_tid != 0) return(1); } return(0); } /* * Filter out records which are never mirrored, such as configuration space * records (for hammer cleanup). * * NOTE: We currently allow HAMMER_RECTYPE_SNAPSHOT records to be mirrored. */ static int hammer_mirror_nomirror(hammer_base_elm_t base) { /* * Certain types of records are never updated when mirroring. * Slaves have their own configuration space. */ if (base->rec_type == HAMMER_RECTYPE_CONFIG) return(1); return(0); } /* * Update a record in-place. Only the delete_tid can change, and * only from zero to non-zero. */ static int hammer_mirror_update(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec) { int error; /* * This case shouldn't occur. */ if (mrec->leaf.base.delete_tid == 0) return(0); /* * Mark the record deleted on the mirror target. */ error = hammer_delete_at_cursor(cursor, HAMMER_DELETE_ADJUST, mrec->leaf.base.delete_tid, mrec->leaf.delete_ts, 1, NULL); cursor->flags |= HAMMER_CURSOR_ATEDISK; return(error); } |