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.59 2008/06/18 01:13:30 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);
312 hammer_io_clear_modlist(&volume->io);
315 * There should be no references on the volume, no clusters, and
318 KKASSERT(volume->io.lock.refs == 0);
320 volume->ondisk = NULL;
322 if (volume->devvp->v_rdev &&
323 volume->devvp->v_rdev->si_mountpoint == hmp->mp
325 volume->devvp->v_rdev->si_mountpoint = NULL;
328 vinvalbuf(volume->devvp, 0, 0, 0);
329 VOP_CLOSE(volume->devvp, FREAD);
331 vinvalbuf(volume->devvp, V_SAVE, 0, 0);
332 VOP_CLOSE(volume->devvp, FREAD|FWRITE);
337 * Destroy the structure
339 RB_REMOVE(hammer_vol_rb_tree, &hmp->rb_vols_root, volume);
340 hammer_free_volume(volume);
346 hammer_free_volume(hammer_volume_t volume)
348 if (volume->vol_name) {
349 kfree(volume->vol_name, M_HAMMER);
350 volume->vol_name = NULL;
353 vrele(volume->devvp);
354 volume->devvp = NULL;
356 --hammer_count_volumes;
357 kfree(volume, M_HAMMER);
361 * Get a HAMMER volume. The volume must already exist.
364 hammer_get_volume(struct hammer_mount *hmp, int32_t vol_no, int *errorp)
366 struct hammer_volume *volume;
369 * Locate the volume structure
371 volume = RB_LOOKUP(hammer_vol_rb_tree, &hmp->rb_vols_root, vol_no);
372 if (volume == NULL) {
376 hammer_ref(&volume->io.lock);
379 * Deal with on-disk info
381 if (volume->ondisk == NULL || volume->io.loading) {
382 *errorp = hammer_load_volume(volume);
384 hammer_rel_volume(volume, 1);
394 hammer_ref_volume(hammer_volume_t volume)
398 hammer_ref(&volume->io.lock);
401 * Deal with on-disk info
403 if (volume->ondisk == NULL || volume->io.loading) {
404 error = hammer_load_volume(volume);
406 hammer_rel_volume(volume, 1);
414 hammer_get_root_volume(struct hammer_mount *hmp, int *errorp)
416 hammer_volume_t volume;
418 volume = hmp->rootvol;
419 KKASSERT(volume != NULL);
420 hammer_ref(&volume->io.lock);
423 * Deal with on-disk info
425 if (volume->ondisk == NULL || volume->io.loading) {
426 *errorp = hammer_load_volume(volume);
428 hammer_rel_volume(volume, 1);
438 * Load a volume's on-disk information. The volume must be referenced and
439 * not locked. We temporarily acquire an exclusive lock to interlock
440 * against releases or multiple get's.
443 hammer_load_volume(hammer_volume_t volume)
447 ++volume->io.loading;
448 hammer_lock_ex(&volume->io.lock);
450 if (volume->ondisk == NULL) {
451 error = hammer_io_read(volume->devvp, &volume->io,
454 volume->ondisk = (void *)volume->io.bp->b_data;
458 --volume->io.loading;
459 hammer_unlock(&volume->io.lock);
464 * Release a volume. Call hammer_io_release on the last reference. We have
465 * to acquire an exclusive lock to interlock against volume->ondisk tests
466 * in hammer_load_volume(), and hammer_io_release() also expects an exclusive
469 * Volumes are not unloaded from memory during normal operation.
472 hammer_rel_volume(hammer_volume_t volume, int flush)
475 if (volume->io.lock.refs == 1) {
476 ++volume->io.loading;
477 hammer_lock_ex(&volume->io.lock);
478 if (volume->io.lock.refs == 1) {
479 volume->ondisk = NULL;
480 hammer_io_release(&volume->io, flush);
482 --volume->io.loading;
483 hammer_unlock(&volume->io.lock);
485 hammer_unref(&volume->io.lock);
489 /************************************************************************
491 ************************************************************************
493 * Manage buffers. Currently all blockmap-backed zones are translated
494 * to zone-2 buffer offsets.
497 hammer_get_buffer(hammer_mount_t hmp, hammer_off_t buf_offset,
498 int isnew, int *errorp)
500 hammer_buffer_t buffer;
501 hammer_volume_t volume;
502 hammer_off_t zone2_offset;
503 hammer_io_type_t iotype;
507 buf_offset &= ~HAMMER_BUFMASK64;
510 * Shortcut if the buffer is already cached
512 buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root, buf_offset);
514 if (buffer->io.lock.refs == 0)
515 ++hammer_count_refedbufs;
516 hammer_ref(&buffer->io.lock);
519 * Onced refed the ondisk field will not be cleared by
522 if (buffer->ondisk && buffer->io.loading == 0) {
528 * The buffer is no longer loose if it has a ref, and
529 * cannot become loose once it gains a ref. Loose
530 * buffers will never be in a modified state. This should
531 * only occur on the 0->1 transition of refs.
533 * lose_list can be modified via a biodone() interrupt.
535 if (buffer->io.mod_list == &hmp->lose_list) {
536 crit_enter(); /* biodone race against list */
537 TAILQ_REMOVE(buffer->io.mod_list, &buffer->io,
540 buffer->io.mod_list = NULL;
541 KKASSERT(buffer->io.modified == 0);
547 * What is the buffer class?
549 zone = HAMMER_ZONE_DECODE(buf_offset);
552 case HAMMER_ZONE_LARGE_DATA_INDEX:
553 case HAMMER_ZONE_SMALL_DATA_INDEX:
554 case HAMMER_ZONE_META_INDEX: /* meta-data isn't a meta-buffer */
555 iotype = HAMMER_STRUCTURE_DATA_BUFFER;
557 case HAMMER_ZONE_UNDO_INDEX:
558 iotype = HAMMER_STRUCTURE_UNDO_BUFFER;
561 iotype = HAMMER_STRUCTURE_META_BUFFER;
566 * Handle blockmap offset translations
568 if (zone >= HAMMER_ZONE_BTREE_INDEX) {
569 zone2_offset = hammer_blockmap_lookup(hmp, buf_offset, errorp);
570 } else if (zone == HAMMER_ZONE_UNDO_INDEX) {
571 zone2_offset = hammer_undo_lookup(hmp, buf_offset, errorp);
573 KKASSERT(zone == HAMMER_ZONE_RAW_BUFFER_INDEX);
574 zone2_offset = buf_offset;
581 * NOTE: zone2_offset and maxbuf_off are both full zone-2 offset
584 KKASSERT((zone2_offset & HAMMER_OFF_ZONE_MASK) ==
585 HAMMER_ZONE_RAW_BUFFER);
586 vol_no = HAMMER_VOL_DECODE(zone2_offset);
587 volume = hammer_get_volume(hmp, vol_no, errorp);
591 KKASSERT(zone2_offset < volume->maxbuf_off);
594 * Allocate a new buffer structure. We will check for races later.
596 ++hammer_count_buffers;
597 buffer = kmalloc(sizeof(*buffer), M_HAMMER, M_WAITOK|M_ZERO);
598 buffer->zone2_offset = zone2_offset;
599 buffer->zoneX_offset = buf_offset;
600 buffer->volume = volume;
602 hammer_io_init(&buffer->io, hmp, iotype);
603 buffer->io.offset = volume->ondisk->vol_buf_beg +
604 (zone2_offset & HAMMER_OFF_SHORT_MASK);
605 TAILQ_INIT(&buffer->clist);
606 hammer_ref(&buffer->io.lock);
609 * Insert the buffer into the RB tree and handle late collisions.
611 if (RB_INSERT(hammer_buf_rb_tree, &hmp->rb_bufs_root, buffer)) {
612 hammer_unref(&buffer->io.lock);
613 --hammer_count_buffers;
614 kfree(buffer, M_HAMMER);
617 ++hammer_count_refedbufs;
621 * Deal with on-disk info and loading races.
623 if (buffer->ondisk == NULL || buffer->io.loading) {
624 *errorp = hammer_load_buffer(buffer, isnew);
626 hammer_rel_buffer(buffer, 1);
636 * Destroy all buffers covering the specified zoneX offset range. This
637 * is called when the related blockmap layer2 entry is freed or when
638 * a direct write bypasses our buffer/buffer-cache subsystem.
640 * The buffers may be referenced by the caller itself. Setting reclaim
641 * will cause the buffer to be destroyed when it's ref count reaches zero.
644 hammer_del_buffers(hammer_mount_t hmp, hammer_off_t base_offset,
645 hammer_off_t zone2_offset, int bytes)
647 hammer_buffer_t buffer;
648 hammer_volume_t volume;
652 vol_no = HAMMER_VOL_DECODE(zone2_offset);
653 volume = hammer_get_volume(hmp, vol_no, &error);
654 KKASSERT(error == 0);
657 buffer = RB_LOOKUP(hammer_buf_rb_tree, &hmp->rb_bufs_root,
660 KKASSERT(buffer->zone2_offset == zone2_offset);
661 hammer_io_clear_modify(&buffer->io);
662 buffer->io.reclaim = 1;
663 KKASSERT(buffer->volume == volume);
664 if (buffer->io.lock.refs == 0)
665 hammer_unload_buffer(buffer, NULL);
667 hammer_io_inval(volume, zone2_offset);
669 base_offset += HAMMER_BUFSIZE;
670 zone2_offset += HAMMER_BUFSIZE;
671 bytes -= HAMMER_BUFSIZE;
673 hammer_rel_volume(volume, 0);
677 hammer_load_buffer(hammer_buffer_t buffer, int isnew)
679 hammer_volume_t volume;
683 * Load the buffer's on-disk info
685 volume = buffer->volume;
686 ++buffer->io.loading;
687 hammer_lock_ex(&buffer->io.lock);
689 if (hammer_debug_io & 0x0001) {
690 kprintf("load_buffer %016llx %016llx isnew=%d od=%p\n",
691 buffer->zoneX_offset, buffer->zone2_offset, isnew,
695 if (buffer->ondisk == NULL) {
697 error = hammer_io_new(volume->devvp, &buffer->io);
699 error = hammer_io_read(volume->devvp, &buffer->io,
703 buffer->ondisk = (void *)buffer->io.bp->b_data;
705 error = hammer_io_new(volume->devvp, &buffer->io);
709 --buffer->io.loading;
710 hammer_unlock(&buffer->io.lock);
715 * NOTE: Called from RB_SCAN, must return >= 0 for scan to continue.
718 hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused)
720 ++hammer_count_refedbufs;
721 hammer_ref(&buffer->io.lock);
722 hammer_flush_buffer_nodes(buffer);
723 KKASSERT(buffer->io.lock.refs == 1);
724 hammer_rel_buffer(buffer, 2);
729 * Reference a buffer that is either already referenced or via a specially
730 * handled pointer (aka cursor->buffer).
733 hammer_ref_buffer(hammer_buffer_t buffer)
737 if (buffer->io.lock.refs == 0)
738 ++hammer_count_refedbufs;
739 hammer_ref(&buffer->io.lock);
742 * At this point a biodone() will not touch the buffer other then
743 * incidental bits. However, lose_list can be modified via
744 * a biodone() interrupt.
748 if (buffer->io.mod_list == &buffer->io.hmp->lose_list) {
750 TAILQ_REMOVE(buffer->io.mod_list, &buffer->io, mod_entry);
751 buffer->io.mod_list = NULL;
755 if (buffer->ondisk == NULL || buffer->io.loading) {
756 error = hammer_load_buffer(buffer, 0);
758 hammer_rel_buffer(buffer, 1);
760 * NOTE: buffer pointer can become stale after
771 * Release a buffer. We have to deal with several places where
772 * another thread can ref the buffer.
774 * Only destroy the structure itself if the related buffer cache buffer
775 * was disassociated from it. This ties the management of the structure
776 * to the buffer cache subsystem. buffer->ondisk determines whether the
777 * embedded io is referenced or not.
780 hammer_rel_buffer(hammer_buffer_t buffer, int flush)
782 hammer_volume_t volume;
786 if (buffer->io.lock.refs == 1) {
787 ++buffer->io.loading; /* force interlock check */
788 hammer_lock_ex(&buffer->io.lock);
789 if (buffer->io.lock.refs == 1) {
790 hammer_io_release(&buffer->io, flush);
792 if (buffer->io.lock.refs == 1)
793 --hammer_count_refedbufs;
795 if (buffer->io.bp == NULL &&
796 buffer->io.lock.refs == 1) {
800 * NOTE: It is impossible for any associated
801 * B-Tree nodes to have refs if the buffer
802 * has no additional refs.
804 RB_REMOVE(hammer_buf_rb_tree,
805 &buffer->io.hmp->rb_bufs_root,
807 volume = buffer->volume;
808 buffer->volume = NULL; /* sanity */
809 hammer_rel_volume(volume, 0);
810 hammer_io_clear_modlist(&buffer->io);
811 hammer_flush_buffer_nodes(buffer);
812 KKASSERT(TAILQ_EMPTY(&buffer->clist));
816 --buffer->io.loading;
817 hammer_unlock(&buffer->io.lock);
819 hammer_unref(&buffer->io.lock);
822 --hammer_count_buffers;
823 kfree(buffer, M_HAMMER);
828 * Access the filesystem buffer containing the specified hammer offset.
829 * buf_offset is a conglomeration of the volume number and vol_buf_beg
830 * relative buffer offset. It must also have bit 55 set to be valid.
831 * (see hammer_off_t in hammer_disk.h).
833 * Any prior buffer in *bufferp will be released and replaced by the
837 hammer_bread(hammer_mount_t hmp, hammer_off_t buf_offset, int *errorp,
838 struct hammer_buffer **bufferp)
840 hammer_buffer_t buffer;
841 int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
843 buf_offset &= ~HAMMER_BUFMASK64;
844 KKASSERT((buf_offset & HAMMER_OFF_ZONE_MASK) != 0);
847 if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
848 buffer->zoneX_offset != buf_offset)) {
850 hammer_rel_buffer(buffer, 0);
851 buffer = hammer_get_buffer(hmp, buf_offset, 0, errorp);
858 * Return a pointer to the buffer data.
863 return((char *)buffer->ondisk + xoff);
867 * Access the filesystem buffer containing the specified hammer offset.
868 * No disk read operation occurs. The result buffer may contain garbage.
870 * Any prior buffer in *bufferp will be released and replaced by the
873 * This function marks the buffer dirty but does not increment its
877 hammer_bnew(hammer_mount_t hmp, hammer_off_t buf_offset, int *errorp,
878 struct hammer_buffer **bufferp)
880 hammer_buffer_t buffer;
881 int32_t xoff = (int32_t)buf_offset & HAMMER_BUFMASK;
883 buf_offset &= ~HAMMER_BUFMASK64;
886 if (buffer == NULL || (buffer->zone2_offset != buf_offset &&
887 buffer->zoneX_offset != buf_offset)) {
889 hammer_rel_buffer(buffer, 0);
890 buffer = hammer_get_buffer(hmp, buf_offset, 1, errorp);
897 * Return a pointer to the buffer data.
902 return((char *)buffer->ondisk + xoff);
905 /************************************************************************
907 ************************************************************************
909 * Manage B-Tree nodes. B-Tree nodes represent the primary indexing
910 * method used by the HAMMER filesystem.
912 * Unlike other HAMMER structures, a hammer_node can be PASSIVELY
913 * associated with its buffer, and will only referenced the buffer while
914 * the node itself is referenced.
916 * A hammer_node can also be passively associated with other HAMMER
917 * structures, such as inodes, while retaining 0 references. These
918 * associations can be cleared backwards using a pointer-to-pointer in
921 * This allows the HAMMER implementation to cache hammer_nodes long-term
922 * and short-cut a great deal of the infrastructure's complexity. In
923 * most cases a cached node can be reacquired without having to dip into
924 * either the buffer or cluster management code.
926 * The caller must pass a referenced cluster on call and will retain
927 * ownership of the reference on return. The node will acquire its own
928 * additional references, if necessary.
931 hammer_get_node(hammer_mount_t hmp, hammer_off_t node_offset,
932 int isnew, int *errorp)
936 KKASSERT((node_offset & HAMMER_OFF_ZONE_MASK) == HAMMER_ZONE_BTREE);
939 * Locate the structure, allocating one if necessary.
942 node = RB_LOOKUP(hammer_nod_rb_tree, &hmp->rb_nods_root, node_offset);
944 ++hammer_count_nodes;
945 node = kmalloc(sizeof(*node), M_HAMMER, M_WAITOK|M_ZERO);
946 node->node_offset = node_offset;
948 TAILQ_INIT(&node->cache_list);
949 if (RB_INSERT(hammer_nod_rb_tree, &hmp->rb_nods_root, node)) {
950 --hammer_count_nodes;
951 kfree(node, M_HAMMER);
955 hammer_ref(&node->lock);
959 *errorp = hammer_load_node(node, isnew);
961 hammer_rel_node(node);
968 * Reference an already-referenced node.
971 hammer_ref_node(hammer_node_t node)
973 KKASSERT(node->lock.refs > 0 && node->ondisk != NULL);
974 hammer_ref(&node->lock);
978 * Load a node's on-disk data reference.
981 hammer_load_node(hammer_node_t node, int isnew)
983 hammer_buffer_t buffer;
984 hammer_off_t buf_offset;
989 hammer_lock_ex(&node->lock);
990 if (node->ondisk == NULL) {
992 * This is a little confusing but the jist is that
993 * node->buffer determines whether the node is on
994 * the buffer's clist and node->ondisk determines
995 * whether the buffer is referenced.
997 * We could be racing a buffer release, in which case
998 * node->buffer may become NULL while we are blocked
999 * referencing the buffer.
1001 if ((buffer = node->buffer) != NULL) {
1002 error = hammer_ref_buffer(buffer);
1003 if (error == 0 && node->buffer == NULL) {
1004 TAILQ_INSERT_TAIL(&buffer->clist,
1006 node->buffer = buffer;
1009 buf_offset = node->node_offset & ~HAMMER_BUFMASK64;
1010 buffer = hammer_get_buffer(node->hmp, buf_offset,
1013 KKASSERT(error == 0);
1014 TAILQ_INSERT_TAIL(&buffer->clist,
1016 node->buffer = buffer;
1021 node->ondisk = (void *)((char *)buffer->ondisk +
1022 (node->node_offset & HAMMER_BUFMASK));
1024 (node->flags & HAMMER_NODE_CRCGOOD) == 0) {
1025 if (hammer_crc_test_btree(node->ondisk) == 0)
1026 Debugger("CRC FAILED: B-TREE NODE");
1027 node->flags |= HAMMER_NODE_CRCGOOD;
1032 hammer_unlock(&node->lock);
1037 * Safely reference a node, interlock against flushes via the IO subsystem.
1040 hammer_ref_node_safe(struct hammer_mount *hmp, hammer_node_cache_t cache,
1047 hammer_ref(&node->lock);
1051 *errorp = hammer_load_node(node, 0);
1053 hammer_rel_node(node);
1063 * Release a hammer_node. On the last release the node dereferences
1064 * its underlying buffer and may or may not be destroyed.
1067 hammer_rel_node(hammer_node_t node)
1069 hammer_buffer_t buffer;
1072 * If this isn't the last ref just decrement the ref count and
1075 if (node->lock.refs > 1) {
1076 hammer_unref(&node->lock);
1081 * If there is no ondisk info or no buffer the node failed to load,
1082 * remove the last reference and destroy the node.
1084 if (node->ondisk == NULL) {
1085 hammer_unref(&node->lock);
1086 hammer_flush_node(node);
1087 /* node is stale now */
1092 * Do not disassociate the node from the buffer if it represents
1093 * a modified B-Tree node that still needs its crc to be generated.
1095 if (node->flags & HAMMER_NODE_NEEDSCRC)
1099 * Do final cleanups and then either destroy the node and leave it
1100 * passively cached. The buffer reference is removed regardless.
1102 buffer = node->buffer;
1103 node->ondisk = NULL;
1105 if ((node->flags & HAMMER_NODE_FLUSH) == 0) {
1106 hammer_unref(&node->lock);
1107 hammer_rel_buffer(buffer, 0);
1114 hammer_unref(&node->lock);
1115 hammer_flush_node(node);
1117 hammer_rel_buffer(buffer, 0);
1121 * Free space on-media associated with a B-Tree node.
1124 hammer_delete_node(hammer_transaction_t trans, hammer_node_t node)
1126 KKASSERT((node->flags & HAMMER_NODE_DELETED) == 0);
1127 node->flags |= HAMMER_NODE_DELETED;
1128 hammer_blockmap_free(trans, node->node_offset, sizeof(*node->ondisk));
1132 * Passively cache a referenced hammer_node. The caller may release
1133 * the node on return.
1136 hammer_cache_node(hammer_node_cache_t cache, hammer_node_t node)
1139 * If the node is being deleted, don't cache it!
1141 if (node->flags & HAMMER_NODE_DELETED)
1143 if (cache->node == node)
1146 hammer_uncache_node(cache);
1147 if (node->flags & HAMMER_NODE_DELETED)
1150 TAILQ_INSERT_TAIL(&node->cache_list, cache, entry);
1154 hammer_uncache_node(hammer_node_cache_t cache)
1158 if ((node = cache->node) != NULL) {
1159 TAILQ_REMOVE(&node->cache_list, cache, entry);
1161 if (TAILQ_EMPTY(&node->cache_list))
1162 hammer_flush_node(node);
1167 * Remove a node's cache references and destroy the node if it has no
1168 * other references or backing store.
1171 hammer_flush_node(hammer_node_t node)
1173 hammer_node_cache_t cache;
1174 hammer_buffer_t buffer;
1176 while ((cache = TAILQ_FIRST(&node->cache_list)) != NULL) {
1177 TAILQ_REMOVE(&node->cache_list, cache, entry);
1180 if (node->lock.refs == 0 && node->ondisk == NULL) {
1181 KKASSERT((node->flags & HAMMER_NODE_NEEDSCRC) == 0);
1182 RB_REMOVE(hammer_nod_rb_tree, &node->hmp->rb_nods_root, node);
1183 if ((buffer = node->buffer) != NULL) {
1184 node->buffer = NULL;
1185 TAILQ_REMOVE(&buffer->clist, node, entry);
1186 /* buffer is unreferenced because ondisk is NULL */
1188 --hammer_count_nodes;
1189 kfree(node, M_HAMMER);
1194 * Flush passively cached B-Tree nodes associated with this buffer.
1195 * This is only called when the buffer is about to be destroyed, so
1196 * none of the nodes should have any references. The buffer is locked.
1198 * We may be interlocked with the buffer.
1201 hammer_flush_buffer_nodes(hammer_buffer_t buffer)
1205 while ((node = TAILQ_FIRST(&buffer->clist)) != NULL) {
1206 KKASSERT(node->ondisk == NULL);
1207 KKASSERT((node->flags & HAMMER_NODE_NEEDSCRC) == 0);
1209 if (node->lock.refs == 0) {
1210 hammer_ref(&node->lock);
1211 node->flags |= HAMMER_NODE_FLUSH;
1212 hammer_rel_node(node);
1214 KKASSERT(node->loading != 0);
1215 KKASSERT(node->buffer != NULL);
1216 buffer = node->buffer;
1217 node->buffer = NULL;
1218 TAILQ_REMOVE(&buffer->clist, node, entry);
1219 /* buffer is unreferenced because ondisk is NULL */
1225 /************************************************************************
1227 ************************************************************************/
1230 * Allocate a B-Tree node.
1233 hammer_alloc_btree(hammer_transaction_t trans, int *errorp)
1235 hammer_buffer_t buffer = NULL;
1236 hammer_node_t node = NULL;
1237 hammer_off_t node_offset;
1239 node_offset = hammer_blockmap_alloc(trans, HAMMER_ZONE_BTREE_INDEX,
1240 sizeof(struct hammer_node_ondisk),
1243 node = hammer_get_node(trans->hmp, node_offset, 1, errorp);
1244 hammer_modify_node_noundo(trans, node);
1245 bzero(node->ondisk, sizeof(*node->ondisk));
1246 hammer_modify_node_done(node);
1249 hammer_rel_buffer(buffer, 0);
1254 * Allocate data. If the address of a data buffer is supplied then
1255 * any prior non-NULL *data_bufferp will be released and *data_bufferp
1256 * will be set to the related buffer. The caller must release it when
1257 * finally done. The initial *data_bufferp should be set to NULL by
1260 * The caller is responsible for making hammer_modify*() calls on the
1264 hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
1265 u_int16_t rec_type, hammer_off_t *data_offsetp,
1266 struct hammer_buffer **data_bufferp, int *errorp)
1276 case HAMMER_RECTYPE_INODE:
1277 case HAMMER_RECTYPE_PSEUDO_INODE:
1278 case HAMMER_RECTYPE_DIRENTRY:
1279 case HAMMER_RECTYPE_EXT:
1280 case HAMMER_RECTYPE_FIX:
1281 zone = HAMMER_ZONE_META_INDEX;
1283 case HAMMER_RECTYPE_DATA:
1284 case HAMMER_RECTYPE_DB:
1285 if (data_len <= HAMMER_BUFSIZE / 2)
1286 zone = HAMMER_ZONE_SMALL_DATA_INDEX;
1288 zone = HAMMER_ZONE_LARGE_DATA_INDEX;
1291 panic("hammer_alloc_data: rec_type %04x unknown",
1293 zone = 0; /* NOT REACHED */
1296 *data_offsetp = hammer_blockmap_alloc(trans, zone,
1301 if (*errorp == 0 && data_bufferp) {
1303 data = hammer_bread(trans->hmp, *data_offsetp, errorp,
1305 KKASSERT(*errorp == 0);
1312 KKASSERT(*errorp == 0);
1317 * Sync dirty buffers to the media and clean-up any loose ends.
1319 static int hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
1320 static int hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
1323 hammer_queue_inodes_flusher(hammer_mount_t hmp, int waitfor)
1325 struct hammer_sync_info info;
1328 info.waitfor = waitfor;
1329 if (waitfor == MNT_WAIT) {
1330 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_ONEPASS,
1331 hammer_sync_scan1, hammer_sync_scan2, &info);
1333 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_ONEPASS|VMSC_NOWAIT,
1334 hammer_sync_scan1, hammer_sync_scan2, &info);
1340 hammer_sync_hmp(hammer_mount_t hmp, int waitfor)
1342 struct hammer_sync_info info;
1345 info.waitfor = waitfor;
1347 vmntvnodescan(hmp->mp, VMSC_GETVP|VMSC_NOWAIT,
1348 hammer_sync_scan1, hammer_sync_scan2, &info);
1349 if (waitfor == MNT_WAIT)
1350 hammer_flusher_sync(hmp);
1352 hammer_flusher_async(hmp);
1358 hammer_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
1360 struct hammer_inode *ip;
1363 if (vp->v_type == VNON || ip == NULL ||
1364 ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1365 RB_EMPTY(&vp->v_rbdirty_tree))) {
1372 hammer_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1374 struct hammer_sync_info *info = data;
1375 struct hammer_inode *ip;
1379 if (vp->v_type == VNON || vp->v_type == VBAD ||
1380 ((ip->flags & HAMMER_INODE_MODMASK) == 0 &&
1381 RB_EMPTY(&vp->v_rbdirty_tree))) {
1384 error = VOP_FSYNC(vp, info->waitfor);
1386 info->error = error;