Merge branch 'vendor/TOP'
[dragonfly.git] / sys / kern / vfs_synth.c
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/thread2.h>
46 #include <sys/devfs.h>
47
48 struct mount    *synth_mp;
49 struct vnode    *synth_vp;
50 static int      synth_inited = 0;
51 static int      synth_synced = 0;
52
53 struct vnode *
54 getsynthvnode(const char *devname)
55 {
56         struct vnode *vp;
57         struct nchandle nch;
58         struct nlookupdata nd;
59         struct ucred *cred = proc0.p_ucred;
60         int error;
61
62         KKASSERT(synth_inited != 0);
63         KKASSERT(synth_mp != NULL);
64         KKASSERT(synth_mp->mnt_ncmountpt.mount != NULL);
65
66         /* Sync devfs/disks twice to make sure all devices are around */
67         if (synth_synced < 2) {
68                 sync_devs();
69                 ++synth_synced;
70         }
71
72         error = nlookup_init_root(&nd, devname, UIO_SYSSPACE, NLC_FOLLOW,
73                                   cred, &synth_mp->mnt_ncmountpt,
74                                   &synth_mp->mnt_ncmountpt);
75
76         if (error) {
77                 panic("synth: nlookup_init_root failed with %d", error);
78                 /* NOTREACHED */
79         }
80
81         error = nlookup(&nd);
82         if (error == 0) {
83                 if (nd.nl_nch.ncp->nc_vp == NULL) {
84                         kprintf("synth: nc_vp == NULL\n");
85                         return (NULL);
86                 }
87                 nch = nd.nl_nch;
88                 cache_zero(&nd.nl_nch);
89         }
90
91         nlookup_done(&nd);
92         if (error) {
93                 if (error != ENOENT) { /* Don't bother warning about ENOENT */
94                         kprintf("synth: nlookup of %s failed with %d\n",
95                             devname, error);
96                 }
97                 return (NULL);
98         }
99
100         vp = nch.ncp->nc_vp;
101         /* A VX locked & refd vnode must be returned. */
102         error = vget(vp, LK_EXCLUSIVE);
103         cache_unlock(&nch);
104
105         if (error) {
106                 kprintf("synth: could not vget vnode\n");
107                 return (NULL);
108         }
109
110         return (vp);
111 }
112
113 static void
114 synthinit(void *arg __unused)
115 {
116         int error;
117
118         if ((error = vfs_rootmountalloc("devfs", "dummy", &synth_mp))) {
119                 panic("synth: vfs_rootmountalloc failed with %d", error);
120                 /* NOTREACHED */
121         }
122         if ((error = VFS_MOUNT(synth_mp, NULL, NULL, proc0.p_ucred))) {
123                 panic("synth: vfs_mount failed with %d", error);
124                 /* NOTREACHED */
125         }
126         if ((error = VFS_ROOT(synth_mp, &synth_vp))) {
127                 panic("synth: vfs_root failed with %d", error);
128                 /* NOTREACHED */
129         }
130         cache_allocroot(&synth_mp->mnt_ncmountpt, synth_mp, synth_vp);
131         cache_unlock(&synth_mp->mnt_ncmountpt); /* leave ref intact */
132         vput(synth_vp);
133
134         synth_inited = 1;
135 }
136
137 SYSINIT(synthinit, SI_SUB_VFS, SI_ORDER_ANY, synthinit, NULL);