]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/cddl/compat/opensolaris/kern/opensolaris_kobj.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / cddl / compat / opensolaris / kern / opensolaris_kobj.c
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
43 kobj_free(void *address, size_t size)
44 {
45
46         kmem_free(address, size);
47 }
48
49 void *
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 *
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 *
67 kobj_open_file_vnode(const char *file)
68 {
69         struct thread *td = curthread;
70         struct nameidata nd;
71         int error, flags;
72
73         if (td->td_proc->p_fd->fd_rdir == NULL)
74                 td->td_proc->p_fd->fd_rdir = rootvnode;
75         if (td->td_proc->p_fd->fd_cdir == NULL)
76                 td->td_proc->p_fd->fd_cdir = rootvnode;
77
78         flags = FREAD;
79         NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, file, td);
80         error = vn_open_cred(&nd, &flags, 0, td->td_ucred, NULL);
81         NDFREE(&nd, NDF_ONLY_PNBUF);
82         if (error != 0)
83                 return (NULL);
84         /* We just unlock so we hold a reference. */
85         VOP_UNLOCK(nd.ni_vp, 0, td);
86         return (nd.ni_vp);
87 }
88
89 static void *
90 kobj_open_file_loader(const char *file)
91 {
92
93         return (preload_search_by_name(file));
94 }
95
96 struct _buf *
97 kobj_open_file(const char *file)
98 {
99         struct _buf *out;
100
101         out = kmem_alloc(sizeof(*out), KM_SLEEP);
102         out->mounted = root_mounted();
103         /*
104          * If root is already mounted we read file using file system,
105          * if not, we use loader.
106          */
107         if (out->mounted)
108                 out->ptr = kobj_open_file_vnode(file);
109         else
110                 out->ptr = kobj_open_file_loader(file);
111         if (out->ptr == NULL) {
112                 kmem_free(out, sizeof(*out));
113                 return ((struct _buf *)-1);
114         }
115         return (out);
116 }
117
118 static int
119 kobj_get_filesize_vnode(struct _buf *file, uint64_t *size)
120 {
121         struct vnode *vp = file->ptr;
122         struct thread *td = curthread;
123         struct vattr va;
124         int error;
125
126         vn_lock(vp, LK_SHARED | LK_RETRY, td);
127         error = VOP_GETATTR(vp, &va, td->td_ucred, td);
128         VOP_UNLOCK(vp, 0, td);
129         if (error == 0)
130                 *size = (uint64_t)va.va_size;
131         return (error);
132 }
133
134 static int
135 kobj_get_filesize_loader(struct _buf *file, uint64_t *size)
136 {
137         void *ptr;
138
139         ptr = preload_search_info(file->ptr, MODINFO_SIZE);
140         if (ptr == NULL)
141                 return (ENOENT);
142         *size = (uint64_t)*(size_t *)ptr;
143         return (0);
144 }
145
146 int
147 kobj_get_filesize(struct _buf *file, uint64_t *size)
148 {
149
150         if (file->mounted)
151                 return (kobj_get_filesize_vnode(file, size));
152         else
153                 return (kobj_get_filesize_loader(file, size));
154 }
155
156 int
157 kobj_read_file_vnode(struct _buf *file, char *buf, unsigned size, unsigned off)
158 {
159         struct vnode *vp = file->ptr;
160         struct thread *td = curthread;
161         struct uio auio;
162         struct iovec aiov;
163         int error;
164
165         bzero(&aiov, sizeof(aiov));
166         bzero(&auio, sizeof(auio));
167
168         aiov.iov_base = buf;
169         aiov.iov_len = size;
170
171         auio.uio_iov = &aiov;
172         auio.uio_offset = (off_t)off;
173         auio.uio_segflg = UIO_SYSSPACE;
174         auio.uio_rw = UIO_READ;
175         auio.uio_iovcnt = 1;
176         auio.uio_resid = size;
177         auio.uio_td = td;
178
179         vn_lock(vp, LK_SHARED | LK_RETRY, td);
180         error = VOP_READ(vp, &auio, IO_UNIT | IO_SYNC, td->td_ucred);
181         VOP_UNLOCK(vp, 0, td);
182         return (error != 0 ? -1 : size - auio.uio_resid);
183 }
184
185 int
186 kobj_read_file_loader(struct _buf *file, char *buf, unsigned size, unsigned off)
187 {
188         char *ptr;
189
190         ptr = preload_search_info(file->ptr, MODINFO_ADDR);
191         if (ptr == NULL)
192                 return (ENOENT);
193         ptr = *(void **)ptr;
194         bcopy(ptr + off, buf, size);
195         return (0);
196 }
197
198 int
199 kobj_read_file(struct _buf *file, char *buf, unsigned size, unsigned off)
200 {
201
202         if (file->mounted)
203                 return (kobj_read_file_vnode(file, buf, size, off));
204         else
205                 return (kobj_read_file_loader(file, buf, size, off));
206 }
207
208 void
209 kobj_close_file(struct _buf *file)
210 {
211
212         if (file->mounted) {
213                 struct vnode *vp = file->ptr;
214                 struct thread *td = curthread;
215                 int flags = FREAD;
216
217                 vn_close(vp, flags, td->td_ucred, td);
218         }
219         kmem_free(file, sizeof(*file));
220 }