netbsd32_fs.c revision 1.59 1 /* $NetBSD: netbsd32_fs.c,v 1.59 2010/04/23 15:19:20 rmind 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_fs.c,v 1.59 2010/04/23 15:19:20 rmind Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/mount.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/stat.h>
38 #include <sys/time.h>
39 #include <sys/ktrace.h>
40 #include <sys/resourcevar.h>
41 #include <sys/vnode.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/namei.h>
45 #include <sys/statvfs.h>
46 #include <sys/syscallargs.h>
47 #include <sys/proc.h>
48 #include <sys/dirent.h>
49 #include <sys/kauth.h>
50 #include <sys/vfs_syscalls.h>
51
52 #include <fs/cd9660/cd9660_mount.h>
53 #include <ufs/ufs/ufsmount.h>
54
55 #include <compat/netbsd32/netbsd32.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.h>
57 #include <compat/netbsd32/netbsd32_conv.h>
58 #include <compat/sys/mount.h>
59
60
61 static int dofilereadv32(int, struct file *, struct netbsd32_iovec *,
62 int, off_t *, int, register_t *);
63 static int dofilewritev32(int, struct file *, struct netbsd32_iovec *,
64 int, off_t *, int, register_t *);
65
66 struct iovec *
67 netbsd32_get_iov(struct netbsd32_iovec *iov32, int iovlen, struct iovec *aiov,
68 int aiov_len)
69 {
70 #define N_IOV32 8
71 struct netbsd32_iovec aiov32[N_IOV32];
72 struct iovec *iov = aiov;
73 struct iovec *iovp;
74 int i, n, j;
75 int error;
76
77 if (iovlen < 0 || iovlen > IOV_MAX)
78 return NULL;
79
80 if (iovlen > aiov_len)
81 iov = kmem_alloc(iovlen * sizeof(*iov), KM_SLEEP);
82
83 iovp = iov;
84 for (i = 0; i < iovlen; iov32 += N_IOV32, i += N_IOV32) {
85 n = iovlen - i;
86 if (n > N_IOV32)
87 n = N_IOV32;
88 error = copyin(iov32, aiov32, n * sizeof (*iov32));
89 if (error != 0) {
90 if (iov != aiov)
91 kmem_free(iov, iovlen * sizeof(*iov));
92 return NULL;
93 }
94 for (j = 0; j < n; iovp++, j++) {
95 iovp->iov_base = NETBSD32PTR64(aiov32[j].iov_base);
96 iovp->iov_len = aiov32[j].iov_len;
97 }
98 }
99 return iov;
100 #undef N_IOV32
101 }
102
103 int
104 netbsd32_readv(struct lwp *l, const struct netbsd32_readv_args *uap, register_t *retval)
105 {
106 /* {
107 syscallarg(int) fd;
108 syscallarg(const netbsd32_iovecp_t) iovp;
109 syscallarg(int) iovcnt;
110 } */
111 int fd = SCARG(uap, fd);
112 file_t *fp;
113
114 if ((fp = fd_getfile(fd)) == NULL)
115 return (EBADF);
116
117 if ((fp->f_flag & FREAD) == 0) {
118 fd_putfile(fd);
119 return (EBADF);
120 }
121
122 return (dofilereadv32(fd, fp,
123 (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
124 SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
125 }
126
127 /* Damn thing copies in the iovec! */
128 int
129 dofilereadv32(int fd, struct file *fp, struct netbsd32_iovec *iovp, int iovcnt, off_t *offset, int flags, register_t *retval)
130 {
131 struct uio auio;
132 struct iovec *iov;
133 struct iovec *needfree;
134 struct iovec aiov[UIO_SMALLIOV];
135 long i, cnt, error = 0;
136 u_int iovlen;
137 struct iovec *ktriov = NULL;
138
139 /* note: can't use iovlen until iovcnt is validated */
140 iovlen = iovcnt * sizeof(struct iovec);
141 if ((u_int)iovcnt > UIO_SMALLIOV) {
142 if ((u_int)iovcnt > IOV_MAX) {
143 error = EINVAL;
144 goto out;
145 }
146 iov = kmem_alloc(iovlen, KM_SLEEP);
147 needfree = iov;
148 } else if ((u_int)iovcnt > 0) {
149 iov = aiov;
150 needfree = NULL;
151 } else {
152 error = EINVAL;
153 goto out;
154 }
155
156 auio.uio_iov = iov;
157 auio.uio_iovcnt = iovcnt;
158 auio.uio_rw = UIO_READ;
159 auio.uio_vmspace = curproc->p_vmspace;
160 error = netbsd32_to_iovecin(iovp, iov, iovcnt);
161 if (error)
162 goto done;
163 auio.uio_resid = 0;
164 for (i = 0; i < iovcnt; i++) {
165 auio.uio_resid += iov->iov_len;
166 /*
167 * Reads return ssize_t because -1 is returned on error.
168 * Therefore we must restrict the length to SSIZE_MAX to
169 * avoid garbage return values.
170 */
171 if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
172 error = EINVAL;
173 goto done;
174 }
175 iov++;
176 }
177
178 /*
179 * if tracing, save a copy of iovec
180 */
181 if (ktrpoint(KTR_GENIO)) {
182 ktriov = kmem_alloc(iovlen, KM_SLEEP);
183 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
184 }
185
186 cnt = auio.uio_resid;
187 error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
188 if (error)
189 if (auio.uio_resid != cnt && (error == ERESTART ||
190 error == EINTR || error == EWOULDBLOCK))
191 error = 0;
192 cnt -= auio.uio_resid;
193
194 if (ktriov != NULL) {
195 ktrgeniov(fd, UIO_READ, ktriov, cnt, error);
196 kmem_free(ktriov, iovlen);
197 }
198
199 *retval = cnt;
200 done:
201 if (needfree)
202 kmem_free(needfree, iovlen);
203 out:
204 fd_putfile(fd);
205 return (error);
206 }
207
208 int
209 netbsd32_writev(struct lwp *l, const struct netbsd32_writev_args *uap, register_t *retval)
210 {
211 /* {
212 syscallarg(int) fd;
213 syscallarg(const netbsd32_iovecp_t) iovp;
214 syscallarg(int) iovcnt;
215 } */
216 int fd = SCARG(uap, fd);
217 file_t *fp;
218
219 if ((fp = fd_getfile(fd)) == NULL)
220 return (EBADF);
221
222 if ((fp->f_flag & FWRITE) == 0) {
223 fd_putfile(fd);
224 return (EBADF);
225 }
226
227 return (dofilewritev32(fd, fp,
228 (struct netbsd32_iovec *)SCARG_P32(uap, iovp),
229 SCARG(uap, iovcnt), &fp->f_offset, FOF_UPDATE_OFFSET, retval));
230 }
231
232 int
233 dofilewritev32(int fd, struct file *fp, struct netbsd32_iovec *iovp, int iovcnt, off_t *offset, int flags, register_t *retval)
234 {
235 struct uio auio;
236 struct iovec *iov;
237 struct iovec *needfree;
238 struct iovec aiov[UIO_SMALLIOV];
239 long i, cnt, error = 0;
240 u_int iovlen;
241 struct iovec *ktriov = NULL;
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 = kmem_alloc(iovlen, KM_SLEEP);
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 = curproc->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
282 /*
283 * if tracing, save a copy of iovec
284 */
285 if (ktrpoint(KTR_GENIO)) {
286 ktriov = kmem_alloc(iovlen, KM_SLEEP);
287 memcpy((void *)ktriov, (void *)auio.uio_iov, iovlen);
288 }
289
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 mutex_enter(proc_lock);
298 psignal(curproc, SIGPIPE);
299 mutex_exit(proc_lock);
300 }
301 }
302 cnt -= auio.uio_resid;
303 if (ktriov != NULL) {
304 ktrgenio(fd, UIO_WRITE, ktriov, cnt, error);
305 kmem_free(ktriov, iovlen);
306 }
307 *retval = cnt;
308 done:
309 if (needfree)
310 kmem_free(needfree, iovlen);
311 out:
312 fd_putfile(fd);
313 return (error);
314 }
315
316 /*
317 * Common routine to set access and modification times given a vnode.
318 */
319 static int
320 get_utimes32(const netbsd32_timevalp_t *tptr, struct timeval *tv,
321 struct timeval **tvp)
322 {
323 int error;
324 struct netbsd32_timeval tv32[2];
325
326 if (tptr == NULL) {
327 *tvp = NULL;
328 return 0;
329 }
330
331 error = copyin(tptr, tv32, sizeof(tv32));
332 if (error)
333 return error;
334 netbsd32_to_timeval(&tv32[0], &tv[0]);
335 netbsd32_to_timeval(&tv32[1], &tv[1]);
336
337 *tvp = tv;
338 return 0;
339 }
340
341 int
342 netbsd32___utimes50(struct lwp *l, const struct netbsd32___utimes50_args *uap, register_t *retval)
343 {
344 /* {
345 syscallarg(const netbsd32_charp) path;
346 syscallarg(const netbsd32_timevalp_t) tptr;
347 } */
348 int error;
349 struct timeval tv[2], *tvp;
350
351 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
352 if (error != 0)
353 return error;
354
355 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), FOLLOW,
356 tvp, UIO_SYSSPACE);
357 }
358
359 static int
360 netbds32_copyout_statvfs(const void *kp, void *up, size_t len)
361 {
362 struct netbsd32_statvfs *sbuf_32;
363 int error;
364
365 sbuf_32 = kmem_alloc(sizeof(*sbuf_32), KM_SLEEP);
366 netbsd32_from_statvfs(kp, sbuf_32);
367 error = copyout(sbuf_32, up, sizeof(*sbuf_32));
368 kmem_free(sbuf_32, sizeof(*sbuf_32));
369
370 return error;
371 }
372
373 int
374 netbsd32_statvfs1(struct lwp *l, const struct netbsd32_statvfs1_args *uap, register_t *retval)
375 {
376 /* {
377 syscallarg(const netbsd32_charp) path;
378 syscallarg(netbsd32_statvfsp_t) buf;
379 syscallarg(int) flags;
380 } */
381 struct statvfs *sb;
382 int error;
383
384 sb = STATVFSBUF_GET();
385 error = do_sys_pstatvfs(l, SCARG_P32(uap, path), SCARG(uap, flags), sb);
386 if (error == 0)
387 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
388 STATVFSBUF_PUT(sb);
389 return error;
390 }
391
392 int
393 netbsd32_fstatvfs1(struct lwp *l, const struct netbsd32_fstatvfs1_args *uap, register_t *retval)
394 {
395 /* {
396 syscallarg(int) fd;
397 syscallarg(netbsd32_statvfsp_t) buf;
398 syscallarg(int) flags;
399 } */
400 struct statvfs *sb;
401 int error;
402
403 sb = STATVFSBUF_GET();
404 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
405 if (error == 0)
406 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
407 STATVFSBUF_PUT(sb);
408 return error;
409 }
410
411 int
412 netbsd32_getvfsstat(struct lwp *l, const struct netbsd32_getvfsstat_args *uap, register_t *retval)
413 {
414 /* {
415 syscallarg(netbsd32_statvfsp_t) buf;
416 syscallarg(netbsd32_size_t) bufsize;
417 syscallarg(int) flags;
418 } */
419
420 return do_sys_getvfsstat(l, SCARG_P32(uap, buf), SCARG(uap, bufsize),
421 SCARG(uap, flags), netbds32_copyout_statvfs,
422 sizeof (struct netbsd32_statvfs), retval);
423 }
424
425 int
426 netbsd32___fhstatvfs140(struct lwp *l, const struct netbsd32___fhstatvfs140_args *uap, register_t *retval)
427 {
428 /* {
429 syscallarg(const netbsd32_pointer_t) fhp;
430 syscallarg(netbsd32_size_t) fh_size;
431 syscallarg(netbsd32_statvfsp_t) buf;
432 syscallarg(int) flags;
433 } */
434 struct statvfs *sb;
435 int error;
436
437 sb = STATVFSBUF_GET();
438 error = do_fhstatvfs(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), sb,
439 SCARG(uap, flags));
440
441 if (error == 0)
442 error = netbds32_copyout_statvfs(sb, SCARG_P32(uap, buf), 0);
443 STATVFSBUF_PUT(sb);
444
445 return error;
446 }
447
448 int
449 netbsd32___futimes50(struct lwp *l, const struct netbsd32___futimes50_args *uap, register_t *retval)
450 {
451 /* {
452 syscallarg(int) fd;
453 syscallarg(const netbsd32_timevalp_t) tptr;
454 } */
455 int error;
456 file_t *fp;
457 struct timeval tv[2], *tvp;
458
459 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
460 if (error != 0)
461 return error;
462
463 /* fd_getvnode() will use the descriptor for us */
464 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
465 return (error);
466
467 error = do_sys_utimes(l, fp->f_data, NULL, 0, tvp, UIO_SYSSPACE);
468
469 fd_putfile(SCARG(uap, fd));
470 return (error);
471 }
472
473 int
474 netbsd32___getdents30(struct lwp *l,
475 const struct netbsd32___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 /* fd_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___lutimes50(struct lwp *l,
502 const struct netbsd32___lutimes50_args *uap, register_t *retval)
503 {
504 /* {
505 syscallarg(const netbsd32_charp) path;
506 syscallarg(const netbsd32_timevalp_t) tptr;
507 } */
508 int error;
509 struct timeval tv[2], *tvp;
510
511 error = get_utimes32(SCARG_P32(uap, tptr), tv, &tvp);
512 if (error != 0)
513 return error;
514
515 return do_sys_utimes(l, NULL, SCARG_P32(uap, path), NOFOLLOW,
516 tvp, UIO_SYSSPACE);
517 }
518
519 int
520 netbsd32___stat50(struct lwp *l, const struct netbsd32___stat50_args *uap, register_t *retval)
521 {
522 /* {
523 syscallarg(const netbsd32_charp) path;
524 syscallarg(netbsd32_statp_t) ub;
525 } */
526 struct netbsd32_stat sb32;
527 struct stat sb;
528 int error;
529 const char *path;
530
531 path = SCARG_P32(uap, path);
532
533 error = do_sys_stat(path, FOLLOW, &sb);
534 if (error)
535 return (error);
536 netbsd32_from_stat(&sb, &sb32);
537 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
538 return (error);
539 }
540
541 int
542 netbsd32___fstat50(struct lwp *l, const struct netbsd32___fstat50_args *uap, register_t *retval)
543 {
544 /* {
545 syscallarg(int) fd;
546 syscallarg(netbsd32_statp_t) sb;
547 } */
548 struct netbsd32_stat sb32;
549 struct stat ub;
550 int error;
551
552 error = do_sys_fstat(SCARG(uap, fd), &ub);
553 if (error == 0) {
554 netbsd32_from_stat(&ub, &sb32);
555 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb32));
556 }
557 return (error);
558 }
559
560 int
561 netbsd32___lstat50(struct lwp *l, const struct netbsd32___lstat50_args *uap, register_t *retval)
562 {
563 /* {
564 syscallarg(const netbsd32_charp) path;
565 syscallarg(netbsd32_statp_t) ub;
566 } */
567 struct netbsd32_stat sb32;
568 struct stat sb;
569 int error;
570 const char *path;
571
572 path = SCARG_P32(uap, path);
573
574 error = do_sys_stat(path, NOFOLLOW, &sb);
575 if (error)
576 return (error);
577 netbsd32_from_stat(&sb, &sb32);
578 error = copyout(&sb32, SCARG_P32(uap, ub), sizeof(sb32));
579 return (error);
580 }
581
582 int
583 netbsd32___fhstat50(struct lwp *l, const struct netbsd32___fhstat50_args *uap, register_t *retval)
584 {
585 /* {
586 syscallarg(const netbsd32_pointer_t) fhp;
587 syscallarg(netbsd32_size_t) fh_size;
588 syscallarg(netbsd32_statp_t) sb;
589 } */
590 struct stat sb;
591 struct netbsd32_stat sb32;
592 int error;
593
594 error = do_fhstat(l, SCARG_P32(uap, fhp), SCARG(uap, fh_size), &sb);
595 if (error != 0) {
596 netbsd32_from_stat(&sb, &sb32);
597 error = copyout(&sb32, SCARG_P32(uap, sb), sizeof(sb));
598 }
599 return error;
600 }
601
602 int
603 netbsd32_preadv(struct lwp *l, const struct netbsd32_preadv_args *uap, register_t *retval)
604 {
605 /* {
606 syscallarg(int) fd;
607 syscallarg(const netbsd32_iovecp_t) iovp;
608 syscallarg(int) iovcnt;
609 syscallarg(int) pad;
610 syscallarg(off_t) offset;
611 } */
612 file_t *fp;
613 struct vnode *vp;
614 off_t offset;
615 int error, fd = SCARG(uap, fd);
616
617 if ((fp = fd_getfile(fd)) == NULL)
618 return (EBADF);
619
620 if ((fp->f_flag & FREAD) == 0) {
621 fd_putfile(fd);
622 return (EBADF);
623 }
624
625 vp = fp->f_data;
626 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
627 error = ESPIPE;
628 goto out;
629 }
630
631 offset = SCARG(uap, offset);
632
633 /*
634 * XXX This works because no file systems actually
635 * XXX take any action on the seek operation.
636 */
637 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
638 goto out;
639
640 return (dofilereadv32(fd, fp, SCARG_P32(uap, iovp),
641 SCARG(uap, iovcnt), &offset, 0, retval));
642
643 out:
644 fd_putfile(fd);
645 return (error);
646 }
647
648 int
649 netbsd32_pwritev(struct lwp *l, const struct netbsd32_pwritev_args *uap, register_t *retval)
650 {
651 /* {
652 syscallarg(int) fd;
653 syscallarg(const netbsd32_iovecp_t) iovp;
654 syscallarg(int) iovcnt;
655 syscallarg(int) pad;
656 syscallarg(off_t) offset;
657 } */
658 file_t *fp;
659 struct vnode *vp;
660 off_t offset;
661 int error, fd = SCARG(uap, fd);
662
663 if ((fp = fd_getfile(fd)) == NULL)
664 return (EBADF);
665
666 if ((fp->f_flag & FWRITE) == 0) {
667 fd_putfile(fd);
668 return (EBADF);
669 }
670
671 vp = fp->f_data;
672 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
673 error = ESPIPE;
674 goto out;
675 }
676
677 offset = SCARG(uap, offset);
678
679 /*
680 * XXX This works because no file systems actually
681 * XXX take any action on the seek operation.
682 */
683 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
684 goto out;
685
686 return (dofilewritev32(fd, fp, SCARG_P32(uap, iovp),
687 SCARG(uap, iovcnt), &offset, 0, retval));
688
689 out:
690 fd_putfile(fd);
691 return (error);
692 }
693
694 /*
695 * Find pathname of process's current directory.
696 *
697 * Use vfs vnode-to-name reverse cache; if that fails, fall back
698 * to reading directory contents.
699 */
700 /* XXX NH Why does this exist */
701 int
702 getcwd_common(struct vnode *, struct vnode *,
703 char **, char *, int, int, struct lwp *);
704
705 int
706 netbsd32___getcwd(struct lwp *l, const struct netbsd32___getcwd_args *uap, register_t *retval)
707 {
708 /* {
709 syscallarg(char *) bufp;
710 syscallarg(size_t) length;
711 } */
712 struct proc *p = l->l_proc;
713 int error;
714 char *path;
715 char *bp, *bend;
716 int len = (int)SCARG(uap, length);
717 int lenused;
718 struct cwdinfo *cwdi;
719
720 if (len > MAXPATHLEN*4)
721 len = MAXPATHLEN*4;
722 else if (len < 2)
723 return ERANGE;
724
725 path = kmem_alloc(len, KM_SLEEP);
726 if (!path)
727 return ENOMEM;
728
729 bp = &path[len];
730 bend = bp;
731 *(--bp) = '\0';
732
733 /*
734 * 5th argument here is "max number of vnodes to traverse".
735 * Since each entry takes up at least 2 bytes in the output buffer,
736 * limit it to N/2 vnodes for an N byte buffer.
737 */
738 #define GETCWD_CHECK_ACCESS 0x0001
739 cwdi = p->p_cwdi;
740 rw_enter(&cwdi->cwdi_lock, RW_READER);
741 error = getcwd_common (cwdi->cwdi_cdir, NULL, &bp, path, len/2,
742 GETCWD_CHECK_ACCESS, l);
743 rw_exit(&cwdi->cwdi_lock);
744
745 if (error)
746 goto out;
747 lenused = bend - bp;
748 *retval = lenused;
749 /* put the result into user buffer */
750 error = copyout(bp, SCARG_P32(uap, bufp), lenused);
751
752 out:
753 kmem_free(path, len);
754 return error;
755 }
756
757 int
758 netbsd32___mount50(struct lwp *l, const struct netbsd32___mount50_args *uap,
759 register_t *retval)
760 {
761 /* {
762 syscallarg(netbsd32_charp) type;
763 syscallarg(netbsd32_charp) path;
764 syscallarg(int) flags;
765 syscallarg(netbsd32_voidp) data;
766 syscallarg(netbsd32_size_t) data_len;
767 } */
768 char mtype[MNAMELEN];
769 union {
770 struct netbsd32_ufs_args ufs_args;
771 struct netbsd32_mfs_args mfs_args;
772 struct netbsd32_iso_args iso_args;
773 } fs_args32;
774 union {
775 struct ufs_args ufs_args;
776 struct mfs_args mfs_args;
777 struct iso_args iso_args;
778 } fs_args;
779 const char *type = SCARG_P32(uap, type);
780 const char *path = SCARG_P32(uap, path);
781 int flags = SCARG(uap, flags);
782 void *data = SCARG_P32(uap, data);
783 size_t data_len = SCARG(uap, data_len);
784 enum uio_seg data_seg;
785 size_t len;
786 int error;
787
788 error = copyinstr(type, mtype, sizeof(mtype), &len);
789 if (error)
790 return error;
791 if (strcmp(mtype, MOUNT_MFS) == 0) {
792 if (data_len != sizeof(fs_args32.mfs_args))
793 return EINVAL;
794 if ((flags & MNT_GETARGS) == 0) {
795 error = copyin(data, &fs_args32.mfs_args,
796 sizeof(fs_args32.mfs_args));
797 if (error)
798 return error;
799 fs_args.mfs_args.fspec =
800 NETBSD32PTR64(fs_args32.mfs_args.fspec);
801 memset(&fs_args.mfs_args._pad1, 0,
802 sizeof(fs_args.mfs_args._pad1));
803 fs_args.mfs_args.base =
804 NETBSD32PTR64(fs_args32.mfs_args.base);
805 fs_args.mfs_args.size = fs_args32.mfs_args.size;
806 }
807 data_seg = UIO_SYSSPACE;
808 data = &fs_args.mfs_args;
809 data_len = sizeof(fs_args.mfs_args);
810 } else if (strcmp(mtype, MOUNT_UFS) == 0) {
811 if (data_len > sizeof(fs_args32.ufs_args))
812 return EINVAL;
813 if ((flags & MNT_GETARGS) == 0) {
814 error = copyin(data, &fs_args32.ufs_args,
815 sizeof(fs_args32.ufs_args));
816 if (error)
817 return error;
818 fs_args.ufs_args.fspec =
819 NETBSD32PTR64(fs_args32.ufs_args.fspec);
820 }
821 data_seg = UIO_SYSSPACE;
822 data = &fs_args.ufs_args;
823 data_len = sizeof(fs_args.ufs_args);
824 } else if (strcmp(mtype, MOUNT_CD9660) == 0) {
825 if (data_len != sizeof(fs_args32.iso_args))
826 return EINVAL;
827 if ((flags & MNT_GETARGS) == 0) {
828 error = copyin(data, &fs_args32.iso_args,
829 sizeof(fs_args32.iso_args));
830 if (error)
831 return error;
832 fs_args.iso_args.fspec =
833 NETBSD32PTR64(fs_args32.iso_args.fspec);
834 memset(&fs_args.iso_args._pad1, 0,
835 sizeof(fs_args.iso_args._pad1));
836 fs_args.iso_args.flags = fs_args32.iso_args.flags;
837 }
838 data_seg = UIO_SYSSPACE;
839 data = &fs_args.iso_args;
840 data_len = sizeof(fs_args.iso_args);
841 } else {
842 data_seg = UIO_USERSPACE;
843 }
844 error = do_sys_mount(l, NULL, type, path, flags, data, data_seg,
845 data_len, retval);
846 if (error)
847 return error;
848 if (flags & MNT_GETARGS) {
849 data_len = *retval;
850 if (strcmp(mtype, MOUNT_MFS) == 0) {
851 if (data_len != sizeof(fs_args.mfs_args))
852 return EINVAL;
853 NETBSD32PTR32(fs_args32.mfs_args.fspec,
854 fs_args.mfs_args.fspec);
855 memset(&fs_args32.mfs_args._pad1, 0,
856 sizeof(fs_args32.mfs_args._pad1));
857 NETBSD32PTR32(fs_args32.mfs_args.base,
858 fs_args.mfs_args.base);
859 fs_args32.mfs_args.size = fs_args.mfs_args.size;
860 error = copyout(&fs_args32.mfs_args, data,
861 sizeof(fs_args32.mfs_args));
862 } else if (strcmp(mtype, MOUNT_UFS) == 0) {
863 if (data_len != sizeof(fs_args.ufs_args))
864 return EINVAL;
865 NETBSD32PTR32(fs_args32.ufs_args.fspec,
866 fs_args.ufs_args.fspec);
867 error = copyout(&fs_args32.ufs_args, data,
868 sizeof(fs_args32.ufs_args));
869 } else if (strcmp(mtype, MOUNT_CD9660) == 0) {
870 if (data_len != sizeof(fs_args.iso_args))
871 return EINVAL;
872 NETBSD32PTR32(fs_args32.iso_args.fspec,
873 fs_args.iso_args.fspec);
874 memset(&fs_args32.iso_args._pad1, 0,
875 sizeof(fs_args32.iso_args._pad1));
876 fs_args32.iso_args.flags = fs_args.iso_args.flags;
877 error = copyout(&fs_args32.iso_args, data,
878 sizeof(fs_args32.iso_args));
879 }
880 }
881 return error;
882 }
883