subr_kobj_vfs.c revision 1.8 1 1.8 pooka /* $NetBSD: subr_kobj_vfs.c,v 1.8 2015/08/24 22:50:32 pooka Exp $ */
2 1.1 pooka
3 1.1 pooka /*-
4 1.1 pooka * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 pooka * All rights reserved.
6 1.1 pooka *
7 1.1 pooka * This code is derived from software developed for The NetBSD Foundation
8 1.1 pooka * by Andrew Doran.
9 1.1 pooka *
10 1.1 pooka * Redistribution and use in source and binary forms, with or without
11 1.1 pooka * modification, are permitted provided that the following conditions
12 1.1 pooka * are met:
13 1.1 pooka * 1. Redistributions of source code must retain the above copyright
14 1.1 pooka * notice, this list of conditions and the following disclaimer.
15 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 pooka * notice, this list of conditions and the following disclaimer in the
17 1.1 pooka * documentation and/or other materials provided with the distribution.
18 1.1 pooka *
19 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 pooka * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 pooka * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 pooka * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 pooka * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 pooka * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 pooka * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pooka * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 pooka * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 pooka * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 pooka * POSSIBILITY OF SUCH DAMAGE.
30 1.1 pooka */
31 1.1 pooka
32 1.1 pooka /*-
33 1.1 pooka * Copyright (c) 1998-2000 Doug Rabson
34 1.1 pooka * Copyright (c) 2004 Peter Wemm
35 1.1 pooka * All rights reserved.
36 1.1 pooka *
37 1.1 pooka * Redistribution and use in source and binary forms, with or without
38 1.1 pooka * modification, are permitted provided that the following conditions
39 1.1 pooka * are met:
40 1.1 pooka * 1. Redistributions of source code must retain the above copyright
41 1.1 pooka * notice, this list of conditions and the following disclaimer.
42 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 pooka * notice, this list of conditions and the following disclaimer in the
44 1.1 pooka * documentation and/or other materials provided with the distribution.
45 1.1 pooka *
46 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 1.1 pooka * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 1.1 pooka * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 1.1 pooka * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 1.1 pooka * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 1.1 pooka * SUCH DAMAGE.
57 1.1 pooka */
58 1.1 pooka
59 1.1 pooka /*
60 1.1 pooka * Kernel loader vfs routines.
61 1.1 pooka */
62 1.1 pooka
63 1.1 pooka #include <sys/kobj_impl.h>
64 1.8 pooka
65 1.8 pooka #ifdef _KERNEL_OPT
66 1.1 pooka #include "opt_modular.h"
67 1.8 pooka #endif
68 1.1 pooka
69 1.1 pooka #ifdef MODULAR
70 1.1 pooka
71 1.1 pooka #include <sys/param.h>
72 1.1 pooka #include <sys/fcntl.h>
73 1.1 pooka #include <sys/module.h>
74 1.1 pooka #include <sys/namei.h>
75 1.1 pooka #include <sys/vnode.h>
76 1.1 pooka
77 1.1 pooka #include <sys/cdefs.h>
78 1.8 pooka __KERNEL_RCSID(0, "$NetBSD: subr_kobj_vfs.c,v 1.8 2015/08/24 22:50:32 pooka Exp $");
79 1.1 pooka
80 1.1 pooka static void
81 1.2 pooka kobj_close_vfs(kobj_t ko)
82 1.1 pooka {
83 1.1 pooka
84 1.3 hannken VOP_UNLOCK(ko->ko_source);
85 1.1 pooka vn_close(ko->ko_source, FREAD, kauth_cred_get());
86 1.1 pooka }
87 1.1 pooka
88 1.1 pooka /*
89 1.1 pooka * kobj_read:
90 1.1 pooka *
91 1.1 pooka * Utility function: read from the object.
92 1.1 pooka */
93 1.1 pooka static int
94 1.2 pooka kobj_read_vfs(kobj_t ko, void **basep, size_t size, off_t off,
95 1.1 pooka bool allocate)
96 1.1 pooka {
97 1.1 pooka size_t resid;
98 1.1 pooka void *base;
99 1.1 pooka int error;
100 1.1 pooka
101 1.1 pooka KASSERT(ko->ko_source != NULL);
102 1.1 pooka
103 1.1 pooka if (allocate) {
104 1.1 pooka base = kmem_alloc(size, KM_SLEEP);
105 1.1 pooka } else {
106 1.1 pooka base = *basep;
107 1.1 pooka KASSERT((uintptr_t)base >= (uintptr_t)ko->ko_address);
108 1.1 pooka KASSERT((uintptr_t)base + size <=
109 1.1 pooka (uintptr_t)ko->ko_address + ko->ko_size);
110 1.1 pooka }
111 1.1 pooka
112 1.1 pooka error = vn_rdwr(UIO_READ, ko->ko_source, base, size, off,
113 1.1 pooka UIO_SYSSPACE, IO_NODELOCKED, curlwp->l_cred, &resid,
114 1.1 pooka curlwp);
115 1.1 pooka
116 1.1 pooka if (error == 0 && resid != 0) {
117 1.1 pooka error = EINVAL;
118 1.1 pooka }
119 1.1 pooka
120 1.1 pooka if (allocate && error != 0) {
121 1.1 pooka kmem_free(base, size);
122 1.1 pooka base = NULL;
123 1.1 pooka }
124 1.1 pooka
125 1.1 pooka if (allocate)
126 1.1 pooka *basep = base;
127 1.1 pooka
128 1.1 pooka return error;
129 1.1 pooka }
130 1.1 pooka
131 1.1 pooka /*
132 1.2 pooka * kobj_load_vfs:
133 1.1 pooka *
134 1.1 pooka * Load an object located in the file system.
135 1.1 pooka */
136 1.1 pooka int
137 1.2 pooka kobj_load_vfs(kobj_t *kop, const char *path, const bool nochroot)
138 1.1 pooka {
139 1.1 pooka struct nameidata nd;
140 1.4 dholland struct pathbuf *pb;
141 1.1 pooka int error;
142 1.1 pooka kobj_t ko;
143 1.1 pooka
144 1.5 mbalmer KASSERT(path != NULL);
145 1.5 mbalmer if (strchr(path, '/') == NULL)
146 1.5 mbalmer return ENOENT;
147 1.5 mbalmer
148 1.1 pooka ko = kmem_zalloc(sizeof(*ko), KM_SLEEP);
149 1.1 pooka if (ko == NULL) {
150 1.1 pooka return ENOMEM;
151 1.1 pooka }
152 1.1 pooka
153 1.4 dholland pb = pathbuf_create(path);
154 1.4 dholland if (pb == NULL) {
155 1.4 dholland kmem_free(ko, sizeof(*ko));
156 1.4 dholland return ENOMEM;
157 1.4 dholland }
158 1.4 dholland
159 1.4 dholland NDINIT(&nd, LOOKUP, FOLLOW | (nochroot ? NOCHROOT : 0), pb);
160 1.1 pooka error = vn_open(&nd, FREAD, 0);
161 1.1 pooka
162 1.1 pooka if (error != 0) {
163 1.4 dholland pathbuf_destroy(pb);
164 1.1 pooka kmem_free(ko, sizeof(*ko));
165 1.1 pooka return error;
166 1.1 pooka }
167 1.1 pooka
168 1.1 pooka ko->ko_type = KT_VNODE;
169 1.6 christos kobj_setname(ko, path);
170 1.1 pooka ko->ko_source = nd.ni_vp;
171 1.2 pooka ko->ko_read = kobj_read_vfs;
172 1.2 pooka ko->ko_close = kobj_close_vfs;
173 1.4 dholland pathbuf_destroy(pb);
174 1.1 pooka
175 1.1 pooka *kop = ko;
176 1.1 pooka return kobj_load(ko);
177 1.1 pooka }
178 1.1 pooka
179 1.1 pooka #else /* MODULAR */
180 1.1 pooka
181 1.1 pooka int
182 1.2 pooka kobj_load_vfs(kobj_t *kop, const char *path, const bool nochroot)
183 1.1 pooka {
184 1.1 pooka
185 1.1 pooka return ENOSYS;
186 1.1 pooka }
187 1.1 pooka
188 1.1 pooka #endif
189