vfs_synth - rewrite
[dragonfly.git] / sys / kern / vfs_synth.c
blob3bc1eb476d4ace565e71d619946358b00028503f
1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung (at) gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/vnode.h>
39 #include <sys/lock.h>
40 #include <sys/disk.h>
41 #include <sys/mount.h>
42 #include <sys/namecache.h>
43 #include <sys/nlookup.h>
44 #include <sys/proc.h>
45 #include <sys/thread2.h>
46 #include <sys/devfs.h>
48 struct mount *synth_mp;
49 struct vnode *synth_vp;
50 static int synth_inited = 0;
51 static int synth_synced = 0;
53 struct vnode *
54 getsynthvnode(const char *devname)
56 struct vnode *vp;
57 struct nchandle nch;
58 struct nlookupdata nd;
59 struct ucred *cred = proc0.p_ucred;
60 int error;
62 KKASSERT(synth_inited != 0);
63 KKASSERT(synth_mp != NULL);
64 KKASSERT(synth_mp->mnt_ncmountpt.mount != NULL);
66 /* Sync devfs/disks twice to make sure all devices are around */
67 if (synth_synced < 2) {
68 sync_devs();
69 ++synth_synced;
72 error = nlookup_init_root(&nd,
73 devname, UIO_SYSSPACE, NLC_FOLLOW,
74 cred, &synth_mp->mnt_ncmountpt,
75 &synth_mp->mnt_ncmountpt);
77 if (error) {
78 panic("synth: nlookup_init_root failed with %d\n", error);
79 /* NOTREACHED */
82 error = nlookup(&nd);
83 if (error == 0) {
84 if (nd.nl_nch.ncp->nc_vp == NULL) {
85 kprintf("synth: nc_vp == NULL\n");
86 return (NULL);
88 nch = nd.nl_nch;
89 cache_zero(&nd.nl_nch);
92 nlookup_done(&nd);
93 if (error) {
94 if (error != ENOENT) { /* Don't bother warning about ENOENT */
95 kprintf("synth: nlookup of %s failed with %d\n",
96 devname, error);
98 return (NULL);
101 vp = nch.ncp->nc_vp;
102 /* A VX locked & refd vnode must be returned. */
103 error = vget(vp, LK_EXCLUSIVE);
104 cache_unlock(&nch);
106 if (error) {
107 kprintf("synth: could not vget vnode\n");
108 return (NULL);
111 return (vp);
114 static void
115 synthinit(void *arg __unused)
117 int error;
119 if ((error = vfs_rootmountalloc("devfs", "dummy", &synth_mp))) {
120 panic("synth: vfs_rootmountalloc failed with %d\n", error);
121 /* NOTREACHED */
123 if ((error = VFS_MOUNT(synth_mp, NULL, NULL, proc0.p_ucred))) {
124 panic("synth: vfs_mount failed with %d\n", error);
125 /* NOTREACHED */
127 if ((error = VFS_ROOT(synth_mp, &synth_vp))) {
128 panic("synth: vfs_root failed with %d\n", error);
129 /* NOTREACHED */
131 cache_allocroot(&synth_mp->mnt_ncmountpt, synth_mp, synth_vp);
132 cache_unlock(&synth_mp->mnt_ncmountpt); /* leave ref intact */
133 vput(synth_vp);
135 synth_inited = 1;
138 SYSINIT(synthinit, SI_SUB_VFS, SI_ORDER_ANY, synthinit, NULL)