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