hammer2 - Refactor frontend part 17
[dragonfly.git] / sys / vfs / hammer2 / hammer2_cluster.c
1 /*
2  * Copyright (c) 2013-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
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 /*
35  * The cluster module collects multiple chains representing the same
36  * information from different nodes into a single entity.  It allows direct
37  * access to media data as long as it is not blockref array data (which
38  * will obviously have to be different at each node).
39  *
40  * This module also handles I/O dispatch, status rollup, and various
41  * mastership arrangements including quorum operations.  It effectively
42  * presents one topology to the vnops layer.
43  *
44  * Many of the API calls mimic chain API calls but operate on clusters
45  * instead of chains.  Please see hammer2_chain.c for more complete code
46  * documentation of the API functions.
47  *
48  * WARNING! This module is *extremely* complex.  It must issue asynchronous
49  *          locks and I/O, do quorum and/or master-slave processing, and
50  *          it must operate properly even if some nodes are broken (which
51  *          can also mean indefinite locks).
52  *
53  *                              CLUSTER OPERATIONS
54  *
55  * Cluster operations can be broken down into three pieces:
56  *
57  * (1) Chain locking and data retrieval.
58  *
59  *      - Most complex functions, quorum management on transaction ids.
60  *
61  *      - Locking and data accesses must be internally asynchronous.
62  *
63  *      - Validate and manage cache coherency primitives (cache state
64  *        is stored in chain topologies but must be validated by these
65  *        functions).
66  *
67  * (2) Lookups and Scans
68  *              hammer2_cluster_lookup()
69  *              hammer2_cluster_next()
70  *
71  *      - Depend on locking & data retrieval functions, but still complex.
72  *
73  *      - Must do quorum management on transaction ids.
74  *
75  *      - Lookup and Iteration ops Must be internally asynchronous.
76  *
77  * (3) Modifying Operations
78  *              hammer2_cluster_create()
79  *
80  *      - Can usually punt on failures, operation continues unless quorum
81  *        is lost.  If quorum is lost, must wait for resynchronization
82  *        (depending on the management mode).
83  *
84  *      - Must disconnect node on failures (also not flush), remount, and
85  *        resynchronize.
86  *
87  *      - Network links (via kdmsg) are relatively easy to issue as the
88  *        complex underworkings of hammer2_chain.c don't have to messed
89  *        with (the protocol is at a higher level than block-level).
90  *
91  *      - Multiple local disk nodes (i.e. block devices) are another matter.
92  *        Chain operations have to be dispatched to per-node threads (xN)
93  *        because we can't asynchronize potentially very complex chain
94  *        operations in hammer2_chain.c (it would be a huge mess).
95  *
96  *        (these threads are also used to terminate incoming kdmsg ops from
97  *        other machines).
98  *
99  *      - Single-node filesystems do not use threads and will simply call
100  *        hammer2_chain.c functions directly.  This short-cut is handled
101  *        at the base of each cluster function.
102  */
103 #include <sys/cdefs.h>
104 #include <sys/param.h>
105 #include <sys/systm.h>
106 #include <sys/types.h>
107 #include <sys/lock.h>
108 #include <sys/uuid.h>
109
110 #include "hammer2.h"
111
112 /*
113  * Returns the bref type of the cluster's foucs.
114  *
115  * If the cluster is errored, returns HAMMER2_BREF_TYPE_EMPTY (0).
116  * The cluster must be locked.
117  */
118 uint8_t
119 hammer2_cluster_type(hammer2_cluster_t *cluster)
120 {
121         if (cluster->error == 0) {
122                 KKASSERT(cluster->focus != NULL);
123                 return(cluster->focus->bref.type);
124         }
125         return 0;
126 }
127
128 /*
129  * Returns non-zero if the cluster's focus is flagged as being modified.
130  *
131  * If the cluster is errored, returns 0.
132  */
133 static
134 int
135 hammer2_cluster_modified(hammer2_cluster_t *cluster)
136 {
137         if (cluster->error == 0) {
138                 KKASSERT(cluster->focus != NULL);
139                 return((cluster->focus->flags & HAMMER2_CHAIN_MODIFIED) != 0);
140         }
141         return 0;
142 }
143
144 /*
145  * Returns the bref of the cluster's focus, sans any data-offset information
146  * (since offset information is per-node and wouldn't be useful).
147  *
148  * Callers use this function to access modify_tid, mirror_tid, type,
149  * key, and keybits.
150  *
151  * If the cluster is errored, returns an empty bref.
152  * The cluster must be locked.
153  */
154 void
155 hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref)
156 {
157         if (cluster->error == 0) {
158                 KKASSERT(cluster->focus != NULL);
159                 *bref = cluster->focus->bref;
160                 bref->data_off = 0;
161         } else {
162                 bzero(bref, sizeof(*bref));
163         }
164 }
165
166 /*
167  * Create a degenerate cluster with one ref from a single locked chain.
168  * The returned cluster will be focused on the chain and inherit its
169  * error state.
170  *
171  * The chain's lock and reference are transfered to the new cluster, so
172  * the caller should not try to unlock the chain separately.
173  *
174  * We fake the flags.
175  */
176 hammer2_cluster_t *
177 hammer2_cluster_from_chain(hammer2_chain_t *chain)
178 {
179         hammer2_cluster_t *cluster;
180
181         cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
182         cluster->array[0].chain = chain;
183         cluster->array[0].flags = HAMMER2_CITEM_FEMOD;
184         cluster->nchains = 1;
185         cluster->focus = chain;
186         cluster->focus_index = 0;
187         cluster->pmp = chain->pmp;
188         cluster->refs = 1;
189         cluster->error = chain->error;
190         cluster->flags = HAMMER2_CLUSTER_LOCKED |
191                          HAMMER2_CLUSTER_WRHARD |
192                          HAMMER2_CLUSTER_RDHARD |
193                          HAMMER2_CLUSTER_MSYNCED |
194                          HAMMER2_CLUSTER_SSYNCED;
195
196         return cluster;
197 }
198
199 /*
200  * Add a reference to a cluster and its underlying chains.
201  *
202  * We must also ref the underlying chains in order to allow ref/unlock
203  * sequences to later re-lock.
204  */
205 void
206 hammer2_cluster_ref(hammer2_cluster_t *cluster)
207 {
208         atomic_add_int(&cluster->refs, 1);
209 }
210
211 /*
212  * Drop the caller's reference to the cluster.  When the ref count drops to
213  * zero this function frees the cluster and drops all underlying chains.
214  *
215  * In-progress read I/Os are typically detached from the cluster once the
216  * first one returns (the remaining stay attached to the DIOs but are then
217  * ignored and drop naturally).
218  */
219 void
220 hammer2_cluster_drop(hammer2_cluster_t *cluster)
221 {
222         hammer2_chain_t *chain;
223         int i;
224
225         KKASSERT(cluster->refs > 0);
226         if (atomic_fetchadd_int(&cluster->refs, -1) == 1) {
227                 cluster->focus = NULL;          /* safety XXX chg to assert */
228                 cluster->focus_index = 0;
229
230                 for (i = 0; i < cluster->nchains; ++i) {
231                         chain = cluster->array[i].chain;
232                         if (chain) {
233                                 hammer2_chain_drop(chain);
234                                 cluster->array[i].chain = NULL; /* safety */
235                         }
236                 }
237                 cluster->nchains = 0;                           /* safety */
238
239                 kfree(cluster, M_HAMMER2);
240                 /* cluster is invalid */
241         }
242 }
243
244 /*
245  * Lock a cluster.  Cluster must already be referenced.  Focus is maintained. 
246  *
247  * WARNING! This function expects the caller to handle resolution of the
248  *          cluster.  We never re-resolve the cluster in this function,
249  *          because it might be used to temporarily unlock/relock a cparent
250  *          in an iteration or recursrion, and the cparents elements do not
251  *          necessarily match.
252  */
253 void
254 hammer2_cluster_lock(hammer2_cluster_t *cluster, int how)
255 {
256         hammer2_chain_t *chain;
257         int i;
258
259         /* cannot be on inode-embedded cluster template, must be on copy */
260         KKASSERT(cluster->refs > 0);
261         KKASSERT((cluster->flags & HAMMER2_CLUSTER_INODE) == 0);
262         if (cluster->flags & HAMMER2_CLUSTER_LOCKED) {
263                 panic("hammer2_cluster_lock: cluster %p already locked!\n",
264                         cluster);
265         }
266         atomic_set_int(&cluster->flags, HAMMER2_CLUSTER_LOCKED);
267
268         /*
269          * Lock chains and resolve state.
270          */
271         for (i = 0; i < cluster->nchains; ++i) {
272                 chain = cluster->array[i].chain;
273                 if (chain == NULL)
274                         continue;
275                 hammer2_chain_lock(chain, how);
276         }
277 }
278
279 /*
280  * Calculate the clustering state for the cluster and set its focus.
281  * This routine must be called with care.  For example, it should not
282  * normally be called after relocking a non-leaf cluster because parent
283  * clusters help iterations and each element might be at a slightly different
284  * indirect node (each node's topology is independently indexed).
285  *
286  * HAMMER2_CITEM_FEMOD flags which elements can be modified by normal
287  * operations.  Typically this is only set on a quorum of MASTERs or
288  * on a SOFT_MASTER.  Also as a degenerate case on SUPROOT.  If a SOFT_MASTER
289  * is present, this bit is *not* set on a quorum of MASTERs.  The
290  * synchronization code ignores this bit, but all hammer2_cluster_*() calls
291  * that create/modify/delete elements use it.
292  *
293  * The chains making up the cluster may be narrowed down based on quorum
294  * acceptability, and if RESOLVE_RDONLY is specified the chains can be
295  * narrowed down to a single chain as long as the entire subtopology is known
296  * to be intact.  So, for example, we can narrow a read-only op to a single
297  * fast SLAVE but if we focus a CACHE chain we must still retain at least
298  * a SLAVE to ensure that the subtopology can be accessed.
299  *
300  * RESOLVE_RDONLY operations are effectively as-of so the quorum does not need
301  * to be maintained once the topology is validated as-of the top level of
302  * the operation.
303  *
304  * If a failure occurs the operation must be aborted by higher-level code and
305  * retried. XXX
306  */
307 void
308 hammer2_cluster_resolve(hammer2_cluster_t *cluster)
309 {
310         hammer2_chain_t *chain;
311         hammer2_chain_t *focus;
312         hammer2_pfs_t *pmp;
313         hammer2_tid_t quorum_tid;
314         hammer2_tid_t last_best_quorum_tid;
315         int focus_pfs_type;
316         uint32_t nflags;
317         int ttlmasters;
318         int ttlslaves;
319         int nmasters;
320         int nslaves;
321         int nquorum;
322         int smpresent;
323         int i;
324
325         cluster->error = 0;
326         cluster->focus = NULL;
327
328         focus_pfs_type = 0;
329         nflags = 0;
330         ttlmasters = 0;
331         ttlslaves = 0;
332         nmasters = 0;
333         nslaves = 0;
334
335         /*
336          * Calculate quorum
337          */
338         pmp = cluster->pmp;
339         KKASSERT(pmp != NULL || cluster->nchains == 0);
340         nquorum = pmp ? pmp->pfs_nmasters / 2 + 1 : 0;
341         smpresent = 0;
342
343         /*
344          * Pass 1
345          *
346          * NOTE: A NULL chain is not necessarily an error, it could be
347          *       e.g. a lookup failure or the end of an iteration.
348          *       Process normally.
349          */
350         for (i = 0; i < cluster->nchains; ++i) {
351                 chain = cluster->array[i].chain;
352                 if (chain && chain->error) {
353                         if (cluster->focus == NULL || cluster->focus == chain) {
354                                 /* error will be overridden by valid focus */
355                                 cluster->error = chain->error;
356                         }
357
358                         /*
359                          * Must count total masters and slaves whether the
360                          * chain is errored or not.
361                          */
362                         switch (cluster->pmp->pfs_types[i]) {
363                         case HAMMER2_PFSTYPE_MASTER:
364                                 ++ttlmasters;
365                                 break;
366                         case HAMMER2_PFSTYPE_SLAVE:
367                                 ++ttlslaves;
368                                 break;
369                         }
370                         continue;
371                 }
372                 switch (cluster->pmp->pfs_types[i]) {
373                 case HAMMER2_PFSTYPE_MASTER:
374                         ++ttlmasters;
375                         break;
376                 case HAMMER2_PFSTYPE_SLAVE:
377                         ++ttlslaves;
378                         break;
379                 case HAMMER2_PFSTYPE_SOFT_MASTER:
380                         nflags |= HAMMER2_CLUSTER_WRSOFT;
381                         nflags |= HAMMER2_CLUSTER_RDSOFT;
382                         smpresent = 1;
383                         break;
384                 case HAMMER2_PFSTYPE_SOFT_SLAVE:
385                         nflags |= HAMMER2_CLUSTER_RDSOFT;
386                         break;
387                 case HAMMER2_PFSTYPE_SUPROOT:
388                         /*
389                          * Degenerate cluster representing the super-root
390                          * topology on a single device.  Fake stuff so
391                          * cluster ops work as expected.
392                          */
393                         nflags |= HAMMER2_CLUSTER_WRHARD;
394                         nflags |= HAMMER2_CLUSTER_RDHARD;
395                         cluster->focus_index = i;
396                         cluster->focus = chain;
397                         cluster->error = chain ? chain->error : 0;
398                         break;
399                 default:
400                         break;
401                 }
402         }
403
404         /*
405          * Pass 2
406          *
407          * Resolve masters.  Calculate nmasters for the highest matching
408          * TID, if a quorum cannot be attained try the next lower matching
409          * TID until we exhaust TIDs.
410          *
411          * NOTE: A NULL chain is not necessarily an error, it could be
412          *       e.g. a lookup failure or the end of an iteration.
413          *       Process normally.
414          */
415         last_best_quorum_tid = HAMMER2_TID_MAX;
416         quorum_tid = 0;         /* fix gcc warning */
417
418         while (nmasters < nquorum && last_best_quorum_tid != 0) {
419                 nmasters = 0;
420                 quorum_tid = 0;
421
422                 for (i = 0; i < cluster->nchains; ++i) {
423                         if (cluster->pmp->pfs_types[i] !=
424                             HAMMER2_PFSTYPE_MASTER) {
425                                 continue;
426                         }
427                         chain = cluster->array[i].chain;
428
429                         if (cluster->array[i].flags & HAMMER2_CITEM_INVALID) {
430                                 /*
431                                  * Invalid as in unsynchronized, cannot be
432                                  * used to calculate the quorum.
433                                  */
434                         } else if (chain == NULL && quorum_tid == 0) {
435                                 /*
436                                  * NULL chain on master matches NULL chains
437                                  * on other masters.
438                                  */
439                                 ++nmasters;
440                         } else if (quorum_tid < last_best_quorum_tid &&
441                                    chain != NULL &&
442                                    (quorum_tid < chain->bref.modify_tid ||
443                                     nmasters == 0)) {
444                                 /*
445                                  * Better TID located, reset nmasters count.
446                                  */
447                                 nmasters = 1;
448                                 quorum_tid = chain->bref.modify_tid;
449                         } else if (chain &&
450                                    quorum_tid == chain->bref.modify_tid) {
451                                 /*
452                                  * TID matches current collection.
453                                  */
454                                 ++nmasters;
455                         }
456                 }
457                 if (nmasters >= nquorum)
458                         break;
459                 last_best_quorum_tid = quorum_tid;
460         }
461
462         /*
463          * Pass 3
464          *
465          * NOTE: A NULL chain is not necessarily an error, it could be
466          *       e.g. a lookup failure or the end of an iteration.
467          *       Process normally.
468          */
469         for (i = 0; i < cluster->nchains; ++i) {
470                 cluster->array[i].flags &= ~HAMMER2_CITEM_FEMOD;
471                 chain = cluster->array[i].chain;
472                 if (chain && chain->error) {
473                         if (cluster->focus == NULL || cluster->focus == chain) {
474                                 /* error will be overridden by valid focus */
475                                 cluster->error = chain->error;
476                         }
477                         continue;
478                 }
479
480                 switch (cluster->pmp->pfs_types[i]) {
481                 case HAMMER2_PFSTYPE_MASTER:
482                         /*
483                          * We must have enough up-to-date masters to reach
484                          * a quorum and the master modify_tid must match
485                          * the quorum's modify_tid.
486                          *
487                          * Do not select an errored or out-of-sync master.
488                          */
489                         if (cluster->array[i].flags & HAMMER2_CITEM_INVALID) {
490                                 nflags |= HAMMER2_CLUSTER_UNHARD;
491                         } else if (nmasters >= nquorum &&
492                                    (chain == NULL || chain->error == 0) &&
493                                    ((chain == NULL && quorum_tid == 0) ||
494                                     (chain != NULL && quorum_tid ==
495                                                   chain->bref.modify_tid))) {
496                                 nflags |= HAMMER2_CLUSTER_WRHARD;
497                                 nflags |= HAMMER2_CLUSTER_RDHARD;
498                                 if (!smpresent) {
499                                         cluster->array[i].flags |=
500                                                         HAMMER2_CITEM_FEMOD;
501                                 }
502                                 if (cluster->focus == NULL ||
503                                     focus_pfs_type == HAMMER2_PFSTYPE_SLAVE) {
504                                         focus_pfs_type = HAMMER2_PFSTYPE_MASTER;
505                                         cluster->focus_index = i;
506                                         cluster->focus = chain; /* NULL ok */
507                                         cluster->error = chain ? chain->error :
508                                                                  0;
509                                 }
510                         } else if (chain == NULL || chain->error == 0) {
511                                 nflags |= HAMMER2_CLUSTER_UNHARD;
512                         }
513                         break;
514                 case HAMMER2_PFSTYPE_SLAVE:
515                         /*
516                          * We must have enough up-to-date masters to reach
517                          * a quorum and the slave modify_tid must match the
518                          * quorum's modify_tid.
519                          *
520                          * Do not select an errored slave.
521                          */
522                         if (cluster->array[i].flags & HAMMER2_CITEM_INVALID) {
523                                 nflags |= HAMMER2_CLUSTER_UNHARD;
524                         } else if (nmasters >= nquorum &&
525                                    (chain == NULL || chain->error == 0) &&
526                                    ((chain == NULL && quorum_tid == 0) ||
527                                     (chain && quorum_tid ==
528                                               chain->bref.modify_tid))) {
529                                 ++nslaves;
530                                 nflags |= HAMMER2_CLUSTER_RDHARD;
531 #if 0
532                                 /* XXX optimize for RESOLVE_RDONLY */
533                                 if (cluster->focus == NULL) {
534                                         focus_pfs_type = HAMMER2_PFSTYPE_SLAVE;
535                                         cluster->focus_index = i;
536                                         cluster->focus = chain; /* NULL ok */
537                                         cluster->error = chain ? chain->error :
538                                                                  0;
539                                 }
540 #endif
541                         } else if (chain == NULL || chain->error == 0) {
542                                 nflags |= HAMMER2_CLUSTER_UNSOFT;
543                         }
544                         break;
545                 case HAMMER2_PFSTYPE_SOFT_MASTER:
546                         /*
547                          * Directly mounted soft master always wins.  There
548                          * should be only one.
549                          */
550                         KKASSERT(focus_pfs_type != HAMMER2_PFSTYPE_SOFT_MASTER);
551                         cluster->focus_index = i;
552                         cluster->focus = chain;
553                         cluster->error = chain ? chain->error : 0;
554                         focus_pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
555                         cluster->array[i].flags |= HAMMER2_CITEM_FEMOD;
556                         break;
557                 case HAMMER2_PFSTYPE_SOFT_SLAVE:
558                         /*
559                          * Directly mounted soft slave always wins.  There
560                          * should be only one.
561                          */
562                         KKASSERT(focus_pfs_type != HAMMER2_PFSTYPE_SOFT_SLAVE);
563                         if (focus_pfs_type != HAMMER2_PFSTYPE_SOFT_MASTER) {
564                                 cluster->focus_index = i;
565                                 cluster->focus = chain;
566                                 cluster->error = chain ? chain->error : 0;
567                                 focus_pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
568                         }
569                         break;
570                 case HAMMER2_PFSTYPE_SUPROOT:
571                         /*
572                          * spmp (degenerate case)
573                          */
574                         KKASSERT(i == 0);
575                         cluster->focus_index = i;
576                         cluster->focus = chain;
577                         cluster->error = chain ? chain->error : 0;
578                         focus_pfs_type = HAMMER2_PFSTYPE_SUPROOT;
579                         cluster->array[i].flags |= HAMMER2_CITEM_FEMOD;
580                         break;
581                 default:
582                         break;
583                 }
584         }
585
586         /*
587          * Focus now set, adjust ddflag.  Skip this pass if the focus
588          * is bad or if we are at the PFS root (the bref won't match at
589          * the PFS root, obviously).
590          */
591         focus = cluster->focus;
592         if (focus) {
593                 cluster->ddflag =
594                         (cluster->focus->bref.type == HAMMER2_BREF_TYPE_INODE);
595         } else {
596                 cluster->ddflag = 0;
597                 goto skip4;
598         }
599         if (cluster->focus->flags & HAMMER2_CHAIN_PFSBOUNDARY)
600                 goto skip4;
601
602         /*
603          * Pass 4
604          *
605          * Validate the elements that were not marked invalid.  They should
606          * match.
607          */
608         for (i = 0; i < cluster->nchains; ++i) {
609                 int ddflag;
610
611                 chain = cluster->array[i].chain;
612
613                 if (chain == NULL)
614                         continue;
615                 if (chain == focus)
616                         continue;
617                 if (cluster->array[i].flags & HAMMER2_CITEM_INVALID)
618                         continue;
619
620                 ddflag = (chain->bref.type == HAMMER2_BREF_TYPE_INODE);
621                 if (chain->bref.type != focus->bref.type ||
622                     chain->bref.key != focus->bref.key ||
623                     chain->bref.keybits != focus->bref.keybits ||
624                     chain->bref.modify_tid != focus->bref.modify_tid ||
625                     chain->bytes != focus->bytes ||
626                     ddflag != cluster->ddflag) {
627                         cluster->array[i].flags |= HAMMER2_CITEM_INVALID;
628                         if (hammer2_debug & 1)
629                         kprintf("cluster_resolve: matching modify_tid failed "
630                                 "bref test: idx=%d type=%02x/%02x "
631                                 "key=%016jx/%d-%016jx/%d "
632                                 "mod=%016jx/%016jx bytes=%u/%u\n",
633                                 i,
634                                 chain->bref.type, focus->bref.type,
635                                 chain->bref.key, chain->bref.keybits,
636                                 focus->bref.key, focus->bref.keybits,
637                                 chain->bref.modify_tid, focus->bref.modify_tid,
638                                 chain->bytes, focus->bytes);
639                         if (hammer2_debug & 0x4000)
640                                 panic("cluster_resolve");
641                         /* flag issue and force resync? */
642                 }
643         }
644 skip4:
645
646         if (ttlslaves == 0)
647                 nflags |= HAMMER2_CLUSTER_NOSOFT;
648         if (ttlmasters == 0)
649                 nflags |= HAMMER2_CLUSTER_NOHARD;
650
651         /*
652          * Set SSYNCED or MSYNCED for slaves and masters respectively if
653          * all available nodes (even if 0 are available) are fully
654          * synchronized.  This is used by the synchronization thread to
655          * determine if there is work it could potentially accomplish.
656          */
657         if (nslaves == ttlslaves)
658                 nflags |= HAMMER2_CLUSTER_SSYNCED;
659         if (nmasters == ttlmasters)
660                 nflags |= HAMMER2_CLUSTER_MSYNCED;
661
662         /*
663          * Determine if the cluster was successfully locked for the
664          * requested operation and generate an error code.  The cluster
665          * will not be locked (or ref'd) if an error is returned.
666          */
667         atomic_set_int(&cluster->flags, nflags);
668         atomic_clear_int(&cluster->flags, HAMMER2_CLUSTER_ZFLAGS & ~nflags);
669 }
670
671 /*
672  * This is used by the XOPS subsystem to calculate the state of
673  * the collection and tell hammer2_xop_collect() what to do with it.
674  * The collection can be in various states of desynchronization, the
675  * caller specifically wants to resolve the passed-in key.
676  *
677  * Return values:
678  *      0               - Quorum agreement, key is valid
679  *
680  *      ENOENT          - Quorum agreement, end of scan
681  *
682  *      ESRCH           - Quorum agreement, key is INVALID (caller should
683  *                        skip key).
684  *
685  *      EIO             - Quorum agreement but all elements had errors.
686  *
687  *      EDEADLK         - No quorum agreement possible for key, a repair
688  *                        may be needed.  Caller has to decide what to do,
689  *                        possibly iterating the key or generating an EIO.
690  *
691  *      EINPROGRESS     - No quorum agreement yet, but agreement is still
692  *                        possible if caller waits for more responses.  Caller
693  *                        should not iterate key.
694  *
695  * XXX needs to handle SOFT_MASTER and SOFT_SLAVE
696  */
697 int
698 hammer2_cluster_check(hammer2_cluster_t *cluster, hammer2_key_t key, int flags)
699 {
700         hammer2_chain_t *chain;
701         hammer2_chain_t *focus;
702         hammer2_pfs_t *pmp;
703         hammer2_tid_t quorum_tid;
704         hammer2_tid_t last_best_quorum_tid;
705         uint32_t nflags;
706         int ttlmasters;
707         int ttlslaves;
708         int nmasters;
709         int nmasters_keymatch;
710         int nslaves;
711         int nquorum;
712         int umasters;   /* unknown masters (still in progress) */
713         int smpresent;
714         int i;
715
716         cluster->error = 0;
717         cluster->focus = NULL;
718
719         nflags = 0;
720         ttlmasters = 0;
721         ttlslaves = 0;
722         nmasters = 0;
723         nmasters_keymatch = 0;
724         umasters = 0;
725         nslaves = 0;
726
727         /*
728          * Calculate quorum
729          */
730         pmp = cluster->pmp;
731         KKASSERT(pmp != NULL || cluster->nchains == 0);
732         nquorum = pmp ? pmp->pfs_nmasters / 2 + 1 : 0;
733         smpresent = 0;
734
735         /*
736          * Pass 1
737          *
738          * NOTE: A NULL chain is not necessarily an error, it could be
739          *       e.g. a lookup failure or the end of an iteration.
740          *       Process normally.
741          */
742         for (i = 0; i < cluster->nchains; ++i) {
743                 cluster->array[i].flags &= ~HAMMER2_CITEM_FEMOD;
744                 cluster->array[i].flags |= HAMMER2_CITEM_INVALID;
745
746                 chain = cluster->array[i].chain;
747                 if (chain && chain->error) {
748                         if (cluster->focus == NULL || cluster->focus == chain) {
749                                 /* error will be overridden by valid focus */
750                                 cluster->error = chain->error;
751                         }
752
753                         /*
754                          * Must count total masters and slaves whether the
755                          * chain is errored or not.
756                          */
757                         switch (cluster->pmp->pfs_types[i]) {
758                         case HAMMER2_PFSTYPE_MASTER:
759                                 ++ttlmasters;
760                                 break;
761                         case HAMMER2_PFSTYPE_SLAVE:
762                                 ++ttlslaves;
763                                 break;
764                         }
765                         continue;
766                 }
767                 switch (cluster->pmp->pfs_types[i]) {
768                 case HAMMER2_PFSTYPE_MASTER:
769                         ++ttlmasters;
770                         break;
771                 case HAMMER2_PFSTYPE_SLAVE:
772                         ++ttlslaves;
773                         break;
774                 case HAMMER2_PFSTYPE_SOFT_MASTER:
775                         nflags |= HAMMER2_CLUSTER_WRSOFT;
776                         nflags |= HAMMER2_CLUSTER_RDSOFT;
777                         smpresent = 1;
778                         break;
779                 case HAMMER2_PFSTYPE_SOFT_SLAVE:
780                         nflags |= HAMMER2_CLUSTER_RDSOFT;
781                         break;
782                 case HAMMER2_PFSTYPE_SUPROOT:
783                         /*
784                          * Degenerate cluster representing the super-root
785                          * topology on a single device.  Fake stuff so
786                          * cluster ops work as expected.
787                          */
788                         nflags |= HAMMER2_CLUSTER_WRHARD;
789                         nflags |= HAMMER2_CLUSTER_RDHARD;
790                         cluster->focus_index = i;
791                         cluster->focus = chain;
792                         cluster->error = chain ? chain->error : 0;
793                         break;
794                 default:
795                         break;
796                 }
797         }
798
799         /*
800          * Pass 2
801          *
802          * Resolve nmasters             - master nodes fully match
803          *
804          * Resolve umasters             - master nodes operation still
805          *                                in progress
806          *
807          * Resolve nmasters_keymatch    - master nodes match the passed-in
808          *                                key and may or may not match
809          *                                the quorum-agreed tid.
810          * 
811          * The quorum-agreed TID is the highest matching TID.
812          */
813         last_best_quorum_tid = HAMMER2_TID_MAX;
814         quorum_tid = 0;         /* fix gcc warning */
815
816         while (nmasters < nquorum && last_best_quorum_tid != 0) {
817                 nmasters = 0;
818                 quorum_tid = 0;
819
820                 for (i = 0; i < cluster->nchains; ++i) {
821                         /* XXX SOFT smpresent handling */
822                         if (cluster->pmp->pfs_types[i] !=
823                             HAMMER2_PFSTYPE_MASTER) {
824                                 continue;
825                         }
826
827                         chain = cluster->array[i].chain;
828
829                         /*
830                          * Skip elements still in progress.  umasters keeps
831                          * track of masters that might still be in-progress.
832                          */
833                         if (chain == NULL && (cluster->array[i].flags &
834                                               HAMMER2_CITEM_NULL) == 0) {
835                                 ++umasters;
836                                 continue;
837                         }
838
839                         /*
840                          * Key match?
841                          */
842                         if (flags & HAMMER2_CHECK_NULL) {
843                                 if (chain == NULL) {
844                                         ++nmasters;
845                                         ++nmasters_keymatch;
846                                 }
847                         } else if (chain &&
848                                    (key == (hammer2_key_t)-1 ||
849                                     chain->bref.key == key)) {
850                                 ++nmasters_keymatch;
851                                 if (quorum_tid < last_best_quorum_tid &&
852                                     (quorum_tid < chain->bref.modify_tid ||
853                                      nmasters == 0)) {
854                                         /*
855                                          * Better TID located, reset
856                                          * nmasters count.
857                                          */
858                                         nmasters = 0;
859                                         quorum_tid = chain->bref.modify_tid;
860                                 }
861                                 if (quorum_tid == chain->bref.modify_tid) {
862                                         /*
863                                          * TID matches current collection.
864                                          */
865                                         ++nmasters;
866                                         if (chain->error == 0) {
867                                                 cluster->focus = chain;
868                                                 cluster->focus_index = i;
869                                         }
870                                 }
871                         }
872                 }
873                 if (nmasters >= nquorum)
874                         break;
875                 last_best_quorum_tid = quorum_tid;
876         }
877
878         /*
879         kprintf("nmasters %d/%d nmaster_keymatch=%d umasters=%d\n",
880                 nmasters, nquorum, nmasters_keymatch, umasters);
881         */
882
883         /*
884          * Early return if we do not have enough masters.
885          */
886         if (nmasters < nquorum) {
887                 if (nmasters + umasters >= nquorum)
888                         return EINPROGRESS;
889                 if (nmasters_keymatch < nquorum) 
890                         return ESRCH;
891                 return EDEADLK;
892         }
893
894         /*
895          * Validated end of scan.
896          */
897         if (flags & HAMMER2_CHECK_NULL)
898                 return ENOENT;
899
900         /*
901          * If we have a NULL focus at this point the agreeing quorum all
902          * had chain errors.
903          */
904         if (cluster->focus == NULL)
905                 return EIO;
906
907         /*
908          * Pass 3
909          *
910          * We have quorum agreement, validate elements, not end of scan.
911          */
912         for (i = 0; i < cluster->nchains; ++i) {
913                 chain = cluster->array[i].chain;
914                 if (chain == NULL ||
915                     chain->bref.key != key ||
916                     chain->bref.modify_tid != quorum_tid) {
917                         continue;
918                 }
919
920                 switch (cluster->pmp->pfs_types[i]) {
921                 case HAMMER2_PFSTYPE_MASTER:
922                         cluster->array[i].flags |= HAMMER2_CITEM_FEMOD;
923                         cluster->array[i].flags &= ~HAMMER2_CITEM_INVALID;
924                         nflags |= HAMMER2_CLUSTER_WRHARD;
925                         nflags |= HAMMER2_CLUSTER_RDHARD;
926                         break;
927                 case HAMMER2_PFSTYPE_SLAVE:
928                         /*
929                          * We must have enough up-to-date masters to reach
930                          * a quorum and the slave modify_tid must match the
931                          * quorum's modify_tid.
932                          *
933                          * Do not select an errored slave.
934                          */
935                         cluster->array[i].flags &= ~HAMMER2_CITEM_INVALID;
936                         nflags |= HAMMER2_CLUSTER_RDHARD;
937                         ++nslaves;
938                         break;
939                 case HAMMER2_PFSTYPE_SOFT_MASTER:
940                         /*
941                          * Directly mounted soft master always wins.  There
942                          * should be only one.
943                          */
944                         cluster->array[i].flags |= HAMMER2_CITEM_FEMOD;
945                         cluster->array[i].flags &= ~HAMMER2_CITEM_INVALID;
946                         break;
947                 case HAMMER2_PFSTYPE_SOFT_SLAVE:
948                         /*
949                          * Directly mounted soft slave always wins.  There
950                          * should be only one.
951                          *
952                          * XXX
953                          */
954                         cluster->array[i].flags &= ~HAMMER2_CITEM_INVALID;
955                         break;
956                 case HAMMER2_PFSTYPE_SUPROOT:
957                         /*
958                          * spmp (degenerate case)
959                          */
960                         cluster->array[i].flags |= HAMMER2_CITEM_FEMOD;
961                         cluster->array[i].flags &= ~HAMMER2_CITEM_INVALID;
962                         break;
963                 default:
964                         break;
965                 }
966         }
967
968         /*
969          * Focus now set, adjust ddflag.  Skip this pass if the focus
970          * is bad or if we are at the PFS root (the bref won't match at
971          * the PFS root, obviously).
972          */
973         focus = cluster->focus;
974         if (focus) {
975                 cluster->ddflag =
976                         (cluster->focus->bref.type == HAMMER2_BREF_TYPE_INODE);
977         } else {
978                 cluster->ddflag = 0;
979                 goto skip4;
980         }
981         if (cluster->focus->flags & HAMMER2_CHAIN_PFSBOUNDARY)
982                 goto skip4;
983
984         /*
985          * Pass 4
986          *
987          * Validate the elements that were not marked invalid.  They should
988          * match.
989          */
990         for (i = 0; i < cluster->nchains; ++i) {
991                 int ddflag;
992
993                 chain = cluster->array[i].chain;
994
995                 if (chain == NULL)
996                         continue;
997                 if (chain == focus)
998                         continue;
999                 if (cluster->array[i].flags & HAMMER2_CITEM_INVALID)
1000                         continue;
1001
1002                 ddflag = (chain->bref.type == HAMMER2_BREF_TYPE_INODE);
1003                 if (chain->bref.type != focus->bref.type ||
1004                     chain->bref.key != focus->bref.key ||
1005                     chain->bref.keybits != focus->bref.keybits ||
1006                     chain->bref.modify_tid != focus->bref.modify_tid ||
1007                     chain->bytes != focus->bytes ||
1008                     ddflag != cluster->ddflag) {
1009                         cluster->array[i].flags |= HAMMER2_CITEM_INVALID;
1010                         if (hammer2_debug & 1)
1011                         kprintf("cluster_resolve: matching modify_tid failed "
1012                                 "bref test: idx=%d type=%02x/%02x "
1013                                 "key=%016jx/%d-%016jx/%d "
1014                                 "mod=%016jx/%016jx bytes=%u/%u\n",
1015                                 i,
1016                                 chain->bref.type, focus->bref.type,
1017                                 chain->bref.key, chain->bref.keybits,
1018                                 focus->bref.key, focus->bref.keybits,
1019                                 chain->bref.modify_tid, focus->bref.modify_tid,
1020                                 chain->bytes, focus->bytes);
1021                         if (hammer2_debug & 0x4000)
1022                                 panic("cluster_resolve");
1023                         /* flag issue and force resync? */
1024                 }
1025         }
1026 skip4:
1027
1028         if (ttlslaves == 0)
1029                 nflags |= HAMMER2_CLUSTER_NOSOFT;
1030         if (ttlmasters == 0)
1031                 nflags |= HAMMER2_CLUSTER_NOHARD;
1032
1033         /*
1034          * Set SSYNCED or MSYNCED for slaves and masters respectively if
1035          * all available nodes (even if 0 are available) are fully
1036          * synchronized.  This is used by the synchronization thread to
1037          * determine if there is work it could potentially accomplish.
1038          */
1039         if (nslaves == ttlslaves)
1040                 nflags |= HAMMER2_CLUSTER_SSYNCED;
1041         if (nmasters == ttlmasters)
1042                 nflags |= HAMMER2_CLUSTER_MSYNCED;
1043
1044         /*
1045          * Determine if the cluster was successfully locked for the
1046          * requested operation and generate an error code.  The cluster
1047          * will not be locked (or ref'd) if an error is returned.
1048          */
1049         atomic_set_int(&cluster->flags, nflags);
1050         atomic_clear_int(&cluster->flags, HAMMER2_CLUSTER_ZFLAGS & ~nflags);
1051
1052         return 0;
1053 }
1054
1055 /*
1056  * This is used by the sync thread to force non-NULL elements of a copy
1057  * of the pmp->iroot cluster to be good which is required to prime the
1058  * sync.
1059  */
1060 void
1061 hammer2_cluster_forcegood(hammer2_cluster_t *cluster)
1062 {
1063         int i;
1064
1065         for (i = 0; i < cluster->nchains; ++i) {
1066                 if (cluster->array[i].chain)
1067                         cluster->array[i].flags &= ~HAMMER2_CITEM_INVALID;
1068         }
1069 }
1070
1071 /*
1072  * Copy a cluster, returned a ref'd cluster.  All underlying chains
1073  * are also ref'd, but not locked.  Focus state is also copied.
1074  *
1075  * Original cluster does not have to be locked but usually is.
1076  * New cluster will not be flagged as locked.
1077  *
1078  * Callers using this function to initialize a new cluster from an inode
1079  * generally lock and resolve the resulting cluster.
1080  *
1081  * Callers which use this function to save/restore a cluster structure
1082  * generally retain the focus state and do not re-resolve it.  Caller should
1083  * not try to re-resolve internal (cparent) node state during an iteration
1084  * as the individual tracking elements of cparent in an iteration may not
1085  * match even though they are correct.
1086  */
1087 hammer2_cluster_t *
1088 hammer2_cluster_copy(hammer2_cluster_t *ocluster)
1089 {
1090         hammer2_pfs_t *pmp = ocluster->pmp;
1091         hammer2_cluster_t *ncluster;
1092         hammer2_chain_t *chain;
1093         int i;
1094
1095         ncluster = kmalloc(sizeof(*ncluster), M_HAMMER2, M_WAITOK | M_ZERO);
1096         ncluster->pmp = pmp;
1097         ncluster->nchains = ocluster->nchains;
1098         ncluster->refs = 1;
1099
1100         for (i = 0; i < ocluster->nchains; ++i) {
1101                 chain = ocluster->array[i].chain;
1102                 ncluster->array[i].chain = chain;
1103                 ncluster->array[i].flags = ocluster->array[i].flags;
1104                 if (chain)
1105                         hammer2_chain_ref(chain);
1106         }
1107         ncluster->focus_index = ocluster->focus_index;
1108         ncluster->focus = ocluster->focus;
1109         ncluster->flags = ocluster->flags & ~(HAMMER2_CLUSTER_LOCKED |
1110                                               HAMMER2_CLUSTER_INODE);
1111
1112         return (ncluster);
1113 }
1114
1115 /*
1116  * Unlock a cluster.  Refcount and focus is maintained.
1117  */
1118 void
1119 hammer2_cluster_unlock(hammer2_cluster_t *cluster)
1120 {
1121         hammer2_chain_t *chain;
1122         int i;
1123
1124         if ((cluster->flags & HAMMER2_CLUSTER_LOCKED) == 0) {
1125                 kprintf("hammer2_cluster_unlock: cluster %p not locked\n",
1126                         cluster);
1127         }
1128         KKASSERT(cluster->flags & HAMMER2_CLUSTER_LOCKED);
1129         KKASSERT(cluster->refs > 0);
1130         atomic_clear_int(&cluster->flags, HAMMER2_CLUSTER_LOCKED);
1131
1132         for (i = 0; i < cluster->nchains; ++i) {
1133                 chain = cluster->array[i].chain;
1134                 if (chain)
1135                         hammer2_chain_unlock(chain);
1136         }
1137 }
1138
1139 /************************************************************************
1140  *                              CLUSTER I/O                             *
1141  ************************************************************************
1142  *
1143  *
1144  * WARNING! blockref[] array data is not universal.  These functions should
1145  *          only be used to access universal data.
1146  *
1147  * NOTE!    The rdata call will wait for at least one of the chain I/Os to
1148  *          complete if necessary.  The I/O's should have already been
1149  *          initiated by the cluster_lock/chain_lock operation.
1150  *
1151  *          The cluster must already be in a modified state before wdata
1152  *          is called.  The data will already be available for this case.
1153  */
1154 const hammer2_media_data_t *
1155 hammer2_cluster_rdata(hammer2_cluster_t *cluster)
1156 {
1157         KKASSERT(cluster->focus != NULL);
1158         return(cluster->focus->data);
1159 }
1160
1161 hammer2_media_data_t *
1162 hammer2_cluster_wdata(hammer2_cluster_t *cluster)
1163 {
1164         KKASSERT(cluster->focus != NULL);
1165         KKASSERT(hammer2_cluster_modified(cluster));
1166         return(cluster->focus->data);
1167 }