vfs_syscalls_30.c revision 1.13 1 /* $NetBSD: vfs_syscalls_30.c,v 1.13 2006/07/31 16:34:43 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: vfs_syscalls_30.c,v 1.13 2006/07/31 16:34:43 martin Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/filedesc.h>
45 #include <sys/kernel.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/socketvar.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/proc.h>
52 #include <sys/uio.h>
53 #include <sys/dirent.h>
54 #include <sys/malloc.h>
55 #include <sys/kauth.h>
56
57 #include <sys/sa.h>
58 #include <sys/syscallargs.h>
59
60 #include <compat/common/compat_util.h>
61 #include <compat/sys/stat.h>
62 #include <compat/sys/dirent.h>
63 #include <compat/sys/mount.h>
64
65 static void cvtstat(struct stat13 *, const struct stat *);
66
67 /*
68 * Convert from a new to an old stat structure.
69 */
70 static void
71 cvtstat(struct stat13 *ost, const struct stat *st)
72 {
73
74 ost->st_dev = st->st_dev;
75 ost->st_ino = (uint32_t)st->st_ino;
76 ost->st_mode = st->st_mode;
77 ost->st_nlink = st->st_nlink;
78 ost->st_uid = st->st_uid;
79 ost->st_gid = st->st_gid;
80 ost->st_rdev = st->st_rdev;
81 ost->st_atimespec = st->st_atimespec;
82 ost->st_mtimespec = st->st_mtimespec;
83 ost->st_ctimespec = st->st_ctimespec;
84 ost->st_birthtimespec = st->st_birthtimespec;
85 ost->st_size = st->st_size;
86 ost->st_blocks = st->st_blocks;
87 ost->st_blksize = st->st_blksize;
88 ost->st_flags = st->st_flags;
89 ost->st_gen = st->st_gen;
90 }
91
92 /*
93 * Get file status; this version follows links.
94 */
95 /* ARGSUSED */
96 int
97 compat_30_sys___stat13(struct lwp *l, void *v, register_t *retval)
98 {
99 struct compat_30_sys___stat13_args /* {
100 syscallarg(const char *) path;
101 syscallarg(struct stat13 *) ub;
102 } */ *uap = v;
103 struct stat sb;
104 struct stat13 osb;
105 int error;
106 struct nameidata nd;
107
108 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
109 SCARG(uap, path), l);
110 if ((error = namei(&nd)) != 0)
111 return error;
112 error = vn_stat(nd.ni_vp, &sb, l);
113 vput(nd.ni_vp);
114 if (error)
115 return error;
116 cvtstat(&osb, &sb);
117 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
118 return error;
119 }
120
121
122 /*
123 * Get file status; this version does not follow links.
124 */
125 /* ARGSUSED */
126 int
127 compat_30_sys___lstat13(struct lwp *l, void *v, register_t *retval)
128 {
129 struct compat_30_sys___lstat13_args /* {
130 syscallarg(const char *) path;
131 syscallarg(struct stat13 *) ub;
132 } */ *uap = v;
133 struct stat sb;
134 struct stat13 osb;
135 int error;
136 struct nameidata nd;
137
138 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
139 SCARG(uap, path), l);
140 if ((error = namei(&nd)) != 0)
141 return error;
142 error = vn_stat(nd.ni_vp, &sb, l);
143 vput(nd.ni_vp);
144 if (error)
145 return error;
146 cvtstat(&osb, &sb);
147 error = copyout(&osb, SCARG(uap, ub), sizeof (osb));
148 return error;
149 }
150
151 /* ARGSUSED */
152 int
153 compat_30_sys_fhstat(struct lwp *l, void *v, register_t *retval)
154 {
155 struct compat_30_sys_fhstat_args /* {
156 syscallarg(const struct compat_30_fhandle *) fhp;
157 syscallarg(struct stat13 *) sb;
158 } */ *uap = v;
159 struct stat sb;
160 struct stat13 osb;
161 int error;
162 struct compat_30_fhandle fh;
163 struct mount *mp;
164 struct vnode *vp;
165
166 /*
167 * Must be super user
168 */
169 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
170 &l->l_acflag)))
171 return (error);
172
173 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fh))) != 0)
174 return (error);
175
176 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
177 return (ESTALE);
178 if (mp->mnt_op->vfs_fhtovp == NULL)
179 return EOPNOTSUPP;
180 if ((error = VFS_FHTOVP(mp, (struct fid*)&fh.fh_fid, &vp)))
181 return (error);
182 error = vn_stat(vp, &sb, l);
183 vput(vp);
184 if (error)
185 return (error);
186 cvtstat(&osb, &sb);
187 error = copyout(&osb, SCARG(uap, sb), sizeof(sb));
188 return (error);
189 }
190
191 /*
192 * Return status information about a file descriptor.
193 */
194 /* ARGSUSED */
195 int
196 compat_30_sys___fstat13(struct lwp *l, void *v, register_t *retval)
197 {
198 struct compat_30_sys___fstat13_args /* {
199 syscallarg(int) fd;
200 syscallarg(struct stat13 *) sb;
201 } */ *uap = v;
202 struct proc *p = l->l_proc;
203 int fd = SCARG(uap, fd);
204 struct filedesc *fdp = p->p_fd;
205 struct file *fp;
206 struct stat sb;
207 struct stat13 osb;
208 int error;
209
210 if ((fp = fd_getfile(fdp, fd)) == NULL)
211 return EBADF;
212
213 FILE_USE(fp);
214 error = (*fp->f_ops->fo_stat)(fp, &sb, l);
215 FILE_UNUSE(fp, l);
216
217 if (error)
218 return error;
219 cvtstat(&osb, &sb);
220 error = copyout(&osb, SCARG(uap, sb), sizeof (osb));
221 return error;
222 }
223
224 /*
225 * Read a block of directory entries in a file system independent format.
226 */
227 int
228 compat_30_sys_getdents(struct lwp *l, void *v, register_t *retval)
229 {
230 struct compat_30_sys_getdents_args /* {
231 syscallarg(int) fd;
232 syscallarg(char *) buf;
233 syscallarg(size_t) count;
234 } */ *uap = v;
235 struct proc *p = l->l_proc;
236 struct dirent *bdp;
237 struct vnode *vp;
238 caddr_t inp, tbuf; /* BSD-format */
239 int len, reclen; /* BSD-format */
240 caddr_t outp; /* NetBSD-3.0-format */
241 int resid;
242 struct file *fp;
243 struct uio auio;
244 struct iovec aiov;
245 struct dirent12 idb;
246 off_t off; /* true file offset */
247 int buflen, error, eofflag;
248 off_t *cookiebuf = NULL, *cookie;
249 int ncookies;
250
251 /* getvnode() will use the descriptor for us */
252 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
253 return error;
254
255 if ((fp->f_flag & FREAD) == 0) {
256 error = EBADF;
257 goto out1;
258 }
259
260 vp = (struct vnode *)fp->f_data;
261 if (vp->v_type != VDIR) {
262 error = EINVAL;
263 goto out1;
264 }
265
266 buflen = min(MAXBSIZE, SCARG(uap, count));
267 tbuf = malloc(buflen, M_TEMP, M_WAITOK);
268 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
269 off = fp->f_offset;
270 again:
271 aiov.iov_base = tbuf;
272 aiov.iov_len = buflen;
273 auio.uio_iov = &aiov;
274 auio.uio_iovcnt = 1;
275 auio.uio_rw = UIO_READ;
276 auio.uio_resid = buflen;
277 auio.uio_offset = off;
278 UIO_SETUP_SYSSPACE(&auio);
279 /*
280 * First we read into the malloc'ed buffer, then
281 * we massage it into user space, one record at a time.
282 */
283 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
284 &ncookies);
285 if (error)
286 goto out;
287
288 inp = tbuf;
289 outp = SCARG(uap, buf);
290 resid = SCARG(uap, count);
291 if ((len = buflen - auio.uio_resid) == 0)
292 goto eof;
293
294 for (cookie = cookiebuf; len > 0; len -= reclen) {
295 bdp = (struct dirent *)inp;
296 reclen = bdp->d_reclen;
297 if (reclen & _DIRENT_ALIGN(bdp))
298 panic("netbsd30_getdents: bad reclen %d", reclen);
299 if (cookie)
300 off = *cookie++; /* each entry points to the next */
301 else
302 off += reclen;
303 if ((off >> 32) != 0) {
304 compat_offseterr(vp, "netbsd30_getdents");
305 error = EINVAL;
306 goto out;
307 }
308 if (bdp->d_namlen >= sizeof(idb.d_name))
309 idb.d_namlen = sizeof(idb.d_name) - 1;
310 else
311 idb.d_namlen = bdp->d_namlen;
312 idb.d_reclen = _DIRENT_SIZE(&idb);
313 if (reclen > len || resid < idb.d_reclen) {
314 /* entry too big for buffer, so just stop */
315 outp++;
316 break;
317 }
318 /*
319 * Massage in place to make a NetBSD-3.0-shaped dirent
320 * (otherwise we have to worry about touching user memory
321 * outside of the copyout() call).
322 */
323 idb.d_fileno = (u_int32_t)bdp->d_fileno;
324 idb.d_type = bdp->d_type;
325 (void)memcpy(idb.d_name, bdp->d_name, idb.d_namlen);
326 memset(idb.d_name + idb.d_namlen, 0,
327 idb.d_reclen - _DIRENT_NAMEOFF(&idb) - idb.d_namlen);
328 if ((error = copyout(&idb, outp, idb.d_reclen)) != 0)
329 goto out;
330 /* advance past this real entry */
331 inp += reclen;
332 /* advance output past NetBSD-3.0-shaped entry */
333 outp += idb.d_reclen;
334 resid -= idb.d_reclen;
335 }
336
337 /* if we squished out the whole block, try again */
338 if (outp == SCARG(uap, buf))
339 goto again;
340 fp->f_offset = off; /* update the vnode offset */
341
342 eof:
343 *retval = SCARG(uap, count) - resid;
344 out:
345 VOP_UNLOCK(vp, 0);
346 if (cookiebuf)
347 free(cookiebuf, M_TEMP);
348 free(tbuf, M_TEMP);
349 out1:
350 FILE_UNUSE(fp, l);
351 return error;
352 }
353
354 /*
355 * Get file handle system call
356 */
357 int
358 compat_30_sys_getfh(struct lwp *l, void *v, register_t *retval)
359 {
360 struct compat_30_sys_getfh_args /* {
361 syscallarg(char *) fname;
362 syscallarg(struct compat_30_fhandle *) fhp;
363 } */ *uap = v;
364 struct vnode *vp;
365 struct compat_30_fhandle fh;
366 int error;
367 struct nameidata nd;
368 size_t sz;
369
370 /*
371 * Must be super user
372 */
373 error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
374 &l->l_acflag);
375 if (error)
376 return (error);
377 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
378 SCARG(uap, fname), l);
379 error = namei(&nd);
380 if (error)
381 return (error);
382 vp = nd.ni_vp;
383 sz = sizeof(struct compat_30_fhandle);
384 error = vfs_composefh(vp, (void *)&fh, &sz);
385 vput(vp);
386 if (error)
387 return (error);
388 error = copyout(&fh, (caddr_t)SCARG(uap, fhp),
389 sizeof(struct compat_30_fhandle));
390 return (error);
391 }
392
393 /*
394 * Open a file given a file handle.
395 *
396 * Check permissions, allocate an open file structure,
397 * and call the device open routine if any.
398 */
399 int
400 compat_30_sys_fhopen(struct lwp *l, void *v, register_t *retval)
401 {
402 struct compat_30_sys_fhopen_args /* {
403 syscallarg(const fhandle_t *) fhp;
404 syscallarg(int) flags;
405 } */ *uap = v;
406 struct filedesc *fdp = l->l_proc->p_fd;
407 struct file *fp;
408 struct vnode *vp = NULL;
409 struct mount *mp;
410 kauth_cred_t cred = l->l_cred;
411 int flags;
412 struct file *nfp;
413 int type, indx, error=0;
414 struct flock lf;
415 struct vattr va;
416 fhandle_t *fh;
417
418 /*
419 * Must be super user
420 */
421 if ((error = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
422 &l->l_acflag)))
423 return (error);
424
425 flags = FFLAGS(SCARG(uap, flags));
426 if ((flags & (FREAD | FWRITE)) == 0)
427 return (EINVAL);
428 if ((flags & O_CREAT))
429 return (EINVAL);
430 /* falloc() will use the file descriptor for us */
431 if ((error = falloc(l, &nfp, &indx)) != 0)
432 return (error);
433 fp = nfp;
434 error = vfs_copyinfh_alloc(SCARG(uap, fhp), &fh);
435 if (error != 0) {
436 goto bad;
437 }
438 error = vfs_fhtovp(fh, &vp);
439 if (error != 0) {
440 goto bad;
441 }
442
443 /* Now do an effective vn_open */
444
445 if (vp->v_type == VSOCK) {
446 error = EOPNOTSUPP;
447 goto bad;
448 }
449 if (flags & FREAD) {
450 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
451 goto bad;
452 }
453 if (flags & (FWRITE | O_TRUNC)) {
454 if (vp->v_type == VDIR) {
455 error = EISDIR;
456 goto bad;
457 }
458 if ((error = vn_writechk(vp)) != 0 ||
459 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
460 goto bad;
461 }
462 if (flags & O_TRUNC) {
463 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
464 goto bad;
465 VOP_UNLOCK(vp, 0); /* XXX */
466 VOP_LEASE(vp, l, cred, LEASE_WRITE);
467 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
468 VATTR_NULL(&va);
469 va.va_size = 0;
470 error = VOP_SETATTR(vp, &va, cred, l);
471 vn_finished_write(mp, 0);
472 if (error)
473 goto bad;
474 }
475 if ((error = VOP_OPEN(vp, flags, cred, l)) != 0)
476 goto bad;
477 if (vp->v_type == VREG &&
478 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
479 error = EIO;
480 goto bad;
481 }
482 if (flags & FWRITE)
483 vp->v_writecount++;
484
485 /* done with modified vn_open, now finish what sys_open does. */
486
487 fp->f_flag = flags & FMASK;
488 fp->f_type = DTYPE_VNODE;
489 fp->f_ops = &vnops;
490 fp->f_data = vp;
491 if (flags & (O_EXLOCK | O_SHLOCK)) {
492 lf.l_whence = SEEK_SET;
493 lf.l_start = 0;
494 lf.l_len = 0;
495 if (flags & O_EXLOCK)
496 lf.l_type = F_WRLCK;
497 else
498 lf.l_type = F_RDLCK;
499 type = F_FLOCK;
500 if ((flags & FNONBLOCK) == 0)
501 type |= F_WAIT;
502 VOP_UNLOCK(vp, 0);
503 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
504 if (error) {
505 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
506 FILE_UNUSE(fp, l);
507 ffree(fp);
508 fdremove(fdp, indx);
509 return (error);
510 }
511 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
512 fp->f_flag |= FHASLOCK;
513 }
514 VOP_UNLOCK(vp, 0);
515 *retval = indx;
516 FILE_SET_MATURE(fp);
517 FILE_UNUSE(fp, l);
518 vfs_copyinfh_free(fh);
519 return (0);
520
521 bad:
522 FILE_UNUSE(fp, l);
523 ffree(fp);
524 fdremove(fdp, indx);
525 if (vp != NULL)
526 vput(vp);
527 vfs_copyinfh_free(fh);
528 return (error);
529 }
530
531 /* ARGSUSED */
532 int
533 compat_30_sys___fhstat30(struct lwp *l, void *v, register_t *retval)
534 {
535 struct compat_30_sys___fhstat30_args /* {
536 syscallarg(const fhandle_t *) fhp;
537 syscallarg(struct stat *) sb;
538 } */ *uap = v;
539 struct stat sb;
540 int error;
541 fhandle_t *fh;
542 struct vnode *vp;
543
544 /*
545 * Must be super user
546 */
547 if ((error = kauth_authorize_generic(l->l_cred,
548 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
549 return (error);
550
551 error = vfs_copyinfh_alloc(SCARG(uap, fhp), &fh);
552 if (error != 0) {
553 goto bad;
554 }
555 error = vfs_fhtovp(fh, &vp);
556 if (error != 0) {
557 goto bad;
558 }
559 error = vn_stat(vp, &sb, l);
560 vput(vp);
561 if (error) {
562 goto bad;
563 }
564 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
565 bad:
566 vfs_copyinfh_free(fh);
567 return error;
568 }
569
570 /* ARGSUSED */
571 int
572 compat_30_sys_fhstatvfs1(struct lwp *l, void *v, register_t *retval)
573 {
574 struct compat_30_sys_fhstatvfs1_args /* {
575 syscallarg(const fhandle_t *) fhp;
576 syscallarg(struct statvfs *) buf;
577 syscallarg(int) flags;
578 } */ *uap = v;
579 struct statvfs *sb = NULL;
580 fhandle_t *fh;
581 struct mount *mp;
582 struct vnode *vp;
583 int error;
584
585 /*
586 * Must be super user
587 */
588 if ((error = kauth_authorize_generic(l->l_cred,
589 KAUTH_GENERIC_ISSUSER, &l->l_acflag)) != 0)
590 return error;
591
592 error = vfs_copyinfh_alloc(SCARG(uap, fhp), &fh);
593 if (error != 0) {
594 goto out;
595 }
596 error = vfs_fhtovp(fh, &vp);
597 if (error != 0) {
598 goto out;
599 }
600 mp = vp->v_mount;
601 sb = STATVFSBUF_GET();
602 if ((error = dostatvfs(mp, sb, l, SCARG(uap, flags), 1)) != 0) {
603 vput(vp);
604 goto out;
605 }
606 vput(vp);
607 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
608 out:
609 if (sb != NULL) {
610 STATVFSBUF_PUT(sb);
611 }
612 vfs_copyinfh_free(fh);
613 return error;
614 }
615