Merge branch 'vendor/BIND' into bind_vendor2
[dragonfly.git] / sys / vfs / hammer / hammer_mirror.c
1 /*
2  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_mirror.c,v 1.17 2008/07/31 22:30:33 dillon Exp $
35  */
36 /*
37  * HAMMER mirroring ioctls - serialize and deserialize modifications made
38  *                           to a filesystem.
39  */
40
41 #include "hammer.h"
42
43 static int hammer_mirror_check(hammer_cursor_t cursor,
44                                 struct hammer_ioc_mrecord_rec *mrec);
45 static int hammer_mirror_update(hammer_cursor_t cursor,
46                                 struct hammer_ioc_mrecord_rec *mrec);
47 static int hammer_ioc_mirror_write_rec(hammer_cursor_t cursor,
48                                 struct hammer_ioc_mrecord_rec *mrec,
49                                 struct hammer_ioc_mirror_rw *mirror,
50                                 u_int32_t localization,
51                                 char *uptr);
52 static int hammer_ioc_mirror_write_pass(hammer_cursor_t cursor,
53                                 struct hammer_ioc_mrecord_rec *mrec,
54                                 struct hammer_ioc_mirror_rw *mirror,
55                                 u_int32_t localization);
56 static int hammer_ioc_mirror_write_skip(hammer_cursor_t cursor,
57                                 struct hammer_ioc_mrecord_skip *mrec,
58                                 struct hammer_ioc_mirror_rw *mirror,
59                                 u_int32_t localization);
60 static int hammer_mirror_delete_to(hammer_cursor_t cursor,
61                                 struct hammer_ioc_mirror_rw *mirror);
62 static int hammer_mirror_nomirror(struct hammer_base_elm *base);
63
64 /*
65  * All B-Tree records within the specified key range which also conform
66  * to the transaction id range are returned.  Mirroring code keeps track
67  * of the last transaction id fully scanned and can efficiently pick up
68  * where it left off if interrupted.
69  *
70  * The PFS is identified in the mirror structure.  The passed ip is just
71  * some directory in the overall HAMMER filesystem and has nothing to
72  * do with the PFS.
73  */
74 int
75 hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip,
76                        struct hammer_ioc_mirror_rw *mirror)
77 {
78         struct hammer_cmirror cmirror;
79         struct hammer_cursor cursor;
80         union hammer_ioc_mrecord_any mrec;
81         hammer_btree_leaf_elm_t elm;
82         const int crc_start = HAMMER_MREC_CRCOFF;
83         char *uptr;
84         int error;
85         int data_len;
86         int bytes;
87         int eatdisk;
88         int mrec_flags;
89         u_int32_t localization;
90         u_int32_t rec_crc;
91
92         localization = (u_int32_t)mirror->pfs_id << 16;
93
94         if ((mirror->key_beg.localization | mirror->key_end.localization) &
95             HAMMER_LOCALIZE_PSEUDOFS_MASK) {
96                 return(EINVAL);
97         }
98         if (hammer_btree_cmp(&mirror->key_beg, &mirror->key_end) > 0)
99                 return(EINVAL);
100
101         mirror->key_cur = mirror->key_beg;
102         mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
103         mirror->key_cur.localization += localization;
104         bzero(&mrec, sizeof(mrec));
105         bzero(&cmirror, sizeof(cmirror));
106
107         /*
108          * Make CRC errors non-fatal (at least on data), causing an EDOM
109          * error instead of EIO.
110          */
111         trans->flags |= HAMMER_TRANSF_CRCDOM;
112
113 retry:
114         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
115         if (error) {
116                 hammer_done_cursor(&cursor);
117                 goto failed;
118         }
119         cursor.key_beg = mirror->key_cur;
120         cursor.key_end = mirror->key_end;
121         cursor.key_end.localization &= HAMMER_LOCALIZE_MASK;
122         cursor.key_end.localization += localization;
123
124         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
125         cursor.flags |= HAMMER_CURSOR_BACKEND;
126
127         /*
128          * This flag filters the search to only return elements whos create
129          * or delete TID is >= mirror_tid.  The B-Tree uses the mirror_tid
130          * field stored with internal and leaf nodes to shortcut the scan.
131          */
132         cursor.flags |= HAMMER_CURSOR_MIRROR_FILTERED;
133         cursor.cmirror = &cmirror;
134         cmirror.mirror_tid = mirror->tid_beg;
135
136         error = hammer_btree_first(&cursor);
137         while (error == 0) {
138                 /*
139                  * Yield to more important tasks
140                  */
141                 if (error == 0) {
142                         error = hammer_signal_check(trans->hmp);
143                         if (error)
144                                 break;
145                 }
146
147                 /*
148                  * An internal node can be returned in mirror-filtered
149                  * mode and indicates that the scan is returning a skip
150                  * range in the cursor->cmirror structure.
151                  */
152                 uptr = (char *)mirror->ubuf + mirror->count;
153                 if (cursor.node->ondisk->type == HAMMER_BTREE_TYPE_INTERNAL) {
154                         /*
155                          * Check space
156                          */
157                         mirror->key_cur = cmirror.skip_beg;
158                         bytes = sizeof(mrec.skip);
159                         if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) >
160                             mirror->size) {
161                                 break;
162                         }
163
164                         /*
165                          * Fill mrec
166                          */
167                         mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
168                         mrec.head.type = HAMMER_MREC_TYPE_SKIP;
169                         mrec.head.rec_size = bytes;
170                         mrec.skip.skip_beg = cmirror.skip_beg;
171                         mrec.skip.skip_end = cmirror.skip_end;
172                         mrec.head.rec_crc = crc32(&mrec.head.rec_size,
173                                                  bytes - crc_start);
174                         error = copyout(&mrec, uptr, bytes);
175                         eatdisk = 0;
176                         goto didwrite;
177                 }
178
179                 /*
180                  * Leaf node.  In full-history mode we could filter out
181                  * elements modified outside the user-requested TID range.
182                  *
183                  * However, such elements must be returned so the writer
184                  * can compare them against the target to determine what
185                  * needs to be deleted on the target, particular for
186                  * no-history mirrors.
187                  */
188                 KKASSERT(cursor.node->ondisk->type == HAMMER_BTREE_TYPE_LEAF);
189                 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
190                 mirror->key_cur = elm->base;
191
192                 /*
193                  * If the record was created after our end point we just
194                  * ignore it.
195                  */
196                 if (elm->base.create_tid > mirror->tid_end) {
197                         error = 0;
198                         bytes = 0;
199                         eatdisk = 1;
200                         goto didwrite;
201                 }
202
203                 /*
204                  * Determine if we should generate a PASS or a REC.  PASS
205                  * records are records without any data payload.  Such
206                  * records will be generated if the target is already expected
207                  * to have the record, allowing it to delete the gaps.
208                  *
209                  * A PASS record is also used to perform deletions on the
210                  * target.
211                  *
212                  * Such deletions are needed if the master or files on the
213                  * master are no-history, or if the slave is so far behind
214                  * the master has already been pruned.
215                  */
216                 if (elm->base.create_tid < mirror->tid_beg) {
217                         bytes = sizeof(mrec.rec);
218                         if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) >
219                             mirror->size) {
220                                 break;
221                         }
222
223                         /*
224                          * Fill mrec.
225                          */
226                         mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
227                         mrec.head.type = HAMMER_MREC_TYPE_PASS;
228                         mrec.head.rec_size = bytes;
229                         mrec.rec.leaf = *elm;
230                         mrec.head.rec_crc = crc32(&mrec.head.rec_size,
231                                                  bytes - crc_start);
232                         error = copyout(&mrec, uptr, bytes);
233                         eatdisk = 1;
234                         goto didwrite;
235                         
236                 }
237
238                 /*
239                  * The core code exports the data to userland.
240                  *
241                  * CRC errors on data are reported but passed through,
242                  * but the data must be washed by the user program.
243                  */
244                 mrec_flags = 0;
245                 data_len = (elm->data_offset) ? elm->data_len : 0;
246                 if (data_len) {
247                         error = hammer_btree_extract(&cursor,
248                                                      HAMMER_CURSOR_GET_DATA);
249                         if (error) {
250                                 if (error != EDOM)
251                                         break;
252                                 mrec_flags |= HAMMER_MRECF_CRC_ERROR |
253                                               HAMMER_MRECF_DATA_CRC_BAD;
254                         }
255                 }
256
257                 bytes = sizeof(mrec.rec) + data_len;
258                 if (mirror->count + HAMMER_HEAD_DOALIGN(bytes) > mirror->size)
259                         break;
260
261                 /*
262                  * Construct the record for userland and copyout.
263                  *
264                  * The user is asking for a snapshot, if the record was
265                  * deleted beyond the user-requested ending tid, the record
266                  * is not considered deleted from the point of view of
267                  * userland and delete_tid is cleared.
268                  */
269                 mrec.head.signature = HAMMER_IOC_MIRROR_SIGNATURE;
270                 mrec.head.type = HAMMER_MREC_TYPE_REC | mrec_flags;
271                 mrec.head.rec_size = bytes;
272                 mrec.rec.leaf = *elm;
273
274                 if (elm->base.delete_tid > mirror->tid_end)
275                         mrec.rec.leaf.base.delete_tid = 0;
276                 rec_crc = crc32(&mrec.head.rec_size,
277                                 sizeof(mrec.rec) - crc_start);
278                 if (data_len)
279                         rec_crc = crc32_ext(cursor.data, data_len, rec_crc);
280                 mrec.head.rec_crc = rec_crc;
281                 error = copyout(&mrec, uptr, sizeof(mrec.rec));
282                 if (data_len && error == 0) {
283                         error = copyout(cursor.data, uptr + sizeof(mrec.rec),
284                                         data_len);
285                 }
286                 eatdisk = 1;
287
288                 /*
289                  * eatdisk controls whether we skip the current cursor
290                  * position on the next scan or not.  If doing a SKIP
291                  * the cursor is already positioned properly for the next
292                  * scan and eatdisk will be 0.
293                  */
294 didwrite:
295                 if (error == 0) {
296                         mirror->count += HAMMER_HEAD_DOALIGN(bytes);
297                         if (eatdisk)
298                                 cursor.flags |= HAMMER_CURSOR_ATEDISK;
299                         else
300                                 cursor.flags &= ~HAMMER_CURSOR_ATEDISK;
301                         error = hammer_btree_iterate(&cursor);
302                 }
303         }
304         if (error == ENOENT) {
305                 mirror->key_cur = mirror->key_end;
306                 error = 0;
307         }
308         hammer_done_cursor(&cursor);
309         if (error == EDEADLK)
310                 goto retry;
311         if (error == EINTR) {
312                 mirror->head.flags |= HAMMER_IOC_HEAD_INTR;
313                 error = 0;
314         }
315 failed:
316         mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
317         return(error);
318 }
319
320 /*
321  * Copy records from userland to the target mirror.
322  *
323  * The PFS is identified in the mirror structure.  The passed ip is just
324  * some directory in the overall HAMMER filesystem and has nothing to
325  * do with the PFS.  In fact, there might not even be a root directory for
326  * the PFS yet!
327  */
328 int
329 hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
330                        struct hammer_ioc_mirror_rw *mirror)
331 {
332         union hammer_ioc_mrecord_any mrec;
333         struct hammer_cursor cursor;
334         u_int32_t localization;
335         int checkspace_count = 0;
336         int error;
337         int bytes;
338         char *uptr;
339         int seq;
340
341         localization = (u_int32_t)mirror->pfs_id << 16;
342         seq = trans->hmp->flusher.act;
343
344         /*
345          * Validate the mirror structure and relocalize the tracking keys.
346          */
347         if (mirror->size < 0 || mirror->size > 0x70000000)
348                 return(EINVAL);
349         mirror->key_beg.localization &= HAMMER_LOCALIZE_MASK;
350         mirror->key_beg.localization += localization;
351         mirror->key_end.localization &= HAMMER_LOCALIZE_MASK;
352         mirror->key_end.localization += localization;
353         mirror->key_cur.localization &= HAMMER_LOCALIZE_MASK;
354         mirror->key_cur.localization += localization;
355
356         /*
357          * Set up our tracking cursor for the loop.  The tracking cursor
358          * is used to delete records that are no longer present on the
359          * master.  The last handled record at key_cur must be skipped.
360          */
361         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
362
363         cursor.key_beg = mirror->key_cur;
364         cursor.key_end = mirror->key_end;
365         cursor.flags |= HAMMER_CURSOR_BACKEND;
366         error = hammer_btree_first(&cursor);
367         if (error == 0)
368                 cursor.flags |= HAMMER_CURSOR_ATEDISK;
369         if (error == ENOENT)
370                 error = 0;
371
372         /*
373          * Loop until our input buffer has been exhausted.
374          */
375         while (error == 0 &&
376                 mirror->count + sizeof(mrec.head) <= mirror->size) {
377
378                 /*
379                  * Don't blow out the buffer cache.  Leave room for frontend
380                  * cache as well.
381                  *
382                  * WARNING: See warnings in hammer_unlock_cursor() function.
383                  */
384                 while (hammer_flusher_meta_halflimit(trans->hmp) ||
385                        hammer_flusher_undo_exhausted(trans, 2)) {
386                         hammer_unlock_cursor(&cursor);
387                         hammer_flusher_wait(trans->hmp, seq);
388                         hammer_lock_cursor(&cursor);
389                         seq = hammer_flusher_async_one(trans->hmp);
390                 }
391
392                 /*
393                  * If there is insufficient free space it may be due to
394                  * reserved bigblocks, which flushing might fix.
395                  */
396                 if (hammer_checkspace(trans->hmp, HAMMER_CHKSPC_MIRROR)) {
397                         if (++checkspace_count == 10) {
398                                 error = ENOSPC;
399                                 break;
400                         }
401                         hammer_unlock_cursor(&cursor);
402                         hammer_flusher_wait(trans->hmp, seq);
403                         hammer_lock_cursor(&cursor);
404                         seq = hammer_flusher_async(trans->hmp, NULL);
405                 }
406
407
408                 /*
409                  * Acquire and validate header
410                  */
411                 if ((bytes = mirror->size - mirror->count) > sizeof(mrec))
412                         bytes = sizeof(mrec);
413                 uptr = (char *)mirror->ubuf + mirror->count;
414                 error = copyin(uptr, &mrec, bytes);
415                 if (error)
416                         break;
417                 if (mrec.head.signature != HAMMER_IOC_MIRROR_SIGNATURE) {
418                         error = EINVAL;
419                         break;
420                 }
421                 if (mrec.head.rec_size < sizeof(mrec.head) ||
422                     mrec.head.rec_size > sizeof(mrec) + HAMMER_XBUFSIZE ||
423                     mirror->count + mrec.head.rec_size > mirror->size) {
424                         error = EINVAL;
425                         break;
426                 }
427
428                 switch(mrec.head.type & HAMMER_MRECF_TYPE_MASK) {
429                 case HAMMER_MREC_TYPE_SKIP:
430                         if (mrec.head.rec_size != sizeof(mrec.skip))
431                                 error = EINVAL;
432                         if (error == 0)
433                                 error = hammer_ioc_mirror_write_skip(&cursor, &mrec.skip, mirror, localization);
434                         break;
435                 case HAMMER_MREC_TYPE_REC:
436                         if (mrec.head.rec_size < sizeof(mrec.rec))
437                                 error = EINVAL;
438                         if (error == 0)
439                                 error = hammer_ioc_mirror_write_rec(&cursor, &mrec.rec, mirror, localization, uptr + sizeof(mrec.rec));
440                         break;
441                 case HAMMER_MREC_TYPE_REC_BADCRC:
442                         /*
443                          * Records with bad data payloads are ignored XXX.
444                          */
445                         if (mrec.head.rec_size < sizeof(mrec.rec))
446                                 error = EINVAL;
447                         break;
448                 case HAMMER_MREC_TYPE_PASS:
449                         if (mrec.head.rec_size != sizeof(mrec.rec))
450                                 error = EINVAL;
451                         if (error == 0)
452                                 error = hammer_ioc_mirror_write_pass(&cursor, &mrec.rec, mirror, localization);
453                         break;
454                 default:
455                         error = EINVAL;
456                         break;
457                 }
458
459                 /*
460                  * Retry the current record on deadlock, otherwise setup
461                  * for the next loop.
462                  */
463                 if (error == EDEADLK) {
464                         while (error == EDEADLK) {
465                                 hammer_sync_lock_sh(trans);
466                                 hammer_recover_cursor(&cursor);
467                                 error = hammer_cursor_upgrade(&cursor);
468                                 hammer_sync_unlock(trans);
469                         }
470                 } else {
471                         if (error == EALREADY)
472                                 error = 0;
473                         if (error == 0) {
474                                 mirror->count += 
475                                         HAMMER_HEAD_DOALIGN(mrec.head.rec_size);
476                         }
477                 }
478         }
479         hammer_done_cursor(&cursor);
480
481         /*
482          * cumulative error 
483          */
484         if (error) {
485                 mirror->head.flags |= HAMMER_IOC_HEAD_ERROR;
486                 mirror->head.error = error;
487         }
488
489         /*
490          * ioctls don't update the RW data structure if an error is returned,
491          * always return 0.
492          */
493         return(0);
494 }
495
496 /*
497  * Handle skip records.
498  *
499  * We must iterate from the last resolved record position at mirror->key_cur
500  * to skip_beg non-inclusive and delete any records encountered.
501  *
502  * mirror->key_cur must be carefully set when we succeed in processing
503  * this mrec.
504  */
505 static int
506 hammer_ioc_mirror_write_skip(hammer_cursor_t cursor,
507                              struct hammer_ioc_mrecord_skip *mrec,
508                              struct hammer_ioc_mirror_rw *mirror,
509                              u_int32_t localization)
510 {
511         int error;
512
513         /*
514          * Relocalize the skip range
515          */
516         mrec->skip_beg.localization &= HAMMER_LOCALIZE_MASK;
517         mrec->skip_beg.localization += localization;
518         mrec->skip_end.localization &= HAMMER_LOCALIZE_MASK;
519         mrec->skip_end.localization += localization;
520
521         /*
522          * Iterate from current position to skip_beg, deleting any records
523          * we encounter.  The record at skip_beg is not included (it is
524          * skipped).
525          */
526         cursor->key_end = mrec->skip_beg;
527         cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
528         cursor->flags |= HAMMER_CURSOR_BACKEND;
529         error = hammer_mirror_delete_to(cursor, mirror);
530
531         /*
532          * Now skip past the skip (which is the whole point point of
533          * having a skip record).  The sender has not sent us any records
534          * for the skip area so we wouldn't know what to keep and what
535          * to delete anyway.
536          *
537          * Clear ATEDISK because skip_end is non-inclusive, so we can't
538          * count an exact match if we happened to get one.
539          */
540         if (error == 0) {
541                 mirror->key_cur = mrec->skip_end;
542                 cursor->key_beg = mrec->skip_end;
543                 error = hammer_btree_lookup(cursor);
544                 cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
545                 if (error == ENOENT)
546                         error = 0;
547         }
548         return(error);
549 }
550
551 /*
552  * Handle B-Tree records.
553  *
554  * We must iterate to mrec->base.key (non-inclusively), and then process
555  * the record.  We are allowed to write a new record or delete an existing
556  * record, but cannot replace an existing record.
557  *
558  * mirror->key_cur must be carefully set when we succeed in processing
559  * this mrec.
560  */
561 static int
562 hammer_ioc_mirror_write_rec(hammer_cursor_t cursor,
563                             struct hammer_ioc_mrecord_rec *mrec,
564                             struct hammer_ioc_mirror_rw *mirror,
565                             u_int32_t localization,
566                             char *uptr)
567 {
568         hammer_transaction_t trans;
569         u_int32_t rec_crc;
570         int error;
571
572         trans = cursor->trans;
573         rec_crc = crc32(mrec, sizeof(*mrec));
574
575         if (mrec->leaf.data_len < 0 || 
576             mrec->leaf.data_len > HAMMER_XBUFSIZE ||
577             mrec->leaf.data_len + sizeof(*mrec) > mrec->head.rec_size) {
578                 return(EINVAL);
579         }
580
581         /*
582          * Re-localize for target.  relocalization of data is handled
583          * by hammer_mirror_write().
584          */
585         mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK;
586         mrec->leaf.base.localization += localization;
587
588         /*
589          * Delete records through until we reach (non-inclusively) the
590          * target record.
591          */
592         cursor->key_end = mrec->leaf.base;
593         cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
594         cursor->flags |= HAMMER_CURSOR_BACKEND;
595         error = hammer_mirror_delete_to(cursor, mirror);
596
597         /*
598          * Certain records are not part of the mirroring operation
599          */
600         if (hammer_mirror_nomirror(&mrec->leaf.base))
601                 return(0);
602
603         /*
604          * Locate the record.
605          *
606          * If the record exists only the delete_tid may be updated.
607          *
608          * If the record does not exist we can create it only if the
609          * create_tid is not too old.  If the create_tid is too old
610          * it may have already been destroyed on the slave from pruning.
611          *
612          * Note that mirror operations are effectively as-of operations
613          * and delete_tid can be 0 for mirroring purposes even if it is
614          * not actually 0 at the originator.
615          *
616          * These functions can return EDEADLK
617          */
618         cursor->key_beg = mrec->leaf.base;
619         cursor->flags |= HAMMER_CURSOR_BACKEND;
620         cursor->flags &= ~HAMMER_CURSOR_INSERT;
621         error = hammer_btree_lookup(cursor);
622
623         if (error == 0 && hammer_mirror_check(cursor, mrec)) {
624                 error = hammer_mirror_update(cursor, mrec);
625         } else if (error == ENOENT) {
626                 if (mrec->leaf.base.create_tid >= mirror->tid_beg) {
627                         error = hammer_create_at_cursor(
628                                         cursor, &mrec->leaf,
629                                         uptr, HAMMER_CREATE_MODE_UMIRROR);
630                 } else {
631                         error = 0;
632                 }
633         }
634         if (error == 0 || error == EALREADY)
635                 mirror->key_cur = mrec->leaf.base;
636         return(error);
637 }
638
639 /*
640  * This works like write_rec but no write or update is necessary,
641  * and no data payload is included so we couldn't do a write even
642  * if we wanted to.
643  *
644  * We must still iterate for deletions, and we can validate the
645  * record header which is a good way to test for corrupted mirror
646  * targets XXX.
647  *
648  * mirror->key_cur must be carefully set when we succeed in processing
649  * this mrec.
650  */
651 static
652 int
653 hammer_ioc_mirror_write_pass(hammer_cursor_t cursor,
654                              struct hammer_ioc_mrecord_rec *mrec,
655                              struct hammer_ioc_mirror_rw *mirror,
656                              u_int32_t localization)
657 {
658         hammer_transaction_t trans;
659         u_int32_t rec_crc;
660         int error;
661
662         trans = cursor->trans;
663         rec_crc = crc32(mrec, sizeof(*mrec));
664
665         /*
666          * Re-localize for target.  Relocalization of data is handled
667          * by hammer_mirror_write().
668          */
669         mrec->leaf.base.localization &= HAMMER_LOCALIZE_MASK;
670         mrec->leaf.base.localization += localization;
671
672         /*
673          * Delete records through until we reach (non-inclusively) the
674          * target record.
675          */
676         cursor->key_end = mrec->leaf.base;
677         cursor->flags &= ~HAMMER_CURSOR_END_INCLUSIVE;
678         cursor->flags |= HAMMER_CURSOR_BACKEND;
679         error = hammer_mirror_delete_to(cursor, mirror);
680
681         /*
682          * Certain records are not part of the mirroring operation
683          */
684         if (hammer_mirror_nomirror(&mrec->leaf.base))
685                 return(0);
686
687         /*
688          * Locate the record and get past it by setting ATEDISK.  Perform
689          * any necessary deletions.  We have no data payload and cannot
690          * create a new record.
691          */
692         if (error == 0) {
693                 mirror->key_cur = mrec->leaf.base;
694                 cursor->key_beg = mrec->leaf.base;
695                 cursor->flags |= HAMMER_CURSOR_BACKEND;
696                 cursor->flags &= ~HAMMER_CURSOR_INSERT;
697                 error = hammer_btree_lookup(cursor);
698                 if (error == 0) {
699                         if (hammer_mirror_check(cursor, mrec))
700                                 error = hammer_mirror_update(cursor, mrec);
701                         cursor->flags |= HAMMER_CURSOR_ATEDISK;
702                 } else {
703                         cursor->flags &= ~HAMMER_CURSOR_ATEDISK;
704                 }
705                 if (error == ENOENT)
706                         error = 0;
707         }
708         return(error);
709 }
710
711 /*
712  * As part of the mirror write we iterate across swaths of records
713  * on the target which no longer exist on the source, and mark them
714  * deleted.
715  *
716  * The caller has indexed the cursor and set up key_end.  We iterate
717  * through to key_end.
718  *
719  * There is an edge case where the master has deleted a record whos
720  * create_tid exactly matches our end_tid.  We cannot delete this
721  * record on the slave yet because we cannot assign delete_tid == create_tid.
722  * The deletion should be picked up on the next sequence since in order
723  * to have been deleted on the master a transaction must have occured with
724  * a TID greater then the create_tid of the record.
725  *
726  * To support incremental re-mirroring, just for robustness, we do not
727  * touch any records created beyond (or equal to) mirror->tid_end.
728  */
729 static
730 int
731 hammer_mirror_delete_to(hammer_cursor_t cursor,
732                        struct hammer_ioc_mirror_rw *mirror)
733 {
734         hammer_btree_leaf_elm_t elm;
735         int error;
736
737         error = hammer_btree_iterate(cursor);
738         while (error == 0) {
739                 elm = &cursor->node->ondisk->elms[cursor->index].leaf;
740                 KKASSERT(elm->base.btype == HAMMER_BTREE_TYPE_RECORD);
741                 cursor->flags |= HAMMER_CURSOR_ATEDISK;
742
743                 /*
744                  * Certain records are not part of the mirroring operation
745                  */
746                 if (hammer_mirror_nomirror(&elm->base)) {
747                         error = hammer_btree_iterate(cursor);
748                         continue;
749                 }
750
751                 /*
752                  * Note: Must still delete records with create_tid < tid_beg,
753                  *       as record may have been pruned-away on source.
754                  */
755                 if (elm->base.delete_tid == 0 &&
756                     elm->base.create_tid < mirror->tid_end) {
757                         error = hammer_delete_at_cursor(cursor,
758                                                         HAMMER_DELETE_ADJUST,
759                                                         mirror->tid_end,
760                                                         time_second,
761                                                         1, NULL);
762                 }
763                 if (error == 0)
764                         error = hammer_btree_iterate(cursor);
765         }
766         if (error == ENOENT)
767                 error = 0;
768         return(error);
769 }
770
771 /*
772  * Check whether an update is needed in the case where a match already
773  * exists on the target.  The only type of update allowed in this case
774  * is an update of the delete_tid.
775  *
776  * Return non-zero if the update should proceed.
777  */
778 static
779 int
780 hammer_mirror_check(hammer_cursor_t cursor, struct hammer_ioc_mrecord_rec *mrec)
781 {
782         hammer_btree_leaf_elm_t leaf = cursor->leaf;
783
784         if (leaf->base.delete_tid != mrec->leaf.base.delete_tid) {
785                 if (mrec->leaf.base.delete_tid != 0)
786                         return(1);
787         }
788         return(0);
789 }
790
791 /*
792  * Filter out records which are never mirrored, such as configuration space
793  * records (for hammer cleanup).
794  *
795  * NOTE: We currently allow HAMMER_RECTYPE_SNAPSHOT records to be mirrored.
796  */
797 static
798 int
799 hammer_mirror_nomirror(struct hammer_base_elm *base)
800 {
801         /*
802          * Certain types of records are never updated when mirroring.
803          * Slaves have their own configuration space.
804          */
805         if (base->rec_type == HAMMER_RECTYPE_CONFIG)
806                 return(1);
807         return(0);
808 }
809
810
811 /*
812  * Update a record in-place.  Only the delete_tid can change, and
813  * only from zero to non-zero.
814  */
815 static
816 int
817 hammer_mirror_update(hammer_cursor_t cursor,
818                      struct hammer_ioc_mrecord_rec *mrec)
819 {
820         int error;
821
822         /*
823          * This case shouldn't occur.
824          */
825         if (mrec->leaf.base.delete_tid == 0)
826                 return(0);
827
828         /*
829          * Mark the record deleted on the mirror target.
830          */
831         error = hammer_delete_at_cursor(cursor, HAMMER_DELETE_ADJUST,
832                                         mrec->leaf.base.delete_tid,
833                                         mrec->leaf.delete_ts,
834                                         1, NULL);
835         cursor->flags |= HAMMER_CURSOR_ATEDISK;
836         return(error);
837 }
838
839 #if 0
840 /*
841  * MOVED TO HAMMER_OBJECT.C: hammer_create_at_cursor()
842  */
843
844 static int hammer_mirror_localize_data(hammer_data_ondisk_t data,
845                                 hammer_btree_leaf_elm_t leaf);
846
847 /*
848  * Write out a new record.
849  */
850 static
851 int
852 hammer_mirror_write(hammer_cursor_t cursor,
853                     struct hammer_ioc_mrecord_rec *mrec,
854                     char *udata)
855 {
856         hammer_transaction_t trans;
857         hammer_buffer_t data_buffer;
858         hammer_off_t ndata_offset;
859         hammer_tid_t high_tid;
860         void *ndata;
861         int error;
862         int doprop;
863
864         trans = cursor->trans;
865         data_buffer = NULL;
866
867         /*
868          * Get the sync lock so the whole mess is atomic
869          */
870         hammer_sync_lock_sh(trans);
871
872         /*
873          * Allocate and adjust data
874          */
875         if (mrec->leaf.data_len && mrec->leaf.data_offset) {
876                 ndata = hammer_alloc_data(trans, mrec->leaf.data_len,
877                                           mrec->leaf.base.rec_type,
878                                           &ndata_offset, &data_buffer,
879                                           0, &error);
880                 if (ndata == NULL)
881                         return(error);
882                 mrec->leaf.data_offset = ndata_offset;
883                 hammer_modify_buffer(trans, data_buffer, NULL, 0);
884                 error = copyin(udata, ndata, mrec->leaf.data_len);
885                 if (error == 0) {
886                         if (hammer_crc_test_leaf(ndata, &mrec->leaf) == 0) {
887                                 kprintf("data crc mismatch on pipe\n");
888                                 error = EINVAL;
889                         } else {
890                                 error = hammer_mirror_localize_data(
891                                                         ndata, &mrec->leaf);
892                         }
893                 }
894                 hammer_modify_buffer_done(data_buffer);
895         } else {
896                 mrec->leaf.data_offset = 0;
897                 error = 0;
898                 ndata = NULL;
899         }
900         if (error)
901                 goto failed;
902
903         /*
904          * Do the insertion.  This can fail with a EDEADLK or EALREADY
905          */
906         cursor->flags |= HAMMER_CURSOR_INSERT;
907         error = hammer_btree_lookup(cursor);
908         if (error != ENOENT) {
909                 if (error == 0)
910                         error = EALREADY;
911                 goto failed;
912         }
913
914         error = hammer_btree_insert(cursor, &mrec->leaf, &doprop);
915
916         /*
917          * Cursor is left on the current element, we want to skip it now.
918          */
919         cursor->flags |= HAMMER_CURSOR_ATEDISK;
920         cursor->flags &= ~HAMMER_CURSOR_INSERT;
921
922         /*
923          * Track a count of active inodes.
924          */
925         if (error == 0 &&
926             mrec->leaf.base.rec_type == HAMMER_RECTYPE_INODE &&
927             mrec->leaf.base.delete_tid == 0) {
928                 hammer_modify_volume_field(trans,
929                                            trans->rootvol,
930                                            vol0_stat_inodes);
931                 ++trans->hmp->rootvol->ondisk->vol0_stat_inodes;
932                 hammer_modify_volume_done(trans->rootvol);
933         }
934
935         /*
936          * vol0_next_tid must track the highest TID stored in the filesystem.
937          * We do not need to generate undo for this update.
938          */
939         high_tid = mrec->leaf.base.create_tid;
940         if (high_tid < mrec->leaf.base.delete_tid)
941                 high_tid = mrec->leaf.base.delete_tid;
942         if (trans->rootvol->ondisk->vol0_next_tid < high_tid) {
943                 hammer_modify_volume(trans, trans->rootvol, NULL, 0);
944                 trans->rootvol->ondisk->vol0_next_tid = high_tid;
945                 hammer_modify_volume_done(trans->rootvol);
946         }
947
948         /*
949          * WARNING!  cursor's leaf pointer may have changed after
950          *           do_propagation returns.
951          */
952         if (error == 0 && doprop)
953                 hammer_btree_do_propagation(cursor, NULL, &mrec->leaf);
954
955 failed:
956         /*
957          * Cleanup
958          */
959         if (error && mrec->leaf.data_offset) {
960                 hammer_blockmap_free(cursor->trans,
961                                      mrec->leaf.data_offset,
962                                      mrec->leaf.data_len);
963         }
964         hammer_sync_unlock(trans);
965         if (data_buffer)
966                 hammer_rel_buffer(data_buffer, 0);
967         return(error);
968 }
969
970 /*
971  * Localize the data payload.  Directory entries may need their
972  * localization adjusted.
973  */
974 static
975 int
976 hammer_mirror_localize_data(hammer_data_ondisk_t data,
977                             hammer_btree_leaf_elm_t leaf)
978 {
979         u_int32_t localization;
980
981         if (leaf->base.rec_type == HAMMER_RECTYPE_DIRENTRY) {
982                 localization = leaf->base.localization &
983                                HAMMER_LOCALIZE_PSEUDOFS_MASK;
984                 if (data->entry.localization != localization) {
985                         data->entry.localization = localization;
986                         hammer_crc_set_leaf(data, leaf);
987                 }
988         }
989         return(0);
990 }
991
992 #endif