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