netbsd32_fs.c revision 1.43 1 /* $NetBSD: netbsd32_fs.c,v 1.43 2007/05/12 17:28:19 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.43 2007/05/12 17:28:19 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
70 int
71 netbsd32_readv(l, v, retval)
72 struct lwp *l;
73 void *v;
74 register_t *retval;
75 {
76 struct netbsd32_readv_args /* {
77 syscallarg(int) fd;
78 syscallarg(const netbsd32_iovecp_t) iovp;
79 syscallarg(int) iovcnt;
80 } */ *uap = v;
81 int fd = SCARG(uap, fd);
82 struct proc *p = l->l_proc;
83 struct file *fp;
84 struct filedesc *fdp = p->p_fd;
85
86 if ((fp = fd_getfile(fdp, fd)) == NULL)
87 return (EBADF);
88
89 if ((fp->f_flag & FREAD) == 0)
90 return (EBADF);
91
92 FILE_USE(fp);
93
94 return (dofilereadv32(l, fd, fp,
95 (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
96 SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
97 }
98
99 /* Damn thing copies in the iovec! */
100 int
101 dofilereadv32(l, fd, fp, iovp, iovcnt, offset, flags, retval)
102 struct lwp *l;
103 int fd;
104 struct file *fp;
105 struct netbsd32_iovec *iovp;
106 int iovcnt;
107 off_t *offset;
108 int flags;
109 register_t *retval;
110 {
111 struct uio auio;
112 struct iovec *iov;
113 struct iovec *needfree;
114 struct iovec aiov[UIO_SMALLIOV];
115 long i, cnt, error = 0;
116 u_int iovlen;
117 #ifdef KTRACE
118 struct iovec *ktriov = NULL;
119 #endif
120
121 /* note: can't use iovlen until iovcnt is validated */
122 iovlen = iovcnt * sizeof(struct iovec);
123 if ((u_int)iovcnt > UIO_SMALLIOV) {
124 if ((u_int)iovcnt > IOV_MAX) {
125 error = EINVAL;
126 goto out;
127 }
128 iov = malloc(iovlen, M_IOV, M_WAITOK);
129 needfree = iov;
130 } else if ((u_int)iovcnt > 0) {
131 iov = aiov;
132 needfree = NULL;
133 } else {
134 error = EINVAL;
135 goto out;
136 }
137
138 auio.uio_iov = iov;
139 auio.uio_iovcnt = iovcnt;
140 auio.uio_rw = UIO_READ;
141 auio.uio_vmspace = l->l_proc->p_vmspace;
142 error = netbsd32_to_iovecin(iovp, iov, iovcnt);
143 if (error)
144 goto done;
145 auio.uio_resid = 0;
146 for (i = 0; i < iovcnt; i++) {
147 auio.uio_resid += iov->iov_len;
148 /*
149 * Reads return ssize_t because -1 is returned on error.
150 * Therefore we must restrict the length to SSIZE_MAX to
151 * avoid garbage return values.
152 */
153 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
154 error = EINVAL;
155 goto done;
156 }
157 iov++;
158 }
159 #ifdef KTRACE
160 /*
161 * if tracing, save a copy of iovec
162 */
163 if (KTRPOINT(l->l_proc, KTR_GENIO)) {
164 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
165 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
166 }
167 #endif
168 cnt = auio.uio_resid;
169 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
170 if (error)
171 if (auio.uio_resid != cnt && (error == ERESTART ||
172 error == EINTR || error == EWOULDBLOCK))
173 error = 0;
174 cnt -= auio.uio_resid;
175 #ifdef KTRACE
176 if (KTRPOINT(l->l_proc, KTR_GENIO))
177 if (error == 0) {
178 ktrgenio(l, fd, UIO_READ, ktriov, cnt,
179 error);
180 free(ktriov, M_TEMP);
181 }
182 #endif
183 *retval = cnt;
184 done:
185 if (needfree)
186 free(needfree, M_IOV);
187 out:
188 FILE_UNUSE(fp, l);
189 return (error);
190 }
191
192 int
193 netbsd32_writev(l, v, retval)
194 struct lwp *l;
195 void *v;
196 register_t *retval;
197 {
198 struct netbsd32_writev_args /* {
199 syscallarg(int) fd;
200 syscallarg(const netbsd32_iovecp_t) iovp;
201 syscallarg(int) iovcnt;
202 } */ *uap = v;
203 int fd = SCARG(uap, fd);
204 struct file *fp;
205 struct proc *p = l->l_proc;
206 struct filedesc *fdp = p->p_fd;
207
208 if ((fp = fd_getfile(fdp, fd)) == NULL)
209 return (EBADF);
210
211 if ((fp->f_flag & FWRITE) == 0)
212 return (EBADF);
213
214 FILE_USE(fp);
215
216 return (dofilewritev32(l, fd, fp,
217 (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
218 SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
219 }
220
221 int
222 dofilewritev32(l, fd, fp, iovp, iovcnt, offset, flags, retval)
223 struct lwp *l;
224 int fd;
225 struct file *fp;
226 struct netbsd32_iovec *iovp;
227 int iovcnt;
228 off_t *offset;
229 int flags;
230 register_t *retval;
231 {
232 struct uio auio;
233 struct iovec *iov;
234 struct iovec *needfree;
235 struct iovec aiov[UIO_SMALLIOV];
236 struct proc *p = l->l_proc;
237 long i, cnt, error = 0;
238 u_int iovlen;
239 #ifdef KTRACE
240 struct iovec *ktriov = NULL;
241 #endif
242
243 /* note: can't use iovlen until iovcnt is validated */
244 iovlen = iovcnt * sizeof(struct iovec);
245 if ((u_int)iovcnt > UIO_SMALLIOV) {
246 if ((u_int)iovcnt > IOV_MAX) {
247 error = EINVAL;
248 goto out;
249 }
250 iov = malloc(iovlen, M_IOV, M_WAITOK);
251 needfree = iov;
252 } else if ((u_int)iovcnt > 0) {
253 iov = aiov;
254 needfree = NULL;
255 } else {
256 error = EINVAL;
257 goto out;
258 }
259
260 auio.uio_iov = iov;
261 auio.uio_iovcnt = iovcnt;
262 auio.uio_rw = UIO_WRITE;
263 auio.uio_vmspace = l->l_proc->p_vmspace;
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((void *)ktriov, (void *)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 /*
318 * Common routine to set access and modification times given a vnode.
319 */
320 static int
321 get_utimes32(const netbsd32_timevalp_t *tptr, struct timeval *tv,
322 struct timeval **tvp)
323 {
324 int error;
325 struct netbsd32_timeval tv32[2];
326
327 if (tptr == NULL) {
328 *tvp = NULL;
329 return 0;
330 }
331
332 error = copyin(tptr, tv32, sizeof(tv32));
333 if (error)
334 return error;
335 netbsd32_to_timeval(&tv32[0], &tv[0]);
336 netbsd32_to_timeval(&tv32[1], &tv[1]);
337
338 *tvp = tv;
339 return 0;
340 }
341
342 int
343 netbsd32_utimes(l, v, retval)
344 struct lwp *l;
345 void *v;
346 register_t *retval;
347 {
348 struct netbsd32_utimes_args /* {
349 syscallarg(const netbsd32_charp) path;
350 syscallarg(const netbsd32_timevalp_t) tptr;
351 } */ *uap = v;
352 int error;
353 struct timeval tv[2], *tvp;
354
355 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
356 if (error != 0)
357 return error;
358
359 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
360 tvp, UIO_SYSSPACE);
361 }
362
363 static int
364 netbds32_copyout_statvfs(const void *kp, void *up, size_t len)
365 {
366 struct netbsd32_statvfs *sbuf_32;
367 int error;
368
369 sbuf_32 = malloc(sizeof *sbuf_32, M_TEMP, M_WAITOK);
370 netbsd32_from_statvfs(kp, sbuf_32);
371 error = copyout(sbuf_32, up, sizeof(*sbuf_32));
372 free(sbuf_32, M_TEMP);
373
374 return error;
375 }
376
377 int
378 netbsd32_statvfs1(l, v, retval)
379 struct lwp *l;
380 void *v;
381 register_t *retval;
382 {
383 struct netbsd32_statvfs1_args /* {
384 syscallarg(const netbsd32_charp) path;
385 syscallarg(netbsd32_statvfsp_t) buf;
386 syscallarg(int) flags;
387 } */ *uap = v;
388 struct statvfs *sb;
389 int error;
390
391 sb = STATVFSBUF_GET();
392 error = do_sys_pstatvfs(l, SCARG_P32(uap, path), SCARG(uap, flags), sb);
393 if (error == 0)
394 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
395 STATVFSBUF_PUT(sb);
396 return error;
397 }
398
399 int
400 netbsd32_fstatvfs1(l, v, retval)
401 struct lwp *l;
402 void *v;
403 register_t *retval;
404 {
405 struct netbsd32_fstatvfs1_args /* {
406 syscallarg(int) fd;
407 syscallarg(netbsd32_statvfsp_t) buf;
408 syscallarg(int) flags;
409 } */ *uap = v;
410 struct statvfs *sb;
411 int error;
412
413 sb = STATVFSBUF_GET();
414 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
415 if (error == 0)
416 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
417 STATVFSBUF_PUT(sb);
418 return error;
419 }
420
421 int
422 netbsd32_getvfsstat(l, v, retval)
423 struct lwp *l;
424 void *v;
425 register_t *retval;
426 {
427 struct netbsd32_getvfsstat_args /* {
428 syscallarg(netbsd32_statvfsp_t) buf;
429 syscallarg(netbsd32_size_t) bufsize;
430 syscallarg(int) flags;
431 } */ *uap = v;
432
433 return do_sys_getvfsstat(l, SCARG_P32(uap, buf), SCARG(uap, bufsize),
434 SCARG(uap, flags), netbds32_copyout_statvfs,
435 sizeof (struct netbsd32_statvfs), retval);
436 }
437
438 int
439 netbsd32___fhstatvfs140(l, v, retval)
440 struct lwp *l;
441 void *v;
442 register_t *retval;
443 {
444 struct netbsd32___fhstatvfs140_args /* {
445 syscallarg(const netbsd32_pointer_t) fhp;
446 syscallarg(netbsd32_size_t) fh_size;
447 syscallarg(netbsd32_statvfsp_t) buf;
448 syscallarg(int) flags;
449 } */ *uap = v;
450 struct statvfs *sb;
451 int error;
452
453 sb = STATVFSBUF_GET();
454 error = do_fhstatvfs(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), sb,
455 SCARG(uap, flags));
456
457 if (error == 0)
458 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
459 STATVFSBUF_PUT(sb);
460
461 return error;
462 }
463
464 int
465 netbsd32_futimes(l, v, retval)
466 struct lwp *l;
467 void *v;
468 register_t *retval;
469 {
470 struct netbsd32_futimes_args /* {
471 syscallarg(int) fd;
472 syscallarg(const netbsd32_timevalp_t) tptr;
473 } */ *uap = v;
474 int error;
475 struct file *fp;
476 struct timeval tv[2], *tvp;
477
478 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
479 if (error != 0)
480 return error;
481
482 /* getvnode() will use the descriptor for us */
483 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
484 return (error);
485
486 error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
487
488 FILE_UNUSE(fp, l);
489 return (error);
490 }
491
492 int
493 netbsd32_sys___getdents30(l, v, retval)
494 struct lwp *l;
495 void *v;
496 register_t *retval;
497 {
498 struct netbsd32_sys___getdents30_args /* {
499 syscallarg(int) fd;
500 syscallarg(netbsd32_charp) buf;
501 syscallarg(netbsd32_size_t) count;
502 } */ *uap = v;
503 struct file *fp;
504 int error, done;
505 struct proc *p = l->l_proc;
506
507 /* getvnode() will use the descriptor for us */
508 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
509 return (error);
510 if ((fp->f_flag & FREAD) == 0) {
511 error = EBADF;
512 goto out;
513 }
514 error = vn_readdir(fp, SCARG_P32(uap, buf),
515 UIO_USERSPACE, SCARG(uap, count), &done, l, 0, 0);
516 *retval = done;
517 out:
518 FILE_UNUSE(fp, l);
519 return (error);
520 }
521
522 int
523 netbsd32_lutimes(l, v, retval)
524 struct lwp *l;
525 void *v;
526 register_t *retval;
527 {
528 struct netbsd32_lutimes_args /* {
529 syscallarg(const netbsd32_charp) path;
530 syscallarg(const netbsd32_timevalp_t) tptr;
531 } */ *uap = v;
532 int error;
533 struct timeval tv[2], *tvp;
534
535 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
536 if (error != 0)
537 return error;
538
539 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
540 tvp, UIO_SYSSPACE);
541 }
542
543 int
544 netbsd32_sys___stat30(l, v, retval)
545 struct lwp *l;
546 void *v;
547 register_t *retval;
548 {
549 struct netbsd32_sys___stat30_args /* {
550 syscallarg(const netbsd32_charp) path;
551 syscallarg(netbsd32_statp_t) ub;
552 } */ *uap = v;
553 struct netbsd32_stat sb32;
554 struct stat sb;
555 int error;
556 const char *path;
557
558 path = SCARG_P32(uap, path);
559
560 error = do_sys_stat(l, path, FOLLOW, &sb);
561 if (error)
562 return (error);
563 netbsd32_from___stat30(&sb, &sb32);
564 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
565 return (error);
566 }
567
568 int
569 netbsd32_sys___fstat30(l, v, retval)
570 struct lwp *l;
571 void *v;
572 register_t *retval;
573 {
574 struct netbsd32_sys___fstat30_args /* {
575 syscallarg(int) fd;
576 syscallarg(netbsd32_statp_t) sb;
577 } */ *uap = v;
578 int fd = SCARG(uap, fd);
579 struct proc *p = l->l_proc;
580 struct filedesc *fdp = p->p_fd;
581 struct file *fp;
582 struct netbsd32_stat sb32;
583 struct stat ub;
584 int error = 0;
585
586 if ((fp = fd_getfile(fdp, fd)) == NULL)
587 return (EBADF);
588
589 FILE_USE(fp);
590 error = (*fp->f_ops->fo_stat)(fp, &ub, l);
591 FILE_UNUSE(fp, l);
592
593 if (error == 0) {
594 netbsd32_from___stat30(&ub, &sb32);
595 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
596 }
597 return (error);
598 }
599
600 int
601 netbsd32_sys___lstat30(l, v, retval)
602 struct lwp *l;
603 void *v;
604 register_t *retval;
605 {
606 struct netbsd32_sys___lstat30_args /* {
607 syscallarg(const netbsd32_charp) path;
608 syscallarg(netbsd32_statp_t) ub;
609 } */ *uap = v;
610 struct netbsd32_stat sb32;
611 struct stat sb;
612 int error;
613 const char *path;
614
615 path = SCARG_P32(uap, path);
616
617 error = do_sys_stat(l, path, NOFOLLOW, &sb);
618 if (error)
619 return (error);
620 netbsd32_from___stat30(&sb, &sb32);
621 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
622 return (error);
623 }
624
625 int netbsd32___fhstat40(l, v, retval)
626 struct lwp *l;
627 void *v;
628 register_t *retval;
629 {
630 struct netbsd32___fhstat40_args /* {
631 syscallarg(const netbsd32_pointer_t) fhp;
632 syscallarg(netbsd32_size_t) fh_size;
633 syscallarg(netbsd32_statp_t) sb;
634 } */ *uap = v;
635 struct stat sb;
636 struct netbsd32_stat sb32;
637 int error;
638
639 error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
640 if (error != 0) {
641 netbsd32_from___stat30(&sb, &sb32);
642 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
643 }
644 return error;
645 }
646
647 int
648 netbsd32_preadv(l, v, retval)
649 struct lwp *l;
650 void *v;
651 register_t *retval;
652 {
653 struct netbsd32_preadv_args /* {
654 syscallarg(int) fd;
655 syscallarg(const netbsd32_iovecp_t) iovp;
656 syscallarg(int) iovcnt;
657 syscallarg(int) pad;
658 syscallarg(off_t) offset;
659 } */ *uap = v;
660 struct proc *p = l->l_proc;
661 struct filedesc *fdp = p->p_fd;
662 struct file *fp;
663 struct vnode *vp;
664 off_t offset;
665 int error, fd = SCARG(uap, fd);
666
667 if ((fp = fd_getfile(fdp, fd)) == NULL)
668 return (EBADF);
669
670 if ((fp->f_flag & FREAD) == 0)
671 return (EBADF);
672
673 FILE_USE(fp);
674
675 vp = (struct vnode *)fp->f_data;
676 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
677 error = ESPIPE;
678 goto out;
679 }
680
681 offset = SCARG(uap, offset);
682
683 /*
684 * XXX This works because no file systems actually
685 * XXX take any action on the seek operation.
686 */
687 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
688 goto out;
689
690 return (dofilereadv32(l, fd, fp, SCARG_P32(uap, iovp),
691 SCARG(uap, iovcnt), &offset, 0, retval));
692
693 out:
694 FILE_UNUSE(fp, l);
695 return (error);
696 }
697
698 int
699 netbsd32_pwritev(l, v, retval)
700 struct lwp *l;
701 void *v;
702 register_t *retval;
703 {
704 struct netbsd32_pwritev_args /* {
705 syscallarg(int) fd;
706 syscallarg(const netbsd32_iovecp_t) iovp;
707 syscallarg(int) iovcnt;
708 syscallarg(int) pad;
709 syscallarg(off_t) offset;
710 } */ *uap = v;
711 struct proc *p = l->l_proc;
712 struct filedesc *fdp = p->p_fd;
713 struct file *fp;
714 struct vnode *vp;
715 off_t offset;
716 int error, fd = SCARG(uap, fd);
717
718 if ((fp = fd_getfile(fdp, fd)) == NULL)
719 return (EBADF);
720
721 if ((fp->f_flag & FWRITE) == 0)
722 return (EBADF);
723
724 FILE_USE(fp);
725
726 vp = (struct vnode *)fp->f_data;
727 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
728 error = ESPIPE;
729 goto out;
730 }
731
732 offset = SCARG(uap, offset);
733
734 /*
735 * XXX This works because no file systems actually
736 * XXX take any action on the seek operation.
737 */
738 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
739 goto out;
740
741 return (dofilewritev32(l, fd, fp, SCARG_P32(uap, iovp),
742 SCARG(uap, iovcnt), &offset, 0, retval));
743
744 out:
745 FILE_UNUSE(fp, l);
746 return (error);
747 }
748
749 /*
750 * Find pathname of process's current directory.
751 *
752 * Use vfs vnode-to-name reverse cache; if that fails, fall back
753 * to reading directory contents.
754 */
755 /* XXX NH Why does this exist */
756 int
757 getcwd_common __P((struct vnode *, struct vnode *,
758 char **, char *, int, int, struct lwp *));
759
760 int netbsd32___getcwd(l, v, retval)
761 struct lwp *l;
762 void *v;
763 register_t *retval;
764 {
765 struct netbsd32___getcwd_args /* {
766 syscallarg(char *) bufp;
767 syscallarg(size_t) length;
768 } */ *uap = v;
769 struct proc *p = l->l_proc;
770 int error;
771 char *path;
772 char *bp, *bend;
773 int len = (int)SCARG(uap, length);
774 int lenused;
775
776 if (len > MAXPATHLEN*4)
777 len = MAXPATHLEN*4;
778 else if (len < 2)
779 return ERANGE;
780
781 path = (char *)malloc(len, M_TEMP, M_WAITOK);
782 if (!path)
783 return ENOMEM;
784
785 bp = &path[len];
786 bend = bp;
787 *(--bp) = '\0';
788
789 /*
790 * 5th argument here is "max number of vnodes to traverse".
791 * Since each entry takes up at least 2 bytes in the output buffer,
792 * limit it to N/2 vnodes for an N byte buffer.
793 */
794 #define GETCWD_CHECK_ACCESS 0x0001
795 error = getcwd_common (p->p_cwdi->cwdi_cdir, NULL, &bp, path, len/2,
796 GETCWD_CHECK_ACCESS, l);
797
798 if (error)
799 goto out;
800 lenused = bend - bp;
801 *retval = lenused;
802 /* put the result into user buffer */
803 error = copyout(bp, SCARG_P32(uap, bufp), lenused);
804
805 out:
806 free(path, M_TEMP);
807 return error;
808 }
809