2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_ondisk.c,v 1.25 2008/01/25 05:49:08 dillon Exp $
37 * Manage HAMMER's on-disk structures. These routines are primarily
38 * responsible for interfacing with the kernel's I/O subsystem and for
39 * managing in-memory structures.
43 #include <sys/fcntl.h>
44 #include <sys/nlookup.h>
48 static void hammer_free_volume(hammer_volume_t volume);
49 static int hammer_load_volume(hammer_volume_t volume);
50 static int hammer_load_supercl(hammer_supercl_t supercl,
51 hammer_alloc_state_t isnew);
52 static int hammer_load_cluster(hammer_cluster_t cluster, int getflags);
53 static int hammer_load_buffer(hammer_buffer_t buffer, u_int64_t buf_type);
54 static int hammer_load_node(hammer_node_t node);
55 static void alloc_new_buffer(hammer_cluster_t cluster, u_int64_t type,
57 int32_t start, int *errorp,
58 struct hammer_buffer **bufferp);
60 static void readhammerbuf(hammer_volume_t vol, void *data,
62 static void writehammerbuf(hammer_volume_t vol, const void *data,
65 static int64_t calculate_cluster_offset(hammer_volume_t vol, int32_t clu_no);
66 static int64_t calculate_supercl_offset(hammer_volume_t vol, int32_t scl_no);
67 static int32_t hammer_alloc_master(hammer_cluster_t cluster, int nblks,
68 int32_t start, int isfwd);
69 static void hammer_adjust_stats(hammer_cluster_t cluster,
70 u_int64_t buf_type, int nblks);
72 struct hammer_alist_config Buf_alist_config;
73 struct hammer_alist_config Vol_normal_alist_config;
74 struct hammer_alist_config Vol_super_alist_config;
75 struct hammer_alist_config Supercl_alist_config;
76 struct hammer_alist_config Clu_master_alist_config;
77 struct hammer_alist_config Clu_slave_alist_config;
80 * Red-Black tree support for various structures
83 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
85 if (ip1->obj_id < ip2->obj_id)
87 if (ip1->obj_id > ip2->obj_id)
89 if (ip1->obj_asof < ip2->obj_asof)
91 if (ip1->obj_asof > ip2->obj_asof)
97 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
99 if (info->obj_id < ip->obj_id)
101 if (info->obj_id > ip->obj_id)
103 if (info->obj_asof < ip->obj_asof)
105 if (info->obj_asof > ip->obj_asof)
111 hammer_vol_rb_compare(hammer_volume_t vol1, hammer_volume_t vol2)
113 if (vol1->vol_no < vol2->vol_no)
115 if (vol1->vol_no > vol2->vol_no)
121 hammer_scl_rb_compare(hammer_supercl_t cl1, hammer_supercl_t cl2)
123 if (cl1->scl_no < cl2->scl_no)
125 if (cl1->scl_no > cl2->scl_no)
131 hammer_clu_rb_compare(hammer_cluster_t cl1, hammer_cluster_t cl2)
133 if (cl1->clu_no < cl2->clu_no)
135 if (cl1->clu_no > cl2->clu_no)
141 hammer_buf_rb_compare(hammer_buffer_t buf1, hammer_buffer_t buf2)
143 if (buf1->buf_no < buf2->buf_no)
145 if (buf1->buf_no > buf2->buf_no)
151 hammer_nod_rb_compare(hammer_node_t node1, hammer_node_t node2)
153 if (node1->node_offset < node2->node_offset)
155 if (node1->node_offset > node2->node_offset)
161 * Note: The lookup function for hammer_ino_rb_tree winds up being named
162 * hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info). The other lookup
163 * functions are normal, e.g. hammer_clu_rb_tree_RB_LOOKUP(root, clu_no).
165 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
166 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
167 hammer_inode_info_cmp, hammer_inode_info_t);
168 RB_GENERATE2(hammer_vol_rb_tree, hammer_volume, rb_node,
169 hammer_vol_rb_compare, int32_t, vol_no);
170 RB_GENERATE2(hammer_scl_rb_tree, hammer_supercl, rb_node,
171 hammer_scl_rb_compare, int32_t, scl_no);
172 RB_GENERATE2(hammer_clu_rb_tree, hammer_cluster, rb_node,
173 hammer_clu_rb_compare, int32_t, clu_no);
174 RB_GENERATE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
175 hammer_buf_rb_compare, int32_t, buf_no);
176 RB_GENERATE2(hammer_nod_rb_tree, hammer_node, rb_node,
177 hammer_nod_rb_compare, int32_t, node_offset);
179 /************************************************************************
181 ************************************************************************
183 * Load a HAMMER volume by name. Returns 0 on success or a positive error
184 * code on failure. Volumes must be loaded at mount time, get_volume() will
185 * not load a new volume.
187 * Calls made to hammer_load_volume() or single-threaded
190 hammer_install_volume(struct hammer_mount *hmp, const char *volname)
193 hammer_volume_t volume;
194 struct hammer_volume_ondisk *ondisk;
195 struct nlookupdata nd;
196 struct buf *bp = NULL;
201 ronly = ((mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
204 * Allocate a volume structure
206 ++hammer_count_volumes;
207 volume = kmalloc(sizeof(*volume), M_HAMMER, M_WAITOK|M_ZERO);
208 volume->vol_name = kstrdup(volname, M_HAMMER);
210 hammer_io_init(&volume->io, HAMMER_STRUCTURE_VOLUME);
211 volume->io.offset = 0LL;
214 * Get the device vnode
216 error = nlookup_init(&nd, volume->vol_name, UIO_SYSSPACE, NLC_FOLLOW);
218 error = nlookup(&nd);
220 error = cache_vref(&nd.nl_nch, nd.nl_cred, &volume->devvp);
223 vn_isdisk(volume->devvp, &error);
226 vn_lock(volume->devvp, LK_EXCLUSIVE | LK_RETRY);
227 error = VOP_OPEN(volume->devvp, (ronly ? FREAD : FREAD|FWRITE),
229 vn_unlock(volume->devvp);
232 hammer_free_volume(volume);
237 * Extract the volume number from the volume header and do various
240 error = bread(volume->devvp, 0LL, HAMMER_BUFSIZE, &bp);
243 ondisk = (void *)bp->b_data;
244 if (ondisk->head.buf_type != HAMMER_FSBUF_VOLUME) {
245 kprintf("hammer_mount: volume %s has an invalid header\n",
250 volume->vol_no = ondisk->vol_no;
251 volume->cluster_base = ondisk->vol_clo_beg;
252 volume->vol_clsize = ondisk->vol_clsize;
253 volume->vol_flags = ondisk->vol_flags;
254 volume->nblocks = ondisk->vol_nblocks;
255 RB_INIT(&volume->rb_clus_root);
256 RB_INIT(&volume->rb_scls_root);
258 hmp->mp->mnt_stat.f_blocks += volume->nblocks;
260 if (RB_EMPTY(&hmp->rb_vols_root)) {
261 hmp->fsid = ondisk->vol_fsid;
262 } else if (bcmp(&hmp->fsid, &ondisk->vol_fsid, sizeof(uuid_t))) {
263 kprintf("hammer_mount: volume %s's fsid does not match "
264 "other volumes\n", volume->vol_name);
270 * Insert the volume structure into the red-black tree.
272 if (RB_INSERT(hammer_vol_rb_tree, &hmp->rb_vols_root, volume)) {
273 kprintf("hammer_mount: volume %s has a duplicate vol_no %d\n",
274 volume->vol_name, volume->vol_no);
279 * Set the root volume and load the root cluster. HAMMER special
280 * cases rootvol and rootcl and will not deallocate the structures.
281 * We do not hold a ref because this would prevent related I/O
282 * from being flushed.
284 if (error == 0 && ondisk->vol_rootvol == ondisk->vol_no) {
285 hmp->rootvol = volume;
290 hammer_ref_volume(volume);
291 hmp->rootcl = hammer_get_cluster(volume,
292 ondisk->vol0_root_clu_no,
293 &error, GET_CLUSTER_NORECOVER);
294 hammer_rel_cluster(hmp->rootcl, 0);
295 hammer_rel_volume(volume, 0);
296 hmp->fsid_udev = dev2udev(vn_todev(volume->devvp));
302 /*vinvalbuf(volume->devvp, V_SAVE, 0, 0);*/
303 VOP_CLOSE(volume->devvp, ronly ? FREAD : FREAD|FWRITE);
304 hammer_free_volume(volume);
310 * Unload and free a HAMMER volume. Must return >= 0 to continue scan
311 * so returns -1 on failure.
314 hammer_unload_volume(hammer_volume_t volume, void *data __unused)
316 struct hammer_mount *hmp = volume->hmp;
317 hammer_cluster_t rootcl;
318 int ronly = ((hmp->mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
321 * Sync clusters, sync volume
324 hmp->mp->mnt_stat.f_blocks -= volume->nblocks;
327 * Clean up the root cluster, which is held unlocked in the root
330 if (hmp->rootvol == volume) {
331 if ((rootcl = hmp->rootcl) != NULL)
337 * Unload clusters and super-clusters. Unloading a super-cluster
338 * also unloads related clusters, but the filesystem may not be
339 * using super-clusters so unload clusters anyway.
341 RB_SCAN(hammer_clu_rb_tree, &volume->rb_clus_root, NULL,
342 hammer_unload_cluster, NULL);
343 RB_SCAN(hammer_scl_rb_tree, &volume->rb_scls_root, NULL,
344 hammer_unload_supercl, NULL);
345 hammer_io_waitdep(&volume->io);
348 * Release our buffer and flush anything left in the buffer cache.
350 hammer_io_release(&volume->io, 2);
353 * There should be no references on the volume, no clusters, and
356 KKASSERT(volume->io.lock.refs == 0);
357 KKASSERT(RB_EMPTY(&volume->rb_clus_root));
358 KKASSERT(RB_EMPTY(&volume->rb_scls_root));
360 volume->ondisk = NULL;
363 vinvalbuf(volume->devvp, 0, 0, 0);
364 VOP_CLOSE(volume->devvp, FREAD);
366 vinvalbuf(volume->devvp, V_SAVE, 0, 0);
367 VOP_CLOSE(volume->devvp, FREAD|FWRITE);
372 * Destroy the structure
374 RB_REMOVE(hammer_vol_rb_tree, &hmp->rb_vols_root, volume);
375 hammer_free_volume(volume);
381 hammer_free_volume(hammer_volume_t volume)
383 if (volume->vol_name) {
384 kfree(volume->vol_name, M_HAMMER);
385 volume->vol_name = NULL;
388 vrele(volume->devvp);
389 volume->devvp = NULL;
391 --hammer_count_volumes;
392 kfree(volume, M_HAMMER);
396 * Get a HAMMER volume. The volume must already exist.
399 hammer_get_volume(struct hammer_mount *hmp, int32_t vol_no, int *errorp)
401 struct hammer_volume *volume;
404 * Locate the volume structure
406 volume = RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, vol_no);
407 if (volume == NULL) {
411 hammer_ref(&volume->io.lock);
414 * Deal with on-disk info
416 if (volume->ondisk == NULL || volume->io.loading) {
417 *errorp = hammer_load_volume(volume);
419 hammer_rel_volume(volume, 1);
429 hammer_ref_volume(hammer_volume_t volume)
433 hammer_ref(&volume->io.lock);
436 * Deal with on-disk info
438 if (volume->ondisk == NULL || volume->io.loading) {
439 error = hammer_load_volume(volume);
441 hammer_rel_volume(volume, 1);
449 hammer_get_root_volume(struct hammer_mount *hmp, int *errorp)
451 hammer_volume_t volume;
453 volume = hmp->rootvol;
454 KKASSERT(volume != NULL);
455 hammer_ref(&volume->io.lock);
458 * Deal with on-disk info
460 if (volume->ondisk == NULL || volume->io.loading) {
461 *errorp = hammer_load_volume(volume);
463 hammer_rel_volume(volume, 1);
473 * Load a volume's on-disk information. The volume must be referenced and
474 * not locked. We temporarily acquire an exclusive lock to interlock
475 * against releases or multiple get's.
478 hammer_load_volume(hammer_volume_t volume)
480 struct hammer_volume_ondisk *ondisk;
483 hammer_lock_ex(&volume->io.lock);
484 KKASSERT(volume->io.loading == 0);
485 volume->io.loading = 1;
487 if (volume->ondisk == NULL) {
488 error = hammer_io_read(volume->devvp, &volume->io);
490 volume->io.loading = 0;
491 hammer_unlock(&volume->io.lock);
494 volume->ondisk = ondisk = (void *)volume->io.bp->b_data;
497 * Configure the volume's A-lists. These are used to
500 if (volume->vol_flags & HAMMER_VOLF_USINGSUPERCL) {
501 volume->alist.config = &Vol_super_alist_config;
502 volume->alist.meta = ondisk->vol_almeta.super;
503 volume->alist.info = volume;
505 volume->alist.config = &Vol_normal_alist_config;
506 volume->alist.meta = ondisk->vol_almeta.normal;
507 volume->alist.info = NULL;
512 volume->io.loading = 0;
513 hammer_unlock(&volume->io.lock);
518 * Release a volume. Call hammer_io_release on the last reference. We have
519 * to acquire an exclusive lock to interlock against volume->ondisk tests
520 * in hammer_load_volume(), and hammer_io_release() also expects an exclusive
523 * Volumes are not unloaded from memory during normal operation.
526 hammer_rel_volume(hammer_volume_t volume, int flush)
528 if (volume->io.lock.refs == 1) {
529 hammer_lock_ex(&volume->io.lock);
530 if (volume->io.lock.refs == 1) {
531 volume->ondisk = NULL;
532 hammer_io_release(&volume->io, flush);
534 hammer_io_flush(&volume->io);
536 hammer_unlock(&volume->io.lock);
538 hammer_unref(&volume->io.lock);
541 /************************************************************************
543 ************************************************************************
545 * Manage super-clusters. Note that a supercl holds a reference to its
549 hammer_find_supercl(hammer_volume_t volume, int32_t scl_no)
551 if (RB_LOOKUP(hammer_scl_rb_tree, &volume->rb_scls_root, scl_no))
557 hammer_get_supercl(hammer_volume_t volume, int32_t scl_no,
558 int *errorp, hammer_alloc_state_t isnew)
560 hammer_supercl_t supercl;
563 * Locate and lock the super-cluster structure, creating one
567 supercl = RB_LOOKUP(hammer_scl_rb_tree, &volume->rb_scls_root, scl_no);
568 if (supercl == NULL) {
569 ++hammer_count_supercls;
570 supercl = kmalloc(sizeof(*supercl), M_HAMMER, M_WAITOK|M_ZERO);
571 supercl->scl_no = scl_no;
572 supercl->volume = volume;
573 supercl->io.offset = calculate_supercl_offset(volume, scl_no);
574 hammer_io_init(&supercl->io, HAMMER_STRUCTURE_SUPERCL);
575 hammer_ref(&supercl->io.lock);
578 * Insert the cluster into the RB tree and handle late
581 if (RB_INSERT(hammer_scl_rb_tree, &volume->rb_scls_root, supercl)) {
582 hammer_unref(&supercl->io.lock);
583 --hammer_count_supercls;
584 kfree(supercl, M_HAMMER);
587 hammer_ref(&volume->io.lock);
589 hammer_ref(&supercl->io.lock);
593 * Deal with on-disk info
595 if (supercl->ondisk == NULL || isnew || supercl->io.loading) {
596 *errorp = hammer_load_supercl(supercl, isnew);
598 hammer_rel_supercl(supercl, 1);
608 hammer_load_supercl(hammer_supercl_t supercl, hammer_alloc_state_t isnew)
610 struct hammer_supercl_ondisk *ondisk;
611 hammer_volume_t volume = supercl->volume;
615 hammer_lock_ex(&supercl->io.lock);
616 KKASSERT(supercl->io.loading == 0);
617 supercl->io.loading = 1;
619 if (supercl->ondisk == NULL) {
621 error = hammer_io_new(volume->devvp, &supercl->io);
623 error = hammer_io_read(volume->devvp, &supercl->io);
625 supercl->io.loading = 0;
626 hammer_unlock(&supercl->io.lock);
629 supercl->ondisk = ondisk = (void *)supercl->io.bp->b_data;
631 supercl->alist.config = &Supercl_alist_config;
632 supercl->alist.meta = ondisk->scl_meta;
633 supercl->alist.info = NULL;
635 error = hammer_io_new(volume->devvp, &supercl->io);
639 if (error == 0 && isnew) {
641 * If this is a new super-cluster we have to initialize
642 * various ondisk structural elements. The caller is
643 * responsible for the remainder.
645 struct hammer_alist_live dummy;
647 hammer_modify_supercl(supercl);
649 ondisk = supercl->ondisk;
650 dummy.config = &Buf_alist_config;
651 dummy.meta = ondisk->head.buf_almeta;
653 hammer_initbuffer(&dummy, &ondisk->head, HAMMER_FSBUF_SUPERCL);
655 nclusters = volume->ondisk->vol_nclusters -
656 ((int64_t)supercl->scl_no * HAMMER_SCL_MAXCLUSTERS);
657 KKASSERT(nclusters > 0);
658 if (nclusters > HAMMER_SCL_MAXCLUSTERS)
659 nclusters = HAMMER_SCL_MAXCLUSTERS;
660 hammer_alist_init(&supercl->alist, 0, (int32_t)nclusters,
663 supercl->io.loading = 0;
664 hammer_unlock(&supercl->io.lock);
669 * NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
672 hammer_unload_supercl(hammer_supercl_t supercl, void *data __unused)
674 KKASSERT(supercl->io.lock.refs == 0);
675 hammer_ref(&supercl->io.lock);
676 hammer_rel_supercl(supercl, 2);
681 * Release a super-cluster. We have to deal with several places where
682 * another thread can ref the super-cluster.
684 * Only destroy the structure itself if the related buffer cache buffer
685 * was disassociated from it. This ties the management of the structure
686 * to the buffer cache subsystem.
689 hammer_rel_supercl(hammer_supercl_t supercl, int flush)
691 hammer_volume_t volume;
693 if (supercl->io.lock.refs == 1) {
694 hammer_lock_ex(&supercl->io.lock);
695 if (supercl->io.lock.refs == 1) {
696 hammer_io_release(&supercl->io, flush);
697 if (supercl->io.bp == NULL &&
698 supercl->io.lock.refs == 1) {
699 volume = supercl->volume;
700 RB_REMOVE(hammer_scl_rb_tree,
701 &volume->rb_scls_root, supercl);
702 supercl->volume = NULL; /* sanity */
703 --hammer_count_supercls;
704 kfree(supercl, M_HAMMER);
705 hammer_rel_volume(volume, 0);
709 hammer_io_flush(&supercl->io);
711 hammer_unlock(&supercl->io.lock);
713 hammer_unref(&supercl->io.lock);
716 /************************************************************************
718 ************************************************************************
722 hammer_get_cluster(hammer_volume_t volume, int32_t clu_no,
723 int *errorp, int getflags)
725 hammer_cluster_t cluster;
728 cluster = RB_LOOKUP(hammer_clu_rb_tree, &volume->rb_clus_root, clu_no);
729 if (cluster == NULL) {
730 ++hammer_count_clusters;
731 cluster = kmalloc(sizeof(*cluster), M_HAMMER, M_WAITOK|M_ZERO);
732 cluster->clu_no = clu_no;
733 cluster->volume = volume;
734 RB_INIT(&cluster->rb_bufs_root);
735 RB_INIT(&cluster->rb_nods_root);
736 hammer_io_init(&cluster->io, HAMMER_STRUCTURE_CLUSTER);
737 cluster->io.offset = calculate_cluster_offset(volume, clu_no);
738 hammer_ref(&cluster->io.lock);
739 /* NOTE: cluster->io.validated expected to be 0 */
742 * Insert the cluster into the RB tree and handle late
745 if (RB_INSERT(hammer_clu_rb_tree, &volume->rb_clus_root, cluster)) {
746 hammer_unref(&cluster->io.lock);
747 --hammer_count_clusters;
748 kfree(cluster, M_HAMMER);
751 hammer_ref(&volume->io.lock);
753 hammer_ref(&cluster->io.lock);
757 * Deal with on-disk info
759 if (cluster->ondisk == NULL || getflags || cluster->io.validated == 0) {
760 *errorp = hammer_load_cluster(cluster, getflags);
762 hammer_rel_cluster(cluster, 1);
772 hammer_get_root_cluster(struct hammer_mount *hmp, int *errorp)
774 hammer_cluster_t cluster;
776 cluster = hmp->rootcl;
777 KKASSERT(cluster != NULL);
778 hammer_ref(&cluster->io.lock);
781 * Deal with on-disk info
783 if (cluster->ondisk == NULL || cluster->io.validated == 0) {
784 *errorp = hammer_load_cluster(cluster, 0);
786 hammer_rel_cluster(cluster, 1);
797 hammer_load_cluster(hammer_cluster_t cluster, int getflags)
799 hammer_volume_t volume = cluster->volume;
800 struct hammer_cluster_ondisk *ondisk;
803 hammer_lock_ex(&cluster->io.lock);
804 KKASSERT(cluster->io.loading == 0);
805 cluster->io.loading = 1;
807 if (cluster->ondisk == NULL) {
808 KKASSERT(TAILQ_EMPTY(&cluster->io.deplist));
811 * Unmodified buffers may be present, indicating that we
812 * had already validated the cluster even though we no longer
813 * have its ondisk info.
815 if (!RB_EMPTY(&cluster->rb_bufs_root))
816 KKASSERT(cluster->io.validated);
817 if (getflags & GET_CLUSTER_NEW)
818 error = hammer_io_new(volume->devvp, &cluster->io);
820 error = hammer_io_read(volume->devvp, &cluster->io);
822 cluster->io.loading = 0;
823 hammer_unlock(&cluster->io.lock);
826 cluster->ondisk = ondisk = (void *)cluster->io.bp->b_data;
828 cluster->alist_master.config = &Clu_master_alist_config;
829 cluster->alist_master.meta = ondisk->clu_master_meta;
830 cluster->alist_btree.config = &Clu_slave_alist_config;
831 cluster->alist_btree.meta = ondisk->clu_btree_meta;
832 cluster->alist_btree.info = cluster;
833 cluster->alist_record.config = &Clu_slave_alist_config;
834 cluster->alist_record.meta = ondisk->clu_record_meta;
835 cluster->alist_record.info = cluster;
836 cluster->alist_mdata.config = &Clu_slave_alist_config;
837 cluster->alist_mdata.meta = ondisk->clu_mdata_meta;
838 cluster->alist_mdata.info = cluster;
840 if ((getflags & GET_CLUSTER_NEW) == 0) {
842 * Load cluster range info for easy access
844 cluster->clu_btree_beg = ondisk->clu_btree_beg;
845 cluster->clu_btree_end = ondisk->clu_btree_end;
847 } else if (getflags & GET_CLUSTER_NEW) {
848 error = hammer_io_new(volume->devvp, &cluster->io);
852 if (error == 0 && (getflags & GET_CLUSTER_NEW)) {
854 * If this is a new cluster we have to initialize
855 * various ondisk structural elements. The caller is
856 * responsible for the remainder.
858 struct hammer_alist_live dummy;
860 hammer_volume_ondisk_t voldisk;
863 cluster->flags &= ~HAMMER_CLUSTER_DELETED;
865 hammer_modify_cluster(cluster);
866 ondisk = cluster->ondisk;
867 voldisk = volume->ondisk;
869 dummy.config = &Buf_alist_config;
870 dummy.meta = ondisk->head.buf_almeta;
872 hammer_initbuffer(&dummy, &ondisk->head, HAMMER_FSBUF_CLUSTER);
874 ondisk->vol_fsid = voldisk->vol_fsid;
875 ondisk->vol_fstype = voldisk->vol_fstype;
877 ondisk->clu_id = 0; /* XXX */
878 ondisk->clu_no = cluster->clu_no;
879 ondisk->clu_flags = 0;
880 ondisk->clu_start = HAMMER_BUFSIZE;
881 ondisk->synchronized_rec_id = 1; /* XXX timestamp */
882 KKASSERT(voldisk->vol_clo_end > cluster->io.offset);
883 if (voldisk->vol_clo_end - cluster->io.offset >
884 voldisk->vol_clsize) {
885 ondisk->clu_limit = voldisk->vol_clsize;
887 ondisk->clu_limit = (int32_t)(voldisk->vol_clo_end -
890 nbuffers = ondisk->clu_limit / HAMMER_BUFSIZE;
891 hammer_alist_init(&cluster->alist_master, 1, nbuffers - 1,
893 hammer_alist_init(&cluster->alist_btree,
894 HAMMER_FSBUF_MAXBLKS,
895 (nbuffers - 1) * HAMMER_FSBUF_MAXBLKS,
896 HAMMER_ASTATE_ALLOC);
897 hammer_alist_init(&cluster->alist_record,
898 HAMMER_FSBUF_MAXBLKS,
899 (nbuffers - 1) * HAMMER_FSBUF_MAXBLKS,
900 HAMMER_ASTATE_ALLOC);
901 hammer_alist_init(&cluster->alist_mdata,
902 HAMMER_FSBUF_MAXBLKS,
903 (nbuffers - 1) * HAMMER_FSBUF_MAXBLKS,
904 HAMMER_ASTATE_ALLOC);
906 ondisk->idx_data = 1 * HAMMER_FSBUF_MAXBLKS;
907 ondisk->idx_index = 0 * HAMMER_FSBUF_MAXBLKS;
908 ondisk->idx_record = nbuffers * HAMMER_FSBUF_MAXBLKS;
911 * Initialize the B-Tree. We don't know what the caller
912 * intends to do with the cluster so make sure it causes
913 * an assertion if the caller makes no changes.
915 ondisk->clu_btree_parent_vol_no = -2;
916 ondisk->clu_btree_parent_clu_no = -2;
917 ondisk->clu_btree_parent_offset = -2;
918 ondisk->clu_btree_parent_clu_gen = -2;
920 croot = hammer_alloc_btree(cluster, &error);
922 hammer_modify_node(croot);
923 bzero(croot->ondisk, sizeof(*croot->ondisk));
924 croot->ondisk->count = 0;
925 croot->ondisk->type = HAMMER_BTREE_TYPE_LEAF;
926 hammer_modify_cluster(cluster);
927 ondisk->clu_btree_root = croot->node_offset;
928 hammer_rel_node(croot);
932 * We just formatted this cluster, don't try to recover it!
934 cluster->io.validated = 1;
938 * If no error occured handle automatic cluster recovery unless
939 * the NORECOVER flag is passed (prevents recovery recursions) or
940 * the cluster has been flagged for deletion (prevents an attempt
941 * to recover a cluster which is no longer hooked into the tree).
943 * Setting hammer_debug_recover to 1 will force recovery on load
944 * whether or not the cluster is marked open.
946 * Setting hammer_debug_recover to -1 will force NO recovery
947 * regardless of state.
949 * io.validated can only be cleared if the buffer RB list is empty,
950 * preventing us from trying to recover an actively referenced
951 * cluster (which would blow the filesystem to smithereens).
953 if (error == 0 && cluster->io.validated == 0) {
954 if ((getflags & GET_CLUSTER_NORECOVER) == 0 &&
955 (cluster->flags & HAMMER_CLUSTER_DELETED) == 0) {
956 if ((cluster->ondisk->clu_flags & HAMMER_CLUF_OPEN) ||
957 hammer_debug_recover > 0) {
958 if (hammer_debug_recover >= 0)
959 hammer_recover(cluster);
961 cluster->io.validated = 1;
962 } else if ((cluster->ondisk->clu_flags & HAMMER_CLUF_OPEN)==0) {
963 cluster->io.validated = 1;
966 cluster->io.loading = 0;
967 hammer_unlock(&cluster->io.lock);
972 * NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
975 hammer_unload_cluster(hammer_cluster_t cluster, void *data __unused)
977 hammer_ref(&cluster->io.lock);
978 RB_SCAN(hammer_buf_rb_tree, &cluster->rb_bufs_root, NULL,
979 hammer_unload_buffer, NULL);
980 hammer_io_waitdep(&cluster->io);
981 KKASSERT(cluster->io.lock.refs == 1);
982 hammer_rel_cluster(cluster, 2);
987 * Update the cluster's synchronization TID, which is used during cluster
988 * recovery. NOTE: The cluster header is not written out until all related
989 * records have been written out.
992 hammer_alloc_recid(hammer_cluster_t cluster)
996 hammer_modify_cluster(cluster);
997 recid = cluster->ondisk->synchronized_rec_id++;
1003 hammer_update_syncid(hammer_cluster_t cluster, hammer_tid_t tid)
1005 hammer_modify_cluster(cluster);
1006 if (cluster->ondisk->synchronized_tid < tid)
1007 cluster->ondisk->synchronized_tid = tid;
1012 * Reference a cluster that is either already referenced or via a specially
1013 * handled pointer (aka rootcl).
1016 hammer_ref_cluster(hammer_cluster_t cluster)
1020 KKASSERT(cluster != NULL);
1021 hammer_ref(&cluster->io.lock);
1024 * Deal with on-disk info
1026 if (cluster->ondisk == NULL || cluster->io.validated == 0) {
1027 error = hammer_load_cluster(cluster, 0);
1029 hammer_rel_cluster(cluster, 1);
1037 * Release a cluster. We have to deal with several places where
1038 * another thread can ref the cluster.
1040 * Only destroy the structure itself if we no longer have an IO or any
1041 * hammer buffers associated with the structure.
1044 hammer_rel_cluster(hammer_cluster_t cluster, int flush)
1046 hammer_volume_t volume;
1049 * Free a deleted cluster back to the pool when its last
1050 * active reference is released. This prevents the cluster
1051 * from being reallocated until all its prior references go away.
1053 * XXX implement a discard dependancy list which holds references
1054 * on clusters, preventing their deletion, until their parent cluster
1055 * has been flushed to disk.
1057 if (cluster->io.lock.refs == 1) {
1058 if (cluster->flags & HAMMER_CLUSTER_DELETED) {
1059 cluster->flags &= ~HAMMER_CLUSTER_DELETED;
1060 kprintf("FREE CLUSTER %d", cluster->clu_no);
1061 if (cluster->ondisk->stat_records) {
1062 struct hammer_sync_info info;
1065 info.waitfor = MNT_WAIT;
1066 kprintf(" (still has %d records!)\n",
1067 cluster->ondisk->stat_records);
1068 Debugger("continue to recover cluster");
1069 hammer_recover(cluster);
1070 Debugger("continue to sync cluster");
1071 hammer_sync_cluster(cluster, &info);
1072 Debugger("now debug it");
1077 * Clean up any statistics we left hanging in the
1080 hammer_adjust_stats(cluster, HAMMER_FSBUF_BTREE,
1081 -cluster->ondisk->stat_idx_bufs);
1082 hammer_adjust_stats(cluster, HAMMER_FSBUF_DATA,
1083 -cluster->ondisk->stat_data_bufs);
1084 hammer_adjust_stats(cluster, HAMMER_FSBUF_RECORDS,
1085 -cluster->ondisk->stat_rec_bufs);
1087 * hammer_discard_cluster(cluster) - throw away
1088 * dirty backing store, recurse to any underlying
1091 hammer_free_cluster(cluster);
1095 if (cluster->io.lock.refs == 1) {
1096 hammer_lock_ex(&cluster->io.lock);
1097 if (cluster->io.lock.refs == 1) {
1099 * Release the I/O. If we or the kernel wants to
1100 * flush, this will release the bp. Otherwise the
1101 * bp may be written and flushed passively by the
1104 hammer_io_release(&cluster->io, flush);
1109 if (cluster != cluster->volume->hmp->rootcl &&
1110 cluster->io.bp == NULL &&
1111 cluster->io.lock.refs == 1 &&
1112 RB_EMPTY(&cluster->rb_bufs_root)) {
1113 KKASSERT(RB_EMPTY(&cluster->rb_nods_root));
1114 volume = cluster->volume;
1115 RB_REMOVE(hammer_clu_rb_tree,
1116 &volume->rb_clus_root, cluster);
1117 cluster->volume = NULL; /* sanity */
1118 --hammer_count_clusters;
1119 kfree(cluster, M_HAMMER);
1120 hammer_rel_volume(volume, 0);
1124 hammer_io_flush(&cluster->io);
1126 hammer_unlock(&cluster->io.lock);
1128 hammer_unref(&cluster->io.lock);
1131 /************************************************************************
1133 ************************************************************************
1135 * Manage buffers. Note that a buffer holds a reference to its associated
1136 * cluster, and its cluster will hold a reference to the cluster's volume.
1138 * A non-zero buf_type indicates that a new buffer should be created and
1142 hammer_get_buffer(hammer_cluster_t cluster, int32_t buf_no,
1143 u_int64_t buf_type, int *errorp)
1145 hammer_buffer_t buffer;
1148 * Find the buffer. Note that buffer 0 corresponds to the cluster
1149 * header and should never be requested.
1151 KKASSERT(buf_no >= cluster->ondisk->clu_start / HAMMER_BUFSIZE &&
1152 buf_no < cluster->ondisk->clu_limit / HAMMER_BUFSIZE);
1155 * Locate and lock the buffer structure, creating one if necessary.
1158 buffer = RB_LOOKUP(hammer_buf_rb_tree, &cluster->rb_bufs_root, buf_no);
1159 if (buffer == NULL) {
1160 ++hammer_count_buffers;
1161 buffer = kmalloc(sizeof(*buffer), M_HAMMER, M_WAITOK|M_ZERO);
1162 buffer->buf_no = buf_no;
1163 buffer->cluster = cluster;
1164 buffer->volume = cluster->volume;
1165 hammer_io_init(&buffer->io, HAMMER_STRUCTURE_BUFFER);
1166 buffer->io.offset = cluster->io.offset +
1167 (buf_no * HAMMER_BUFSIZE);
1168 TAILQ_INIT(&buffer->clist);
1169 hammer_ref(&buffer->io.lock);
1172 * Insert the cluster into the RB tree and handle late
1175 if (RB_INSERT(hammer_buf_rb_tree, &cluster->rb_bufs_root, buffer)) {
1176 hammer_unref(&buffer->io.lock);
1177 --hammer_count_buffers;
1178 kfree(buffer, M_HAMMER);
1181 hammer_ref(&cluster->io.lock);
1183 hammer_ref(&buffer->io.lock);
1187 * Deal with on-disk info
1189 if (buffer->ondisk == NULL || buf_type || buffer->io.loading) {
1190 *errorp = hammer_load_buffer(buffer, buf_type);
1192 hammer_rel_buffer(buffer, 1);
1202 hammer_load_buffer(hammer_buffer_t buffer, u_int64_t buf_type)
1204 hammer_volume_t volume;
1205 hammer_fsbuf_ondisk_t ondisk;
1209 * Load the buffer's on-disk info
1211 volume = buffer->volume;
1212 hammer_lock_ex(&buffer->io.lock);
1213 KKASSERT(buffer->io.loading == 0);
1214 buffer->io.loading = 1;
1216 if (buffer->ondisk == NULL) {
1218 error = hammer_io_new(volume->devvp, &buffer->io);
1220 error = hammer_io_read(volume->devvp, &buffer->io);
1223 buffer->io.loading = 0;
1224 hammer_unlock(&buffer->io.lock);
1227 buffer->ondisk = ondisk = (void *)buffer->io.bp->b_data;
1228 buffer->alist.config = &Buf_alist_config;
1229 buffer->alist.meta = ondisk->head.buf_almeta;
1230 buffer->buf_type = ondisk->head.buf_type;
1231 } else if (buf_type) {
1232 error = hammer_io_new(volume->devvp, &buffer->io);
1236 if (error == 0 && buf_type) {
1237 hammer_modify_buffer(buffer);
1238 ondisk = buffer->ondisk;
1239 hammer_initbuffer(&buffer->alist, &ondisk->head, buf_type);
1240 buffer->buf_type = ondisk->head.buf_type;
1242 buffer->io.loading = 0;
1243 hammer_unlock(&buffer->io.lock);
1248 * NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
1251 hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused)
1253 hammer_ref(&buffer->io.lock);
1254 hammer_flush_buffer_nodes(buffer);
1255 KKASSERT(buffer->io.lock.refs == 1);
1256 hammer_rel_buffer(buffer, 2);
1261 * Reference a buffer that is either already referenced or via a specially
1262 * handled pointer (aka cursor->buffer).
1265 hammer_ref_buffer(hammer_buffer_t buffer)
1269 hammer_ref(&buffer->io.lock);
1270 if (buffer->ondisk == NULL || buffer->io.loading) {
1271 error = hammer_load_buffer(buffer, 0);
1273 hammer_rel_buffer(buffer, 1);
1275 * NOTE: buffer pointer can become stale after
1276 * the above release.
1279 KKASSERT(buffer->buf_type ==
1280 buffer->ondisk->head.buf_type);
1289 * Release a buffer. We have to deal with several places where
1290 * another thread can ref the buffer.
1292 * Only destroy the structure itself if the related buffer cache buffer
1293 * was disassociated from it. This ties the management of the structure
1294 * to the buffer cache subsystem. buffer->ondisk determines whether the
1295 * embedded io is referenced or not.
1298 hammer_rel_buffer(hammer_buffer_t buffer, int flush)
1300 hammer_cluster_t cluster;
1302 if (buffer->io.lock.refs == 1) {
1303 hammer_lock_ex(&buffer->io.lock);
1304 if (buffer->io.lock.refs == 1) {
1305 hammer_io_release(&buffer->io, flush);
1307 if (buffer->io.bp == NULL &&
1308 buffer->io.lock.refs == 1) {
1309 hammer_flush_buffer_nodes(buffer);
1310 KKASSERT(TAILQ_EMPTY(&buffer->clist));
1311 cluster = buffer->cluster;
1312 RB_REMOVE(hammer_buf_rb_tree,
1313 &cluster->rb_bufs_root, buffer);
1314 buffer->cluster = NULL; /* sanity */
1315 --hammer_count_buffers;
1316 kfree(buffer, M_HAMMER);
1317 hammer_rel_cluster(cluster, 0);
1321 hammer_io_flush(&buffer->io);
1323 hammer_unlock(&buffer->io.lock);
1325 hammer_unref(&buffer->io.lock);
1328 /************************************************************************
1330 ************************************************************************
1332 * Manage B-Tree nodes. B-Tree nodes represent the primary indexing
1333 * method used by the HAMMER filesystem.
1335 * Unlike other HAMMER structures, a hammer_node can be PASSIVELY
1336 * associated with its buffer, and will only referenced the buffer while
1337 * the node itself is referenced.
1339 * A hammer_node can also be passively associated with other HAMMER
1340 * structures, such as inodes, while retaining 0 references. These
1341 * associations can be cleared backwards using a pointer-to-pointer in
1344 * This allows the HAMMER implementation to cache hammer_nodes long-term
1345 * and short-cut a great deal of the infrastructure's complexity. In
1346 * most cases a cached node can be reacquired without having to dip into
1347 * either the buffer or cluster management code.
1349 * The caller must pass a referenced cluster on call and will retain
1350 * ownership of the reference on return. The node will acquire its own
1351 * additional references, if necessary.
1354 hammer_get_node(hammer_cluster_t cluster, int32_t node_offset, int *errorp)
1359 * Locate the structure, allocating one if necessary.
1362 node = RB_LOOKUP(hammer_nod_rb_tree, &cluster->rb_nods_root,
1365 ++hammer_count_nodes;
1366 node = kmalloc(sizeof(*node), M_HAMMER, M_WAITOK|M_ZERO);
1367 node->node_offset = node_offset;
1368 node->cluster = cluster;
1369 if (RB_INSERT(hammer_nod_rb_tree, &cluster->rb_nods_root,
1371 --hammer_count_nodes;
1372 kfree(node, M_HAMMER);
1376 hammer_ref(&node->lock);
1377 *errorp = hammer_load_node(node);
1379 hammer_rel_node(node);
1386 * Reference an already-referenced node.
1389 hammer_ref_node(hammer_node_t node)
1393 KKASSERT(node->lock.refs > 0);
1394 hammer_ref(&node->lock);
1395 if ((error = hammer_load_node(node)) != 0)
1396 hammer_rel_node(node);
1401 * Load a node's on-disk data reference.
1404 hammer_load_node(hammer_node_t node)
1406 hammer_buffer_t buffer;
1413 hammer_lock_ex(&node->lock);
1414 if (node->ondisk == NULL) {
1416 * This is a little confusing but the jist is that
1417 * node->buffer determines whether the node is on
1418 * the buffer's clist and node->ondisk determines
1419 * whether the buffer is referenced.
1421 if ((buffer = node->buffer) != NULL) {
1422 error = hammer_ref_buffer(buffer);
1424 buf_no = node->node_offset / HAMMER_BUFSIZE;
1425 buffer = hammer_get_buffer(node->cluster,
1428 KKASSERT(error == 0);
1429 TAILQ_INSERT_TAIL(&buffer->clist,
1431 node->buffer = buffer;
1435 node->ondisk = (void *)((char *)buffer->ondisk +
1436 (node->node_offset & HAMMER_BUFMASK));
1439 hammer_unlock(&node->lock);
1444 * Safely reference a node, interlock against flushes via the IO subsystem.
1447 hammer_ref_node_safe(struct hammer_mount *hmp, struct hammer_node **cache,
1452 if ((node = *cache) != NULL)
1453 hammer_ref(&node->lock);
1455 *errorp = hammer_load_node(node);
1457 hammer_rel_node(node);
1467 * Release a hammer_node. On the last release the node dereferences
1468 * its underlying buffer and may or may not be destroyed.
1471 hammer_rel_node(hammer_node_t node)
1473 hammer_cluster_t cluster;
1474 hammer_buffer_t buffer;
1475 int32_t node_offset;
1479 * If this isn't the last ref just decrement the ref count and
1482 if (node->lock.refs > 1) {
1483 hammer_unref(&node->lock);
1488 * If there is no ondisk info or no buffer the node failed to load,
1489 * remove the last reference and destroy the node.
1491 if (node->ondisk == NULL) {
1492 hammer_unref(&node->lock);
1493 hammer_flush_node(node);
1494 /* node is stale now */
1499 * Do final cleanups and then either destroy the node and leave it
1500 * passively cached. The buffer reference is removed regardless.
1502 buffer = node->buffer;
1503 node->ondisk = NULL;
1505 if ((node->flags & (HAMMER_NODE_DELETED|HAMMER_NODE_FLUSH)) == 0) {
1506 hammer_unref(&node->lock);
1507 hammer_rel_buffer(buffer, 0);
1512 * Destroy the node. Record pertainant data because the node
1513 * becomes stale the instant we flush it.
1515 flags = node->flags;
1516 node_offset = node->node_offset;
1517 hammer_unref(&node->lock);
1518 hammer_flush_node(node);
1521 cluster = buffer->cluster;
1522 if (flags & HAMMER_NODE_DELETED) {
1523 if (node_offset == cluster->ondisk->clu_btree_root)
1524 KKASSERT(cluster->flags & HAMMER_CLUSTER_DELETED);
1525 hammer_free_btree(cluster, node_offset);
1527 hammer_rel_buffer(buffer, 0);
1531 * Passively cache a referenced hammer_node in *cache. The caller may
1532 * release the node on return.
1535 hammer_cache_node(hammer_node_t node, struct hammer_node **cache)
1540 * If the node is being deleted, don't cache it!
1542 if (node->flags & HAMMER_NODE_DELETED)
1546 * Cache the node. If we previously cached a different node we
1547 * have to give HAMMER a chance to destroy it.
1550 if (node->cache1 != cache) {
1551 if (node->cache2 != cache) {
1552 if ((old = *cache) != NULL) {
1553 KKASSERT(node->lock.refs != 0);
1554 hammer_uncache_node(cache);
1558 *node->cache2 = NULL;
1559 node->cache2 = node->cache1;
1560 node->cache1 = cache;
1563 struct hammer_node **tmp;
1565 node->cache1 = node->cache2;
1572 hammer_uncache_node(struct hammer_node **cache)
1576 if ((node = *cache) != NULL) {
1578 if (node->cache1 == cache) {
1579 node->cache1 = node->cache2;
1580 node->cache2 = NULL;
1581 } else if (node->cache2 == cache) {
1582 node->cache2 = NULL;
1584 panic("hammer_uncache_node: missing cache linkage");
1586 if (node->cache1 == NULL && node->cache2 == NULL)
1587 hammer_flush_node(node);
1592 * Remove a node's cache references and destroy the node if it has no
1593 * other references or backing store.
1596 hammer_flush_node(hammer_node_t node)
1598 hammer_buffer_t buffer;
1601 *node->cache1 = NULL;
1603 *node->cache2 = NULL;
1604 if (node->lock.refs == 0 && node->ondisk == NULL) {
1605 RB_REMOVE(hammer_nod_rb_tree, &node->cluster->rb_nods_root,
1607 if ((buffer = node->buffer) != NULL) {
1608 node->buffer = NULL;
1609 TAILQ_REMOVE(&buffer->clist, node, entry);
1610 /* buffer is unreferenced because ondisk is NULL */
1612 --hammer_count_nodes;
1613 kfree(node, M_HAMMER);
1618 * Flush passively cached B-Tree nodes associated with this buffer.
1619 * This is only called when the buffer is about to be destroyed, so
1620 * none of the nodes should have any references.
1623 hammer_flush_buffer_nodes(hammer_buffer_t buffer)
1627 while ((node = TAILQ_FIRST(&buffer->clist)) != NULL) {
1628 KKASSERT(node->lock.refs == 0 && node->ondisk == NULL);
1629 hammer_ref(&node->lock);
1630 node->flags |= HAMMER_NODE_FLUSH;
1631 hammer_rel_node(node);
1635 /************************************************************************
1636 * A-LIST ALLOCATORS *
1637 ************************************************************************/
1640 * Allocate HAMMER clusters
1643 hammer_alloc_cluster(hammer_mount_t hmp, hammer_cluster_t cluster_hint,
1646 hammer_volume_t volume;
1647 hammer_cluster_t cluster;
1654 * Figure out our starting volume and hint.
1657 vol_beg = cluster_hint->volume->vol_no;
1658 clu_hint = cluster_hint->clu_no;
1660 vol_beg = hmp->volume_iterator;
1665 * Loop through volumes looking for a free cluster. If allocating
1666 * a new cluster relative to an existing cluster try to find a free
1667 * cluster on either side (clu_hint >= 0), otherwise just do a
1668 * forwards iteration.
1672 volume = hammer_get_volume(hmp, vol_no, errorp);
1673 kprintf("VOLUME %p %d\n", volume, vol_no);
1675 clu_no = HAMMER_ALIST_BLOCK_NONE;
1678 hammer_modify_volume(volume);
1679 if (clu_hint == -1) {
1680 clu_hint = volume->clu_iterator;
1681 clu_no = hammer_alist_alloc_fwd(&volume->alist, 1,
1683 if (clu_no == HAMMER_ALIST_BLOCK_NONE) {
1684 clu_no = hammer_alist_alloc_fwd(&volume->alist,
1688 clu_no = hammer_alist_alloc_fwd(&volume->alist, 1,
1690 if (clu_no == HAMMER_ALIST_BLOCK_NONE) {
1691 clu_no = hammer_alist_alloc_rev(&volume->alist,
1695 if (clu_no != HAMMER_ALIST_BLOCK_NONE)
1697 hammer_rel_volume(volume, 0);
1700 vol_no = (vol_no + 1) % hmp->nvolumes;
1702 } while (vol_no != vol_beg);
1705 * Acquire the cluster. On success this will force *errorp to 0.
1707 if (clu_no != HAMMER_ALIST_BLOCK_NONE) {
1708 kprintf("ALLOC CLUSTER %d:%d\n", volume->vol_no, clu_no);
1709 cluster = hammer_get_cluster(volume, clu_no, errorp,
1711 volume->clu_iterator = clu_no;
1712 hammer_rel_volume(volume, 0);
1717 hammer_lock_ex(&cluster->io.lock);
1722 hammer_init_cluster(hammer_cluster_t cluster, hammer_base_elm_t left_bound,
1723 hammer_base_elm_t right_bound)
1725 hammer_cluster_ondisk_t ondisk = cluster->ondisk;
1727 hammer_modify_cluster(cluster);
1728 ondisk->clu_btree_beg = *left_bound;
1729 ondisk->clu_btree_end = *right_bound;
1730 cluster->clu_btree_beg = ondisk->clu_btree_beg;
1731 cluster->clu_btree_end = ondisk->clu_btree_end;
1735 * Deallocate a cluster
1738 hammer_free_cluster(hammer_cluster_t cluster)
1740 hammer_modify_volume(cluster->volume);
1741 hammer_alist_free(&cluster->volume->alist, cluster->clu_no, 1);
1745 * Allocate HAMMER elements - btree nodes, data storage, and record elements
1747 * The passed *bufferp should be initialized to NULL. On successive calls
1748 * *bufferp caches the most recent buffer used until put away by the caller.
1749 * Note that previously returned pointers using the cached buffer become
1750 * invalid on successive calls which reuse *bufferp.
1752 * All allocations first attempt to use the block found at the specified
1753 * iterator. If that fails the first available block is used. If that
1754 * fails a new buffer is allocated and associated with the buffer type
1755 * A-list and the element is allocated out of the new buffer.
1757 * This function also ensures that the required minimum number of buffers is
1758 * reserved to guarantee that recovery operations succeed.
1762 hammer_alloc_btree(hammer_cluster_t cluster, int *errorp)
1764 hammer_buffer_t buffer;
1765 hammer_alist_t live;
1769 int32_t node_offset;
1772 hammer_modify_cluster(cluster);
1774 live = &cluster->alist_btree;
1777 * If we aren't recovering then ensure the required minimum
1778 * reservation is met. XXX if the recovery code packs the B-Tree
1779 * we don't have to do this.
1781 * Calculate the number of buffers needed to hold the B-Tree.
1783 if (cluster->io.validated) {
1784 n = (cluster->ondisk->stat_records * 3 /
1785 HAMMER_BTREE_INT_ELMS / HAMMER_BTREE_NODES) + 1;
1786 if (cluster->ondisk->stat_idx_bufs < n) {
1787 kprintf("hammer_alloc_btree: %d/%d buffers\n",
1788 cluster->ondisk->stat_idx_bufs, n);
1790 while (cluster->ondisk->stat_idx_bufs < n) {
1791 alloc_new_buffer(cluster, HAMMER_FSBUF_BTREE, live,
1792 cluster->ondisk->idx_index, errorp,
1796 hammer_rel_buffer(buffer, 0);
1804 * Allocate a B-Tree element
1806 elm_no = hammer_alist_alloc_fwd(live, 1, cluster->ondisk->idx_index);
1807 if (elm_no == HAMMER_ALIST_BLOCK_NONE)
1808 elm_no = hammer_alist_alloc_fwd(live, 1, 0);
1809 if (elm_no == HAMMER_ALIST_BLOCK_NONE) {
1810 alloc_new_buffer(cluster, HAMMER_FSBUF_BTREE, live,
1811 cluster->ondisk->idx_index, errorp, &buffer);
1812 elm_no = hammer_alist_alloc(live, 1);
1813 if (elm_no == HAMMER_ALIST_BLOCK_NONE) {
1816 hammer_rel_buffer(buffer, 0);
1820 cluster->ondisk->idx_index = elm_no;
1821 KKASSERT((elm_no & HAMMER_FSBUF_BLKMASK) < HAMMER_BTREE_NODES);
1824 * Load and return the B-Tree element
1826 buf_no = elm_no / HAMMER_FSBUF_MAXBLKS;
1827 node_offset = buf_no * HAMMER_BUFSIZE +
1828 offsetof(union hammer_fsbuf_ondisk,
1829 btree.nodes[elm_no & HAMMER_FSBUF_BLKMASK]);
1830 node = hammer_get_node(cluster, node_offset, errorp);
1832 hammer_modify_node(node);
1833 bzero(node->ondisk, sizeof(*node->ondisk));
1834 KKASSERT((node->flags & (HAMMER_NODE_DELETED)) == 0);
1836 hammer_alist_free(live, elm_no, 1);
1837 hammer_rel_node(node);
1841 hammer_rel_buffer(buffer, 0);
1846 hammer_alloc_data(hammer_cluster_t cluster, int32_t bytes,
1847 int *errorp, struct hammer_buffer **bufferp)
1849 hammer_buffer_t buffer;
1850 hammer_alist_t live;
1857 * Deal with large data blocks. The blocksize is HAMMER_BUFSIZE
1858 * for these allocations.
1860 hammer_modify_cluster(cluster);
1861 if ((bytes & HAMMER_BUFMASK) == 0) {
1862 nblks = bytes / HAMMER_BUFSIZE;
1863 /* only one block allowed for now (so buffer can hold it) */
1864 KKASSERT(nblks == 1);
1866 buf_no = hammer_alloc_master(cluster, nblks,
1867 cluster->ondisk->idx_ldata, 1);
1868 if (buf_no == HAMMER_ALIST_BLOCK_NONE) {
1872 hammer_adjust_stats(cluster, HAMMER_FSBUF_DATA, nblks);
1873 cluster->ondisk->idx_ldata = buf_no;
1875 *bufferp = hammer_get_buffer(cluster, buf_no, -1, errorp);
1877 hammer_rel_buffer(buffer, 0);
1879 return(buffer->ondisk);
1883 * Allocate a data element. The block size is HAMMER_DATA_BLKSIZE
1884 * (64 bytes) for these allocations.
1886 nblks = (bytes + HAMMER_DATA_BLKMASK) & ~HAMMER_DATA_BLKMASK;
1887 nblks /= HAMMER_DATA_BLKSIZE;
1888 live = &cluster->alist_mdata;
1889 elm_no = hammer_alist_alloc_fwd(live, nblks, cluster->ondisk->idx_data);
1890 if (elm_no == HAMMER_ALIST_BLOCK_NONE)
1891 elm_no = hammer_alist_alloc_fwd(live, nblks, 0);
1892 if (elm_no == HAMMER_ALIST_BLOCK_NONE) {
1893 alloc_new_buffer(cluster, HAMMER_FSBUF_DATA, live,
1894 cluster->ondisk->idx_data, errorp, bufferp);
1895 elm_no = hammer_alist_alloc(live, nblks);
1896 if (elm_no == HAMMER_ALIST_BLOCK_NONE) {
1901 cluster->ondisk->idx_index = elm_no;
1904 * Load and return the B-Tree element
1906 buf_no = elm_no / HAMMER_FSBUF_MAXBLKS;
1908 if (buffer == NULL || buffer->cluster != cluster ||
1909 buffer->buf_no != buf_no) {
1911 hammer_rel_buffer(buffer, 0);
1912 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
1915 KKASSERT(buffer->ondisk->head.buf_type == HAMMER_FSBUF_DATA);
1916 KKASSERT((elm_no & HAMMER_FSBUF_BLKMASK) < HAMMER_DATA_NODES);
1917 hammer_modify_buffer(buffer);
1918 item = &buffer->ondisk->data.data[elm_no & HAMMER_FSBUF_BLKMASK];
1919 bzero(item, nblks * HAMMER_DATA_BLKSIZE);
1925 hammer_alloc_record(hammer_cluster_t cluster, int *errorp,
1926 u_int8_t rec_type, struct hammer_buffer **bufferp)
1928 hammer_buffer_t buffer;
1929 hammer_alist_t live;
1935 * Allocate a record element
1937 hammer_modify_cluster(cluster);
1938 live = &cluster->alist_record;
1939 elm_no = hammer_alist_alloc_rev(live, 1, cluster->ondisk->idx_record);
1940 if (elm_no == HAMMER_ALIST_BLOCK_NONE)
1941 elm_no = hammer_alist_alloc_rev(live, 1,HAMMER_ALIST_BLOCK_MAX);
1942 if (elm_no == HAMMER_ALIST_BLOCK_NONE) {
1943 alloc_new_buffer(cluster, HAMMER_FSBUF_RECORDS, live,
1944 cluster->ondisk->idx_record, errorp, bufferp);
1945 elm_no = hammer_alist_alloc_rev(live, 1,HAMMER_ALIST_BLOCK_MAX);
1946 kprintf("hammer_alloc_record elm again %08x\n", elm_no);
1947 if (elm_no == HAMMER_ALIST_BLOCK_NONE) {
1952 cluster->ondisk->idx_record = elm_no;
1955 * Load and return the record element
1957 buf_no = elm_no / HAMMER_FSBUF_MAXBLKS;
1959 if (buffer == NULL || buffer->cluster != cluster ||
1960 buffer->buf_no != buf_no) {
1962 hammer_rel_buffer(buffer, 0);
1963 buffer = hammer_get_buffer(cluster, buf_no, 0, errorp);
1966 KKASSERT(buffer->ondisk->head.buf_type == HAMMER_FSBUF_RECORDS);
1967 KASSERT((elm_no & HAMMER_FSBUF_BLKMASK) < HAMMER_RECORD_NODES,
1968 ("elm_no %d (%d) out of bounds", elm_no, elm_no & HAMMER_FSBUF_BLKMASK));
1969 hammer_modify_buffer(buffer);
1970 item = &buffer->ondisk->record.recs[elm_no & HAMMER_FSBUF_BLKMASK];
1971 bzero(item, sizeof(union hammer_record_ondisk));
1973 ++cluster->ondisk->stat_records;
1974 if (rec_type == HAMMER_RECTYPE_CLUSTER)
1975 ++cluster->ondisk->stat_records;
1980 hammer_free_data_ptr(hammer_buffer_t buffer, void *data, int bytes)
1984 hammer_alist_t live;
1986 hammer_modify_cluster(buffer->cluster);
1987 if ((bytes & HAMMER_BUFMASK) == 0) {
1988 nblks = bytes / HAMMER_BUFSIZE;
1989 KKASSERT(nblks == 1 && data == (void *)buffer->ondisk);
1990 hammer_alist_free(&buffer->cluster->alist_master,
1991 buffer->buf_no, nblks);
1992 hammer_adjust_stats(buffer->cluster, HAMMER_FSBUF_DATA, -nblks);
1996 elm_no = ((char *)data - (char *)buffer->ondisk->data.data) /
1997 HAMMER_DATA_BLKSIZE;
1998 KKASSERT(elm_no >= 0 && elm_no < HAMMER_DATA_NODES);
1999 elm_no += buffer->buf_no * HAMMER_FSBUF_MAXBLKS;
2000 nblks = (bytes + HAMMER_DATA_BLKMASK) & ~HAMMER_DATA_BLKMASK;
2001 nblks /= HAMMER_DATA_BLKSIZE;
2002 live = &buffer->cluster->alist_mdata;
2003 hammer_alist_free(live, elm_no, nblks);
2007 hammer_free_record_ptr(hammer_buffer_t buffer, union hammer_record_ondisk *rec,
2011 hammer_alist_t live;
2013 hammer_modify_cluster(buffer->cluster);
2014 elm_no = rec - &buffer->ondisk->record.recs[0];
2015 KKASSERT(elm_no >= 0 && elm_no < HAMMER_BTREE_NODES);
2016 elm_no += buffer->buf_no * HAMMER_FSBUF_MAXBLKS;
2017 live = &buffer->cluster->alist_record;
2018 hammer_alist_free(live, elm_no, 1);
2019 --buffer->cluster->ondisk->stat_records;
2020 if (rec_type == HAMMER_RECTYPE_CLUSTER)
2021 --buffer->cluster->ondisk->stat_records;
2025 hammer_free_btree(hammer_cluster_t cluster, int32_t bclu_offset)
2027 const int32_t blksize = sizeof(struct hammer_node_ondisk);
2028 int32_t fsbuf_offset = bclu_offset & HAMMER_BUFMASK;
2029 hammer_alist_t live;
2032 hammer_modify_cluster(cluster);
2033 elm_no = bclu_offset / HAMMER_BUFSIZE * HAMMER_FSBUF_MAXBLKS;
2034 fsbuf_offset -= offsetof(union hammer_fsbuf_ondisk, btree.nodes[0]);
2035 live = &cluster->alist_btree;
2036 KKASSERT(fsbuf_offset >= 0 && fsbuf_offset % blksize == 0);
2037 elm_no += fsbuf_offset / blksize;
2038 hammer_alist_free(live, elm_no, 1);
2042 hammer_free_data(hammer_cluster_t cluster, int32_t bclu_offset, int32_t bytes)
2044 const int32_t blksize = HAMMER_DATA_BLKSIZE;
2045 int32_t fsbuf_offset = bclu_offset & HAMMER_BUFMASK;
2046 hammer_alist_t live;
2051 hammer_modify_cluster(cluster);
2052 if ((bytes & HAMMER_BUFMASK) == 0) {
2053 nblks = bytes / HAMMER_BUFSIZE;
2054 KKASSERT(nblks == 1 && (bclu_offset & HAMMER_BUFMASK) == 0);
2055 buf_no = bclu_offset / HAMMER_BUFSIZE;
2056 hammer_alist_free(&cluster->alist_master, buf_no, nblks);
2057 hammer_adjust_stats(cluster, HAMMER_FSBUF_DATA, -nblks);
2061 elm_no = bclu_offset / HAMMER_BUFSIZE * HAMMER_FSBUF_MAXBLKS;
2062 fsbuf_offset -= offsetof(union hammer_fsbuf_ondisk, data.data[0][0]);
2063 live = &cluster->alist_mdata;
2064 nblks = (bytes + HAMMER_DATA_BLKMASK) & ~HAMMER_DATA_BLKMASK;
2065 nblks /= HAMMER_DATA_BLKSIZE;
2066 KKASSERT(fsbuf_offset >= 0 && fsbuf_offset % blksize == 0);
2067 elm_no += fsbuf_offset / blksize;
2068 hammer_alist_free(live, elm_no, nblks);
2072 hammer_free_record(hammer_cluster_t cluster, int32_t bclu_offset,
2075 const int32_t blksize = sizeof(union hammer_record_ondisk);
2076 int32_t fsbuf_offset = bclu_offset & HAMMER_BUFMASK;
2077 hammer_alist_t live;
2080 hammer_modify_cluster(cluster);
2081 elm_no = bclu_offset / HAMMER_BUFSIZE * HAMMER_FSBUF_MAXBLKS;
2082 fsbuf_offset -= offsetof(union hammer_fsbuf_ondisk, record.recs[0]);
2083 live = &cluster->alist_record;
2084 KKASSERT(fsbuf_offset >= 0 && fsbuf_offset % blksize == 0);
2085 elm_no += fsbuf_offset / blksize;
2086 hammer_alist_free(live, elm_no, 1);
2087 --cluster->ondisk->stat_records;
2088 if (rec_type == HAMMER_RECTYPE_CLUSTER)
2089 --cluster->ondisk->stat_records;
2094 * Allocate a new filesystem buffer and assign it to the specified
2095 * filesystem buffer type. The new buffer will be added to the
2096 * type-specific A-list and initialized.
2099 alloc_new_buffer(hammer_cluster_t cluster, u_int64_t type, hammer_alist_t live,
2100 int start, int *errorp, struct hammer_buffer **bufferp)
2102 hammer_buffer_t buffer;
2108 hammer_rel_buffer(*bufferp, 0);
2111 start = start / HAMMER_FSBUF_MAXBLKS; /* convert to buf_no */
2112 isfwd = (type != HAMMER_FSBUF_RECORDS);
2113 buf_no = hammer_alloc_master(cluster, 1, start, isfwd);
2114 if (buf_no == HAMMER_ALIST_BLOCK_NONE) {
2120 * The new buffer must be initialized (type != 0) regardless of
2121 * whether we already have it cached or not, so don't try to
2122 * optimize the cached buffer check. Just call hammer_get_buffer().
2124 buffer = hammer_get_buffer(cluster, buf_no, type, errorp);
2128 * Do a meta-free of the buffer's elements into the type-specific
2129 * A-list and update our statistics to reflect the allocation.
2133 kprintf("alloc_new_buffer buf_no %d type %016llx nelms %d\n",
2134 buf_no, type, nelements);
2136 hammer_modify_buffer(buffer); /*XXX*/
2137 hammer_adjust_stats(cluster, type, 1);
2140 * Free the buffer to the appropriate slave list so the
2141 * cluster-based allocator sees it.
2143 base_blk = buf_no * HAMMER_FSBUF_MAXBLKS;
2146 case HAMMER_FSBUF_BTREE:
2147 hammer_alist_free(live, base_blk, HAMMER_BTREE_NODES);
2149 case HAMMER_FSBUF_DATA:
2150 hammer_alist_free(live, base_blk, HAMMER_DATA_NODES);
2152 case HAMMER_FSBUF_RECORDS:
2153 hammer_alist_free(live, base_blk, HAMMER_RECORD_NODES);
2160 * Sync dirty buffers to the media
2163 static int hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
2164 static int hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
2167 hammer_sync_hmp(hammer_mount_t hmp, int waitfor)
2169 struct hammer_sync_info info;
2172 info.waitfor = waitfor;
2174 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_NOWAIT,
2175 hammer_sync_scan1, hammer_sync_scan2, &info);
2177 RB_SCAN(hammer_vol_rb_tree, &hmp->rb_vols_root, NULL,
2178 hammer_sync_volume, &info);
2183 hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
2185 struct hammer_inode *ip;
2188 if (vp->v_type == VNON || ip == NULL ||
2189 ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2190 RB_EMPTY(&vp->v_rbdirty_tree))) {
2197 hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2199 struct hammer_sync_info *info = data;
2200 struct hammer_inode *ip;
2204 if (vp->v_type == VNON || vp->v_type == VBAD ||
2205 ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
2206 RB_EMPTY(&vp->v_rbdirty_tree))) {
2209 if (vp->v_type != VCHR) {
2210 error = VOP_FSYNC(vp, info->waitfor);
2212 info->error = error;
2218 hammer_sync_volume(hammer_volume_t volume, void *data)
2220 struct hammer_sync_info *info = data;
2222 hammer_ref(&volume->io.lock);
2223 RB_SCAN(hammer_clu_rb_tree, &volume->rb_clus_root, NULL,
2224 hammer_sync_cluster, info);
2225 hammer_rel_volume(volume, 1);
2230 hammer_sync_cluster(hammer_cluster_t cluster, void *data)
2232 struct hammer_sync_info *info = data;
2235 * XXX check if cluster deleted and don't bother to sync it?
2237 hammer_ref(&cluster->io.lock);
2238 RB_SCAN(hammer_buf_rb_tree, &cluster->rb_bufs_root, NULL,
2239 hammer_sync_buffer, info);
2240 /*hammer_io_waitdep(&cluster->io);*/
2241 hammer_rel_cluster(cluster, 1);
2246 hammer_sync_buffer(hammer_buffer_t buffer, void *data __unused)
2248 hammer_ref(&buffer->io.lock);
2249 hammer_rel_buffer(buffer, 1);
2254 * Generic buffer initialization. Initialize the A-list into an all-allocated
2255 * state with the free block limit properly set.
2257 * Note that alloc_new_buffer() will free the appropriate block range via
2258 * the appropriate cluster alist, so the free count is properly propogated.
2261 hammer_initbuffer(hammer_alist_t live, hammer_fsbuf_head_t head, u_int64_t type)
2263 head->buf_type = type;
2266 case HAMMER_FSBUF_BTREE:
2267 hammer_alist_init(live, 0, HAMMER_BTREE_NODES,
2268 HAMMER_ASTATE_ALLOC);
2270 case HAMMER_FSBUF_DATA:
2271 hammer_alist_init(live, 0, HAMMER_DATA_NODES,
2272 HAMMER_ASTATE_ALLOC);
2274 case HAMMER_FSBUF_RECORDS:
2275 hammer_alist_init(live, 0, HAMMER_RECORD_NODES,
2276 HAMMER_ASTATE_ALLOC);
2279 hammer_alist_init(live, 0, 0, HAMMER_ASTATE_ALLOC);
2285 * Calculate the cluster's offset in the volume. This calculation is
2286 * slightly more complex when using superclusters because superclusters
2287 * are grouped in blocks of 16, followed by 16 x N clusters where N
2288 * is the number of clusters a supercluster can manage.
2291 calculate_cluster_offset(hammer_volume_t volume, int32_t clu_no)
2294 int64_t scl_group_size;
2297 if (volume->vol_flags & HAMMER_VOLF_USINGSUPERCL) {
2298 scl_group = clu_no / HAMMER_VOL_SUPERCLUSTER_GROUP /
2299 HAMMER_SCL_MAXCLUSTERS;
2301 ((int64_t)HAMMER_BUFSIZE *
2302 HAMMER_VOL_SUPERCLUSTER_GROUP) +
2303 ((int64_t)HAMMER_VOL_SUPERCLUSTER_GROUP *
2304 volume->vol_clsize * HAMMER_SCL_MAXCLUSTERS);
2306 HAMMER_VOL_SUPERCLUSTER_GROUP * HAMMER_BUFSIZE;
2308 off = volume->cluster_base +
2309 scl_group * scl_group_size +
2310 (HAMMER_BUFSIZE * HAMMER_VOL_SUPERCLUSTER_GROUP) +
2311 ((int64_t)clu_no % ((int64_t)HAMMER_SCL_MAXCLUSTERS *
2312 HAMMER_VOL_SUPERCLUSTER_GROUP))
2313 * volume->vol_clsize;
2315 off = volume->cluster_base +
2316 (int64_t)clu_no * volume->vol_clsize;
2322 * Calculate a super-cluster's offset in the volume.
2325 calculate_supercl_offset(hammer_volume_t volume, int32_t scl_no)
2329 int64_t scl_group_size;
2331 KKASSERT (volume->vol_flags & HAMMER_VOLF_USINGSUPERCL);
2332 scl_group = scl_no / HAMMER_VOL_SUPERCLUSTER_GROUP;
2335 ((int64_t)HAMMER_BUFSIZE *
2336 HAMMER_VOL_SUPERCLUSTER_GROUP) +
2337 ((int64_t)HAMMER_VOL_SUPERCLUSTER_GROUP *
2338 volume->vol_clsize * HAMMER_SCL_MAXCLUSTERS);
2340 HAMMER_VOL_SUPERCLUSTER_GROUP * HAMMER_BUFSIZE;
2341 off = volume->cluster_base + (scl_group * scl_group_size) +
2342 (scl_no % HAMMER_VOL_SUPERCLUSTER_GROUP) * HAMMER_BUFSIZE;
2344 off = volume->cluster_base + (scl_no * HAMMER_BUFSIZE);
2350 * Allocate nblks buffers from the cluster's master alist.
2353 hammer_alloc_master(hammer_cluster_t cluster, int nblks,
2354 int32_t start, int isfwd)
2358 hammer_modify_cluster(cluster);
2360 buf_no = hammer_alist_alloc_fwd(&cluster->alist_master,
2362 if (buf_no == HAMMER_ALIST_BLOCK_NONE) {
2363 buf_no = hammer_alist_alloc_fwd(&cluster->alist_master,
2367 buf_no = hammer_alist_alloc_rev(&cluster->alist_master,
2369 if (buf_no == HAMMER_ALIST_BLOCK_NONE) {
2370 buf_no = hammer_alist_alloc_rev(&cluster->alist_master,
2371 nblks, HAMMER_ALIST_BLOCK_MAX);
2376 * Recover space from empty record, b-tree, and data a-lists.
2383 * Adjust allocation statistics
2386 hammer_adjust_stats(hammer_cluster_t cluster, u_int64_t buf_type, int nblks)
2391 hammer_modify_cluster(cluster);
2392 hammer_modify_volume(cluster->volume);
2393 hammer_modify_volume(cluster->volume->hmp->rootvol);
2396 case HAMMER_FSBUF_BTREE:
2397 cluster->ondisk->stat_idx_bufs += nblks;
2398 cluster->volume->ondisk->vol_stat_idx_bufs += nblks;
2399 cluster->volume->hmp->rootvol->ondisk->vol0_stat_idx_bufs += nblks;
2401 case HAMMER_FSBUF_DATA:
2402 cluster->ondisk->stat_data_bufs += nblks;
2403 cluster->volume->ondisk->vol_stat_data_bufs += nblks;
2404 cluster->volume->hmp->rootvol->ondisk->vol0_stat_data_bufs += nblks;
2406 case HAMMER_FSBUF_RECORDS:
2407 cluster->ondisk->stat_rec_bufs += nblks;
2408 cluster->volume->ondisk->vol_stat_rec_bufs += nblks;
2409 cluster->volume->hmp->rootvol->ondisk->vol0_stat_rec_bufs += nblks;
2417 * Setup the parameters for the various A-lists we use in hammer. The
2418 * supercluster A-list must be chained to the cluster A-list and cluster
2419 * slave A-lists are chained to buffer A-lists.
2421 * See hammer_init_alist_config() below.
2425 * A-LIST - cluster recursion into a filesystem buffer
2427 * In the init case the buffer has already been initialized by
2428 * alloc_new_buffer() when it allocated the buffer out of the master
2429 * alist and marked it as free in the slave alist.
2431 * Because we use a somewhat odd mechanism to assign buffers to slave
2432 * pools we can't actually free the buffer back to the master alist in
2433 * buffer_alist_destroy(), but instead must deal with that logic somewhere
2437 buffer_alist_init(void *info, int32_t blk, int32_t radix,
2438 hammer_alloc_state_t state)
2444 * Note: This routine is only called when freeing the last elements of
2445 * an initialized buffer. Freeing all elements of the buffer when the
2446 * buffer was not previously initialized does not call this routine.
2449 buffer_alist_destroy(void *info, int32_t blk, int32_t radix)
2451 hammer_cluster_t cluster = info;
2454 buf_no = blk / HAMMER_FSBUF_MAXBLKS;
2455 kprintf("destroy buffer %d:%d:%d\n", cluster->volume->vol_no, cluster->clu_no, buf_no);
2460 * Note: atblk can be negative and atblk - blk can go negative.
2463 buffer_alist_alloc_fwd(void *info, int32_t blk, int32_t radix,
2464 int32_t count, int32_t atblk, int32_t *fullp)
2466 hammer_cluster_t cluster = info;
2467 hammer_buffer_t buffer;
2472 buf_no = blk / HAMMER_FSBUF_MAXBLKS;
2473 buffer = hammer_get_buffer(cluster, buf_no, 0, &error);
2475 KKASSERT(buffer->ondisk->head.buf_type != 0);
2477 hammer_modify_buffer(buffer);
2478 r = hammer_alist_alloc_fwd(&buffer->alist, count, atblk - blk);
2479 if (r != HAMMER_ALIST_BLOCK_NONE)
2481 *fullp = hammer_alist_isfull(&buffer->alist);
2482 hammer_rel_buffer(buffer, 0);
2484 r = HAMMER_ALIST_BLOCK_NONE;
2491 buffer_alist_alloc_rev(void *info, int32_t blk, int32_t radix,
2492 int32_t count, int32_t atblk, int32_t *fullp)
2494 hammer_cluster_t cluster = info;
2495 hammer_buffer_t buffer;
2500 buf_no = blk / HAMMER_FSBUF_MAXBLKS;
2501 buffer = hammer_get_buffer(cluster, buf_no, 0, &error);
2503 KKASSERT(buffer->ondisk->head.buf_type != 0);
2504 hammer_modify_buffer(buffer);
2505 r = hammer_alist_alloc_rev(&buffer->alist, count, atblk - blk);
2506 if (r != HAMMER_ALIST_BLOCK_NONE)
2508 *fullp = hammer_alist_isfull(&buffer->alist);
2509 hammer_rel_buffer(buffer, 0);
2511 r = HAMMER_ALIST_BLOCK_NONE;
2518 buffer_alist_free(void *info, int32_t blk, int32_t radix,
2519 int32_t base_blk, int32_t count, int32_t *emptyp)
2521 hammer_cluster_t cluster = info;
2522 hammer_buffer_t buffer;
2526 buf_no = blk / HAMMER_FSBUF_MAXBLKS;
2527 buffer = hammer_get_buffer(cluster, buf_no, 0, &error);
2529 KKASSERT(buffer->ondisk->head.buf_type != 0);
2530 hammer_modify_buffer(buffer);
2531 hammer_alist_free(&buffer->alist, base_blk, count);
2532 *emptyp = hammer_alist_isempty(&buffer->alist);
2533 hammer_rel_buffer(buffer, 0);
2540 buffer_alist_find(void *info, int32_t blk, int32_t radix, int32_t atblk,
2543 hammer_cluster_t cluster = info;
2544 hammer_buffer_t buffer;
2549 buf_no = blk / HAMMER_FSBUF_MAXBLKS;
2550 buffer = hammer_get_buffer(cluster, buf_no, 0, &error);
2552 KKASSERT(buffer->ondisk->head.buf_type != 0);
2553 switch(buffer->ondisk->head.buf_type) {
2554 case HAMMER_FSBUF_RECORDS:
2555 maxblks = HAMMER_RECORD_NODES;
2557 case HAMMER_FSBUF_BTREE:
2558 maxblks = HAMMER_BTREE_NODES;
2560 case HAMMER_FSBUF_DATA:
2561 maxblks = HAMMER_DATA_NODES;
2564 panic("buffer_alist_find: unknown buffer type");
2568 blk = hammer_alist_find(&buffer->alist, atblk - blk, maxblks,
2570 hammer_rel_buffer(buffer, 0);
2572 blk = HAMMER_ALIST_BLOCK_NONE;
2578 buffer_alist_print(void *info, int32_t blk, int32_t radix, int tab)
2583 * A-LIST - super-cluster recursion into a cluster and cluster recursion
2584 * into a filesystem buffer. A-List's are mostly self-contained entities,
2585 * but callbacks must be installed to recurse from one A-List to another.
2587 * Implementing these callbacks allows us to operate a multi-layered A-List
2588 * as a single entity.
2592 * This occurs when allocating a cluster via the volume a-list and the
2593 * entry in the volume a-list indicated all-free. The underlying supercl
2594 * has not yet been initialized.
2597 super_alist_init(void *info, int32_t blk, int32_t radix,
2598 hammer_alloc_state_t state)
2600 hammer_volume_t volume = info;
2601 hammer_supercl_t supercl;
2606 * Calculate the super-cluster number containing the cluster (blk)
2607 * and obtain the super-cluster buffer.
2609 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2610 supercl = hammer_get_supercl(volume, scl_no, &error, state);
2612 hammer_rel_supercl(supercl, 0);
2617 super_alist_recover(void *info, int32_t blk, int32_t radix, int32_t count)
2619 hammer_volume_t volume = info;
2620 hammer_supercl_t supercl;
2625 * Calculate the super-cluster number containing the cluster (blk)
2626 * and obtain the super-cluster buffer.
2628 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2629 supercl = hammer_get_supercl(volume, scl_no, &error,
2630 HAMMER_ASTATE_NONE);
2632 hammer_modify_supercl(supercl);
2633 error = hammer_alist_recover(&supercl->alist, blk, 0, count);
2634 /* free block count is returned if >= 0 */
2635 hammer_rel_supercl(supercl, 0);
2643 * This occurs when freeing a cluster via the volume a-list and the
2644 * supercl is now 100% free. We can destroy the supercl.
2646 * What we actually do is just unset the modify bit so it doesn't get
2650 super_alist_destroy(void *info, int32_t blk, int32_t radix)
2652 hammer_volume_t volume = info;
2653 hammer_supercl_t supercl;
2658 * Calculate the super-cluster number containing the cluster (blk)
2659 * and obtain the super-cluster buffer.
2661 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2662 if (hammer_find_supercl(volume, scl_no)) {
2663 supercl = hammer_get_supercl(volume, scl_no, &error,
2664 HAMMER_ASTATE_FREE);
2667 hammer_io_clear_modify(&supercl->io);
2668 hammer_rel_supercl(supercl, 0);
2675 super_alist_alloc_fwd(void *info, int32_t blk, int32_t radix,
2676 int32_t count, int32_t atblk, int32_t *fullp)
2678 hammer_volume_t volume = info;
2679 hammer_supercl_t supercl;
2684 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2685 supercl = hammer_get_supercl(volume, scl_no, &error, 0);
2687 hammer_modify_supercl(supercl);
2688 r = hammer_alist_alloc_fwd(&supercl->alist, count, atblk - blk);
2689 if (r != HAMMER_ALIST_BLOCK_NONE)
2691 *fullp = hammer_alist_isfull(&supercl->alist);
2692 hammer_rel_supercl(supercl, 0);
2694 r = HAMMER_ALIST_BLOCK_NONE;
2701 super_alist_alloc_rev(void *info, int32_t blk, int32_t radix,
2702 int32_t count, int32_t atblk, int32_t *fullp)
2704 hammer_volume_t volume = info;
2705 hammer_supercl_t supercl;
2710 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2711 supercl = hammer_get_supercl(volume, scl_no, &error, 0);
2713 hammer_modify_supercl(supercl);
2714 r = hammer_alist_alloc_rev(&supercl->alist, count, atblk - blk);
2715 if (r != HAMMER_ALIST_BLOCK_NONE)
2717 *fullp = hammer_alist_isfull(&supercl->alist);
2718 hammer_rel_supercl(supercl, 0);
2720 r = HAMMER_ALIST_BLOCK_NONE;
2727 super_alist_free(void *info, int32_t blk, int32_t radix,
2728 int32_t base_blk, int32_t count, int32_t *emptyp)
2730 hammer_volume_t volume = info;
2731 hammer_supercl_t supercl;
2735 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2736 supercl = hammer_get_supercl(volume, scl_no, &error, 0);
2738 hammer_modify_supercl(supercl);
2739 hammer_alist_free(&supercl->alist, base_blk, count);
2740 *emptyp = hammer_alist_isempty(&supercl->alist);
2741 hammer_rel_supercl(supercl, 0);
2748 super_alist_find(void *info, int32_t blk, int32_t radix, int32_t atblk,
2751 hammer_volume_t volume = info;
2752 hammer_supercl_t supercl;
2757 scl_no = blk / HAMMER_SCL_MAXCLUSTERS;
2758 supercl = hammer_get_supercl(volume, scl_no, &error, 0);
2760 nclusters = supercl->volume->ondisk->vol_nclusters -
2761 ((int64_t)supercl->scl_no * HAMMER_SCL_MAXCLUSTERS);
2762 KKASSERT(nclusters > 0);
2763 if (nclusters > HAMMER_SCL_MAXCLUSTERS)
2764 nclusters = HAMMER_SCL_MAXCLUSTERS;
2765 blk = hammer_alist_find(&supercl->alist, atblk - blk,
2767 hammer_rel_supercl(supercl, 0);
2769 blk = HAMMER_ALIST_BLOCK_NONE;
2775 super_alist_print(void *info, int32_t blk, int32_t radix, int tab)
2780 hammer_init_alist_config(void)
2782 hammer_alist_config_t config;
2784 hammer_alist_template(&Buf_alist_config, HAMMER_FSBUF_MAXBLKS,
2785 1, HAMMER_FSBUF_METAELMS, 0);
2786 hammer_alist_template(&Vol_normal_alist_config, HAMMER_VOL_MAXCLUSTERS,
2787 1, HAMMER_VOL_METAELMS_1LYR, 0);
2788 hammer_alist_template(&Vol_super_alist_config,
2789 HAMMER_VOL_MAXSUPERCLUSTERS * HAMMER_SCL_MAXCLUSTERS,
2790 HAMMER_SCL_MAXCLUSTERS, HAMMER_VOL_METAELMS_2LYR,
2792 hammer_alist_template(&Supercl_alist_config, HAMMER_VOL_MAXCLUSTERS,
2793 1, HAMMER_SUPERCL_METAELMS, 0);
2794 hammer_alist_template(&Clu_master_alist_config, HAMMER_CLU_MAXBUFFERS,
2795 1, HAMMER_CLU_MASTER_METAELMS, 0);
2796 hammer_alist_template(&Clu_slave_alist_config,
2797 HAMMER_CLU_MAXBUFFERS * HAMMER_FSBUF_MAXBLKS,
2798 HAMMER_FSBUF_MAXBLKS, HAMMER_CLU_SLAVE_METAELMS,
2801 config = &Vol_super_alist_config;
2802 config->bl_radix_init = super_alist_init;
2803 config->bl_radix_recover = super_alist_recover;
2804 config->bl_radix_destroy = super_alist_destroy;
2805 config->bl_radix_alloc_fwd = super_alist_alloc_fwd;
2806 config->bl_radix_alloc_rev = super_alist_alloc_rev;
2807 config->bl_radix_free = super_alist_free;
2808 config->bl_radix_find = super_alist_find;
2809 config->bl_radix_print = super_alist_print;
2811 config = &Clu_slave_alist_config;
2812 config->bl_radix_init = buffer_alist_init;
2813 config->bl_radix_recover = buffer_alist_recover;
2814 config->bl_radix_destroy = buffer_alist_destroy;
2815 config->bl_radix_alloc_fwd = buffer_alist_alloc_fwd;
2816 config->bl_radix_alloc_rev = buffer_alist_alloc_rev;
2817 config->bl_radix_free = buffer_alist_free;
2818 config->bl_radix_find = buffer_alist_find;
2819 config->bl_radix_print = buffer_alist_print;