2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_ondisk.c,v 1.51 2008/06/08 18:16:26 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_buffer(hammer_buffer_t buffer, int isnew);
51 static int hammer_load_node(hammer_node_t node, int isnew);
54 * Red-Black tree support for various structures
57 hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2)
59 if (ip1->obj_id < ip2->obj_id)
61 if (ip1->obj_id > ip2->obj_id)
63 if (ip1->obj_asof < ip2->obj_asof)
65 if (ip1->obj_asof > ip2->obj_asof)
71 hammer_inode_info_cmp(hammer_inode_info_t info, hammer_inode_t ip)
73 if (info->obj_id < ip->obj_id)
75 if (info->obj_id > ip->obj_id)
77 if (info->obj_asof < ip->obj_asof)
79 if (info->obj_asof > ip->obj_asof)
85 hammer_vol_rb_compare(hammer_volume_t vol1, hammer_volume_t vol2)
87 if (vol1->vol_no < vol2->vol_no)
89 if (vol1->vol_no > vol2->vol_no)
95 hammer_buf_rb_compare(hammer_buffer_t buf1, hammer_buffer_t buf2)
97 if (buf1->zoneX_offset < buf2->zoneX_offset)
99 if (buf1->zoneX_offset > buf2->zoneX_offset)
105 hammer_nod_rb_compare(hammer_node_t node1, hammer_node_t node2)
107 if (node1->node_offset < node2->node_offset)
109 if (node1->node_offset > node2->node_offset)
115 * Note: The lookup function for hammer_ino_rb_tree winds up being named
116 * hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info). The other lookup
117 * functions are normal, e.g. hammer_buf_rb_tree_RB_LOOKUP(root, zone2_offset).
119 RB_GENERATE(hammer_ino_rb_tree, hammer_inode, rb_node, hammer_ino_rb_compare);
120 RB_GENERATE_XLOOKUP(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
121 hammer_inode_info_cmp, hammer_inode_info_t);
122 RB_GENERATE2(hammer_vol_rb_tree, hammer_volume, rb_node,
123 hammer_vol_rb_compare, int32_t, vol_no);
124 RB_GENERATE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
125 hammer_buf_rb_compare, hammer_off_t, zoneX_offset);
126 RB_GENERATE2(hammer_nod_rb_tree, hammer_node, rb_node,
127 hammer_nod_rb_compare, hammer_off_t, node_offset);
129 /************************************************************************
131 ************************************************************************
133 * Load a HAMMER volume by name. Returns 0 on success or a positive error
134 * code on failure. Volumes must be loaded at mount time, get_volume() will
135 * not load a new volume.
137 * Calls made to hammer_load_volume() or single-threaded
140 hammer_install_volume(struct hammer_mount *hmp, const char *volname)
143 hammer_volume_t volume;
144 struct hammer_volume_ondisk *ondisk;
145 struct nlookupdata nd;
146 struct buf *bp = NULL;
152 ronly = ((mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
155 * Allocate a volume structure
157 ++hammer_count_volumes;
158 volume = kmalloc(sizeof(*volume), M_HAMMER, M_WAITOK|M_ZERO);
159 volume->vol_name = kstrdup(volname, M_HAMMER);
160 hammer_io_init(&volume->io, hmp, HAMMER_STRUCTURE_VOLUME);
161 volume->io.offset = 0LL;
164 * Get the device vnode
166 error = nlookup_init(&nd, volume->vol_name, UIO_SYSSPACE, NLC_FOLLOW);
168 error = nlookup(&nd);
170 error = cache_vref(&nd.nl_nch, nd.nl_cred, &volume->devvp);
173 if (vn_isdisk(volume->devvp, &error)) {
174 error = vfs_mountedon(volume->devvp);
178 count_udev(volume->devvp->v_umajor, volume->devvp->v_uminor) > 0) {
182 vn_lock(volume->devvp, LK_EXCLUSIVE | LK_RETRY);
183 error = vinvalbuf(volume->devvp, V_SAVE, 0, 0);
185 error = VOP_OPEN(volume->devvp,
186 (ronly ? FREAD : FREAD|FWRITE),
189 vn_unlock(volume->devvp);
192 hammer_free_volume(volume);
195 volume->devvp->v_rdev->si_mountpoint = mp;
199 * Extract the volume number from the volume header and do various
202 error = bread(volume->devvp, 0LL, HAMMER_BUFSIZE, &bp);
205 ondisk = (void *)bp->b_data;
206 if (ondisk->vol_signature != HAMMER_FSBUF_VOLUME) {
207 kprintf("hammer_mount: volume %s has an invalid header\n",
212 volume->vol_no = ondisk->vol_no;
213 volume->buffer_base = ondisk->vol_buf_beg;
214 volume->vol_flags = ondisk->vol_flags;
215 volume->nblocks = ondisk->vol_nblocks;
216 volume->maxbuf_off = HAMMER_ENCODE_RAW_BUFFER(volume->vol_no,
217 ondisk->vol_buf_end - ondisk->vol_buf_beg);
218 volume->maxraw_off = ondisk->vol_buf_end;
220 if (RB_EMPTY(&hmp->rb_vols_root)) {
221 hmp->fsid = ondisk->vol_fsid;
222 } else if (bcmp(&hmp->fsid, &ondisk->vol_fsid, sizeof(uuid_t))) {
223 kprintf("hammer_mount: volume %s's fsid does not match "
224 "other volumes\n", volume->vol_name);
230 * Insert the volume structure into the red-black tree.
232 if (RB_INSERT(hammer_vol_rb_tree, &hmp->rb_vols_root, volume)) {
233 kprintf("hammer_mount: volume %s has a duplicate vol_no %d\n",
234 volume->vol_name, volume->vol_no);
239 * Set the root volume . HAMMER special cases rootvol the structure.
240 * We do not hold a ref because this would prevent related I/O
241 * from being flushed.
243 if (error == 0 && ondisk->vol_rootvol == ondisk->vol_no) {
244 hmp->rootvol = volume;
249 hmp->fsid_udev = dev2udev(vn_todev(volume->devvp));
250 hmp->mp->mnt_stat.f_blocks += ondisk->vol0_stat_bigblocks *
251 (HAMMER_LARGEBLOCK_SIZE / HAMMER_BUFSIZE);
252 hmp->mp->mnt_vstat.f_blocks += ondisk->vol0_stat_bigblocks *
253 (HAMMER_LARGEBLOCK_SIZE / HAMMER_BUFSIZE);
259 /*vinvalbuf(volume->devvp, V_SAVE, 0, 0);*/
261 volume->devvp->v_rdev->si_mountpoint = NULL;
262 VOP_CLOSE(volume->devvp, ronly ? FREAD : FREAD|FWRITE);
263 hammer_free_volume(volume);
269 * This is called for each volume when updating the mount point from
270 * read-write to read-only or vise-versa.
273 hammer_adjust_volume_mode(hammer_volume_t volume, void *data __unused)
276 vn_lock(volume->devvp, LK_EXCLUSIVE | LK_RETRY);
277 if (volume->io.hmp->ronly) {
278 /* do not call vinvalbuf */
279 VOP_OPEN(volume->devvp, FREAD, FSCRED, NULL);
280 VOP_CLOSE(volume->devvp, FREAD|FWRITE);
282 /* do not call vinvalbuf */
283 VOP_OPEN(volume->devvp, FREAD|FWRITE, FSCRED, NULL);
284 VOP_CLOSE(volume->devvp, FREAD);
286 vn_unlock(volume->devvp);
292 * Unload and free a HAMMER volume. Must return >= 0 to continue scan
293 * so returns -1 on failure.
296 hammer_unload_volume(hammer_volume_t volume, void *data __unused)
298 struct hammer_mount *hmp = volume->io.hmp;
299 int ronly = ((hmp->mp->mnt_flag & MNT_RDONLY) ? 1 : 0);
302 * Clean up the root volume pointer, which is held unlocked in hmp.
304 if (hmp->rootvol == volume)
308 * Release our buffer and flush anything left in the buffer cache.
310 volume->io.waitdep = 1;
311 hammer_io_release(&volume->io, 1);
314 * There should be no references on the volume, no clusters, and
317 KKASSERT(volume->io.lock.refs == 0);
319 volume->ondisk = NULL;
321 if (volume->devvp->v_rdev &&
322 volume->devvp->v_rdev->si_mountpoint == hmp->mp
324 volume->devvp->v_rdev->si_mountpoint = NULL;
327 vinvalbuf(volume->devvp, 0, 0, 0);
328 VOP_CLOSE(volume->devvp, FREAD);
330 vinvalbuf(volume->devvp, V_SAVE, 0, 0);
331 VOP_CLOSE(volume->devvp, FREAD|FWRITE);
336 * Destroy the structure
338 RB_REMOVE(hammer_vol_rb_tree, &hmp->rb_vols_root, volume);
339 hammer_free_volume(volume);
345 hammer_free_volume(hammer_volume_t volume)
347 if (volume->vol_name) {
348 kfree(volume->vol_name, M_HAMMER);
349 volume->vol_name = NULL;
352 vrele(volume->devvp);
353 volume->devvp = NULL;
355 --hammer_count_volumes;
356 kfree(volume, M_HAMMER);
360 * Get a HAMMER volume. The volume must already exist.
363 hammer_get_volume(struct hammer_mount *hmp, int32_t vol_no, int *errorp)
365 struct hammer_volume *volume;
368 * Locate the volume structure
370 volume = RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, vol_no);
371 if (volume == NULL) {
375 hammer_ref(&volume->io.lock);
378 * Deal with on-disk info
380 if (volume->ondisk == NULL || volume->io.loading) {
381 *errorp = hammer_load_volume(volume);
383 hammer_rel_volume(volume, 1);
393 hammer_ref_volume(hammer_volume_t volume)
397 hammer_ref(&volume->io.lock);
400 * Deal with on-disk info
402 if (volume->ondisk == NULL || volume->io.loading) {
403 error = hammer_load_volume(volume);
405 hammer_rel_volume(volume, 1);
413 hammer_get_root_volume(struct hammer_mount *hmp, int *errorp)
415 hammer_volume_t volume;
417 volume = hmp->rootvol;
418 KKASSERT(volume != NULL);
419 hammer_ref(&volume->io.lock);
422 * Deal with on-disk info
424 if (volume->ondisk == NULL || volume->io.loading) {
425 *errorp = hammer_load_volume(volume);
427 hammer_rel_volume(volume, 1);
437 * Load a volume's on-disk information. The volume must be referenced and
438 * not locked. We temporarily acquire an exclusive lock to interlock
439 * against releases or multiple get's.
442 hammer_load_volume(hammer_volume_t volume)
446 ++volume->io.loading;
447 hammer_lock_ex(&volume->io.lock);
449 if (volume->ondisk == NULL) {
450 error = hammer_io_read(volume->devvp, &volume->io,
453 volume->ondisk = (void *)volume->io.bp->b_data;
457 --volume->io.loading;
458 hammer_unlock(&volume->io.lock);
463 * Release a volume. Call hammer_io_release on the last reference. We have
464 * to acquire an exclusive lock to interlock against volume->ondisk tests
465 * in hammer_load_volume(), and hammer_io_release() also expects an exclusive
468 * Volumes are not unloaded from memory during normal operation.
471 hammer_rel_volume(hammer_volume_t volume, int flush)
474 if (volume->io.lock.refs == 1) {
475 ++volume->io.loading;
476 hammer_lock_ex(&volume->io.lock);
477 if (volume->io.lock.refs == 1) {
478 volume->ondisk = NULL;
479 hammer_io_release(&volume->io, flush);
481 --volume->io.loading;
482 hammer_unlock(&volume->io.lock);
484 hammer_unref(&volume->io.lock);
488 /************************************************************************
490 ************************************************************************
492 * Manage buffers. Currently all blockmap-backed zones are translated
493 * to zone-2 buffer offsets.
496 hammer_get_buffer(hammer_mount_t hmp, hammer_off_t buf_offset,
497 int isnew, int *errorp)
499 hammer_buffer_t buffer;
500 hammer_volume_t volume;
501 hammer_off_t zone2_offset;
502 hammer_io_type_t iotype;
508 * Shortcut if the buffer is already cached
510 buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root,
511 buf_offset & ~HAMMER_BUFMASK64);
513 hammer_ref(&buffer->io.lock);
514 if (buffer->ondisk && buffer->io.loading == 0) {
520 * The buffer is no longer loose if it has a ref. Loose
521 * buffers will never be in a modified state. This should
522 * only occur on the 0->1 transition of refs.
524 if (buffer->io.mod_list == &hmp->lose_list) {
525 TAILQ_REMOVE(buffer->io.mod_list, &buffer->io,
527 buffer->io.mod_list = NULL;
528 KKASSERT(buffer->io.modified == 0);
534 * What is the buffer class?
536 zone = HAMMER_ZONE_DECODE(buf_offset);
539 case HAMMER_ZONE_LARGE_DATA_INDEX:
540 case HAMMER_ZONE_SMALL_DATA_INDEX:
541 iotype = HAMMER_STRUCTURE_DATA_BUFFER;
543 case HAMMER_ZONE_UNDO_INDEX:
544 iotype = HAMMER_STRUCTURE_UNDO_BUFFER;
547 iotype = HAMMER_STRUCTURE_META_BUFFER;
552 * Handle blockmap offset translations
554 if (zone >= HAMMER_ZONE_BTREE_INDEX) {
555 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, errorp);
556 } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
557 zone2_offset = hammer_undo_lookup(hmp, buf_offset, errorp);
559 KKASSERT(zone == HAMMER_ZONE_RAW_BUFFER_INDEX);
560 zone2_offset = buf_offset;
567 * Calculate the base zone2-offset and acquire the volume
569 * NOTE: zone2_offset and maxbuf_off are both full zone-2 offset
572 zone2_offset &= ~HAMMER_BUFMASK64;
573 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
574 HAMMER_ZONE_RAW_BUFFER);
575 vol_no = HAMMER_VOL_DECODE(zone2_offset);
576 volume = hammer_get_volume(hmp, vol_no, errorp);
580 KKASSERT(zone2_offset < volume->maxbuf_off);
583 * Allocate a new buffer structure. We will check for races later.
585 ++hammer_count_buffers;
586 buffer = kmalloc(sizeof(*buffer), M_HAMMER, M_WAITOK|M_ZERO);
587 buffer->zone2_offset = zone2_offset;
588 buffer->zoneX_offset = buf_offset;
589 buffer->volume = volume;
591 hammer_io_init(&buffer->io, hmp, iotype);
592 buffer->io.offset = volume->ondisk->vol_buf_beg +
593 (zone2_offset & HAMMER_OFF_SHORT_MASK);
594 TAILQ_INIT(&buffer->clist);
595 hammer_ref(&buffer->io.lock);
598 * Insert the buffer into the RB tree and handle late collisions.
600 if (RB_INSERT(hammer_buf_rb_tree, &hmp->rb_bufs_root, buffer)) {
601 hammer_unref(&buffer->io.lock);
602 --hammer_count_buffers;
603 kfree(buffer, M_HAMMER);
609 * Deal with on-disk info and loading races.
611 if (buffer->ondisk == NULL || buffer->io.loading) {
612 *errorp = hammer_load_buffer(buffer, isnew);
614 hammer_rel_buffer(buffer, 1);
624 * Destroy all buffers covering the specified zoneX offset range. This
625 * is called when the related blockmap layer2 entry is freed. The buffers
626 * must not be in use or modified.
629 hammer_del_buffers(hammer_mount_t hmp, hammer_off_t base_offset,
630 hammer_off_t zone2_offset, int bytes)
632 hammer_buffer_t buffer;
633 hammer_volume_t volume;
637 vol_no = HAMMER_VOL_DECODE(zone2_offset);
638 volume = hammer_get_volume(hmp, vol_no, &error);
639 KKASSERT(error == 0);
642 buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root,
645 KKASSERT(buffer->io.lock.refs == 0);
646 KKASSERT(buffer->io.modified == 0);
647 KKASSERT(buffer->zone2_offset == zone2_offset);
648 KKASSERT(buffer->volume == volume);
649 hammer_unload_buffer(buffer, NULL);
651 hammer_io_inval(volume, zone2_offset);
652 base_offset += HAMMER_BUFSIZE;
653 zone2_offset += HAMMER_BUFSIZE;
654 bytes -= HAMMER_BUFSIZE;
656 hammer_rel_volume(volume, 0);
660 hammer_load_buffer(hammer_buffer_t buffer, int isnew)
662 hammer_volume_t volume;
666 * Load the buffer's on-disk info
668 volume = buffer->volume;
669 ++buffer->io.loading;
670 hammer_lock_ex(&buffer->io.lock);
672 if (hammer_debug_io & 0x0001) {
673 kprintf("load_buffer %016llx %016llx\n",
674 buffer->zoneX_offset, buffer->zone2_offset);
677 if (buffer->ondisk == NULL) {
679 error = hammer_io_new(volume->devvp, &buffer->io);
681 error = hammer_io_read(volume->devvp, &buffer->io,
685 buffer->ondisk = (void *)buffer->io.bp->b_data;
687 error = hammer_io_new(volume->devvp, &buffer->io);
691 --buffer->io.loading;
692 hammer_unlock(&buffer->io.lock);
697 * NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
700 hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused)
702 hammer_ref(&buffer->io.lock);
703 hammer_flush_buffer_nodes(buffer);
704 KKASSERT(buffer->io.lock.refs == 1);
705 hammer_rel_buffer(buffer, 2);
710 * Reference a buffer that is either already referenced or via a specially
711 * handled pointer (aka cursor->buffer).
714 hammer_ref_buffer(hammer_buffer_t buffer)
718 hammer_ref(&buffer->io.lock);
723 if (buffer->io.mod_list == &buffer->io.hmp->lose_list) {
724 TAILQ_REMOVE(buffer->io.mod_list, &buffer->io, mod_entry);
725 buffer->io.mod_list = NULL;
728 if (buffer->ondisk == NULL || buffer->io.loading) {
729 error = hammer_load_buffer(buffer, 0);
731 hammer_rel_buffer(buffer, 1);
733 * NOTE: buffer pointer can become stale after
744 * Release a buffer. We have to deal with several places where
745 * another thread can ref the buffer.
747 * Only destroy the structure itself if the related buffer cache buffer
748 * was disassociated from it. This ties the management of the structure
749 * to the buffer cache subsystem. buffer->ondisk determines whether the
750 * embedded io is referenced or not.
753 hammer_rel_buffer(hammer_buffer_t buffer, int flush)
755 hammer_volume_t volume;
759 if (buffer->io.lock.refs == 1) {
760 ++buffer->io.loading; /* force interlock check */
761 hammer_lock_ex(&buffer->io.lock);
762 if (buffer->io.lock.refs == 1) {
763 hammer_io_release(&buffer->io, flush);
764 hammer_flush_buffer_nodes(buffer);
765 KKASSERT(TAILQ_EMPTY(&buffer->clist));
767 if (buffer->io.bp == NULL &&
768 buffer->io.lock.refs == 1) {
772 RB_REMOVE(hammer_buf_rb_tree,
773 &buffer->io.hmp->rb_bufs_root,
775 volume = buffer->volume;
776 buffer->volume = NULL; /* sanity */
777 hammer_rel_volume(volume, 0);
781 --buffer->io.loading;
782 hammer_unlock(&buffer->io.lock);
784 hammer_unref(&buffer->io.lock);
787 KKASSERT(buffer->io.mod_list == NULL);
788 --hammer_count_buffers;
789 kfree(buffer, M_HAMMER);
794 * Access the filesystem buffer containing the specified hammer offset.
795 * buf_offset is a conglomeration of the volume number and vol_buf_beg
796 * relative buffer offset. It must also have bit 55 set to be valid.
797 * (see hammer_off_t in hammer_disk.h).
799 * Any prior buffer in *bufferp will be released and replaced by the
803 hammer_bread(hammer_mount_t hmp, hammer_off_t buf_offset, int *errorp,
804 struct hammer_buffer **bufferp)
806 hammer_buffer_t buffer;
807 int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
809 buf_offset &= ~HAMMER_BUFMASK64;
810 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) != 0);
813 if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
814 buffer->zoneX_offset != buf_offset)) {
816 hammer_rel_buffer(buffer, 0);
817 buffer = hammer_get_buffer(hmp, buf_offset, 0, errorp);
824 * Return a pointer to the buffer data.
829 return((char *)buffer->ondisk + xoff);
833 * Access the filesystem buffer containing the specified hammer offset.
834 * No disk read operation occurs. The result buffer may contain garbage.
836 * Any prior buffer in *bufferp will be released and replaced by the
839 * This function marks the buffer dirty but does not increment its
843 hammer_bnew(hammer_mount_t hmp, hammer_off_t buf_offset, int *errorp,
844 struct hammer_buffer **bufferp)
846 hammer_buffer_t buffer;
847 int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
849 buf_offset &= ~HAMMER_BUFMASK64;
852 if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
853 buffer->zoneX_offset != buf_offset)) {
855 hammer_rel_buffer(buffer, 0);
856 buffer = hammer_get_buffer(hmp, buf_offset, 1, errorp);
863 * Return a pointer to the buffer data.
868 return((char *)buffer->ondisk + xoff);
871 /************************************************************************
873 ************************************************************************
875 * Manage B-Tree nodes. B-Tree nodes represent the primary indexing
876 * method used by the HAMMER filesystem.
878 * Unlike other HAMMER structures, a hammer_node can be PASSIVELY
879 * associated with its buffer, and will only referenced the buffer while
880 * the node itself is referenced.
882 * A hammer_node can also be passively associated with other HAMMER
883 * structures, such as inodes, while retaining 0 references. These
884 * associations can be cleared backwards using a pointer-to-pointer in
887 * This allows the HAMMER implementation to cache hammer_nodes long-term
888 * and short-cut a great deal of the infrastructure's complexity. In
889 * most cases a cached node can be reacquired without having to dip into
890 * either the buffer or cluster management code.
892 * The caller must pass a referenced cluster on call and will retain
893 * ownership of the reference on return. The node will acquire its own
894 * additional references, if necessary.
897 hammer_get_node(hammer_mount_t hmp, hammer_off_t node_offset,
898 int isnew, int *errorp)
902 KKASSERT((node_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_BTREE);
905 * Locate the structure, allocating one if necessary.
908 node = RB_LOOKUP(hammer_nod_rb_tree, &hmp->rb_nods_root, node_offset);
910 ++hammer_count_nodes;
911 node = kmalloc(sizeof(*node), M_HAMMER, M_WAITOK|M_ZERO);
912 node->node_offset = node_offset;
914 if (RB_INSERT(hammer_nod_rb_tree, &hmp->rb_nods_root, node)) {
915 --hammer_count_nodes;
916 kfree(node, M_HAMMER);
920 hammer_ref(&node->lock);
924 *errorp = hammer_load_node(node, isnew);
926 hammer_rel_node(node);
933 * Reference an already-referenced node.
936 hammer_ref_node(hammer_node_t node)
938 KKASSERT(node->lock.refs > 0 && node->ondisk != NULL);
939 hammer_ref(&node->lock);
943 * Load a node's on-disk data reference.
946 hammer_load_node(hammer_node_t node, int isnew)
948 hammer_buffer_t buffer;
949 hammer_off_t buf_offset;
954 hammer_lock_ex(&node->lock);
955 if (node->ondisk == NULL) {
957 * This is a little confusing but the jist is that
958 * node->buffer determines whether the node is on
959 * the buffer's clist and node->ondisk determines
960 * whether the buffer is referenced.
962 * We could be racing a buffer release, in which case
963 * node->buffer may become NULL while we are blocked
964 * referencing the buffer.
966 if ((buffer = node->buffer) != NULL) {
967 error = hammer_ref_buffer(buffer);
968 if (error == 0 && node->buffer == NULL) {
969 TAILQ_INSERT_TAIL(&buffer->clist,
971 node->buffer = buffer;
974 buf_offset = node->node_offset & ~HAMMER_BUFMASK64;
975 buffer = hammer_get_buffer(node->hmp, buf_offset,
978 KKASSERT(error == 0);
979 TAILQ_INSERT_TAIL(&buffer->clist,
981 node->buffer = buffer;
985 node->ondisk = (void *)((char *)buffer->ondisk +
986 (node->node_offset & HAMMER_BUFMASK));
988 hammer_crc_test_btree(node->ondisk) == 0) {
989 Debugger("CRC FAILED: B-TREE NODE");
994 hammer_unlock(&node->lock);
999 * Safely reference a node, interlock against flushes via the IO subsystem.
1002 hammer_ref_node_safe(struct hammer_mount *hmp, struct hammer_node **cache,
1009 hammer_ref(&node->lock);
1013 *errorp = hammer_load_node(node, 0);
1015 hammer_rel_node(node);
1025 * Release a hammer_node. On the last release the node dereferences
1026 * its underlying buffer and may or may not be destroyed.
1029 hammer_rel_node(hammer_node_t node)
1031 hammer_buffer_t buffer;
1034 * If this isn't the last ref just decrement the ref count and
1037 if (node->lock.refs > 1) {
1038 hammer_unref(&node->lock);
1043 * If there is no ondisk info or no buffer the node failed to load,
1044 * remove the last reference and destroy the node.
1046 if (node->ondisk == NULL) {
1047 hammer_unref(&node->lock);
1048 hammer_flush_node(node);
1049 /* node is stale now */
1054 * Do final cleanups and then either destroy the node and leave it
1055 * passively cached. The buffer reference is removed regardless.
1057 buffer = node->buffer;
1058 node->ondisk = NULL;
1060 if ((node->flags & HAMMER_NODE_FLUSH) == 0) {
1061 hammer_unref(&node->lock);
1062 hammer_rel_buffer(buffer, 0);
1069 hammer_unref(&node->lock);
1070 hammer_flush_node(node);
1072 hammer_rel_buffer(buffer, 0);
1076 * Free space on-media associated with a B-Tree node.
1079 hammer_delete_node(hammer_transaction_t trans, hammer_node_t node)
1081 KKASSERT((node->flags & HAMMER_NODE_DELETED) == 0);
1082 node->flags |= HAMMER_NODE_DELETED;
1083 hammer_blockmap_free(trans, node->node_offset, sizeof(*node->ondisk));
1087 * Passively cache a referenced hammer_node in *cache. The caller may
1088 * release the node on return.
1091 hammer_cache_node(hammer_node_t node, struct hammer_node **cache)
1096 * If the node is being deleted, don't cache it!
1098 if (node->flags & HAMMER_NODE_DELETED)
1102 * Cache the node. If we previously cached a different node we
1103 * have to give HAMMER a chance to destroy it.
1106 if (node->cache1 != cache) {
1107 if (node->cache2 != cache) {
1108 if ((old = *cache) != NULL) {
1109 KKASSERT(node->lock.refs != 0);
1110 hammer_uncache_node(cache);
1114 *node->cache2 = NULL;
1115 node->cache2 = node->cache1;
1116 node->cache1 = cache;
1119 struct hammer_node **tmp;
1121 node->cache1 = node->cache2;
1128 hammer_uncache_node(struct hammer_node **cache)
1132 if ((node = *cache) != NULL) {
1134 if (node->cache1 == cache) {
1135 node->cache1 = node->cache2;
1136 node->cache2 = NULL;
1137 } else if (node->cache2 == cache) {
1138 node->cache2 = NULL;
1140 panic("hammer_uncache_node: missing cache linkage");
1142 if (node->cache1 == NULL && node->cache2 == NULL)
1143 hammer_flush_node(node);
1148 * Remove a node's cache references and destroy the node if it has no
1149 * other references or backing store.
1152 hammer_flush_node(hammer_node_t node)
1154 hammer_buffer_t buffer;
1157 *node->cache1 = NULL;
1159 *node->cache2 = NULL;
1160 if (node->lock.refs == 0 && node->ondisk == NULL) {
1161 RB_REMOVE(hammer_nod_rb_tree, &node->hmp->rb_nods_root, node);
1162 if ((buffer = node->buffer) != NULL) {
1163 node->buffer = NULL;
1164 TAILQ_REMOVE(&buffer->clist, node, entry);
1165 /* buffer is unreferenced because ondisk is NULL */
1167 --hammer_count_nodes;
1168 kfree(node, M_HAMMER);
1173 * Flush passively cached B-Tree nodes associated with this buffer.
1174 * This is only called when the buffer is about to be destroyed, so
1175 * none of the nodes should have any references. The buffer is locked.
1177 * We may be interlocked with the buffer.
1180 hammer_flush_buffer_nodes(hammer_buffer_t buffer)
1184 while ((node = TAILQ_FIRST(&buffer->clist)) != NULL) {
1185 KKASSERT(node->ondisk == NULL);
1187 if (node->lock.refs == 0) {
1188 hammer_ref(&node->lock);
1189 node->flags |= HAMMER_NODE_FLUSH;
1190 hammer_rel_node(node);
1192 KKASSERT(node->loading != 0);
1193 KKASSERT(node->buffer != NULL);
1194 buffer = node->buffer;
1195 node->buffer = NULL;
1196 TAILQ_REMOVE(&buffer->clist, node, entry);
1197 /* buffer is unreferenced because ondisk is NULL */
1203 /************************************************************************
1205 ************************************************************************/
1208 * Allocate a B-Tree node.
1211 hammer_alloc_btree(hammer_transaction_t trans, int *errorp)
1213 hammer_buffer_t buffer = NULL;
1214 hammer_node_t node = NULL;
1215 hammer_off_t node_offset;
1217 node_offset = hammer_blockmap_alloc(trans, HAMMER_ZONE_BTREE_INDEX,
1218 sizeof(struct hammer_node_ondisk),
1221 node = hammer_get_node(trans->hmp, node_offset, 1, errorp);
1222 hammer_modify_node_noundo(trans, node);
1223 bzero(node->ondisk, sizeof(*node->ondisk));
1224 hammer_modify_node_done(node);
1227 hammer_rel_buffer(buffer, 0);
1232 * Allocate data. If the address of a data buffer is supplied then
1233 * any prior non-NULL *data_bufferp will be released and *data_bufferp
1234 * will be set to the related buffer. The caller must release it when
1235 * finally done. The initial *data_bufferp should be set to NULL by
1238 * The caller is responsible for making hammer_modify*() calls on the
1242 hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
1243 hammer_off_t *data_offsetp,
1244 struct hammer_buffer **data_bufferp, int *errorp)
1252 if (data_len < HAMMER_BUFSIZE) {
1253 *data_offsetp = hammer_blockmap_alloc(trans,
1254 HAMMER_ZONE_SMALL_DATA_INDEX,
1257 *data_offsetp = hammer_blockmap_alloc(trans,
1258 HAMMER_ZONE_LARGE_DATA_INDEX,
1264 if (*errorp == 0 && data_bufferp) {
1266 data = hammer_bread(trans->hmp, *data_offsetp, errorp,
1268 KKASSERT(*errorp == 0);
1275 KKASSERT(*errorp == 0);
1280 * Sync dirty buffers to the media and clean-up any loose ends.
1282 static int hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
1283 static int hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
1286 hammer_queue_inodes_flusher(hammer_mount_t hmp, int waitfor)
1288 struct hammer_sync_info info;
1291 info.waitfor = waitfor;
1292 if (waitfor == MNT_WAIT) {
1293 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_ONEPASS,
1294 hammer_sync_scan1, hammer_sync_scan2, &info);
1296 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_ONEPASS|VMSC_NOWAIT,
1297 hammer_sync_scan1, hammer_sync_scan2, &info);
1303 hammer_sync_hmp(hammer_mount_t hmp, int waitfor)
1305 struct hammer_sync_info info;
1308 info.waitfor = waitfor;
1310 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_NOWAIT,
1311 hammer_sync_scan1, hammer_sync_scan2, &info);
1312 if (waitfor == MNT_WAIT)
1313 hammer_flusher_sync(hmp);
1315 hammer_flusher_async(hmp);
1321 hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1323 struct hammer_inode *ip;
1326 if (vp->v_type == VNON || ip == NULL ||
1327 ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1328 RB_EMPTY(&vp->v_rbdirty_tree))) {
1335 hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1337 struct hammer_sync_info *info = data;
1338 struct hammer_inode *ip;
1342 if (vp->v_type == VNON || vp->v_type == VBAD ||
1343 ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1344 RB_EMPTY(&vp->v_rbdirty_tree))) {
1347 error = VOP_FSYNC(vp, info->waitfor);
1349 info->error = error;