2 * Copyright (c) 2011, 2012 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 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/nlookup.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/fcntl.h>
43 #include <sys/vfsops.h>
44 #include <sys/sysctl.h>
47 #include "hammer2_disk.h"
48 #include "hammer2_mount.h"
50 struct hammer2_sync_info {
55 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
56 static struct hammer2_mntlist hammer2_mntlist;
57 static struct lock hammer2_mntlk;
60 int hammer2_cluster_enable = 1;
61 int hammer2_hardlink_enable = 1;
62 long hammer2_iod_file_read;
63 long hammer2_iod_meta_read;
64 long hammer2_iod_indr_read;
65 long hammer2_iod_file_write;
66 long hammer2_iod_meta_write;
67 long hammer2_iod_indr_write;
68 long hammer2_iod_volu_write;
69 long hammer2_ioa_file_read;
70 long hammer2_ioa_meta_read;
71 long hammer2_ioa_indr_read;
72 long hammer2_ioa_file_write;
73 long hammer2_ioa_meta_write;
74 long hammer2_ioa_indr_write;
75 long hammer2_ioa_volu_write;
77 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
79 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
80 &hammer2_debug, 0, "");
81 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
82 &hammer2_cluster_enable, 0, "");
83 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
84 &hammer2_hardlink_enable, 0, "");
85 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
86 &hammer2_iod_file_read, 0, "");
87 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
88 &hammer2_iod_meta_read, 0, "");
89 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
90 &hammer2_iod_indr_read, 0, "");
91 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
92 &hammer2_iod_file_write, 0, "");
93 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
94 &hammer2_iod_meta_write, 0, "");
95 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
96 &hammer2_iod_indr_write, 0, "");
97 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
98 &hammer2_iod_volu_write, 0, "");
99 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
100 &hammer2_ioa_file_read, 0, "");
101 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
102 &hammer2_ioa_meta_read, 0, "");
103 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
104 &hammer2_ioa_indr_read, 0, "");
105 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
106 &hammer2_ioa_file_write, 0, "");
107 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
108 &hammer2_ioa_meta_write, 0, "");
109 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
110 &hammer2_ioa_indr_write, 0, "");
111 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
112 &hammer2_ioa_volu_write, 0, "");
114 static int hammer2_vfs_init(struct vfsconf *conf);
115 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
117 static int hammer2_remount(struct mount *, char *, struct vnode *,
119 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
120 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
121 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
123 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
125 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
126 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
127 ino_t ino, struct vnode **vpp);
128 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
129 struct fid *fhp, struct vnode **vpp);
130 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
131 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
132 int *exflagsp, struct ucred **credanonp);
134 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
135 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
136 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
139 * HAMMER2 vfs operations.
141 static struct vfsops hammer2_vfsops = {
142 .vfs_init = hammer2_vfs_init,
143 .vfs_sync = hammer2_vfs_sync,
144 .vfs_mount = hammer2_vfs_mount,
145 .vfs_unmount = hammer2_vfs_unmount,
146 .vfs_root = hammer2_vfs_root,
147 .vfs_statfs = hammer2_vfs_statfs,
148 .vfs_statvfs = hammer2_vfs_statvfs,
149 .vfs_vget = hammer2_vfs_vget,
150 .vfs_vptofh = hammer2_vfs_vptofh,
151 .vfs_fhtovp = hammer2_vfs_fhtovp,
152 .vfs_checkexp = hammer2_vfs_checkexp
155 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
157 VFS_SET(hammer2_vfsops, hammer2, 0);
158 MODULE_VERSION(hammer2, 1);
162 hammer2_vfs_init(struct vfsconf *conf)
168 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
170 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
172 if (HAMMER2_ALLOCREF_BYTES != sizeof(struct hammer2_allocref))
174 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
178 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
180 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
181 TAILQ_INIT(&hammer2_mntlist);
187 * Mount or remount HAMMER2 fileystem from physical media
190 * mp mount point structure
196 * mp mount point structure
197 * path path to mount point
198 * data pointer to argument structure in user space
199 * volume volume path (device@LABEL form)
200 * hflags user mount flags
201 * cred user credentials
208 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
211 struct hammer2_mount_info info;
212 hammer2_pfsmount_t *pmp;
213 hammer2_mount_t *hmp;
216 struct nlookupdata nd;
217 hammer2_chain_t *parent;
218 hammer2_chain_t *schain;
219 hammer2_chain_t *rchain;
220 char devstr[MNAMELEN];
235 kprintf("hammer2_mount\n");
244 * Non-root mount or updating a mount
246 error = copyin(data, &info, sizeof(info));
250 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
254 /* Extract device and label */
256 label = strchr(devstr, '@');
258 ((label + 1) - dev) > done) {
266 if (mp->mnt_flag & MNT_UPDATE) {
268 /* HAMMER2 implements NFS export via mountctl */
271 error = hammer2_remount(mp, path, devvp, cred);
279 /* Lookup name and verify it refers to a block device */
280 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
282 error = nlookup(&nd);
284 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
288 if (vn_isdisk(devvp, &error))
289 error = vfs_mountedon(devvp);
293 * Determine if the device has already been mounted. After this
294 * check hmp will be non-NULL if we are doing the second or more
295 * hammer2 mounts from the same device.
297 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
298 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
299 if (hmp->devvp == devvp)
304 * Open the device if this isn't a secondary mount
310 if (error == 0 && vcount(devvp) > 0)
314 * Now open the device
317 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
318 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
319 error = vinvalbuf(devvp, V_SAVE, 0, 0);
321 error = VOP_OPEN(devvp,
322 ronly ? FREAD : FREAD | FWRITE,
327 if (error && devvp) {
332 lockmgr(&hammer2_mntlk, LK_RELEASE);
338 * Block device opened successfully, finish initializing the
341 * From this point on we have to call hammer2_unmount() on failure.
343 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
344 mp->mnt_data = (qaddr_t)pmp;
348 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
351 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
352 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
353 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
357 lockmgr(&hammer2_mntlk, LK_RELEASE);
358 kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
360 mp->mnt_flag = MNT_LOCAL;
361 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
365 * vchain setup. vchain.data is special cased to NULL.
366 * vchain.refs is initialized and will never drop to 0.
368 hmp->vchain.refs = 1;
369 hmp->vchain.data = (void *)&hmp->voldata;
370 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
371 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
372 hmp->vchain.bref_flush = hmp->vchain.bref;
373 /* hmp->vchain.u.xxx is left NULL */
374 lockinit(&hmp->vchain.lk, "volume", 0, LK_CANRECURSE);
375 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
376 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
379 * Install the volume header
381 error = hammer2_install_volume_header(hmp);
383 hammer2_vfs_unmount(mp, MNT_FORCE);
389 * required mount structure initializations
391 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
392 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
394 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
395 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
400 mp->mnt_iosize_max = MAXPHYS;
403 * First locate the super-root inode, which is key 0 relative to the
404 * volume header's blockset.
406 * Then locate the root inode by scanning the directory keyspace
407 * represented by the label.
410 parent = &hmp->vchain;
411 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
412 schain = hammer2_chain_lookup(hmp, &parent,
413 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
414 hammer2_chain_unlock(hmp, parent);
415 if (schain == NULL) {
416 kprintf("hammer2_mount: invalid super-root\n");
417 hammer2_vfs_unmount(mp, MNT_FORCE);
420 hammer2_chain_ref(hmp, schain); /* for hmp->schain */
421 hmp->schain = schain; /* left locked */
423 schain = hmp->schain;
424 hammer2_chain_lock(hmp, schain, HAMMER2_RESOLVE_ALWAYS);
428 lhc = hammer2_dirhash(label, strlen(label));
429 rchain = hammer2_chain_lookup(hmp, &parent,
430 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
433 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
435 strcmp(label, rchain->data->ipdata.filename) == 0) {
438 rchain = hammer2_chain_next(hmp, &parent, rchain,
439 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
442 hammer2_chain_unlock(hmp, parent);
443 if (rchain == NULL) {
444 kprintf("hammer2_mount: PFS label not found\n");
445 hammer2_vfs_unmount(mp, MNT_FORCE);
448 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
449 hammer2_chain_unlock(hmp, rchain);
450 kprintf("hammer2_mount: PFS label already mounted!\n");
451 hammer2_vfs_unmount(mp, MNT_FORCE);
454 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
456 hammer2_chain_ref(hmp, rchain); /* for pmp->rchain */
457 hammer2_chain_unlock(hmp, rchain);
458 pmp->rchain = rchain; /* left held & unlocked */
459 pmp->iroot = rchain->u.ip; /* implied hold from rchain */
460 pmp->iroot->pmp = pmp;
461 kprintf("iroot %p\n", pmp->iroot);
464 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
465 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
466 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
468 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
469 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
470 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
471 copyinstr(path, mp->mnt_stat.f_mntonname,
472 sizeof(mp->mnt_stat.f_mntonname) - 1,
475 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
482 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
490 hammer2_vfs_unmount(struct mount *mp, int mntflags)
492 hammer2_pfsmount_t *pmp;
493 hammer2_mount_t *hmp;
496 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
503 if (mntflags & MNT_FORCE)
506 hammer2_mount_exlock(hmp);
509 * If mount initialization proceeded far enough we must flush
513 error = vflush(mp, 0, flags);
518 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
520 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
523 * Flush any left over chains. The voldata lock is only used
524 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
526 hammer2_voldata_lock(hmp);
527 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
528 HAMMER2_CHAIN_MODIFIED_AUX |
529 HAMMER2_CHAIN_SUBMODIFIED)) {
530 hammer2_voldata_unlock(hmp);
531 hammer2_vfs_sync(mp, MNT_WAIT);
533 hammer2_voldata_unlock(hmp);
535 if (hmp->pmp_count == 0) {
536 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
537 HAMMER2_CHAIN_MODIFIED_AUX |
538 HAMMER2_CHAIN_SUBMODIFIED)) {
539 kprintf("hammer2_unmount: chains left over after "
541 if (hammer2_debug & 0x0010)
542 Debugger("entered debugger");
547 * Cleanup the root and super-root chain elements (which should be
552 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
553 KKASSERT(pmp->rchain->refs == 1);
554 hammer2_chain_drop(hmp, pmp->rchain);
557 if (hmp->pmp_count == 0) {
559 KKASSERT(hmp->schain->refs == 1);
560 hammer2_chain_drop(hmp, hmp->schain);
565 * Finish up with the device vnode
567 if ((devvp = hmp->devvp) != NULL) {
568 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
570 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
575 hammer2_mount_unlock(hmp);
581 kfree(pmp, M_HAMMER2);
582 if (hmp->pmp_count == 0) {
583 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
584 kmalloc_destroy(&hmp->minode);
585 kmalloc_destroy(&hmp->mchain);
586 kfree(hmp, M_HAMMER2);
588 lockmgr(&hammer2_mntlk, LK_RELEASE);
594 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
595 ino_t ino, struct vnode **vpp)
597 kprintf("hammer2_vget\n");
603 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
605 hammer2_pfsmount_t *pmp;
610 hammer2_mount_exlock(pmp->hmp);
611 if (pmp->iroot == NULL) {
615 vp = hammer2_igetv(pmp->iroot, &error);
618 kprintf("vnodefail\n");
620 hammer2_mount_unlock(pmp->hmp);
628 * XXX incorporate pmp->iroot->ip_data.inode_quota and data_quota
632 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
634 hammer2_pfsmount_t *pmp;
635 hammer2_mount_t *hmp;
640 mp->mnt_stat.f_files = pmp->iroot->ip_data.inode_count +
641 pmp->iroot->delta_icount;
642 mp->mnt_stat.f_ffree = 0;
643 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
644 mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
645 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
646 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
654 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
656 hammer2_pfsmount_t *pmp;
657 hammer2_mount_t *hmp;
662 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
663 mp->mnt_vstat.f_files = pmp->iroot->ip_data.inode_count +
664 pmp->iroot->delta_icount;
665 mp->mnt_vstat.f_ffree = 0;
666 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
667 mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
668 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
669 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
671 *sbp = mp->mnt_vstat;
676 * Sync the entire filesystem; this is called from the filesystem syncer
677 * process periodically and whenever a user calls sync(1) on the hammer
680 * Currently is actually called from the syncer! \o/
682 * This task will have to snapshot the state of the dirty inode chain.
683 * From that, it will have to make sure all of the inodes on the dirty
684 * chain have IO initiated. We make sure that io is initiated for the root
687 * If waitfor is set, we wait for media to acknowledge the new rootblock.
689 * THINKS: side A vs side B, to have sync not stall all I/O?
693 hammer2_vfs_sync(struct mount *mp, int waitfor)
695 struct hammer2_sync_info info;
696 hammer2_mount_t *hmp;
704 if (waitfor & MNT_LAZY)
705 flags |= VMSC_ONEPASS;
708 info.waitfor = MNT_NOWAIT;
709 vmntvnodescan(mp, flags | VMSC_NOWAIT,
711 hammer2_sync_scan2, &info);
712 if (info.error == 0 && (waitfor & MNT_WAIT)) {
713 info.waitfor = waitfor;
714 vmntvnodescan(mp, flags,
716 hammer2_sync_scan2, &info);
720 if (waitfor == MNT_WAIT) {
726 hammer2_chain_lock(hmp, &hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
727 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
728 HAMMER2_CHAIN_MODIFIED_AUX |
729 HAMMER2_CHAIN_SUBMODIFIED)) {
730 hammer2_chain_flush(hmp, &hmp->vchain, 0);
735 hammer2_chain_unlock(hmp, &hmp->vchain);
739 if ((waitfor & MNT_LAZY) == 0) {
740 waitfor = MNT_NOWAIT;
741 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
742 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
743 vn_unlock(hmp->devvp);
746 if (error == 0 && haswork) {
750 * Synchronize the disk before flushing the volume
754 bp->b_bio1.bio_offset = 0;
757 bp->b_cmd = BUF_CMD_FLUSH;
758 bp->b_bio1.bio_done = biodone_sync;
759 bp->b_bio1.bio_flags |= BIO_SYNC;
760 vn_strategy(hmp->devvp, &bp->b_bio1);
761 biowait(&bp->b_bio1, "h2vol");
765 * Then we can safely flush the volume header. Volume
766 * data is locked separately to prevent ioctl functions
767 * from deadlocking due to a configuration issue.
769 bp = getblk(hmp->devvp, 0, HAMMER2_PBUFSIZE, 0, 0);
770 hammer2_voldata_lock(hmp);
771 bcopy(&hmp->voldata, bp->b_data, HAMMER2_PBUFSIZE);
772 hammer2_voldata_unlock(hmp);
781 * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
782 * won't flush on those flags. The syncer code above will do a
783 * general meta-data flush globally that will catch these flags.
786 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
791 if (vp->v_type == VNON || ip == NULL ||
792 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
793 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
794 RB_EMPTY(&vp->v_rbdirty_tree))) {
801 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
803 struct hammer2_sync_info *info = data;
808 if (vp->v_type == VNON || vp->v_type == VBAD ||
809 ((ip->chain.flags & (HAMMER2_CHAIN_MODIFIED |
810 HAMMER2_CHAIN_DIRTYEMBED)) == 0 &&
811 RB_EMPTY(&vp->v_rbdirty_tree))) {
814 error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
822 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
829 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
830 struct fid *fhp, struct vnode **vpp)
837 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
838 int *exflagsp, struct ucred **credanonp)
844 * Support code for hammer2_mount(). Read, verify, and install the volume
845 * header into the HMP
847 * XXX read four volhdrs and use the one with the highest TID whos CRC
852 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
853 * nonexistant locations.
855 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
859 hammer2_install_volume_header(hammer2_mount_t *hmp)
861 hammer2_volume_data_t *vd;
863 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
875 * There are up to 4 copies of the volume header (syncs iterate
876 * between them so there is no single master). We don't trust the
877 * volu_size field so we don't know precisely how large the filesystem
878 * is, so depend on the OS to return an error if we go beyond the
879 * block device's EOF.
881 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
882 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
883 HAMMER2_VOLUME_BYTES, &bp);
890 vd = (struct hammer2_volume_data *) bp->b_data;
891 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
892 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
898 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
899 /* XXX: Reversed-endianness filesystem */
900 kprintf("hammer2: reverse-endian filesystem detected");
906 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
907 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
908 HAMMER2_VOLUME_ICRC0_SIZE);
909 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
910 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
911 HAMMER2_VOLUME_ICRC1_SIZE);
912 if ((crc0 != crc) || (bcrc0 != bcrc)) {
913 kprintf("hammer2 volume header crc "
914 "mismatch copy #%d\t%08x %08x",
921 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
931 kprintf("hammer2: a valid volume header was found\n");
934 kprintf("hammer2: no valid volume headers found!\n");