2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * HAMMER mirroring ioctls - serialize and deserialize modifications made
41 static int hammer_mirror_check(hammer_cursor_t cursor,
42 struct hammer_ioc_mrecord_rec *mrec);
43 static int hammer_mirror_update(hammer_cursor_t cursor,
44 struct hammer_ioc_mrecord_rec *mrec);
45 static int hammer_ioc_mirror_write_rec(hammer_cursor_t cursor,
46 struct hammer_ioc_mrecord_rec *mrec,
47 struct hammer_ioc_mirror_rw *mirror,
48 u_int32_t localization,
50 static int hammer_ioc_mirror_write_pass(hammer_cursor_t cursor,
51 struct hammer_ioc_mrecord_rec *mrec,
52 struct hammer_ioc_mirror_rw *mirror,
53 u_int32_t localization);
54 static int hammer_ioc_mirror_write_skip(hammer_cursor_t cursor,
55 struct hammer_ioc_mrecord_skip *mrec,
56 struct hammer_ioc_mirror_rw *mirror,
57 u_int32_t localization);
58 static int hammer_mirror_delete_to(hammer_cursor_t cursor,
59 struct hammer_ioc_mirror_rw *mirror);
60 static int hammer_mirror_nomirror(struct hammer_base_elm *base);
63 * All B-Tree records within the specified key range which also conform
64 * to the transaction id range are returned. Mirroring code keeps track
65 * of the last transaction id fully scanned and can efficiently pick up
66 * where it left off if interrupted.
68 * The PFS is identified in the mirror structure. The passed ip is just
69 * some directory in the overall HAMMER filesystem and has nothing to
73 hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip,
74 struct hammer_ioc_mirror_rw *mirror)
76 struct hammer_cmirror cmirror;
77 struct hammer_cursor cursor;
78 union hammer_ioc_mrecord_any mrec;
79 hammer_btree_leaf_elm_t elm;
80 const int crc_start = HAMMER_MREC_CRCOFF;
87 u_int32_t localization;
90 localization = (u_int32_t)mirror->pfs_id << 16;
92 if ((mirror->key_beg.localization | mirror->key_end.localization) &
93 HAMMER_LOCALIZE_PSEUDOFS_MASK) {
96 if (hammer_btree_cmp(&mirror->key_beg, &mirror->key_end) > 0)
99 mirror->key_cur = mirror->key_beg;
100 mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
101 mirror->key_cur.localization += localization;
102 bzero(&mrec, sizeof(mrec));
103 bzero(&cmirror, sizeof(cmirror));
106 * Make CRC errors non-fatal (at least on data), causing an EDOM
107 * error instead of EIO.
109 trans->flags |= HAMMER_TRANSF_CRCDOM;
112 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
114 hammer_done_cursor(&cursor);
117 cursor.key_beg = mirror->key_cur;
118 cursor.key_end = mirror->key_end;
119 cursor.key_end.localization &= HAMMER_LOCALIZE_MASK;
120 cursor.key_end.localization += localization;
122 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
123 cursor.flags |= HAMMER_CURSOR_BACKEND;
126 * This flag filters the search to only return elements whos create
127 * or delete TID is >= mirror_tid. The B-Tree uses the mirror_tid
128 * field stored with internal and leaf nodes to shortcut the scan.
130 cursor.flags |= HAMMER_CURSOR_MIRROR_FILTERED;
131 cursor.cmirror = &cmirror;
132 cmirror.mirror_tid = mirror->tid_beg;
134 error = hammer_btree_first(&cursor);
137 * Yield to more important tasks
140 error = hammer_signal_check(trans->hmp);
146 * An internal node can be returned in mirror-filtered
147 * mode and indicates that the scan is returning a skip
148 * range in the cursor->cmirror structure.
150 uptr = (char *)mirror->ubuf + mirror->count;
151 if (cursor.node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
155 mirror->key_cur = cmirror.skip_beg;
156 bytes = sizeof(mrec.skip);
157 if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) >
165 mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
166 mrec.head.type = HAMMER_MREC_TYPE_SKIP;
167 mrec.head.rec_size = bytes;
168 mrec.skip.skip_beg = cmirror.skip_beg;
169 mrec.skip.skip_end = cmirror.skip_end;
170 mrec.head.rec_crc = crc32(&mrec.head.rec_size,
172 error = copyout(&mrec, uptr, bytes);
178 * Leaf node. In full-history mode we could filter out
179 * elements modified outside the user-requested TID range.
181 * However, such elements must be returned so the writer
182 * can compare them against the target to determine what
183 * needs to be deleted on the target, particular for
184 * no-history mirrors.
186 KKASSERT(cursor.node->ondisk->type == HAMMER_BTREE_TYPE_LEAF);
187 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
188 mirror->key_cur = elm->base;
191 * If the record was created after our end point we just
194 if (elm->base.create_tid > mirror->tid_end) {
202 * Determine if we should generate a PASS or a REC. PASS
203 * records are records without any data payload. Such
204 * records will be generated if the target is already expected
205 * to have the record, allowing it to delete the gaps.
207 * A PASS record is also used to perform deletions on the
210 * Such deletions are needed if the master or files on the
211 * master are no-history, or if the slave is so far behind
212 * the master has already been pruned.
214 if (elm->base.create_tid < mirror->tid_beg) {
215 bytes = sizeof(mrec.rec);
216 if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) >
224 mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
225 mrec.head.type = HAMMER_MREC_TYPE_PASS;
226 mrec.head.rec_size = bytes;
227 mrec.rec.leaf = *elm;
228 mrec.head.rec_crc = crc32(&mrec.head.rec_size,
230 error = copyout(&mrec, uptr, bytes);
236 * The core code exports the data to userland.
238 * CRC errors on data are reported but passed through,
239 * but the data must be washed by the user program.
241 * If userland just wants the btree records it can
242 * request that bulk data not be returned. This is
243 * use during mirror-stream histogram generation.
246 data_len = (elm->data_offset) ? elm->data_len : 0;
248 (mirror->head.flags & HAMMER_IOC_MIRROR_NODATA)) {
250 mrec_flags |= HAMMER_MRECF_NODATA;
253 error = hammer_btree_extract(&cursor,
254 HAMMER_CURSOR_GET_DATA);
258 mrec_flags |= HAMMER_MRECF_CRC_ERROR |
259 HAMMER_MRECF_DATA_CRC_BAD;
263 bytes = sizeof(mrec.rec) + data_len;
264 if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) > mirror->size)
268 * Construct the record for userland and copyout.
270 * The user is asking for a snapshot, if the record was
271 * deleted beyond the user-requested ending tid, the record
272 * is not considered deleted from the point of view of
273 * userland and delete_tid is cleared.
275 mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
276 mrec.head.type = HAMMER_MREC_TYPE_REC | mrec_flags;
277 mrec.head.rec_size = bytes;
278 mrec.rec.leaf = *elm;
280 if (elm->base.delete_tid > mirror->tid_end)
281 mrec.rec.leaf.base.delete_tid = 0;
282 rec_crc = crc32(&mrec.head.rec_size,
283 sizeof(mrec.rec) - crc_start);
285 rec_crc = crc32_ext(cursor.data, data_len, rec_crc);
286 mrec.head.rec_crc = rec_crc;
287 error = copyout(&mrec, uptr, sizeof(mrec.rec));
288 if (data_len && error == 0) {
289 error = copyout(cursor.data, uptr + sizeof(mrec.rec),
295 * eatdisk controls whether we skip the current cursor
296 * position on the next scan or not. If doing a SKIP
297 * the cursor is already positioned properly for the next
298 * scan and eatdisk will be 0.
302 mirror->count += HAMMER_HEAD_DOALIGN(bytes);
304 cursor.flags |= HAMMER_CURSOR_ATEDISK;
306 cursor.flags &= ~HAMMER_CURSOR_ATEDISK;
307 error = hammer_btree_iterate(&cursor);
310 if (error == ENOENT) {
311 mirror->key_cur = mirror->key_end;
314 hammer_done_cursor(&cursor);
315 if (error == EDEADLK)
317 if (error == EINTR) {
318 mirror->head.flags |= HAMMER_IOC_HEAD_INTR;
322 mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
327 * Copy records from userland to the target mirror.
329 * The PFS is identified in the mirror structure. The passed ip is just
330 * some directory in the overall HAMMER filesystem and has nothing to
331 * do with the PFS. In fact, there might not even be a root directory for
335 hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
336 struct hammer_ioc_mirror_rw *mirror)
338 union hammer_ioc_mrecord_any mrec;
339 struct hammer_cursor cursor;
340 u_int32_t localization;
341 int checkspace_count = 0;
347 localization = (u_int32_t)mirror->pfs_id << 16;
348 seq = trans->hmp->flusher.done;
351 * Validate the mirror structure and relocalize the tracking keys.
353 if (mirror->size < 0 || mirror->size > 0x70000000)
355 mirror->key_beg.localization &= HAMMER_LOCALIZE_MASK;
356 mirror->key_beg.localization += localization;
357 mirror->key_end.localization &= HAMMER_LOCALIZE_MASK;
358 mirror->key_end.localization += localization;
359 mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
360 mirror->key_cur.localization += localization;
363 * Set up our tracking cursor for the loop. The tracking cursor
364 * is used to delete records that are no longer present on the
365 * master. The last handled record at key_cur must be skipped.
367 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
369 cursor.key_beg = mirror->key_cur;
370 cursor.key_end = mirror->key_end;
371 cursor.flags |= HAMMER_CURSOR_BACKEND;
372 error = hammer_btree_first(&cursor);
374 cursor.flags |= HAMMER_CURSOR_ATEDISK;
379 * Loop until our input buffer has been exhausted.
382 mirror->count + sizeof(mrec.head) <= mirror->size) {
385 * Don't blow out the buffer cache. Leave room for frontend
388 * WARNING: See warnings in hammer_unlock_cursor() function.
390 while (hammer_flusher_meta_halflimit(trans->hmp) ||
391 hammer_flusher_undo_exhausted(trans, 2)) {
392 hammer_unlock_cursor(&cursor);
393 hammer_flusher_wait(trans->hmp, seq);
394 hammer_lock_cursor(&cursor);
395 seq = hammer_flusher_async_one(trans->hmp);
399 * If there is insufficient free space it may be due to
400 * reserved big-blocks, which flushing might fix.
402 if (hammer_checkspace(trans->hmp, HAMMER_CHKSPC_MIRROR)) {
403 if (++checkspace_count == 10) {
407 hammer_unlock_cursor(&cursor);
408 hammer_flusher_wait(trans->hmp, seq);
409 hammer_lock_cursor(&cursor);
410 seq = hammer_flusher_async(trans->hmp, NULL);
415 * Acquire and validate header
417 if ((bytes = mirror->size - mirror->count) > sizeof(mrec))
418 bytes = sizeof(mrec);
419 uptr = (char *)mirror->ubuf + mirror->count;
420 error = copyin(uptr, &mrec, bytes);
423 if (mrec.head.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
427 if (mrec.head.rec_size < sizeof(mrec.head) ||
428 mrec.head.rec_size > sizeof(mrec) + HAMMER_XBUFSIZE ||
429 mirror->count + mrec.head.rec_size > mirror->size) {
434 switch(mrec.head.type & HAMMER_MRECF_TYPE_MASK) {
435 case HAMMER_MREC_TYPE_SKIP:
436 if (mrec.head.rec_size != sizeof(mrec.skip))
439 error = hammer_ioc_mirror_write_skip(&cursor, &mrec.skip, mirror, localization);
441 case HAMMER_MREC_TYPE_REC:
442 if (mrec.head.rec_size < sizeof(mrec.rec))
445 error = hammer_ioc_mirror_write_rec(&cursor, &mrec.rec, mirror, localization, uptr + sizeof(mrec.rec));
447 case HAMMER_MREC_TYPE_REC_NODATA:
448 case HAMMER_MREC_TYPE_REC_BADCRC:
450 * Records with bad data payloads are ignored XXX.
451 * Records with no data payload have to be skipped
452 * (they shouldn't have been written in the first
455 if (mrec.head.rec_size < sizeof(mrec.rec))
458 case HAMMER_MREC_TYPE_PASS:
459 if (mrec.head.rec_size != sizeof(mrec.rec))
462 error = hammer_ioc_mirror_write_pass(&cursor, &mrec.rec, mirror, localization);
470 * Retry the current record on deadlock, otherwise setup
473 if (error == EDEADLK) {
474 while (error == EDEADLK) {
475 hammer_sync_lock_sh(trans);
476 hammer_recover_cursor(&cursor);
477 error = hammer_cursor_upgrade(&cursor);
478 hammer_sync_unlock(trans);
481 if (error == EALREADY)
485 HAMMER_HEAD_DOALIGN(mrec.head.rec_size);
489 hammer_done_cursor(&cursor);
495 mirror->head.flags |= HAMMER_IOC_HEAD_ERROR;
496 mirror->head.error = error;
500 * ioctls don't update the RW data structure if an error is returned,
507 * Handle skip records.
509 * We must iterate from the last resolved record position at mirror->key_cur
510 * to skip_beg non-inclusive and delete any records encountered.
512 * mirror->key_cur must be carefully set when we succeed in processing
516 hammer_ioc_mirror_write_skip(hammer_cursor_t cursor,
517 struct hammer_ioc_mrecord_skip *mrec,
518 struct hammer_ioc_mirror_rw *mirror,
519 u_int32_t localization)
524 * Relocalize the skip range
526 mrec->skip_beg.localization &= HAMMER_LOCALIZE_MASK;
527 mrec->skip_beg.localization += localization;
528 mrec->skip_end.localization &= HAMMER_LOCALIZE_MASK;
529 mrec->skip_end.localization += localization;
532 * Iterate from current position to skip_beg, deleting any records
533 * we encounter. The record at skip_beg is not included (it is
536 cursor->key_end = mrec->skip_beg;
537 cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
538 cursor->flags |= HAMMER_CURSOR_BACKEND;
539 error = hammer_mirror_delete_to(cursor, mirror);
542 * Now skip past the skip (which is the whole point point of
543 * having a skip record). The sender has not sent us any records
544 * for the skip area so we wouldn't know what to keep and what
547 * Clear ATEDISK because skip_end is non-inclusive, so we can't
548 * count an exact match if we happened to get one.
551 mirror->key_cur = mrec->skip_end;
552 cursor->key_beg = mrec->skip_end;
553 error = hammer_btree_lookup(cursor);
554 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
562 * Handle B-Tree records.
564 * We must iterate to mrec->base.key (non-inclusively), and then process
565 * the record. We are allowed to write a new record or delete an existing
566 * record, but cannot replace an existing record.
568 * mirror->key_cur must be carefully set when we succeed in processing
572 hammer_ioc_mirror_write_rec(hammer_cursor_t cursor,
573 struct hammer_ioc_mrecord_rec *mrec,
574 struct hammer_ioc_mirror_rw *mirror,
575 u_int32_t localization,
580 if (mrec->leaf.data_len < 0 ||
581 mrec->leaf.data_len > HAMMER_XBUFSIZE ||
582 mrec->leaf.data_len + sizeof(*mrec) > mrec->head.rec_size) {
587 * Re-localize for target. relocalization of data is handled
588 * by hammer_mirror_write().
590 mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK;
591 mrec->leaf.base.localization += localization;
594 * Delete records through until we reach (non-inclusively) the
597 cursor->key_end = mrec->leaf.base;
598 cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
599 cursor->flags |= HAMMER_CURSOR_BACKEND;
600 error = hammer_mirror_delete_to(cursor, mirror);
603 * Certain records are not part of the mirroring operation
605 if (error == 0 && hammer_mirror_nomirror(&mrec->leaf.base))
611 * If the record exists only the delete_tid may be updated.
613 * If the record does not exist we can create it only if the
614 * create_tid is not too old. If the create_tid is too old
615 * it may have already been destroyed on the slave from pruning.
617 * Note that mirror operations are effectively as-of operations
618 * and delete_tid can be 0 for mirroring purposes even if it is
619 * not actually 0 at the originator.
621 * These functions can return EDEADLK
624 cursor->key_beg = mrec->leaf.base;
625 cursor->flags |= HAMMER_CURSOR_BACKEND;
626 cursor->flags &= ~HAMMER_CURSOR_INSERT;
627 error = hammer_btree_lookup(cursor);
630 if (error == 0 && hammer_mirror_check(cursor, mrec)) {
631 error = hammer_mirror_update(cursor, mrec);
632 } else if (error == ENOENT) {
633 if (mrec->leaf.base.create_tid >= mirror->tid_beg) {
634 error = hammer_create_at_cursor(
636 uptr, HAMMER_CREATE_MODE_UMIRROR);
641 if (error == 0 || error == EALREADY)
642 mirror->key_cur = mrec->leaf.base;
647 * This works like write_rec but no write or update is necessary,
648 * and no data payload is included so we couldn't do a write even
651 * We must still iterate for deletions, and we can validate the
652 * record header which is a good way to test for corrupted mirror
655 * mirror->key_cur must be carefully set when we succeed in processing
660 hammer_ioc_mirror_write_pass(hammer_cursor_t cursor,
661 struct hammer_ioc_mrecord_rec *mrec,
662 struct hammer_ioc_mirror_rw *mirror,
663 u_int32_t localization)
668 * Re-localize for target. Relocalization of data is handled
669 * by hammer_mirror_write().
671 mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK;
672 mrec->leaf.base.localization += localization;
675 * Delete records through until we reach (non-inclusively) the
678 cursor->key_end = mrec->leaf.base;
679 cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
680 cursor->flags |= HAMMER_CURSOR_BACKEND;
681 error = hammer_mirror_delete_to(cursor, mirror);
684 * Certain records are not part of the mirroring operation
686 if (hammer_mirror_nomirror(&mrec->leaf.base))
690 * Locate the record and get past it by setting ATEDISK. Perform
691 * any necessary deletions. We have no data payload and cannot
692 * create a new record.
695 mirror->key_cur = mrec->leaf.base;
696 cursor->key_beg = mrec->leaf.base;
697 cursor->flags |= HAMMER_CURSOR_BACKEND;
698 cursor->flags &= ~HAMMER_CURSOR_INSERT;
699 error = hammer_btree_lookup(cursor);
701 if (hammer_mirror_check(cursor, mrec))
702 error = hammer_mirror_update(cursor, mrec);
703 cursor->flags |= HAMMER_CURSOR_ATEDISK;
705 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
714 * As part of the mirror write we iterate across swaths of records
715 * on the target which no longer exist on the source, and mark them
718 * The caller has indexed the cursor and set up key_end. We iterate
719 * through to key_end.
721 * There is an edge case where the master has deleted a record whos
722 * create_tid exactly matches our end_tid. We cannot delete this
723 * record on the slave yet because we cannot assign delete_tid == create_tid.
724 * The deletion should be picked up on the next sequence since in order
725 * to have been deleted on the master a transaction must have occured with
726 * a TID greater then the create_tid of the record.
728 * To support incremental re-mirroring, just for robustness, we do not
729 * touch any records created beyond (or equal to) mirror->tid_end.
733 hammer_mirror_delete_to(hammer_cursor_t cursor,
734 struct hammer_ioc_mirror_rw *mirror)
736 hammer_btree_leaf_elm_t elm;
739 error = hammer_btree_iterate(cursor);
741 elm = &cursor->node->ondisk->elms[cursor->index].leaf;
742 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
743 cursor->flags |= HAMMER_CURSOR_ATEDISK;
746 * Certain records are not part of the mirroring operation
748 if (hammer_mirror_nomirror(&elm->base)) {
749 error = hammer_btree_iterate(cursor);
754 * Note: Must still delete records with create_tid < tid_beg,
755 * as record may have been pruned-away on source.
757 if (elm->base.delete_tid == 0 &&
758 elm->base.create_tid < mirror->tid_end) {
759 error = hammer_delete_at_cursor(cursor,
760 HAMMER_DELETE_ADJUST,
766 error = hammer_btree_iterate(cursor);
774 * Check whether an update is needed in the case where a match already
775 * exists on the target. The only type of update allowed in this case
776 * is an update of the delete_tid.
778 * Return non-zero if the update should proceed.
782 hammer_mirror_check(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec)
784 hammer_btree_leaf_elm_t leaf = cursor->leaf;
786 if (leaf->base.delete_tid != mrec->leaf.base.delete_tid) {
787 if (mrec->leaf.base.delete_tid != 0)
794 * Filter out records which are never mirrored, such as configuration space
795 * records (for hammer cleanup).
797 * NOTE: We currently allow HAMMER_RECTYPE_SNAPSHOT records to be mirrored.
801 hammer_mirror_nomirror(struct hammer_base_elm *base)
804 * Certain types of records are never updated when mirroring.
805 * Slaves have their own configuration space.
807 if (base->rec_type == HAMMER_RECTYPE_CONFIG)
814 * Update a record in-place. Only the delete_tid can change, and
815 * only from zero to non-zero.
819 hammer_mirror_update(hammer_cursor_t cursor,
820 struct hammer_ioc_mrecord_rec *mrec)
825 * This case shouldn't occur.
827 if (mrec->leaf.base.delete_tid == 0)
831 * Mark the record deleted on the mirror target.
833 error = hammer_delete_at_cursor(cursor, HAMMER_DELETE_ADJUST,
834 mrec->leaf.base.delete_tid,
835 mrec->leaf.delete_ts,
837 cursor->flags |= HAMMER_CURSOR_ATEDISK;
843 * MOVED TO HAMMER_OBJECT.C: hammer_create_at_cursor()
846 static int hammer_mirror_localize_data(hammer_data_ondisk_t data,
847 hammer_btree_leaf_elm_t leaf);
850 * Write out a new record.
854 hammer_mirror_write(hammer_cursor_t cursor,
855 struct hammer_ioc_mrecord_rec *mrec,
858 hammer_transaction_t trans;
859 hammer_buffer_t data_buffer;
860 hammer_off_t ndata_offset;
861 hammer_tid_t high_tid;
866 trans = cursor->trans;
870 * Get the sync lock so the whole mess is atomic
872 hammer_sync_lock_sh(trans);
875 * Allocate and adjust data
877 if (mrec->leaf.data_len && mrec->leaf.data_offset) {
878 ndata = hammer_alloc_data(trans, mrec->leaf.data_len,
879 mrec->leaf.base.rec_type,
880 &ndata_offset, &data_buffer,
884 mrec->leaf.data_offset = ndata_offset;
885 hammer_modify_buffer_noundo(trans, data_buffer);
886 error = copyin(udata, ndata, mrec->leaf.data_len);
888 if (hammer_crc_test_leaf(ndata, &mrec->leaf) == 0) {
889 kprintf("data crc mismatch on pipe\n");
892 error = hammer_mirror_localize_data(
896 hammer_modify_buffer_done(data_buffer);
898 mrec->leaf.data_offset = 0;
906 * Do the insertion. This can fail with a EDEADLK or EALREADY
908 cursor->flags |= HAMMER_CURSOR_INSERT;
909 error = hammer_btree_lookup(cursor);
910 if (error != ENOENT) {
916 error = hammer_btree_insert(cursor, &mrec->leaf, &doprop);
919 * Cursor is left on the current element, we want to skip it now.
921 cursor->flags |= HAMMER_CURSOR_ATEDISK;
922 cursor->flags &= ~HAMMER_CURSOR_INSERT;
925 * Track a count of active inodes.
928 mrec->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
929 mrec->leaf.base.delete_tid == 0) {
930 hammer_modify_volume_field(trans,
933 ++trans->hmp->rootvol->ondisk->vol0_stat_inodes;
934 hammer_modify_volume_done(trans->rootvol);
938 * vol0_next_tid must track the highest TID stored in the filesystem.
939 * We do not need to generate undo for this update.
941 high_tid = mrec->leaf.base.create_tid;
942 if (high_tid < mrec->leaf.base.delete_tid)
943 high_tid = mrec->leaf.base.delete_tid;
944 if (trans->rootvol->ondisk->vol0_next_tid < high_tid) {
945 hammer_modify_volume_noundo(trans, trans->rootvol);
946 trans->rootvol->ondisk->vol0_next_tid = high_tid;
947 hammer_modify_volume_done(trans->rootvol);
951 * WARNING! cursor's leaf pointer may have changed after
952 * do_propagation returns.
954 if (error == 0 && doprop)
955 hammer_btree_do_propagation(cursor, NULL, &mrec->leaf);
961 if (error && mrec->leaf.data_offset) {
962 hammer_blockmap_free(cursor->trans,
963 mrec->leaf.data_offset,
964 mrec->leaf.data_len);
966 hammer_sync_unlock(trans);
968 hammer_rel_buffer(data_buffer, 0);
973 * Localize the data payload. Directory entries may need their
974 * localization adjusted.
978 hammer_mirror_localize_data(hammer_data_ondisk_t data,
979 hammer_btree_leaf_elm_t leaf)
981 u_int32_t localization;
983 if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
984 localization = leaf->base.localization &
985 HAMMER_LOCALIZE_PSEUDOFS_MASK;
986 if (data->entry.localization != localization) {
987 data->entry.localization = localization;
988 hammer_crc_set_leaf(data, leaf);