netbsd32_fs.c revision 1.53.4.1 1 /* $NetBSD: netbsd32_fs.c,v 1.53.4.1 2008/05/10 23:48:57 wrstuden 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.53.4.1 2008/05/10 23:48:57 wrstuden Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/malloc.h>
37 #include <sys/mount.h>
38 #include <sys/socket.h>
39 #include <sys/socketvar.h>
40 #include <sys/stat.h>
41 #include <sys/time.h>
42 #include <sys/ktrace.h>
43 #include <sys/resourcevar.h>
44 #include <sys/vnode.h>
45 #include <sys/file.h>
46 #include <sys/filedesc.h>
47 #include <sys/namei.h>
48 #include <sys/sa.h>
49 #include <sys/statvfs.h>
50 #include <sys/syscallargs.h>
51 #include <sys/proc.h>
52 #include <sys/dirent.h>
53 #include <sys/kauth.h>
54 #include <sys/vfs_syscalls.h>
55
56 #include <compat/netbsd32/netbsd32.h>
57 #include <compat/netbsd32/netbsd32_syscallargs.h>
58 #include <compat/netbsd32/netbsd32_conv.h>
59 #include <compat/sys/mount.h>
60
61
62 static int dofilereadv32(int, struct file *, struct netbsd32_iovec *,
63 int, off_t *, int, register_t *);
64 static int dofilewritev32(int, struct file *, struct netbsd32_iovec *,
65 int, off_t *, int, register_t *);
66
67 struct iovec *
68 netbsd32_get_iov(struct netbsd32_iovec *iov32, int iovlen, struct iovec *aiov,
69 int aiov_len)
70 {
71 #define N_IOV32 8
72 struct netbsd32_iovec aiov32[N_IOV32];
73 struct iovec *iov = aiov;
74 struct iovec *iovp;
75 int i, n, j;
76 int error;
77
78 if (iovlen < 0 || iovlen > IOV_MAX)
79 return NULL;
80
81 if (iovlen > aiov_len)
82 iov = malloc(iovlen * sizeof (*iov), M_TEMP, M_WAITOK);
83
84 iovp = iov;
85 for (i = 0; i < iovlen; iov32 += N_IOV32, i += N_IOV32) {
86 n = iovlen - i;
87 if (n > N_IOV32)
88 n = N_IOV32;
89 error = copyin(iov32, aiov32, n * sizeof (*iov32));
90 if (error != 0) {
91 if (iov != aiov)
92 free(iov, M_TEMP);
93 return NULL;
94 }
95 for (j = 0; j < n; iovp++, j++) {
96 iovp->iov_base = NETBSD32PTR64(aiov32[j].iov_base);
97 iovp->iov_len = aiov32[j].iov_len;
98 }
99 }
100 return iov;
101 #undef N_IOV32
102 }
103
104 int
105 netbsd32_readv(struct lwp *l, const struct netbsd32_readv_args *uap, register_t *retval)
106 {
107 /* {
108 syscallarg(int) fd;
109 syscallarg(const netbsd32_iovecp_t) iovp;
110 syscallarg(int) iovcnt;
111 } */
112 int fd = SCARG(uap, fd);
113 file_t *fp;
114
115 if ((fp = fd_getfile(fd)) == NULL)
116 return (EBADF);
117
118 if ((fp->f_flag & FREAD) == 0) {
119 fd_putfile(fd);
120 return (EBADF);
121 }
122
123 return (dofilereadv32(fd, fp,
124 (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
125 SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
126 }
127
128 /* Damn thing copies in the iovec! */
129 int
130 dofilereadv32(int fd, struct file *fp, struct netbsd32_iovec *iovp, int iovcnt, off_t *offset, int flags, register_t *retval)
131 {
132 struct uio auio;
133 struct iovec *iov;
134 struct iovec *needfree;
135 struct iovec aiov[UIO_SMALLIOV];
136 long i, cnt, error = 0;
137 u_int iovlen;
138 struct iovec *ktriov = NULL;
139
140 /* note: can't use iovlen until iovcnt is validated */
141 iovlen = iovcnt * sizeof(struct iovec);
142 if ((u_int)iovcnt > UIO_SMALLIOV) {
143 if ((u_int)iovcnt > IOV_MAX) {
144 error = EINVAL;
145 goto out;
146 }
147 iov = malloc(iovlen, M_IOV, M_WAITOK);
148 needfree = iov;
149 } else if ((u_int)iovcnt > 0) {
150 iov = aiov;
151 needfree = NULL;
152 } else {
153 error = EINVAL;
154 goto out;
155 }
156
157 auio.uio_iov = iov;
158 auio.uio_iovcnt = iovcnt;
159 auio.uio_rw = UIO_READ;
160 auio.uio_vmspace = curproc->p_vmspace;
161 error = netbsd32_to_iovecin(iovp, iov, iovcnt);
162 if (error)
163 goto done;
164 auio.uio_resid = 0;
165 for (i = 0; i < iovcnt; i++) {
166 auio.uio_resid += iov->iov_len;
167 /*
168 * Reads return ssize_t because -1 is returned on error.
169 * Therefore we must restrict the length to SSIZE_MAX to
170 * avoid garbage return values.
171 */
172 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
173 error = EINVAL;
174 goto done;
175 }
176 iov++;
177 }
178
179 /*
180 * if tracing, save a copy of iovec
181 */
182 if (ktrpoint(KTR_GENIO)) {
183 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
184 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
185 }
186
187 cnt = auio.uio_resid;
188 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
189 if (error)
190 if (auio.uio_resid != cnt && (error == ERESTART ||
191 error == EINTR || error == EWOULDBLOCK))
192 error = 0;
193 cnt -= auio.uio_resid;
194
195 if (ktriov != NULL) {
196 ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
197 free(ktriov, M_TEMP);
198 }
199
200 *retval = cnt;
201 done:
202 if (needfree)
203 free(needfree, M_IOV);
204 out:
205 fd_putfile(fd);
206 return (error);
207 }
208
209 int
210 netbsd32_writev(struct lwp *l, const struct netbsd32_writev_args *uap, register_t *retval)
211 {
212 /* {
213 syscallarg(int) fd;
214 syscallarg(const netbsd32_iovecp_t) iovp;
215 syscallarg(int) iovcnt;
216 } */
217 int fd = SCARG(uap, fd);
218 file_t *fp;
219
220 if ((fp = fd_getfile(fd)) == NULL)
221 return (EBADF);
222
223 if ((fp->f_flag & FWRITE) == 0) {
224 fd_putfile(fd);
225 return (EBADF);
226 }
227
228 return (dofilewritev32(fd, fp,
229 (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
230 SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
231 }
232
233 int
234 dofilewritev32(int fd, struct file *fp, struct netbsd32_iovec *iovp, int iovcnt, off_t *offset, int flags, register_t *retval)
235 {
236 struct uio auio;
237 struct iovec *iov;
238 struct iovec *needfree;
239 struct iovec aiov[UIO_SMALLIOV];
240 long i, cnt, error = 0;
241 u_int iovlen;
242 struct iovec *ktriov = NULL;
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 = curproc->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
283 /*
284 * if tracing, save a copy of iovec
285 */
286 if (ktrpoint(KTR_GENIO)) {
287 ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
288 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
289 }
290
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 mutex_enter(proc_lock);
299 psignal(curproc, SIGPIPE);
300 mutex_exit(proc_lock);
301 }
302 }
303 cnt -= auio.uio_resid;
304 if (ktriov != NULL) {
305 ktrgenio(fd, UIO_WRITE, ktriov, cnt, error);
306 free(ktriov, M_TEMP);
307 }
308 *retval = cnt;
309 done:
310 if (needfree)
311 free(needfree, M_IOV);
312 out:
313 fd_putfile(fd);
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(struct lwp *l, const struct netbsd32_utimes_args *uap, register_t *retval)
344 {
345 /* {
346 syscallarg(const netbsd32_charp) path;
347 syscallarg(const netbsd32_timevalp_t) tptr;
348 } */
349 int error;
350 struct timeval tv[2], *tvp;
351
352 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
353 if (error != 0)
354 return error;
355
356 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
357 tvp, UIO_SYSSPACE);
358 }
359
360 static int
361 netbds32_copyout_statvfs(const void *kp, void *up, size_t len)
362 {
363 struct netbsd32_statvfs *sbuf_32;
364 int error;
365
366 sbuf_32 = malloc(sizeof *sbuf_32, M_TEMP, M_WAITOK);
367 netbsd32_from_statvfs(kp, sbuf_32);
368 error = copyout(sbuf_32, up, sizeof(*sbuf_32));
369 free(sbuf_32, M_TEMP);
370
371 return error;
372 }
373
374 int
375 netbsd32_statvfs1(struct lwp *l, const struct netbsd32_statvfs1_args *uap, register_t *retval)
376 {
377 /* {
378 syscallarg(const netbsd32_charp) path;
379 syscallarg(netbsd32_statvfsp_t) buf;
380 syscallarg(int) flags;
381 } */
382 struct statvfs *sb;
383 int error;
384
385 sb = STATVFSBUF_GET();
386 error = do_sys_pstatvfs(l, SCARG_P32(uap, path), SCARG(uap, flags), sb);
387 if (error == 0)
388 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
389 STATVFSBUF_PUT(sb);
390 return error;
391 }
392
393 int
394 netbsd32_fstatvfs1(struct lwp *l, const struct netbsd32_fstatvfs1_args *uap, register_t *retval)
395 {
396 /* {
397 syscallarg(int) fd;
398 syscallarg(netbsd32_statvfsp_t) buf;
399 syscallarg(int) flags;
400 } */
401 struct statvfs *sb;
402 int error;
403
404 sb = STATVFSBUF_GET();
405 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
406 if (error == 0)
407 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
408 STATVFSBUF_PUT(sb);
409 return error;
410 }
411
412 int
413 netbsd32_getvfsstat(struct lwp *l, const struct netbsd32_getvfsstat_args *uap, register_t *retval)
414 {
415 /* {
416 syscallarg(netbsd32_statvfsp_t) buf;
417 syscallarg(netbsd32_size_t) bufsize;
418 syscallarg(int) flags;
419 } */
420
421 return do_sys_getvfsstat(l, SCARG_P32(uap, buf), SCARG(uap, bufsize),
422 SCARG(uap, flags), netbds32_copyout_statvfs,
423 sizeof (struct netbsd32_statvfs), retval);
424 }
425
426 int
427 netbsd32___fhstatvfs140(struct lwp *l, const struct netbsd32___fhstatvfs140_args *uap, register_t *retval)
428 {
429 /* {
430 syscallarg(const netbsd32_pointer_t) fhp;
431 syscallarg(netbsd32_size_t) fh_size;
432 syscallarg(netbsd32_statvfsp_t) buf;
433 syscallarg(int) flags;
434 } */
435 struct statvfs *sb;
436 int error;
437
438 sb = STATVFSBUF_GET();
439 error = do_fhstatvfs(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), sb,
440 SCARG(uap, flags));
441
442 if (error == 0)
443 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
444 STATVFSBUF_PUT(sb);
445
446 return error;
447 }
448
449 int
450 netbsd32_futimes(struct lwp *l, const struct netbsd32_futimes_args *uap, register_t *retval)
451 {
452 /* {
453 syscallarg(int) fd;
454 syscallarg(const netbsd32_timevalp_t) tptr;
455 } */
456 int error;
457 file_t *fp;
458 struct timeval tv[2], *tvp;
459
460 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
461 if (error != 0)
462 return error;
463
464 /* getvnode() will use the descriptor for us */
465 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
466 return (error);
467
468 error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
469
470 fd_putfile(SCARG(uap, fd));
471 return (error);
472 }
473
474 int
475 netbsd32_sys___getdents30(struct lwp *l, const struct netbsd32_sys___getdents30_args *uap, register_t *retval)
476 {
477 /* {
478 syscallarg(int) fd;
479 syscallarg(netbsd32_charp) buf;
480 syscallarg(netbsd32_size_t) count;
481 } */
482 file_t *fp;
483 int error, done;
484
485 /* getvnode() will use the descriptor for us */
486 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
487 return (error);
488 if ((fp->f_flag & FREAD) == 0) {
489 error = EBADF;
490 goto out;
491 }
492 error = vn_readdir(fp, SCARG_P32(uap, buf),
493 UIO_USERSPACE, SCARG(uap, count), &done, l, 0, 0);
494 *retval = done;
495 out:
496 fd_putfile(SCARG(uap, fd));
497 return (error);
498 }
499
500 int
501 netbsd32_lutimes(struct lwp *l, const struct netbsd32_lutimes_args *uap, register_t *retval)
502 {
503 /* {
504 syscallarg(const netbsd32_charp) path;
505 syscallarg(const netbsd32_timevalp_t) tptr;
506 } */
507 int error;
508 struct timeval tv[2], *tvp;
509
510 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
511 if (error != 0)
512 return error;
513
514 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
515 tvp, UIO_SYSSPACE);
516 }
517
518 int
519 netbsd32_sys___stat30(struct lwp *l, const struct netbsd32_sys___stat30_args *uap, register_t *retval)
520 {
521 /* {
522 syscallarg(const netbsd32_charp) path;
523 syscallarg(netbsd32_statp_t) ub;
524 } */
525 struct netbsd32_stat sb32;
526 struct stat sb;
527 int error;
528 const char *path;
529
530 path = SCARG_P32(uap, path);
531
532 error = do_sys_stat(path, FOLLOW, &sb);
533 if (error)
534 return (error);
535 netbsd32_from___stat30(&sb, &sb32);
536 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
537 return (error);
538 }
539
540 int
541 netbsd32_sys___fstat30(struct lwp *l, const struct netbsd32_sys___fstat30_args *uap, register_t *retval)
542 {
543 /* {
544 syscallarg(int) fd;
545 syscallarg(netbsd32_statp_t) sb;
546 } */
547 int fd = SCARG(uap, fd);
548 file_t *fp;
549 struct netbsd32_stat sb32;
550 struct stat ub;
551 int error = 0;
552
553 if ((fp = fd_getfile(fd)) == NULL)
554 return (EBADF);
555 error = (*fp->f_ops->fo_stat)(fp, &ub);
556 fd_putfile(fd);
557 if (error == 0) {
558 netbsd32_from___stat30(&ub, &sb32);
559 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
560 }
561 return (error);
562 }
563
564 int
565 netbsd32_sys___lstat30(struct lwp *l, const struct netbsd32_sys___lstat30_args *uap, register_t *retval)
566 {
567 /* {
568 syscallarg(const netbsd32_charp) path;
569 syscallarg(netbsd32_statp_t) ub;
570 } */
571 struct netbsd32_stat sb32;
572 struct stat sb;
573 int error;
574 const char *path;
575
576 path = SCARG_P32(uap, path);
577
578 error = do_sys_stat(path, NOFOLLOW, &sb);
579 if (error)
580 return (error);
581 netbsd32_from___stat30(&sb, &sb32);
582 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
583 return (error);
584 }
585
586 int
587 netbsd32___fhstat40(struct lwp *l, const struct netbsd32___fhstat40_args *uap, register_t *retval)
588 {
589 /* {
590 syscallarg(const netbsd32_pointer_t) fhp;
591 syscallarg(netbsd32_size_t) fh_size;
592 syscallarg(netbsd32_statp_t) sb;
593 } */
594 struct stat sb;
595 struct netbsd32_stat sb32;
596 int error;
597
598 error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
599 if (error != 0) {
600 netbsd32_from___stat30(&sb, &sb32);
601 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
602 }
603 return error;
604 }
605
606 int
607 netbsd32_preadv(struct lwp *l, const struct netbsd32_preadv_args *uap, register_t *retval)
608 {
609 /* {
610 syscallarg(int) fd;
611 syscallarg(const netbsd32_iovecp_t) iovp;
612 syscallarg(int) iovcnt;
613 syscallarg(int) pad;
614 syscallarg(off_t) offset;
615 } */
616 file_t *fp;
617 struct vnode *vp;
618 off_t offset;
619 int error, fd = SCARG(uap, fd);
620
621 if ((fp = fd_getfile(fd)) == NULL)
622 return (EBADF);
623
624 if ((fp->f_flag & FREAD) == 0) {
625 fd_putfile(fd);
626 return (EBADF);
627 }
628
629 vp = fp->f_data;
630 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
631 error = ESPIPE;
632 goto out;
633 }
634
635 offset = SCARG(uap, offset);
636
637 /*
638 * XXX This works because no file systems actually
639 * XXX take any action on the seek operation.
640 */
641 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
642 goto out;
643
644 return (dofilereadv32(fd, fp, SCARG_P32(uap, iovp),
645 SCARG(uap, iovcnt), &offset, 0, retval));
646
647 out:
648 fd_putfile(fd);
649 return (error);
650 }
651
652 int
653 netbsd32_pwritev(struct lwp *l, const struct netbsd32_pwritev_args *uap, register_t *retval)
654 {
655 /* {
656 syscallarg(int) fd;
657 syscallarg(const netbsd32_iovecp_t) iovp;
658 syscallarg(int) iovcnt;
659 syscallarg(int) pad;
660 syscallarg(off_t) offset;
661 } */
662 file_t *fp;
663 struct vnode *vp;
664 off_t offset;
665 int error, fd = SCARG(uap, fd);
666
667 if ((fp = fd_getfile(fd)) == NULL)
668 return (EBADF);
669
670 if ((fp->f_flag & FWRITE) == 0) {
671 fd_putfile(fd);
672 return (EBADF);
673 }
674
675 vp = 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 (dofilewritev32(fd, fp, SCARG_P32(uap, iovp),
691 SCARG(uap, iovcnt), &offset, 0, retval));
692
693 out:
694 fd_putfile(fd);
695 return (error);
696 }
697
698 /*
699 * Find pathname of process's current directory.
700 *
701 * Use vfs vnode-to-name reverse cache; if that fails, fall back
702 * to reading directory contents.
703 */
704 /* XXX NH Why does this exist */
705 int
706 getcwd_common(struct vnode *, struct vnode *,
707 char **, char *, int, int, struct lwp *);
708
709 int
710 netbsd32___getcwd(struct lwp *l, const struct netbsd32___getcwd_args *uap, register_t *retval)
711 {
712 /* {
713 syscallarg(char *) bufp;
714 syscallarg(size_t) length;
715 } */
716 struct proc *p = l->l_proc;
717 int error;
718 char *path;
719 char *bp, *bend;
720 int len = (int)SCARG(uap, length);
721 int lenused;
722 struct cwdinfo *cwdi;
723
724 if (len > MAXPATHLEN*4)
725 len = MAXPATHLEN*4;
726 else if (len < 2)
727 return ERANGE;
728
729 path = (char *)malloc(len, M_TEMP, M_WAITOK);
730 if (!path)
731 return ENOMEM;
732
733 bp = &path[len];
734 bend = bp;
735 *(--bp) = '\0';
736
737 /*
738 * 5th argument here is "max number of vnodes to traverse".
739 * Since each entry takes up at least 2 bytes in the output buffer,
740 * limit it to N/2 vnodes for an N byte buffer.
741 */
742 #define GETCWD_CHECK_ACCESS 0x0001
743 cwdi = p->p_cwdi;
744 rw_enter(&cwdi->cwdi_lock, RW_READER);
745 error = getcwd_common (cwdi->cwdi_cdir, NULL, &bp, path, len/2,
746 GETCWD_CHECK_ACCESS, l);
747 rw_exit(&cwdi->cwdi_lock);
748
749 if (error)
750 goto out;
751 lenused = bend - bp;
752 *retval = lenused;
753 /* put the result into user buffer */
754 error = copyout(bp, SCARG_P32(uap, bufp), lenused);
755
756 out:
757 free(path, M_TEMP);
758 return error;
759 }
760