1 /*-
2 * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/kthread.h>
35 #include <sys/namei.h>
36 #include <sys/proc.h>
37 #include <sys/filedesc.h>
38 #include <sys/fcntl.h>
39 #include <sys/linker.h>
40 #include <sys/kobj.h>
41
42 void
kobj_free(void * address,size_t size)43 kobj_free(void *address, size_t size)
44 {
45
46 kmem_free(address, size);
47 }
48
49 void *
kobj_alloc(size_t size,int flag)50 kobj_alloc(size_t size, int flag)
51 {
52
53 return (kmem_alloc(size, (flag & KM_NOWAIT) ? KM_NOSLEEP : KM_SLEEP));
54 }
55
56 void *
kobj_zalloc(size_t size,int flag)57 kobj_zalloc(size_t size, int flag)
58 {
59 void *p;
60
61 if ((p = kobj_alloc(size, flag)) != NULL)
62 bzero(p, size);
63 return (p);
64 }
65
66 static void *
kobj_open_file_vnode(const char * file)67 kobj_open_file_vnode(const char *file)
68 {
69 struct thread *td = curthread;
70 struct nameidata nd;
71 int error, flags;
72
73 pwd_ensure_dirs();
74
75 flags = FREAD | O_NOFOLLOW;
76 NDINIT(&nd, LOOKUP, 0, UIO_SYSSPACE, file, td);
77 error = vn_open_cred(&nd, &flags, 0, 0, curthread->td_ucred, NULL);
78 if (error != 0)
79 return (NULL);
80 NDFREE(&nd, NDF_ONLY_PNBUF);
81 /* We just unlock so we hold a reference. */
82 VOP_UNLOCK(nd.ni_vp, 0);
83 return (nd.ni_vp);
84 }
85
86 static void *
kobj_open_file_loader(const char * file)87 kobj_open_file_loader(const char *file)
88 {
89
90 return (preload_search_by_name(file));
91 }
92
93 struct _buf *
kobj_open_file(const char * file)94 kobj_open_file(const char *file)
95 {
96 struct _buf *out;
97
98 out = kmem_alloc(sizeof(*out), KM_SLEEP);
99 out->mounted = root_mounted();
100 /*
101 * If root is already mounted we read file using file system,
102 * if not, we use loader.
103 */
104 if (out->mounted)
105 out->ptr = kobj_open_file_vnode(file);
106 else
107 out->ptr = kobj_open_file_loader(file);
108 if (out->ptr == NULL) {
109 kmem_free(out, sizeof(*out));
110 return ((struct _buf *)-1);
111 }
112 return (out);
113 }
114
115 static int
kobj_get_filesize_vnode(struct _buf * file,uint64_t * size)116 kobj_get_filesize_vnode(struct _buf *file, uint64_t *size)
117 {
118 struct vnode *vp = file->ptr;
119 struct vattr va;
120 int error;
121
122 vn_lock(vp, LK_SHARED | LK_RETRY);
123 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
124 VOP_UNLOCK(vp, 0);
125 if (error == 0)
126 *size = (uint64_t)va.va_size;
127 return (error);
128 }
129
130 static int
kobj_get_filesize_loader(struct _buf * file,uint64_t * size)131 kobj_get_filesize_loader(struct _buf *file, uint64_t *size)
132 {
133 void *ptr;
134
135 ptr = preload_search_info(file->ptr, MODINFO_SIZE);
136 if (ptr == NULL)
137 return (ENOENT);
138 *size = (uint64_t)*(size_t *)ptr;
139 return (0);
140 }
141
142 int
kobj_get_filesize(struct _buf * file,uint64_t * size)143 kobj_get_filesize(struct _buf *file, uint64_t *size)
144 {
145
146 if (file->mounted)
147 return (kobj_get_filesize_vnode(file, size));
148 else
149 return (kobj_get_filesize_loader(file, size));
150 }
151
152 int
kobj_read_file_vnode(struct _buf * file,char * buf,unsigned size,unsigned off)153 kobj_read_file_vnode(struct _buf *file, char *buf, unsigned size, unsigned off)
154 {
155 struct vnode *vp = file->ptr;
156 struct thread *td = curthread;
157 struct uio auio;
158 struct iovec aiov;
159 int error;
160
161 bzero(&aiov, sizeof(aiov));
162 bzero(&auio, sizeof(auio));
163
164 aiov.iov_base = buf;
165 aiov.iov_len = size;
166
167 auio.uio_iov = &aiov;
168 auio.uio_offset = (off_t)off;
169 auio.uio_segflg = UIO_SYSSPACE;
170 auio.uio_rw = UIO_READ;
171 auio.uio_iovcnt = 1;
172 auio.uio_resid = size;
173 auio.uio_td = td;
174
175 vn_lock(vp, LK_SHARED | LK_RETRY);
176 error = VOP_READ(vp, &auio, IO_UNIT | IO_SYNC, td->td_ucred);
177 VOP_UNLOCK(vp, 0);
178 return (error != 0 ? -1 : size - auio.uio_resid);
179 }
180
181 int
kobj_read_file_loader(struct _buf * file,char * buf,unsigned size,unsigned off)182 kobj_read_file_loader(struct _buf *file, char *buf, unsigned size, unsigned off)
183 {
184 char *ptr;
185
186 ptr = preload_fetch_addr(file->ptr);
187 if (ptr == NULL)
188 return (ENOENT);
189 bcopy(ptr + off, buf, size);
190 return (0);
191 }
192
193 int
kobj_read_file(struct _buf * file,char * buf,unsigned size,unsigned off)194 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
195 {
196
197 if (file->mounted)
198 return (kobj_read_file_vnode(file, buf, size, off));
199 else
200 return (kobj_read_file_loader(file, buf, size, off));
201 }
202
203 void
kobj_close_file(struct _buf * file)204 kobj_close_file(struct _buf *file)
205 {
206
207 if (file->mounted)
208 vn_close(file->ptr, FREAD, curthread->td_ucred, curthread);
209 kmem_free(file, sizeof(*file));
210 }
211