2 * Copyright (c) 2013-2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * The cluster module collects multiple chains representing the same
36 * information into a single entity. It allows direct access to media
37 * data as long as it is not blockref array data. Meaning, basically,
38 * just inode and file data.
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.
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.
48 #include <sys/cdefs.h>
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/types.h>
58 hammer2_cluster_bytes(hammer2_cluster_t *cluster)
60 return(cluster->focus->bytes);
64 hammer2_cluster_type(hammer2_cluster_t *cluster)
66 return(cluster->focus->bref.type);
70 * NOTE: When modifying a cluster object via hammer2_cluster_wdata()
71 * and hammer2_cluster_modsync(), remember that block array
72 * entries are not copied to the elements of the cluster.
74 const hammer2_media_data_t *
75 hammer2_cluster_data(hammer2_cluster_t *cluster)
77 return(cluster->focus->data);
80 hammer2_media_data_t *
81 hammer2_cluster_wdata(hammer2_cluster_t *cluster)
83 return(cluster->focus->data);
87 hammer2_cluster_modified(hammer2_cluster_t *cluster)
89 return((cluster->focus->flags & HAMMER2_CHAIN_MODIFIED) != 0);
93 * Return a bref representative of the cluster. Any data offset is removed
94 * (since it would only be applicable to a particular chain in the cluster).
96 * However, the radix portion of data_off is used for many purposes and will
100 hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref)
102 *bref = cluster->focus->bref;
103 bref->data_off &= HAMMER2_OFF_MASK_RADIX;
107 hammer2_cluster_set_chainflags(hammer2_cluster_t *cluster, uint32_t flags)
109 hammer2_chain_t *chain;
112 for (i = 0; i < cluster->nchains; ++i) {
113 chain = cluster->array[i];
115 atomic_set_int(&chain->flags, flags);
120 hammer2_cluster_setflush(hammer2_trans_t *trans, hammer2_cluster_t *cluster)
122 hammer2_chain_t *chain;
125 for (i = 0; i < cluster->nchains; ++i) {
126 chain = cluster->array[i];
128 hammer2_chain_setflush(trans, chain);
133 * Create a cluster with one ref from the specified chain. The chain
134 * is not further referenced. The caller typically supplies a locked
135 * chain and transfers ownership to the cluster.
138 hammer2_cluster_from_chain(hammer2_chain_t *chain)
140 hammer2_cluster_t *cluster;
142 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
143 cluster->array[0] = chain;
144 cluster->nchains = 1;
145 cluster->focus = chain;
146 cluster->pmp = chain->pmp;
153 * Allocates a cluster and its underlying chain structures. The underlying
154 * chains will be locked. The cluster and underlying chains will have one
158 hammer2_cluster_alloc(hammer2_pfsmount_t *pmp,
159 hammer2_trans_t *trans, hammer2_blockref_t *bref)
161 hammer2_cluster_t *cluster;
162 hammer2_cluster_t *rcluster;
163 hammer2_chain_t *chain;
165 u_int bytes = 1U << (int)(bref->data_off & HAMMER2_OFF_MASK_RADIX);
169 KKASSERT(pmp != NULL);
172 * Construct the appropriate system structure.
175 case HAMMER2_BREF_TYPE_INODE:
176 case HAMMER2_BREF_TYPE_INDIRECT:
177 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
178 case HAMMER2_BREF_TYPE_DATA:
179 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
181 * Chain's are really only associated with the hmp but we
182 * maintain a pmp association for per-mount memory tracking
183 * purposes. The pmp can be NULL.
186 case HAMMER2_BREF_TYPE_VOLUME:
187 case HAMMER2_BREF_TYPE_FREEMAP:
189 panic("hammer2_cluster_alloc volume type illegal for op");
192 panic("hammer2_cluster_alloc: unrecognized blockref type: %d",
196 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
199 rcluster = &pmp->iroot->cluster;
200 for (i = 0; i < rcluster->nchains; ++i) {
201 chain = hammer2_chain_alloc(rcluster->array[i]->hmp,
204 chain->hmp = rcluster->array[i]->hmp;
206 chain->bytes = bytes;
208 chain->flags = HAMMER2_CHAIN_ALLOCATED;
212 * NOTE: When loading a chain from backing store or creating a
213 * snapshot, trans will be NULL and the caller is
214 * responsible for setting these fields.
216 cluster->array[i] = chain;
218 cluster->nchains = i;
220 cluster->focus = cluster->array[0];
226 * Add a reference to a cluster.
228 * We must also ref the underlying chains in order to allow ref/unlock
229 * sequences to later re-lock.
232 hammer2_cluster_ref(hammer2_cluster_t *cluster)
234 hammer2_chain_t *chain;
237 atomic_add_int(&cluster->refs, 1);
238 for (i = 0; i < cluster->nchains; ++i) {
239 chain = cluster->array[i];
241 hammer2_chain_ref(chain);
246 * Drop the caller's reference to the cluster. When the ref count drops to
247 * zero this function frees the cluster and drops all underlying chains.
250 hammer2_cluster_drop(hammer2_cluster_t *cluster)
252 hammer2_chain_t *chain;
255 KKASSERT(cluster->refs > 0);
256 for (i = 0; i < cluster->nchains; ++i) {
257 chain = cluster->array[i];
259 hammer2_chain_drop(chain);
260 if (cluster->refs == 1)
261 cluster->array[i] = NULL;
264 if (atomic_fetchadd_int(&cluster->refs, -1) == 1) {
265 cluster->focus = NULL;
266 kfree(cluster, M_HAMMER2);
267 /* cluster = NULL; safety */
272 hammer2_cluster_wait(hammer2_cluster_t *cluster)
274 tsleep(cluster->focus, 0, "h2clcw", 1);
278 * Lock and ref a cluster. This adds a ref to the cluster and its chains
279 * and then locks them.
282 hammer2_cluster_lock(hammer2_cluster_t *cluster, int how)
284 hammer2_chain_t *chain;
289 atomic_add_int(&cluster->refs, 1);
290 for (i = 0; i < cluster->nchains; ++i) {
291 chain = cluster->array[i];
293 error = hammer2_chain_lock(chain, how);
296 hammer2_chain_unlock(cluster->array[i]);
297 atomic_add_int(&cluster->refs, -1);
306 * Replace the contents of dst with src, adding a reference to src's chains.
307 * dst is assumed to already have a ref and any chains present in dst are
308 * assumed to be locked and will be unlocked.
310 * If the chains in src are locked, only one of (src) or (dst) should be
311 * considered locked by the caller after return, not both.
314 hammer2_cluster_replace(hammer2_cluster_t *dst, hammer2_cluster_t *src)
316 hammer2_chain_t *chain;
319 KKASSERT(dst->refs == 1);
322 for (i = 0; i < src->nchains; ++i) {
323 chain = src->array[i];
325 hammer2_chain_ref(chain);
326 if (i < dst->nchains && dst->array[i])
327 hammer2_chain_unlock(dst->array[i]);
328 dst->array[i] = chain;
329 if (dst->focus == NULL)
333 while (i < dst->nchains) {
334 chain = dst->array[i];
336 hammer2_chain_unlock(chain);
337 dst->array[i] = NULL;
341 dst->nchains = src->nchains;
345 * Replace the contents of the locked destination with the contents of the
346 * locked source. Destination must have one ref.
348 * Returns with the destination still with one ref and the copied chains
349 * with an additional lock (representing their state on the destination).
350 * The original chains associated with the destination are unlocked.
353 hammer2_cluster_replace_locked(hammer2_cluster_t *dst, hammer2_cluster_t *src)
355 hammer2_chain_t *chain;
358 KKASSERT(dst->refs == 1);
361 for (i = 0; i < src->nchains; ++i) {
362 chain = src->array[i];
364 hammer2_chain_lock(chain, 0);
365 if (i < dst->nchains && dst->array[i])
366 hammer2_chain_unlock(dst->array[i]);
367 dst->array[i] = src->array[i];
368 if (dst->focus == NULL)
372 while (i < dst->nchains) {
373 chain = dst->array[i];
375 hammer2_chain_unlock(chain);
376 dst->array[i] = NULL;
380 dst->nchains = src->nchains;
384 * Copy a cluster, returned a ref'd cluster. All underlying chains
385 * are also ref'd, but not locked.
387 * If HAMMER2_CLUSTER_COPY_CHAINS is specified, the chains are copied
388 * to the new cluster and a reference is nominally added to them and to
389 * the cluster. The cluster will have 1 ref.
391 * If HAMMER2_CLUSTER_COPY_NOREF is specified along with CHAINS, the chains
392 * are copied but no additional references are made and the cluster will have
393 * 0 refs. Callers must ref the cluster and the chains before dropping it
394 * (typically by locking it).
396 * If flags are passed as 0 the copy is setup as if it contained the chains
397 * but the chains will not be copied over, and the cluster will have 0 refs.
398 * Callers must ref the cluster before dropping it (typically by locking it).
401 hammer2_cluster_copy(hammer2_cluster_t *ocluster, int copy_flags)
403 hammer2_pfsmount_t *pmp = ocluster->pmp;
404 hammer2_cluster_t *ncluster;
405 hammer2_chain_t *chain;
408 ncluster = kmalloc(sizeof(*ncluster), M_HAMMER2, M_WAITOK | M_ZERO);
410 ncluster->nchains = ocluster->nchains;
411 ncluster->refs = (copy_flags & HAMMER2_CLUSTER_COPY_NOREF) ? 0 : 1;
412 if ((copy_flags & HAMMER2_CLUSTER_COPY_NOCHAINS) == 0) {
413 ncluster->focus = ocluster->focus;
414 for (i = 0; i < ocluster->nchains; ++i) {
415 chain = ocluster->array[i];
416 ncluster->array[i] = chain;
417 if ((copy_flags & HAMMER2_CLUSTER_COPY_NOREF) == 0 &&
419 hammer2_chain_ref(chain);
427 * Unlock and deref a cluster. The cluster is destroyed if this is the
431 hammer2_cluster_unlock(hammer2_cluster_t *cluster)
433 hammer2_chain_t *chain;
436 KKASSERT(cluster->refs > 0);
437 for (i = 0; i < cluster->nchains; ++i) {
438 chain = cluster->array[i];
440 hammer2_chain_unlock(chain);
441 if (cluster->refs == 1)
442 cluster->array[i] = NULL; /* safety */
445 if (atomic_fetchadd_int(&cluster->refs, -1) == 1) {
446 cluster->focus = NULL;
447 kfree(cluster, M_HAMMER2);
448 /* cluster = NULL; safety */
453 * Resize the cluster's physical storage allocation in-place. This may
454 * replace the cluster's chains.
457 hammer2_cluster_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
458 hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
459 int nradix, int flags)
463 KKASSERT(cparent->pmp == cluster->pmp); /* can be NULL */
464 KKASSERT(cparent->nchains == cluster->nchains);
466 cluster->focus = NULL;
467 for (i = 0; i < cluster->nchains; ++i) {
468 if (cluster->array[i]) {
469 KKASSERT(cparent->array[i]);
470 hammer2_chain_resize(trans, ip,
474 if (cluster->focus == NULL)
475 cluster->focus = cluster->array[i];
481 * Set an inode's cluster modified, marking the related chains RW and
482 * duplicating them if necessary.
484 * The passed-in chain is a localized copy of the chain previously acquired
485 * when the inode was locked (and possilby replaced in the mean time), and
486 * must also be updated. In fact, we update it first and then synchronize
487 * the inode's cluster cache.
489 hammer2_inode_data_t *
490 hammer2_cluster_modify_ip(hammer2_trans_t *trans, hammer2_inode_t *ip,
491 hammer2_cluster_t *cluster, int flags)
493 atomic_set_int(&ip->flags, HAMMER2_INODE_MODIFIED);
494 hammer2_cluster_modify(trans, cluster, flags);
496 hammer2_inode_repoint(ip, NULL, cluster);
499 return (&hammer2_cluster_wdata(cluster)->ipdata);
503 * Adjust the cluster's chains to allow modification.
506 hammer2_cluster_modify(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
511 cluster->focus = NULL;
512 for (i = 0; i < cluster->nchains; ++i) {
513 if (cluster->array[i]) {
514 hammer2_chain_modify(trans, cluster->array[i], flags);
515 if (cluster->focus == NULL)
516 cluster->focus = cluster->array[i];
522 * Synchronize modifications with other chains in a cluster.
524 * Nominal front-end operations only edit non-block-table data in a single
525 * chain. This code copies such modifications to the other chains in the
526 * cluster. Blocktable modifications are handled on a chain-by-chain basis
527 * by both the frontend and the backend and will explode in fireworks if
531 hammer2_cluster_modsync(hammer2_cluster_t *cluster)
533 hammer2_chain_t *focus;
534 hammer2_chain_t *scan;
535 const hammer2_inode_data_t *ripdata;
536 hammer2_inode_data_t *wipdata;
539 focus = cluster->focus;
540 KKASSERT(focus->flags & HAMMER2_CHAIN_MODIFIED);
542 for (i = 0; i < cluster->nchains; ++i) {
543 scan = cluster->array[i];
544 if (scan == NULL || scan == focus)
546 KKASSERT(scan->flags & HAMMER2_CHAIN_MODIFIED);
547 KKASSERT(focus->bytes == scan->bytes &&
548 focus->bref.type == scan->bref.type);
549 switch(focus->bref.type) {
550 case HAMMER2_BREF_TYPE_INODE:
551 ripdata = &focus->data->ipdata;
552 wipdata = &scan->data->ipdata;
553 if ((ripdata->op_flags &
554 HAMMER2_OPFLAG_DIRECTDATA) == 0) {
555 bcopy(ripdata, wipdata,
556 offsetof(hammer2_inode_data_t, u));
560 case HAMMER2_BREF_TYPE_DATA:
561 bcopy(focus->data, scan->data, focus->bytes);
563 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
564 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
565 case HAMMER2_BREF_TYPE_FREEMAP:
566 case HAMMER2_BREF_TYPE_VOLUME:
567 panic("hammer2_cluster_modsync: illegal node type");
571 panic("hammer2_cluster_modsync: unknown node type");
578 * Lookup initialization/completion API
581 hammer2_cluster_lookup_init(hammer2_cluster_t *cparent, int flags)
583 hammer2_cluster_t *cluster;
586 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
587 cluster->pmp = cparent->pmp; /* can be NULL */
588 /* cluster->focus = NULL; already null */
590 for (i = 0; i < cparent->nchains; ++i) {
591 cluster->array[i] = cparent->array[i];
592 if (cluster->focus == NULL)
593 cluster->focus = cluster->array[i];
595 cluster->nchains = cparent->nchains;
598 * Independently lock (this will also give cluster 1 ref)
600 if (flags & HAMMER2_LOOKUP_SHARED) {
601 hammer2_cluster_lock(cluster, HAMMER2_RESOLVE_ALWAYS |
602 HAMMER2_RESOLVE_SHARED);
604 hammer2_cluster_lock(cluster, HAMMER2_RESOLVE_ALWAYS);
610 hammer2_cluster_lookup_done(hammer2_cluster_t *cparent)
613 hammer2_cluster_unlock(cparent);
617 * Locate first match or overlap under parent, return a new cluster
620 hammer2_cluster_lookup(hammer2_cluster_t *cparent, hammer2_key_t *key_nextp,
621 hammer2_key_t key_beg, hammer2_key_t key_end,
622 int flags, int *ddflagp)
624 hammer2_pfsmount_t *pmp;
625 hammer2_cluster_t *cluster;
626 hammer2_chain_t *chain;
627 hammer2_key_t key_accum;
628 hammer2_key_t key_next;
629 hammer2_key_t bref_key;
637 pmp = cparent->pmp; /* can be NULL */
638 key_accum = *key_nextp;
645 cluster = kmalloc(sizeof(*cluster), M_HAMMER2, M_WAITOK | M_ZERO);
646 cluster->pmp = pmp; /* can be NULL */
648 /* cluster->focus = NULL; already null */
649 cparent->focus = NULL;
652 for (i = 0; i < cparent->nchains; ++i) {
653 key_next = *key_nextp;
654 if (cparent->array[i] == NULL) {
658 chain = hammer2_chain_lookup(&cparent->array[i], &key_next,
660 &cparent->cache_index[i],
662 if (cparent->focus == NULL)
663 cparent->focus = cparent->array[i];
664 cluster->array[i] = chain;
668 if (cluster->focus == NULL) {
669 bref_type = chain->bref.type;
670 bref_key = chain->bref.key;
671 bref_keybits = chain->bref.keybits;
672 bytes = chain->bytes;
674 cluster->focus = chain;
676 KKASSERT(bref_type == chain->bref.type);
677 KKASSERT(bref_key == chain->bref.key);
678 KKASSERT(bref_keybits == chain->bref.keybits);
679 KKASSERT(bytes == chain->bytes);
680 KKASSERT(*ddflagp == ddflag);
682 if (key_accum > key_next)
683 key_accum = key_next;
685 *key_nextp = key_accum;
686 cluster->nchains = i;
688 if (null_count == i) {
689 hammer2_cluster_drop(cluster);
697 * Locate next match or overlap under parent, replace cluster
700 hammer2_cluster_next(hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
701 hammer2_key_t *key_nextp,
702 hammer2_key_t key_beg, hammer2_key_t key_end, int flags)
704 hammer2_chain_t *chain;
705 hammer2_key_t key_accum;
706 hammer2_key_t key_next;
710 key_accum = *key_nextp;
712 cluster->focus = NULL;
713 cparent->focus = NULL;
715 for (i = 0; i < cparent->nchains; ++i) {
716 key_next = *key_nextp;
717 chain = cluster->array[i];
719 if (cparent->focus == NULL)
720 cparent->focus = cparent->array[i];
724 if (cparent->array[i] == NULL) {
725 if (flags & HAMMER2_LOOKUP_NOLOCK)
726 hammer2_chain_drop(chain);
728 hammer2_chain_unlock(chain);
732 chain = hammer2_chain_next(&cparent->array[i], chain,
733 &key_next, key_beg, key_end,
734 &cparent->cache_index[i], flags);
735 if (cparent->focus == NULL)
736 cparent->focus = cparent->array[i];
737 cluster->array[i] = chain;
740 } else if (cluster->focus == NULL) {
741 cluster->focus = chain;
743 if (key_accum > key_next)
744 key_accum = key_next;
747 if (null_count == i) {
748 hammer2_cluster_drop(cluster);
756 * XXX initial NULL cluster needs reworking (pass **clusterp ?)
758 * The raw scan function is similar to lookup/next but does not seek to a key.
759 * Blockrefs are iterated via first_chain = (parent, NULL) and
760 * next_chain = (parent, chain).
762 * The passed-in parent must be locked and its data resolved. The returned
763 * chain will be locked. Pass chain == NULL to acquire the first sub-chain
764 * under parent and then iterate with the passed-in chain (which this
765 * function will unlock).
768 hammer2_cluster_scan(hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
771 hammer2_chain_t *chain;
777 for (i = 0; i < cparent->nchains; ++i) {
778 chain = cluster->array[i];
783 if (cparent->array[i] == NULL) {
784 if (flags & HAMMER2_LOOKUP_NOLOCK)
785 hammer2_chain_drop(chain);
787 hammer2_chain_unlock(chain);
792 chain = hammer2_chain_scan(cparent->array[i], chain,
793 &cparent->cache_index[i], flags);
794 cluster->array[i] = chain;
799 if (null_count == i) {
800 hammer2_cluster_drop(cluster);
809 * Create a new cluster using the specified key
812 hammer2_cluster_create(hammer2_trans_t *trans, hammer2_cluster_t *cparent,
813 hammer2_cluster_t **clusterp,
814 hammer2_key_t key, int keybits, int type, size_t bytes)
816 hammer2_cluster_t *cluster;
817 hammer2_pfsmount_t *pmp;
821 pmp = trans->pmp; /* can be NULL */
823 if ((cluster = *clusterp) == NULL) {
824 cluster = kmalloc(sizeof(*cluster), M_HAMMER2,
826 cluster->pmp = pmp; /* can be NULL */
829 cluster->focus = NULL;
830 cparent->focus = NULL;
833 * NOTE: cluster->array[] entries can initially be NULL. If
834 * *clusterp is supplied, skip NULL entries, otherwise
837 for (i = 0; i < cparent->nchains; ++i) {
838 if (*clusterp && cluster->array[i] == NULL) {
839 if (cparent->focus == NULL)
840 cparent->focus = cparent->array[i];
843 error = hammer2_chain_create(trans, &cparent->array[i],
844 &cluster->array[i], pmp,
845 key, keybits, type, bytes);
846 KKASSERT(error == 0);
847 if (cparent->focus == NULL)
848 cparent->focus = cparent->array[i];
849 if (cluster->focus == NULL)
850 cluster->focus = cluster->array[i];
852 cluster->nchains = i;
859 * Rename a cluster to a new parent.
861 * WARNING! Unlike hammer2_chain_rename(), only the key and keybits fields
862 * are used from a passed-in non-NULL bref pointer. All other fields
863 * are extracted from the original chain for each chain in the
867 hammer2_cluster_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
868 hammer2_cluster_t *cparent, hammer2_cluster_t *cluster)
870 hammer2_chain_t *chain;
871 hammer2_blockref_t xbref;
874 cluster->focus = NULL;
875 cparent->focus = NULL;
877 for (i = 0; i < cluster->nchains; ++i) {
878 chain = cluster->array[i];
882 xbref.key = bref->key;
883 xbref.keybits = bref->keybits;
884 hammer2_chain_rename(trans, &xbref,
888 hammer2_chain_rename(trans, NULL,
892 cluster->array[i] = chain;
893 if (cluster->focus == NULL)
894 cluster->focus = chain;
895 if (cparent->focus == NULL)
896 cparent->focus = cparent->array[i];
898 if (cparent->focus == NULL)
899 cparent->focus = cparent->array[i];
905 * Mark a cluster deleted
908 hammer2_cluster_delete(hammer2_trans_t *trans, hammer2_cluster_t *cparent,
909 hammer2_cluster_t *cluster, int flags)
911 hammer2_chain_t *chain;
912 hammer2_chain_t *parent;
915 if (cparent == NULL) {
916 kprintf("cparent is NULL\n");
920 for (i = 0; i < cluster->nchains; ++i) {
921 parent = (i < cparent->nchains) ? cparent->array[i] : NULL;
922 chain = cluster->array[i];
925 if (chain->parent != parent) {
926 kprintf("hammer2_cluster_delete: parent "
927 "mismatch chain=%p parent=%p against=%p\n",
928 chain, chain->parent, parent);
930 hammer2_chain_delete(trans, parent, chain, flags);
936 * Create a snapshot of the specified {parent, ochain} with the specified
937 * label. The originating hammer2_inode must be exclusively locked for
940 * The ioctl code has already synced the filesystem.
943 hammer2_cluster_snapshot(hammer2_trans_t *trans, hammer2_cluster_t *ocluster,
944 hammer2_ioc_pfs_t *pfs)
946 hammer2_mount_t *hmp;
947 hammer2_cluster_t *ncluster;
948 const hammer2_inode_data_t *ipdata;
949 hammer2_inode_data_t *wipdata;
950 hammer2_inode_t *nip;
957 kprintf("snapshot %s\n", pfs->name);
959 name_len = strlen(pfs->name);
960 lhc = hammer2_dirhash(pfs->name, name_len);
962 ipdata = &hammer2_cluster_data(ocluster)->ipdata;
963 opfs_clid = ipdata->pfs_clid;
964 hmp = ocluster->focus->hmp;
967 * Create the snapshot directory under the super-root
969 * Set PFS type, generate a unique filesystem id, and generate
970 * a cluster id. Use the same clid when snapshotting a PFS root,
971 * which theoretically allows the snapshot to be used as part of
972 * the same cluster (perhaps as a cache).
974 * Copy the (flushed) blockref array. Theoretically we could use
975 * chain_duplicate() but it becomes difficult to disentangle
976 * the shared core so for now just brute-force it.
982 nip = hammer2_inode_create(trans, hmp->spmp->iroot, &vat,
983 proc0.p_ucred, pfs->name, name_len,
987 wipdata = hammer2_cluster_modify_ip(trans, nip, ncluster, 0);
988 wipdata->pfs_type = HAMMER2_PFSTYPE_SNAPSHOT;
989 kern_uuidgen(&wipdata->pfs_fsid, 1);
990 if (ocluster->focus->flags & HAMMER2_CHAIN_PFSROOT)
991 wipdata->pfs_clid = opfs_clid;
993 kern_uuidgen(&wipdata->pfs_clid, 1);
994 hammer2_cluster_set_chainflags(ncluster, HAMMER2_CHAIN_PFSROOT);
996 /* XXX hack blockset copy */
997 /* XXX doesn't work with real cluster */
998 KKASSERT(ocluster->nchains == 1);
999 wipdata->u.blockset = ocluster->focus->data->ipdata.u.blockset;
1000 hammer2_cluster_modsync(ncluster);
1001 hammer2_inode_unlock_ex(nip, ncluster);
1007 * Return locked parent cluster given a locked child. The child remains
1011 hammer2_cluster_parent(hammer2_cluster_t *cluster)
1013 hammer2_cluster_t *cparent;
1016 cparent = hammer2_cluster_copy(cluster, HAMMER2_CLUSTER_COPY_NOCHAINS);
1017 for (i = 0; i < cluster->nchains; ++i) {
1018 hammer2_chain_t *chain;
1019 hammer2_chain_t *rchain;
1021 chain = cluster->array[i];
1024 hammer2_chain_ref(chain);
1025 while ((rchain = chain->parent) != NULL) {
1026 hammer2_chain_ref(rchain);
1027 hammer2_chain_unlock(chain);
1028 hammer2_chain_lock(rchain, HAMMER2_RESOLVE_ALWAYS);
1029 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
1030 hammer2_chain_drop(rchain);
1031 if (chain->parent == rchain)
1033 hammer2_chain_unlock(rchain);
1035 hammer2_chain_drop(chain);
1036 cparent->array[i] = rchain;
1041 /************************************************************************
1043 ************************************************************************
1045 * A node failure can occur for numerous reasons.
1047 * - A read I/O may fail
1048 * - A write I/O may fail
1049 * - An unexpected chain might be found (or be missing)
1050 * - A node might disconnect temporarily and reconnect later
1051 * (for example, a USB stick could get pulled, or a node might
1052 * be programmatically disconnected).
1053 * - A node might run out of space during a modifying operation.
1055 * When a read failure or an unexpected chain state is found, the chain and
1056 * parent chain at the failure point for the nodes involved (the nodes
1057 * which we determine to be in error) are flagged as failed and removed
1058 * from the cluster. The node itself is allowed to remain active. The
1059 * highest common point (usually a parent chain) is queued to the
1060 * resynchronization thread for action.
1062 * When a write I/O fails or a node runs out of space, we first adjust
1063 * as if a read failure occurs but we further disable flushes on the
1064 * ENTIRE node. Concurrent modifying transactions are allowed to complete
1065 * but any new modifying transactions will automatically remove the node
1066 * from consideration in all related cluster structures and not generate
1067 * any new modified chains. The ROOT chain for the failed node(s) is queued
1068 * to the resynchronization thread for action.
1070 * A temporary disconnect is handled as if a write failure occurred.
1072 * Any of these failures might or might not stall related high level VNOPS,
1073 * depending on what has failed, what nodes remain, the type of cluster,
1074 * and the operating state of the cluster.
1076 * FLUSH ON WRITE-DISABLED NODES
1078 * A flush on a write-disabled node is not allowed to write anything because
1079 * we cannot safely update the mirror_tid anywhere on the failed node. The
1080 * synchronization thread uses mirror_tid to calculate incremental resyncs.
1081 * Dirty meta-data related to the failed node is thrown away.
1083 * Dirty buffer cache buffers and inodes are only thrown away if they can be
1084 * retired... that is, if the filesystem still has enough nodes to complete
1088 /************************************************************************
1089 * SYNCHRONIZATION THREAD *
1090 ************************************************************************
1092 * This thread is responsible for [re]synchronizing the cluster representing
1093 * a PFS. Any out-of-sync or failed node starts this thread on a
1094 * node-by-node basis when the failure is detected.
1096 * Clusters needing resynchronization are queued at the highest point
1097 * where the parent on the failed node is still valid, or a special
1098 * incremental scan from the ROOT is queued if no parent exists. This
1099 * thread is also responsible for waiting for reconnections of the failed
1100 * node if the cause was due to a disconnect, and waiting for space to be
1101 * freed up if the cause was due to running out of space.
1103 * If the cause is due to a node running out of space, this thread will also
1104 * remove older (unlocked) snapshots to make new space, recover space, and
1105 * then start resynchronization.
1107 * Each resynchronization pass virtually snapshots the PFS on the good nodes
1108 * and synchronizes using that snapshot against the target node. This
1109 * ensures a consistent chain topology and also avoid interference between
1110 * the resynchronization thread and frontend operations.
1112 * Since these are per-node threads it is possible to resynchronize several