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);
237 * The core code exports the data to userland.
239 * CRC errors on data are reported but passed through,
240 * but the data must be washed by the user program.
242 * If userland just wants the btree records it can
243 * request that bulk data not be returned. This is
244 * use during mirror-stream histogram generation.
247 data_len = (elm->data_offset) ? elm->data_len : 0;
249 (mirror->head.flags & HAMMER_IOC_MIRROR_NODATA)) {
251 mrec_flags |= HAMMER_MRECF_NODATA;
254 error = hammer_btree_extract(&cursor,
255 HAMMER_CURSOR_GET_DATA);
259 mrec_flags |= HAMMER_MRECF_CRC_ERROR |
260 HAMMER_MRECF_DATA_CRC_BAD;
264 bytes = sizeof(mrec.rec) + data_len;
265 if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) > mirror->size)
269 * Construct the record for userland and copyout.
271 * The user is asking for a snapshot, if the record was
272 * deleted beyond the user-requested ending tid, the record
273 * is not considered deleted from the point of view of
274 * userland and delete_tid is cleared.
276 mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
277 mrec.head.type = HAMMER_MREC_TYPE_REC | mrec_flags;
278 mrec.head.rec_size = bytes;
279 mrec.rec.leaf = *elm;
281 if (elm->base.delete_tid > mirror->tid_end)
282 mrec.rec.leaf.base.delete_tid = 0;
283 rec_crc = crc32(&mrec.head.rec_size,
284 sizeof(mrec.rec) - crc_start);
286 rec_crc = crc32_ext(cursor.data, data_len, rec_crc);
287 mrec.head.rec_crc = rec_crc;
288 error = copyout(&mrec, uptr, sizeof(mrec.rec));
289 if (data_len && error == 0) {
290 error = copyout(cursor.data, uptr + sizeof(mrec.rec),
296 * eatdisk controls whether we skip the current cursor
297 * position on the next scan or not. If doing a SKIP
298 * the cursor is already positioned properly for the next
299 * scan and eatdisk will be 0.
303 mirror->count += HAMMER_HEAD_DOALIGN(bytes);
305 cursor.flags |= HAMMER_CURSOR_ATEDISK;
307 cursor.flags &= ~HAMMER_CURSOR_ATEDISK;
308 error = hammer_btree_iterate(&cursor);
311 if (error == ENOENT) {
312 mirror->key_cur = mirror->key_end;
315 hammer_done_cursor(&cursor);
316 if (error == EDEADLK)
318 if (error == EINTR) {
319 mirror->head.flags |= HAMMER_IOC_HEAD_INTR;
323 mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
328 * Copy records from userland to the target mirror.
330 * The PFS is identified in the mirror structure. The passed ip is just
331 * some directory in the overall HAMMER filesystem and has nothing to
332 * do with the PFS. In fact, there might not even be a root directory for
336 hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
337 struct hammer_ioc_mirror_rw *mirror)
339 union hammer_ioc_mrecord_any mrec;
340 struct hammer_cursor cursor;
341 u_int32_t localization;
342 int checkspace_count = 0;
348 localization = (u_int32_t)mirror->pfs_id << 16;
349 seq = trans->hmp->flusher.done;
352 * Validate the mirror structure and relocalize the tracking keys.
354 if (mirror->size < 0 || mirror->size > 0x70000000)
356 mirror->key_beg.localization &= HAMMER_LOCALIZE_MASK;
357 mirror->key_beg.localization += localization;
358 mirror->key_end.localization &= HAMMER_LOCALIZE_MASK;
359 mirror->key_end.localization += localization;
360 mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
361 mirror->key_cur.localization += localization;
364 * Set up our tracking cursor for the loop. The tracking cursor
365 * is used to delete records that are no longer present on the
366 * master. The last handled record at key_cur must be skipped.
368 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
370 cursor.key_beg = mirror->key_cur;
371 cursor.key_end = mirror->key_end;
372 cursor.flags |= HAMMER_CURSOR_BACKEND;
373 error = hammer_btree_first(&cursor);
375 cursor.flags |= HAMMER_CURSOR_ATEDISK;
380 * Loop until our input buffer has been exhausted.
383 mirror->count + sizeof(mrec.head) <= mirror->size) {
386 * Don't blow out the buffer cache. Leave room for frontend
389 * WARNING: See warnings in hammer_unlock_cursor() function.
391 while (hammer_flusher_meta_halflimit(trans->hmp) ||
392 hammer_flusher_undo_exhausted(trans, 2)) {
393 hammer_unlock_cursor(&cursor);
394 hammer_flusher_wait(trans->hmp, seq);
395 hammer_lock_cursor(&cursor);
396 seq = hammer_flusher_async_one(trans->hmp);
400 * If there is insufficient free space it may be due to
401 * reserved bigblocks, which flushing might fix.
403 if (hammer_checkspace(trans->hmp, HAMMER_CHKSPC_MIRROR)) {
404 if (++checkspace_count == 10) {
408 hammer_unlock_cursor(&cursor);
409 hammer_flusher_wait(trans->hmp, seq);
410 hammer_lock_cursor(&cursor);
411 seq = hammer_flusher_async(trans->hmp, NULL);
416 * Acquire and validate header
418 if ((bytes = mirror->size - mirror->count) > sizeof(mrec))
419 bytes = sizeof(mrec);
420 uptr = (char *)mirror->ubuf + mirror->count;
421 error = copyin(uptr, &mrec, bytes);
424 if (mrec.head.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
428 if (mrec.head.rec_size < sizeof(mrec.head) ||
429 mrec.head.rec_size > sizeof(mrec) + HAMMER_XBUFSIZE ||
430 mirror->count + mrec.head.rec_size > mirror->size) {
435 switch(mrec.head.type & HAMMER_MRECF_TYPE_MASK) {
436 case HAMMER_MREC_TYPE_SKIP:
437 if (mrec.head.rec_size != sizeof(mrec.skip))
440 error = hammer_ioc_mirror_write_skip(&cursor, &mrec.skip, mirror, localization);
442 case HAMMER_MREC_TYPE_REC:
443 if (mrec.head.rec_size < sizeof(mrec.rec))
446 error = hammer_ioc_mirror_write_rec(&cursor, &mrec.rec, mirror, localization, uptr + sizeof(mrec.rec));
448 case HAMMER_MREC_TYPE_REC_NODATA:
449 case HAMMER_MREC_TYPE_REC_BADCRC:
451 * Records with bad data payloads are ignored XXX.
452 * Records with no data payload have to be skipped
453 * (they shouldn't have been written in the first
456 if (mrec.head.rec_size < sizeof(mrec.rec))
459 case HAMMER_MREC_TYPE_PASS:
460 if (mrec.head.rec_size != sizeof(mrec.rec))
463 error = hammer_ioc_mirror_write_pass(&cursor, &mrec.rec, mirror, localization);
471 * Retry the current record on deadlock, otherwise setup
474 if (error == EDEADLK) {
475 while (error == EDEADLK) {
476 hammer_sync_lock_sh(trans);
477 hammer_recover_cursor(&cursor);
478 error = hammer_cursor_upgrade(&cursor);
479 hammer_sync_unlock(trans);
482 if (error == EALREADY)
486 HAMMER_HEAD_DOALIGN(mrec.head.rec_size);
490 hammer_done_cursor(&cursor);
496 mirror->head.flags |= HAMMER_IOC_HEAD_ERROR;
497 mirror->head.error = error;
501 * ioctls don't update the RW data structure if an error is returned,
508 * Handle skip records.
510 * We must iterate from the last resolved record position at mirror->key_cur
511 * to skip_beg non-inclusive and delete any records encountered.
513 * mirror->key_cur must be carefully set when we succeed in processing
517 hammer_ioc_mirror_write_skip(hammer_cursor_t cursor,
518 struct hammer_ioc_mrecord_skip *mrec,
519 struct hammer_ioc_mirror_rw *mirror,
520 u_int32_t localization)
525 * Relocalize the skip range
527 mrec->skip_beg.localization &= HAMMER_LOCALIZE_MASK;
528 mrec->skip_beg.localization += localization;
529 mrec->skip_end.localization &= HAMMER_LOCALIZE_MASK;
530 mrec->skip_end.localization += localization;
533 * Iterate from current position to skip_beg, deleting any records
534 * we encounter. The record at skip_beg is not included (it is
537 cursor->key_end = mrec->skip_beg;
538 cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
539 cursor->flags |= HAMMER_CURSOR_BACKEND;
540 error = hammer_mirror_delete_to(cursor, mirror);
543 * Now skip past the skip (which is the whole point point of
544 * having a skip record). The sender has not sent us any records
545 * for the skip area so we wouldn't know what to keep and what
548 * Clear ATEDISK because skip_end is non-inclusive, so we can't
549 * count an exact match if we happened to get one.
552 mirror->key_cur = mrec->skip_end;
553 cursor->key_beg = mrec->skip_end;
554 error = hammer_btree_lookup(cursor);
555 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
563 * Handle B-Tree records.
565 * We must iterate to mrec->base.key (non-inclusively), and then process
566 * the record. We are allowed to write a new record or delete an existing
567 * record, but cannot replace an existing record.
569 * mirror->key_cur must be carefully set when we succeed in processing
573 hammer_ioc_mirror_write_rec(hammer_cursor_t cursor,
574 struct hammer_ioc_mrecord_rec *mrec,
575 struct hammer_ioc_mirror_rw *mirror,
576 u_int32_t localization,
581 if (mrec->leaf.data_len < 0 ||
582 mrec->leaf.data_len > HAMMER_XBUFSIZE ||
583 mrec->leaf.data_len + sizeof(*mrec) > mrec->head.rec_size) {
588 * Re-localize for target. relocalization of data is handled
589 * by hammer_mirror_write().
591 mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK;
592 mrec->leaf.base.localization += localization;
595 * Delete records through until we reach (non-inclusively) the
598 cursor->key_end = mrec->leaf.base;
599 cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
600 cursor->flags |= HAMMER_CURSOR_BACKEND;
601 error = hammer_mirror_delete_to(cursor, mirror);
604 * Certain records are not part of the mirroring operation
606 if (error == 0 && hammer_mirror_nomirror(&mrec->leaf.base))
612 * If the record exists only the delete_tid may be updated.
614 * If the record does not exist we can create it only if the
615 * create_tid is not too old. If the create_tid is too old
616 * it may have already been destroyed on the slave from pruning.
618 * Note that mirror operations are effectively as-of operations
619 * and delete_tid can be 0 for mirroring purposes even if it is
620 * not actually 0 at the originator.
622 * These functions can return EDEADLK
625 cursor->key_beg = mrec->leaf.base;
626 cursor->flags |= HAMMER_CURSOR_BACKEND;
627 cursor->flags &= ~HAMMER_CURSOR_INSERT;
628 error = hammer_btree_lookup(cursor);
631 if (error == 0 && hammer_mirror_check(cursor, mrec)) {
632 error = hammer_mirror_update(cursor, mrec);
633 } else if (error == ENOENT) {
634 if (mrec->leaf.base.create_tid >= mirror->tid_beg) {
635 error = hammer_create_at_cursor(
637 uptr, HAMMER_CREATE_MODE_UMIRROR);
642 if (error == 0 || error == EALREADY)
643 mirror->key_cur = mrec->leaf.base;
648 * This works like write_rec but no write or update is necessary,
649 * and no data payload is included so we couldn't do a write even
652 * We must still iterate for deletions, and we can validate the
653 * record header which is a good way to test for corrupted mirror
656 * mirror->key_cur must be carefully set when we succeed in processing
661 hammer_ioc_mirror_write_pass(hammer_cursor_t cursor,
662 struct hammer_ioc_mrecord_rec *mrec,
663 struct hammer_ioc_mirror_rw *mirror,
664 u_int32_t localization)
669 * Re-localize for target. Relocalization of data is handled
670 * by hammer_mirror_write().
672 mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK;
673 mrec->leaf.base.localization += localization;
676 * Delete records through until we reach (non-inclusively) the
679 cursor->key_end = mrec->leaf.base;
680 cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
681 cursor->flags |= HAMMER_CURSOR_BACKEND;
682 error = hammer_mirror_delete_to(cursor, mirror);
685 * Certain records are not part of the mirroring operation
687 if (hammer_mirror_nomirror(&mrec->leaf.base))
691 * Locate the record and get past it by setting ATEDISK. Perform
692 * any necessary deletions. We have no data payload and cannot
693 * create a new record.
696 mirror->key_cur = mrec->leaf.base;
697 cursor->key_beg = mrec->leaf.base;
698 cursor->flags |= HAMMER_CURSOR_BACKEND;
699 cursor->flags &= ~HAMMER_CURSOR_INSERT;
700 error = hammer_btree_lookup(cursor);
702 if (hammer_mirror_check(cursor, mrec))
703 error = hammer_mirror_update(cursor, mrec);
704 cursor->flags |= HAMMER_CURSOR_ATEDISK;
706 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
715 * As part of the mirror write we iterate across swaths of records
716 * on the target which no longer exist on the source, and mark them
719 * The caller has indexed the cursor and set up key_end. We iterate
720 * through to key_end.
722 * There is an edge case where the master has deleted a record whos
723 * create_tid exactly matches our end_tid. We cannot delete this
724 * record on the slave yet because we cannot assign delete_tid == create_tid.
725 * The deletion should be picked up on the next sequence since in order
726 * to have been deleted on the master a transaction must have occured with
727 * a TID greater then the create_tid of the record.
729 * To support incremental re-mirroring, just for robustness, we do not
730 * touch any records created beyond (or equal to) mirror->tid_end.
734 hammer_mirror_delete_to(hammer_cursor_t cursor,
735 struct hammer_ioc_mirror_rw *mirror)
737 hammer_btree_leaf_elm_t elm;
740 error = hammer_btree_iterate(cursor);
742 elm = &cursor->node->ondisk->elms[cursor->index].leaf;
743 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
744 cursor->flags |= HAMMER_CURSOR_ATEDISK;
747 * Certain records are not part of the mirroring operation
749 if (hammer_mirror_nomirror(&elm->base)) {
750 error = hammer_btree_iterate(cursor);
755 * Note: Must still delete records with create_tid < tid_beg,
756 * as record may have been pruned-away on source.
758 if (elm->base.delete_tid == 0 &&
759 elm->base.create_tid < mirror->tid_end) {
760 error = hammer_delete_at_cursor(cursor,
761 HAMMER_DELETE_ADJUST,
767 error = hammer_btree_iterate(cursor);
775 * Check whether an update is needed in the case where a match already
776 * exists on the target. The only type of update allowed in this case
777 * is an update of the delete_tid.
779 * Return non-zero if the update should proceed.
783 hammer_mirror_check(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec)
785 hammer_btree_leaf_elm_t leaf = cursor->leaf;
787 if (leaf->base.delete_tid != mrec->leaf.base.delete_tid) {
788 if (mrec->leaf.base.delete_tid != 0)
795 * Filter out records which are never mirrored, such as configuration space
796 * records (for hammer cleanup).
798 * NOTE: We currently allow HAMMER_RECTYPE_SNAPSHOT records to be mirrored.
802 hammer_mirror_nomirror(struct hammer_base_elm *base)
805 * Certain types of records are never updated when mirroring.
806 * Slaves have their own configuration space.
808 if (base->rec_type == HAMMER_RECTYPE_CONFIG)
815 * Update a record in-place. Only the delete_tid can change, and
816 * only from zero to non-zero.
820 hammer_mirror_update(hammer_cursor_t cursor,
821 struct hammer_ioc_mrecord_rec *mrec)
826 * This case shouldn't occur.
828 if (mrec->leaf.base.delete_tid == 0)
832 * Mark the record deleted on the mirror target.
834 error = hammer_delete_at_cursor(cursor, HAMMER_DELETE_ADJUST,
835 mrec->leaf.base.delete_tid,
836 mrec->leaf.delete_ts,
838 cursor->flags |= HAMMER_CURSOR_ATEDISK;
844 * MOVED TO HAMMER_OBJECT.C: hammer_create_at_cursor()
847 static int hammer_mirror_localize_data(hammer_data_ondisk_t data,
848 hammer_btree_leaf_elm_t leaf);
851 * Write out a new record.
855 hammer_mirror_write(hammer_cursor_t cursor,
856 struct hammer_ioc_mrecord_rec *mrec,
859 hammer_transaction_t trans;
860 hammer_buffer_t data_buffer;
861 hammer_off_t ndata_offset;
862 hammer_tid_t high_tid;
867 trans = cursor->trans;
871 * Get the sync lock so the whole mess is atomic
873 hammer_sync_lock_sh(trans);
876 * Allocate and adjust data
878 if (mrec->leaf.data_len && mrec->leaf.data_offset) {
879 ndata = hammer_alloc_data(trans, mrec->leaf.data_len,
880 mrec->leaf.base.rec_type,
881 &ndata_offset, &data_buffer,
885 mrec->leaf.data_offset = ndata_offset;
886 hammer_modify_buffer(trans, data_buffer, NULL, 0);
887 error = copyin(udata, ndata, mrec->leaf.data_len);
889 if (hammer_crc_test_leaf(ndata, &mrec->leaf) == 0) {
890 kprintf("data crc mismatch on pipe\n");
893 error = hammer_mirror_localize_data(
897 hammer_modify_buffer_done(data_buffer);
899 mrec->leaf.data_offset = 0;
907 * Do the insertion. This can fail with a EDEADLK or EALREADY
909 cursor->flags |= HAMMER_CURSOR_INSERT;
910 error = hammer_btree_lookup(cursor);
911 if (error != ENOENT) {
917 error = hammer_btree_insert(cursor, &mrec->leaf, &doprop);
920 * Cursor is left on the current element, we want to skip it now.
922 cursor->flags |= HAMMER_CURSOR_ATEDISK;
923 cursor->flags &= ~HAMMER_CURSOR_INSERT;
926 * Track a count of active inodes.
929 mrec->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
930 mrec->leaf.base.delete_tid == 0) {
931 hammer_modify_volume_field(trans,
934 ++trans->hmp->rootvol->ondisk->vol0_stat_inodes;
935 hammer_modify_volume_done(trans->rootvol);
939 * vol0_next_tid must track the highest TID stored in the filesystem.
940 * We do not need to generate undo for this update.
942 high_tid = mrec->leaf.base.create_tid;
943 if (high_tid < mrec->leaf.base.delete_tid)
944 high_tid = mrec->leaf.base.delete_tid;
945 if (trans->rootvol->ondisk->vol0_next_tid < high_tid) {
946 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
947 trans->rootvol->ondisk->vol0_next_tid = high_tid;
948 hammer_modify_volume_done(trans->rootvol);
952 * WARNING! cursor's leaf pointer may have changed after
953 * do_propagation returns.
955 if (error == 0 && doprop)
956 hammer_btree_do_propagation(cursor, NULL, &mrec->leaf);
962 if (error && mrec->leaf.data_offset) {
963 hammer_blockmap_free(cursor->trans,
964 mrec->leaf.data_offset,
965 mrec->leaf.data_len);
967 hammer_sync_unlock(trans);
969 hammer_rel_buffer(data_buffer, 0);
974 * Localize the data payload. Directory entries may need their
975 * localization adjusted.
979 hammer_mirror_localize_data(hammer_data_ondisk_t data,
980 hammer_btree_leaf_elm_t leaf)
982 u_int32_t localization;
984 if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
985 localization = leaf->base.localization &
986 HAMMER_LOCALIZE_PSEUDOFS_MASK;
987 if (data->entry.localization != localization) {
988 data->entry.localization = localization;
989 hammer_crc_set_leaf(data, leaf);