netbsd32_fs.c revision 1.36.2.5 1 1.36.2.5 ad /* $NetBSD: netbsd32_fs.c,v 1.36.2.5 2007/07/15 13:27:13 ad Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1998, 2001 Matthew R. Green
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.1 mrg * Redistribution and use in source and binary forms, with or without
8 1.1 mrg * modification, are permitted provided that the following conditions
9 1.1 mrg * are met:
10 1.1 mrg * 1. Redistributions of source code must retain the above copyright
11 1.1 mrg * notice, this list of conditions and the following disclaimer.
12 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 mrg * notice, this list of conditions and the following disclaimer in the
14 1.1 mrg * documentation and/or other materials provided with the distribution.
15 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
16 1.1 mrg * derived from this software without specific prior written permission.
17 1.1 mrg *
18 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 mrg * SUCH DAMAGE.
29 1.1 mrg */
30 1.7 lukem
31 1.7 lukem #include <sys/cdefs.h>
32 1.36.2.5 ad __KERNEL_RCSID(0, "$NetBSD: netbsd32_fs.c,v 1.36.2.5 2007/07/15 13:27:13 ad Exp $");
33 1.1 mrg
34 1.5 mrg #if defined(_KERNEL_OPT)
35 1.1 mrg #include "opt_ktrace.h"
36 1.1 mrg #endif
37 1.1 mrg
38 1.1 mrg #include <sys/param.h>
39 1.1 mrg #include <sys/systm.h>
40 1.1 mrg #include <sys/malloc.h>
41 1.1 mrg #include <sys/mount.h>
42 1.1 mrg #include <sys/socket.h>
43 1.1 mrg #include <sys/socketvar.h>
44 1.1 mrg #include <sys/stat.h>
45 1.1 mrg #include <sys/time.h>
46 1.1 mrg #include <sys/ktrace.h>
47 1.1 mrg #include <sys/resourcevar.h>
48 1.1 mrg #include <sys/vnode.h>
49 1.1 mrg #include <sys/file.h>
50 1.1 mrg #include <sys/filedesc.h>
51 1.1 mrg #include <sys/namei.h>
52 1.18 cube #include <sys/statvfs.h>
53 1.1 mrg #include <sys/syscallargs.h>
54 1.1 mrg #include <sys/proc.h>
55 1.22 christos #include <sys/dirent.h>
56 1.27 elad #include <sys/kauth.h>
57 1.36.2.1 ad #include <sys/vfs_syscalls.h>
58 1.1 mrg
59 1.1 mrg #include <compat/netbsd32/netbsd32.h>
60 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
61 1.1 mrg #include <compat/netbsd32/netbsd32_conv.h>
62 1.28 martin #include <compat/sys/mount.h>
63 1.1 mrg
64 1.1 mrg
65 1.23 christos static int dofilereadv32 __P((struct lwp *, int, struct file *, struct netbsd32_iovec *,
66 1.1 mrg int, off_t *, int, register_t *));
67 1.23 christos static int dofilewritev32 __P((struct lwp *, int, struct file *, struct netbsd32_iovec *,
68 1.1 mrg int, off_t *, int, register_t *));
69 1.1 mrg
70 1.36.2.5 ad struct iovec *
71 1.36.2.5 ad netbsd32_get_iov(struct netbsd32_iovec *iov32, int iovlen, struct iovec *aiov,
72 1.36.2.5 ad int aiov_len)
73 1.36.2.5 ad {
74 1.36.2.5 ad #define N_IOV32 8
75 1.36.2.5 ad struct netbsd32_iovec aiov32[N_IOV32];
76 1.36.2.5 ad struct iovec *iov = aiov;
77 1.36.2.5 ad struct iovec *iovp;
78 1.36.2.5 ad int i, n, j;
79 1.36.2.5 ad int error;
80 1.36.2.5 ad
81 1.36.2.5 ad if (iovlen < 0 || iovlen > IOV_MAX)
82 1.36.2.5 ad return NULL;
83 1.36.2.5 ad
84 1.36.2.5 ad if (iovlen > aiov_len)
85 1.36.2.5 ad iov = malloc(iovlen * sizeof (*iov), M_TEMP, M_WAITOK);
86 1.36.2.5 ad
87 1.36.2.5 ad iovp = iov;
88 1.36.2.5 ad for (i = 0; i < iovlen; iov32 += N_IOV32, i += N_IOV32) {
89 1.36.2.5 ad n = iovlen - i;
90 1.36.2.5 ad if (n > N_IOV32)
91 1.36.2.5 ad n = N_IOV32;
92 1.36.2.5 ad error = copyin(iov32, aiov32, n * sizeof (*iov32));
93 1.36.2.5 ad if (error != 0) {
94 1.36.2.5 ad if (iov != aiov)
95 1.36.2.5 ad free(iov, M_TEMP);
96 1.36.2.5 ad return NULL;
97 1.36.2.5 ad }
98 1.36.2.5 ad for (j = 0; j < n; iovp++, j++) {
99 1.36.2.5 ad iovp->iov_base = NETBSD32PTR64(aiov32[j].iov_base);
100 1.36.2.5 ad iovp->iov_len = aiov32[j].iov_len;
101 1.36.2.5 ad }
102 1.36.2.5 ad }
103 1.36.2.5 ad return iov;
104 1.36.2.5 ad #undef N_IOV32
105 1.36.2.5 ad }
106 1.36.2.5 ad
107 1.1 mrg int
108 1.11 thorpej netbsd32_readv(l, v, retval)
109 1.11 thorpej struct lwp *l;
110 1.1 mrg void *v;
111 1.1 mrg register_t *retval;
112 1.1 mrg {
113 1.1 mrg struct netbsd32_readv_args /* {
114 1.1 mrg syscallarg(int) fd;
115 1.1 mrg syscallarg(const netbsd32_iovecp_t) iovp;
116 1.1 mrg syscallarg(int) iovcnt;
117 1.1 mrg } */ *uap = v;
118 1.1 mrg int fd = SCARG(uap, fd);
119 1.11 thorpej struct proc *p = l->l_proc;
120 1.1 mrg struct file *fp;
121 1.1 mrg struct filedesc *fdp = p->p_fd;
122 1.1 mrg
123 1.6 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
124 1.6 thorpej return (EBADF);
125 1.6 thorpej
126 1.6 thorpej if ((fp->f_flag & FREAD) == 0)
127 1.1 mrg return (EBADF);
128 1.1 mrg
129 1.9 jdolecek FILE_USE(fp);
130 1.9 jdolecek
131 1.23 christos return (dofilereadv32(l, fd, fp,
132 1.36.2.3 ad (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
133 1.10 scw SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
134 1.1 mrg }
135 1.1 mrg
136 1.1 mrg /* Damn thing copies in the iovec! */
137 1.1 mrg int
138 1.23 christos dofilereadv32(l, fd, fp, iovp, iovcnt, offset, flags, retval)
139 1.23 christos struct lwp *l;
140 1.1 mrg int fd;
141 1.1 mrg struct file *fp;
142 1.1 mrg struct netbsd32_iovec *iovp;
143 1.1 mrg int iovcnt;
144 1.1 mrg off_t *offset;
145 1.1 mrg int flags;
146 1.1 mrg register_t *retval;
147 1.1 mrg {
148 1.1 mrg struct uio auio;
149 1.1 mrg struct iovec *iov;
150 1.1 mrg struct iovec *needfree;
151 1.1 mrg struct iovec aiov[UIO_SMALLIOV];
152 1.1 mrg long i, cnt, error = 0;
153 1.1 mrg u_int iovlen;
154 1.1 mrg #ifdef KTRACE
155 1.1 mrg struct iovec *ktriov = NULL;
156 1.1 mrg #endif
157 1.1 mrg
158 1.1 mrg /* note: can't use iovlen until iovcnt is validated */
159 1.1 mrg iovlen = iovcnt * sizeof(struct iovec);
160 1.1 mrg if ((u_int)iovcnt > UIO_SMALLIOV) {
161 1.9 jdolecek if ((u_int)iovcnt > IOV_MAX) {
162 1.9 jdolecek error = EINVAL;
163 1.9 jdolecek goto out;
164 1.9 jdolecek }
165 1.9 jdolecek iov = malloc(iovlen, M_IOV, M_WAITOK);
166 1.1 mrg needfree = iov;
167 1.1 mrg } else if ((u_int)iovcnt > 0) {
168 1.1 mrg iov = aiov;
169 1.1 mrg needfree = NULL;
170 1.9 jdolecek } else {
171 1.9 jdolecek error = EINVAL;
172 1.9 jdolecek goto out;
173 1.9 jdolecek }
174 1.1 mrg
175 1.1 mrg auio.uio_iov = iov;
176 1.1 mrg auio.uio_iovcnt = iovcnt;
177 1.1 mrg auio.uio_rw = UIO_READ;
178 1.24 yamt auio.uio_vmspace = l->l_proc->p_vmspace;
179 1.1 mrg error = netbsd32_to_iovecin(iovp, iov, iovcnt);
180 1.1 mrg if (error)
181 1.1 mrg goto done;
182 1.1 mrg auio.uio_resid = 0;
183 1.1 mrg for (i = 0; i < iovcnt; i++) {
184 1.1 mrg auio.uio_resid += iov->iov_len;
185 1.1 mrg /*
186 1.1 mrg * Reads return ssize_t because -1 is returned on error.
187 1.1 mrg * Therefore we must restrict the length to SSIZE_MAX to
188 1.1 mrg * avoid garbage return values.
189 1.1 mrg */
190 1.1 mrg if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
191 1.1 mrg error = EINVAL;
192 1.1 mrg goto done;
193 1.1 mrg }
194 1.1 mrg iov++;
195 1.1 mrg }
196 1.1 mrg #ifdef KTRACE
197 1.1 mrg /*
198 1.1 mrg * if tracing, save a copy of iovec
199 1.1 mrg */
200 1.23 christos if (KTRPOINT(l->l_proc, KTR_GENIO)) {
201 1.9 jdolecek ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
202 1.36 christos memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
203 1.1 mrg }
204 1.1 mrg #endif
205 1.1 mrg cnt = auio.uio_resid;
206 1.1 mrg error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
207 1.1 mrg if (error)
208 1.1 mrg if (auio.uio_resid != cnt && (error == ERESTART ||
209 1.1 mrg error == EINTR || error == EWOULDBLOCK))
210 1.1 mrg error = 0;
211 1.1 mrg cnt -= auio.uio_resid;
212 1.1 mrg #ifdef KTRACE
213 1.23 christos if (KTRPOINT(l->l_proc, KTR_GENIO))
214 1.1 mrg if (error == 0) {
215 1.23 christos ktrgenio(l, fd, UIO_READ, ktriov, cnt,
216 1.1 mrg error);
217 1.9 jdolecek free(ktriov, M_TEMP);
218 1.1 mrg }
219 1.1 mrg #endif
220 1.1 mrg *retval = cnt;
221 1.1 mrg done:
222 1.1 mrg if (needfree)
223 1.9 jdolecek free(needfree, M_IOV);
224 1.9 jdolecek out:
225 1.23 christos FILE_UNUSE(fp, l);
226 1.1 mrg return (error);
227 1.1 mrg }
228 1.1 mrg
229 1.1 mrg int
230 1.11 thorpej netbsd32_writev(l, v, retval)
231 1.11 thorpej struct lwp *l;
232 1.1 mrg void *v;
233 1.1 mrg register_t *retval;
234 1.1 mrg {
235 1.1 mrg struct netbsd32_writev_args /* {
236 1.1 mrg syscallarg(int) fd;
237 1.1 mrg syscallarg(const netbsd32_iovecp_t) iovp;
238 1.1 mrg syscallarg(int) iovcnt;
239 1.1 mrg } */ *uap = v;
240 1.1 mrg int fd = SCARG(uap, fd);
241 1.1 mrg struct file *fp;
242 1.11 thorpej struct proc *p = l->l_proc;
243 1.1 mrg struct filedesc *fdp = p->p_fd;
244 1.1 mrg
245 1.6 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
246 1.6 thorpej return (EBADF);
247 1.6 thorpej
248 1.6 thorpej if ((fp->f_flag & FWRITE) == 0)
249 1.1 mrg return (EBADF);
250 1.1 mrg
251 1.9 jdolecek FILE_USE(fp);
252 1.9 jdolecek
253 1.23 christos return (dofilewritev32(l, fd, fp,
254 1.36.2.3 ad (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
255 1.10 scw SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
256 1.1 mrg }
257 1.1 mrg
258 1.1 mrg int
259 1.23 christos dofilewritev32(l, fd, fp, iovp, iovcnt, offset, flags, retval)
260 1.23 christos struct lwp *l;
261 1.1 mrg int fd;
262 1.1 mrg struct file *fp;
263 1.1 mrg struct netbsd32_iovec *iovp;
264 1.1 mrg int iovcnt;
265 1.1 mrg off_t *offset;
266 1.1 mrg int flags;
267 1.1 mrg register_t *retval;
268 1.1 mrg {
269 1.1 mrg struct uio auio;
270 1.1 mrg struct iovec *iov;
271 1.1 mrg struct iovec *needfree;
272 1.1 mrg struct iovec aiov[UIO_SMALLIOV];
273 1.23 christos struct proc *p = l->l_proc;
274 1.1 mrg long i, cnt, error = 0;
275 1.1 mrg u_int iovlen;
276 1.1 mrg #ifdef KTRACE
277 1.1 mrg struct iovec *ktriov = NULL;
278 1.1 mrg #endif
279 1.1 mrg
280 1.1 mrg /* note: can't use iovlen until iovcnt is validated */
281 1.1 mrg iovlen = iovcnt * sizeof(struct iovec);
282 1.1 mrg if ((u_int)iovcnt > UIO_SMALLIOV) {
283 1.9 jdolecek if ((u_int)iovcnt > IOV_MAX) {
284 1.9 jdolecek error = EINVAL;
285 1.9 jdolecek goto out;
286 1.9 jdolecek }
287 1.9 jdolecek iov = malloc(iovlen, M_IOV, M_WAITOK);
288 1.1 mrg needfree = iov;
289 1.1 mrg } else if ((u_int)iovcnt > 0) {
290 1.1 mrg iov = aiov;
291 1.1 mrg needfree = NULL;
292 1.9 jdolecek } else {
293 1.9 jdolecek error = EINVAL;
294 1.9 jdolecek goto out;
295 1.9 jdolecek }
296 1.1 mrg
297 1.1 mrg auio.uio_iov = iov;
298 1.1 mrg auio.uio_iovcnt = iovcnt;
299 1.1 mrg auio.uio_rw = UIO_WRITE;
300 1.24 yamt auio.uio_vmspace = l->l_proc->p_vmspace;
301 1.1 mrg error = netbsd32_to_iovecin(iovp, iov, iovcnt);
302 1.1 mrg if (error)
303 1.1 mrg goto done;
304 1.1 mrg auio.uio_resid = 0;
305 1.1 mrg for (i = 0; i < iovcnt; i++) {
306 1.1 mrg auio.uio_resid += iov->iov_len;
307 1.1 mrg /*
308 1.1 mrg * Writes return ssize_t because -1 is returned on error.
309 1.1 mrg * Therefore we must restrict the length to SSIZE_MAX to
310 1.1 mrg * avoid garbage return values.
311 1.1 mrg */
312 1.1 mrg if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
313 1.1 mrg error = EINVAL;
314 1.1 mrg goto done;
315 1.1 mrg }
316 1.1 mrg iov++;
317 1.1 mrg }
318 1.1 mrg #ifdef KTRACE
319 1.1 mrg /*
320 1.1 mrg * if tracing, save a copy of iovec
321 1.1 mrg */
322 1.14 fvdl if (KTRPOINT(p, KTR_GENIO)) {
323 1.9 jdolecek ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
324 1.36 christos memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
325 1.1 mrg }
326 1.1 mrg #endif
327 1.1 mrg cnt = auio.uio_resid;
328 1.1 mrg error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
329 1.1 mrg if (error) {
330 1.1 mrg if (auio.uio_resid != cnt && (error == ERESTART ||
331 1.1 mrg error == EINTR || error == EWOULDBLOCK))
332 1.1 mrg error = 0;
333 1.36.2.5 ad if (error == EPIPE) {
334 1.36.2.5 ad mutex_enter(&proclist_mutex);
335 1.14 fvdl psignal(p, SIGPIPE);
336 1.36.2.5 ad mutex_exit(&proclist_mutex);
337 1.36.2.5 ad }
338 1.1 mrg }
339 1.1 mrg cnt -= auio.uio_resid;
340 1.1 mrg #ifdef KTRACE
341 1.14 fvdl if (KTRPOINT(p, KTR_GENIO))
342 1.1 mrg if (error == 0) {
343 1.23 christos ktrgenio(l, fd, UIO_WRITE, ktriov, cnt,
344 1.1 mrg error);
345 1.9 jdolecek free(ktriov, M_TEMP);
346 1.1 mrg }
347 1.1 mrg #endif
348 1.1 mrg *retval = cnt;
349 1.1 mrg done:
350 1.1 mrg if (needfree)
351 1.9 jdolecek free(needfree, M_IOV);
352 1.9 jdolecek out:
353 1.23 christos FILE_UNUSE(fp, l);
354 1.1 mrg return (error);
355 1.1 mrg }
356 1.1 mrg
357 1.36.2.4 ad /*
358 1.36.2.4 ad * Common routine to set access and modification times given a vnode.
359 1.36.2.4 ad */
360 1.36.2.4 ad static int
361 1.36.2.4 ad get_utimes32(const netbsd32_timevalp_t *tptr, struct timeval *tv,
362 1.36.2.4 ad struct timeval **tvp)
363 1.36.2.4 ad {
364 1.36.2.4 ad int error;
365 1.36.2.4 ad struct netbsd32_timeval tv32[2];
366 1.36.2.4 ad
367 1.36.2.4 ad if (tptr == NULL) {
368 1.36.2.4 ad *tvp = NULL;
369 1.36.2.4 ad return 0;
370 1.36.2.4 ad }
371 1.36.2.4 ad
372 1.36.2.4 ad error = copyin(tptr, tv32, sizeof(tv32));
373 1.36.2.4 ad if (error)
374 1.36.2.4 ad return error;
375 1.36.2.4 ad netbsd32_to_timeval(&tv32[0], &tv[0]);
376 1.36.2.4 ad netbsd32_to_timeval(&tv32[1], &tv[1]);
377 1.36.2.4 ad
378 1.36.2.4 ad *tvp = tv;
379 1.36.2.4 ad return 0;
380 1.36.2.4 ad }
381 1.36.2.4 ad
382 1.1 mrg int
383 1.11 thorpej netbsd32_utimes(l, v, retval)
384 1.11 thorpej struct lwp *l;
385 1.1 mrg void *v;
386 1.1 mrg register_t *retval;
387 1.1 mrg {
388 1.1 mrg struct netbsd32_utimes_args /* {
389 1.1 mrg syscallarg(const netbsd32_charp) path;
390 1.1 mrg syscallarg(const netbsd32_timevalp_t) tptr;
391 1.1 mrg } */ *uap = v;
392 1.1 mrg int error;
393 1.36.2.4 ad struct timeval tv[2], *tvp;
394 1.1 mrg
395 1.36.2.4 ad error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
396 1.36.2.4 ad if (error != 0)
397 1.36.2.4 ad return error;
398 1.1 mrg
399 1.36.2.4 ad return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
400 1.36.2.4 ad tvp, UIO_SYSSPACE);
401 1.1 mrg }
402 1.1 mrg
403 1.1 mrg static int
404 1.36.2.4 ad netbds32_copyout_statvfs(const void *kp, void *up, size_t len)
405 1.1 mrg {
406 1.36.2.4 ad struct netbsd32_statvfs *sbuf_32;
407 1.1 mrg int error;
408 1.1 mrg
409 1.36.2.4 ad sbuf_32 = malloc(sizeof *sbuf_32, M_TEMP, M_WAITOK);
410 1.36.2.4 ad netbsd32_from_statvfs(kp, sbuf_32);
411 1.36.2.4 ad error = copyout(sbuf_32, up, sizeof(*sbuf_32));
412 1.36.2.4 ad free(sbuf_32, M_TEMP);
413 1.36.2.4 ad
414 1.36.2.4 ad return error;
415 1.1 mrg }
416 1.1 mrg
417 1.1 mrg int
418 1.18 cube netbsd32_statvfs1(l, v, retval)
419 1.11 thorpej struct lwp *l;
420 1.1 mrg void *v;
421 1.1 mrg register_t *retval;
422 1.1 mrg {
423 1.18 cube struct netbsd32_statvfs1_args /* {
424 1.1 mrg syscallarg(const netbsd32_charp) path;
425 1.18 cube syscallarg(netbsd32_statvfsp_t) buf;
426 1.18 cube syscallarg(int) flags;
427 1.1 mrg } */ *uap = v;
428 1.36.2.4 ad struct statvfs *sb;
429 1.1 mrg int error;
430 1.1 mrg
431 1.36.2.4 ad sb = STATVFSBUF_GET();
432 1.36.2.4 ad error = do_sys_pstatvfs(l, SCARG_P32(uap, path), SCARG(uap, flags), sb);
433 1.36.2.4 ad if (error == 0)
434 1.36.2.4 ad error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
435 1.36.2.4 ad STATVFSBUF_PUT(sb);
436 1.36.2.4 ad return error;
437 1.1 mrg }
438 1.1 mrg
439 1.1 mrg int
440 1.18 cube netbsd32_fstatvfs1(l, v, retval)
441 1.11 thorpej struct lwp *l;
442 1.1 mrg void *v;
443 1.1 mrg register_t *retval;
444 1.1 mrg {
445 1.18 cube struct netbsd32_fstatvfs1_args /* {
446 1.1 mrg syscallarg(int) fd;
447 1.18 cube syscallarg(netbsd32_statvfsp_t) buf;
448 1.18 cube syscallarg(int) flags;
449 1.1 mrg } */ *uap = v;
450 1.36.2.4 ad struct statvfs *sb;
451 1.1 mrg int error;
452 1.1 mrg
453 1.36.2.4 ad sb = STATVFSBUF_GET();
454 1.36.2.4 ad error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
455 1.36.2.4 ad if (error == 0)
456 1.36.2.4 ad error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
457 1.36.2.4 ad STATVFSBUF_PUT(sb);
458 1.18 cube return error;
459 1.18 cube }
460 1.18 cube
461 1.18 cube int
462 1.18 cube netbsd32_getvfsstat(l, v, retval)
463 1.18 cube struct lwp *l;
464 1.18 cube void *v;
465 1.18 cube register_t *retval;
466 1.18 cube {
467 1.18 cube struct netbsd32_getvfsstat_args /* {
468 1.18 cube syscallarg(netbsd32_statvfsp_t) buf;
469 1.18 cube syscallarg(netbsd32_size_t) bufsize;
470 1.18 cube syscallarg(int) flags;
471 1.18 cube } */ *uap = v;
472 1.18 cube
473 1.36.2.4 ad return do_sys_getvfsstat(l, SCARG_P32(uap, buf), SCARG(uap, bufsize),
474 1.36.2.4 ad SCARG(uap, flags), netbds32_copyout_statvfs,
475 1.36.2.4 ad sizeof (struct netbsd32_statvfs), retval);
476 1.18 cube }
477 1.18 cube
478 1.18 cube int
479 1.32 martin netbsd32___fhstatvfs140(l, v, retval)
480 1.18 cube struct lwp *l;
481 1.18 cube void *v;
482 1.18 cube register_t *retval;
483 1.18 cube {
484 1.32 martin struct netbsd32___fhstatvfs140_args /* {
485 1.32 martin syscallarg(const netbsd32_pointer_t) fhp;
486 1.32 martin syscallarg(netbsd32_size_t) fh_size;
487 1.18 cube syscallarg(netbsd32_statvfsp_t) buf;
488 1.18 cube syscallarg(int) flags;
489 1.18 cube } */ *uap = v;
490 1.36.2.4 ad struct statvfs *sb;
491 1.18 cube int error;
492 1.18 cube
493 1.36.2.4 ad sb = STATVFSBUF_GET();
494 1.36.2.4 ad error = do_fhstatvfs(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), sb,
495 1.36.2.4 ad SCARG(uap, flags));
496 1.36.2.4 ad
497 1.36.2.4 ad if (error == 0)
498 1.36.2.4 ad error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
499 1.36.2.4 ad STATVFSBUF_PUT(sb);
500 1.18 cube
501 1.36.2.4 ad return error;
502 1.1 mrg }
503 1.1 mrg
504 1.1 mrg int
505 1.11 thorpej netbsd32_futimes(l, v, retval)
506 1.11 thorpej struct lwp *l;
507 1.1 mrg void *v;
508 1.1 mrg register_t *retval;
509 1.1 mrg {
510 1.1 mrg struct netbsd32_futimes_args /* {
511 1.1 mrg syscallarg(int) fd;
512 1.1 mrg syscallarg(const netbsd32_timevalp_t) tptr;
513 1.1 mrg } */ *uap = v;
514 1.1 mrg int error;
515 1.1 mrg struct file *fp;
516 1.36.2.4 ad struct timeval tv[2], *tvp;
517 1.36.2.4 ad
518 1.36.2.4 ad error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
519 1.36.2.4 ad if (error != 0)
520 1.36.2.4 ad return error;
521 1.1 mrg
522 1.1 mrg /* getvnode() will use the descriptor for us */
523 1.36.2.4 ad if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
524 1.1 mrg return (error);
525 1.1 mrg
526 1.36.2.4 ad error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
527 1.36.2.4 ad
528 1.23 christos FILE_UNUSE(fp, l);
529 1.1 mrg return (error);
530 1.1 mrg }
531 1.1 mrg
532 1.1 mrg int
533 1.22 christos netbsd32_sys___getdents30(l, v, retval)
534 1.11 thorpej struct lwp *l;
535 1.1 mrg void *v;
536 1.1 mrg register_t *retval;
537 1.1 mrg {
538 1.22 christos struct netbsd32_sys___getdents30_args /* {
539 1.1 mrg syscallarg(int) fd;
540 1.1 mrg syscallarg(netbsd32_charp) buf;
541 1.1 mrg syscallarg(netbsd32_size_t) count;
542 1.1 mrg } */ *uap = v;
543 1.1 mrg struct file *fp;
544 1.1 mrg int error, done;
545 1.11 thorpej struct proc *p = l->l_proc;
546 1.1 mrg
547 1.1 mrg /* getvnode() will use the descriptor for us */
548 1.1 mrg if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
549 1.1 mrg return (error);
550 1.1 mrg if ((fp->f_flag & FREAD) == 0) {
551 1.1 mrg error = EBADF;
552 1.1 mrg goto out;
553 1.1 mrg }
554 1.36.2.3 ad error = vn_readdir(fp, SCARG_P32(uap, buf),
555 1.23 christos UIO_USERSPACE, SCARG(uap, count), &done, l, 0, 0);
556 1.1 mrg *retval = done;
557 1.1 mrg out:
558 1.23 christos FILE_UNUSE(fp, l);
559 1.1 mrg return (error);
560 1.1 mrg }
561 1.1 mrg
562 1.1 mrg int
563 1.11 thorpej netbsd32_lutimes(l, v, retval)
564 1.11 thorpej struct lwp *l;
565 1.1 mrg void *v;
566 1.1 mrg register_t *retval;
567 1.1 mrg {
568 1.1 mrg struct netbsd32_lutimes_args /* {
569 1.1 mrg syscallarg(const netbsd32_charp) path;
570 1.1 mrg syscallarg(const netbsd32_timevalp_t) tptr;
571 1.1 mrg } */ *uap = v;
572 1.1 mrg int error;
573 1.36.2.4 ad struct timeval tv[2], *tvp;
574 1.1 mrg
575 1.36.2.4 ad error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
576 1.36.2.4 ad if (error != 0)
577 1.36.2.4 ad return error;
578 1.1 mrg
579 1.36.2.4 ad return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
580 1.36.2.4 ad tvp, UIO_SYSSPACE);
581 1.1 mrg }
582 1.1 mrg
583 1.1 mrg int
584 1.22 christos netbsd32_sys___stat30(l, v, retval)
585 1.11 thorpej struct lwp *l;
586 1.1 mrg void *v;
587 1.1 mrg register_t *retval;
588 1.1 mrg {
589 1.22 christos struct netbsd32_sys___stat30_args /* {
590 1.1 mrg syscallarg(const netbsd32_charp) path;
591 1.1 mrg syscallarg(netbsd32_statp_t) ub;
592 1.1 mrg } */ *uap = v;
593 1.1 mrg struct netbsd32_stat sb32;
594 1.1 mrg struct stat sb;
595 1.1 mrg int error;
596 1.1 mrg const char *path;
597 1.1 mrg
598 1.36.2.3 ad path = SCARG_P32(uap, path);
599 1.1 mrg
600 1.36.2.1 ad error = do_sys_stat(l, path, FOLLOW, &sb);
601 1.36.2.4 ad if (error)
602 1.36.2.4 ad return (error);
603 1.22 christos netbsd32_from___stat30(&sb, &sb32);
604 1.36.2.3 ad error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
605 1.1 mrg return (error);
606 1.1 mrg }
607 1.1 mrg
608 1.1 mrg int
609 1.22 christos netbsd32_sys___fstat30(l, v, retval)
610 1.11 thorpej struct lwp *l;
611 1.1 mrg void *v;
612 1.1 mrg register_t *retval;
613 1.1 mrg {
614 1.22 christos struct netbsd32_sys___fstat30_args /* {
615 1.1 mrg syscallarg(int) fd;
616 1.1 mrg syscallarg(netbsd32_statp_t) sb;
617 1.1 mrg } */ *uap = v;
618 1.1 mrg int fd = SCARG(uap, fd);
619 1.14 fvdl struct proc *p = l->l_proc;
620 1.14 fvdl struct filedesc *fdp = p->p_fd;
621 1.1 mrg struct file *fp;
622 1.1 mrg struct netbsd32_stat sb32;
623 1.1 mrg struct stat ub;
624 1.1 mrg int error = 0;
625 1.1 mrg
626 1.6 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
627 1.1 mrg return (EBADF);
628 1.1 mrg
629 1.3 jdolecek FILE_USE(fp);
630 1.23 christos error = (*fp->f_ops->fo_stat)(fp, &ub, l);
631 1.23 christos FILE_UNUSE(fp, l);
632 1.3 jdolecek
633 1.1 mrg if (error == 0) {
634 1.22 christos netbsd32_from___stat30(&ub, &sb32);
635 1.36.2.3 ad error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
636 1.1 mrg }
637 1.1 mrg return (error);
638 1.1 mrg }
639 1.1 mrg
640 1.1 mrg int
641 1.22 christos netbsd32_sys___lstat30(l, v, retval)
642 1.11 thorpej struct lwp *l;
643 1.1 mrg void *v;
644 1.1 mrg register_t *retval;
645 1.1 mrg {
646 1.22 christos struct netbsd32_sys___lstat30_args /* {
647 1.1 mrg syscallarg(const netbsd32_charp) path;
648 1.1 mrg syscallarg(netbsd32_statp_t) ub;
649 1.1 mrg } */ *uap = v;
650 1.1 mrg struct netbsd32_stat sb32;
651 1.1 mrg struct stat sb;
652 1.1 mrg int error;
653 1.1 mrg const char *path;
654 1.1 mrg
655 1.36.2.3 ad path = SCARG_P32(uap, path);
656 1.1 mrg
657 1.36.2.1 ad error = do_sys_stat(l, path, NOFOLLOW, &sb);
658 1.1 mrg if (error)
659 1.1 mrg return (error);
660 1.22 christos netbsd32_from___stat30(&sb, &sb32);
661 1.36.2.3 ad error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
662 1.1 mrg return (error);
663 1.1 mrg }
664 1.1 mrg
665 1.32 martin int netbsd32___fhstat40(l, v, retval)
666 1.26 cube struct lwp *l;
667 1.26 cube void *v;
668 1.26 cube register_t *retval;
669 1.26 cube {
670 1.32 martin struct netbsd32___fhstat40_args /* {
671 1.32 martin syscallarg(const netbsd32_pointer_t) fhp;
672 1.32 martin syscallarg(netbsd32_size_t) fh_size;
673 1.26 cube syscallarg(netbsd32_statp_t) sb;
674 1.26 cube } */ *uap = v;
675 1.26 cube struct stat sb;
676 1.26 cube struct netbsd32_stat sb32;
677 1.26 cube int error;
678 1.29 martin
679 1.36.2.4 ad error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
680 1.36.2.4 ad if (error != 0) {
681 1.36.2.4 ad netbsd32_from___stat30(&sb, &sb32);
682 1.36.2.4 ad error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
683 1.36.2.4 ad }
684 1.29 martin return error;
685 1.26 cube }
686 1.26 cube
687 1.1 mrg int
688 1.11 thorpej netbsd32_preadv(l, v, retval)
689 1.11 thorpej struct lwp *l;
690 1.1 mrg void *v;
691 1.1 mrg register_t *retval;
692 1.1 mrg {
693 1.1 mrg struct netbsd32_preadv_args /* {
694 1.1 mrg syscallarg(int) fd;
695 1.1 mrg syscallarg(const netbsd32_iovecp_t) iovp;
696 1.1 mrg syscallarg(int) iovcnt;
697 1.1 mrg syscallarg(int) pad;
698 1.1 mrg syscallarg(off_t) offset;
699 1.1 mrg } */ *uap = v;
700 1.14 fvdl struct proc *p = l->l_proc;
701 1.14 fvdl struct filedesc *fdp = p->p_fd;
702 1.1 mrg struct file *fp;
703 1.1 mrg struct vnode *vp;
704 1.1 mrg off_t offset;
705 1.1 mrg int error, fd = SCARG(uap, fd);
706 1.1 mrg
707 1.6 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
708 1.6 thorpej return (EBADF);
709 1.6 thorpej
710 1.6 thorpej if ((fp->f_flag & FREAD) == 0)
711 1.1 mrg return (EBADF);
712 1.1 mrg
713 1.9 jdolecek FILE_USE(fp);
714 1.9 jdolecek
715 1.1 mrg vp = (struct vnode *)fp->f_data;
716 1.9 jdolecek if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
717 1.9 jdolecek error = ESPIPE;
718 1.9 jdolecek goto out;
719 1.9 jdolecek }
720 1.1 mrg
721 1.1 mrg offset = SCARG(uap, offset);
722 1.1 mrg
723 1.1 mrg /*
724 1.1 mrg * XXX This works because no file systems actually
725 1.1 mrg * XXX take any action on the seek operation.
726 1.1 mrg */
727 1.1 mrg if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
728 1.9 jdolecek goto out;
729 1.1 mrg
730 1.36.2.3 ad return (dofilereadv32(l, fd, fp, SCARG_P32(uap, iovp),
731 1.10 scw SCARG(uap, iovcnt), &offset, 0, retval));
732 1.9 jdolecek
733 1.9 jdolecek out:
734 1.23 christos FILE_UNUSE(fp, l);
735 1.9 jdolecek return (error);
736 1.1 mrg }
737 1.1 mrg
738 1.1 mrg int
739 1.11 thorpej netbsd32_pwritev(l, v, retval)
740 1.11 thorpej struct lwp *l;
741 1.1 mrg void *v;
742 1.1 mrg register_t *retval;
743 1.1 mrg {
744 1.1 mrg struct netbsd32_pwritev_args /* {
745 1.1 mrg syscallarg(int) fd;
746 1.1 mrg syscallarg(const netbsd32_iovecp_t) iovp;
747 1.1 mrg syscallarg(int) iovcnt;
748 1.1 mrg syscallarg(int) pad;
749 1.1 mrg syscallarg(off_t) offset;
750 1.1 mrg } */ *uap = v;
751 1.14 fvdl struct proc *p = l->l_proc;
752 1.14 fvdl struct filedesc *fdp = p->p_fd;
753 1.1 mrg struct file *fp;
754 1.1 mrg struct vnode *vp;
755 1.1 mrg off_t offset;
756 1.1 mrg int error, fd = SCARG(uap, fd);
757 1.1 mrg
758 1.6 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
759 1.6 thorpej return (EBADF);
760 1.6 thorpej
761 1.6 thorpej if ((fp->f_flag & FWRITE) == 0)
762 1.1 mrg return (EBADF);
763 1.1 mrg
764 1.9 jdolecek FILE_USE(fp);
765 1.9 jdolecek
766 1.1 mrg vp = (struct vnode *)fp->f_data;
767 1.9 jdolecek if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
768 1.9 jdolecek error = ESPIPE;
769 1.9 jdolecek goto out;
770 1.9 jdolecek }
771 1.1 mrg
772 1.1 mrg offset = SCARG(uap, offset);
773 1.1 mrg
774 1.1 mrg /*
775 1.1 mrg * XXX This works because no file systems actually
776 1.1 mrg * XXX take any action on the seek operation.
777 1.1 mrg */
778 1.1 mrg if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
779 1.9 jdolecek goto out;
780 1.1 mrg
781 1.36.2.3 ad return (dofilewritev32(l, fd, fp, SCARG_P32(uap, iovp),
782 1.10 scw SCARG(uap, iovcnt), &offset, 0, retval));
783 1.9 jdolecek
784 1.9 jdolecek out:
785 1.23 christos FILE_UNUSE(fp, l);
786 1.9 jdolecek return (error);
787 1.1 mrg }
788 1.1 mrg
789 1.1 mrg /*
790 1.1 mrg * Find pathname of process's current directory.
791 1.1 mrg *
792 1.1 mrg * Use vfs vnode-to-name reverse cache; if that fails, fall back
793 1.1 mrg * to reading directory contents.
794 1.1 mrg */
795 1.23 christos /* XXX NH Why does this exist */
796 1.14 fvdl int
797 1.14 fvdl getcwd_common __P((struct vnode *, struct vnode *,
798 1.23 christos char **, char *, int, int, struct lwp *));
799 1.14 fvdl
800 1.19 perry int netbsd32___getcwd(l, v, retval)
801 1.11 thorpej struct lwp *l;
802 1.1 mrg void *v;
803 1.1 mrg register_t *retval;
804 1.1 mrg {
805 1.1 mrg struct netbsd32___getcwd_args /* {
806 1.1 mrg syscallarg(char *) bufp;
807 1.1 mrg syscallarg(size_t) length;
808 1.1 mrg } */ *uap = v;
809 1.11 thorpej struct proc *p = l->l_proc;
810 1.1 mrg int error;
811 1.1 mrg char *path;
812 1.1 mrg char *bp, *bend;
813 1.1 mrg int len = (int)SCARG(uap, length);
814 1.1 mrg int lenused;
815 1.1 mrg
816 1.1 mrg if (len > MAXPATHLEN*4)
817 1.1 mrg len = MAXPATHLEN*4;
818 1.1 mrg else if (len < 2)
819 1.1 mrg return ERANGE;
820 1.1 mrg
821 1.1 mrg path = (char *)malloc(len, M_TEMP, M_WAITOK);
822 1.1 mrg if (!path)
823 1.1 mrg return ENOMEM;
824 1.1 mrg
825 1.1 mrg bp = &path[len];
826 1.1 mrg bend = bp;
827 1.1 mrg *(--bp) = '\0';
828 1.1 mrg
829 1.1 mrg /*
830 1.1 mrg * 5th argument here is "max number of vnodes to traverse".
831 1.1 mrg * Since each entry takes up at least 2 bytes in the output buffer,
832 1.1 mrg * limit it to N/2 vnodes for an N byte buffer.
833 1.1 mrg */
834 1.1 mrg #define GETCWD_CHECK_ACCESS 0x0001
835 1.1 mrg error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
836 1.23 christos GETCWD_CHECK_ACCESS, l);
837 1.1 mrg
838 1.1 mrg if (error)
839 1.1 mrg goto out;
840 1.1 mrg lenused = bend - bp;
841 1.1 mrg *retval = lenused;
842 1.1 mrg /* put the result into user buffer */
843 1.36.2.3 ad error = copyout(bp, SCARG_P32(uap, bufp), lenused);
844 1.1 mrg
845 1.1 mrg out:
846 1.1 mrg free(path, M_TEMP);
847 1.1 mrg return error;
848 1.1 mrg }
849