1 /*
2 * Copyright (c) 2010 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung (at) gmail.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
20 *
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.
33 */
34
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/devfs.h>
46
47 struct mount *synth_mp;
48 struct vnode *synth_vp;
49 static int synth_inited = 0;
50 static int synth_synced = 0;
51
52 struct vnode *
getsynthvnode(const char * devname)53 getsynthvnode(const char *devname)
54 {
55 struct vnode *vp;
56 struct nchandle nch;
57 struct nlookupdata nd;
58 struct ucred *cred = proc0.p_ucred;
59 int error;
60
61 KKASSERT(synth_inited != 0);
62 KKASSERT(synth_mp != NULL);
63 KKASSERT(synth_mp->mnt_ncmountpt.mount != NULL);
64
65 /* Sync devfs/disks twice to make sure all devices are around */
66 if (synth_synced < 2) {
67 sync_devs();
68 ++synth_synced;
69 }
70
71 error = nlookup_init_root(&nd, devname, UIO_SYSSPACE, NLC_FOLLOW,
72 cred, &synth_mp->mnt_ncmountpt,
73 &synth_mp->mnt_ncmountpt);
74
75 if (error) {
76 panic("synth: nlookup_init_root failed with %d", error);
77 /* NOTREACHED */
78 }
79
80 error = nlookup(&nd);
81 if (error == 0) {
82 if (nd.nl_nch.ncp->nc_vp == NULL) {
83 kprintf("synth: nc_vp == NULL\n");
84 return (NULL);
85 }
86 nch = nd.nl_nch;
87 cache_zero(&nd.nl_nch);
88 }
89
90 nlookup_done(&nd);
91 if (error) {
92 if (error != ENOENT) { /* Don't bother warning about ENOENT */
93 kprintf("synth: nlookup of %s failed with %d\n",
94 devname, error);
95 }
96 return (NULL);
97 }
98
99 vp = nch.ncp->nc_vp;
100 /* A VX locked & refd vnode must be returned. */
101 error = vget(vp, LK_EXCLUSIVE);
102 cache_unlock(&nch);
103
104 if (error) {
105 kprintf("synth: could not vget vnode\n");
106 return (NULL);
107 }
108
109 return (vp);
110 }
111
112 static void
synthinit(void * arg __unused)113 synthinit(void *arg __unused)
114 {
115 int error;
116
117 if ((error = vfs_rootmountalloc("devfs", "dummy", &synth_mp))) {
118 panic("synth: vfs_rootmountalloc failed with %d", error);
119 /* NOTREACHED */
120 }
121 if ((error = VFS_MOUNT(synth_mp, NULL, NULL, proc0.p_ucred))) {
122 panic("synth: vfs_mount failed with %d", error);
123 /* NOTREACHED */
124 }
125 if ((error = VFS_ROOT(synth_mp, &synth_vp))) {
126 panic("synth: vfs_root failed with %d", error);
127 /* NOTREACHED */
128 }
129 cache_allocroot(&synth_mp->mnt_ncmountpt, synth_mp, synth_vp);
130 cache_unlock(&synth_mp->mnt_ncmountpt); /* leave ref intact */
131 vput(synth_vp);
132
133 synth_inited = 1;
134 }
135
136 SYSINIT(synthinit, SI_SUB_VFS, SI_ORDER_ANY, synthinit, NULL);
137