vfs_syscalls.c revision 1.80 1 /* $NetBSD: vfs_syscalls.c,v 1.80 1997/04/04 13:32:48 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)vfs_syscalls.c 8.28 (Berkeley) 12/10/94
41 */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
46 #include <sys/filedesc.h>
47 #include <sys/kernel.h>
48 #include <sys/file.h>
49 #include <sys/stat.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/proc.h>
53 #include <sys/uio.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56
57 #include <sys/syscallargs.h>
58
59 #include <vm/vm.h>
60 #include <sys/sysctl.h>
61
62 static int change_dir __P((struct nameidata *, struct proc *));
63
64 void checkdirs __P((struct vnode *));
65 int dounmount __P((struct mount *, int, struct proc *));
66
67 /*
68 * Virtual File System System Calls
69 */
70
71 /*
72 * Mount a file system.
73 */
74 /* ARGSUSED */
75 int
76 sys_mount(p, v, retval)
77 struct proc *p;
78 void *v;
79 register_t *retval;
80 {
81 register struct sys_mount_args /* {
82 syscallarg(const char *) type;
83 syscallarg(const char *) path;
84 syscallarg(int) flags;
85 syscallarg(void *) data;
86 } */ *uap = v;
87 register struct vnode *vp;
88 register struct mount *mp;
89 int error, flag = 0;
90 u_long fsindex = 0;
91 char fstypename[MFSNAMELEN];
92 struct vattr va;
93 struct nameidata nd;
94
95 /*
96 * Get vnode to be covered
97 */
98 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
99 SCARG(uap, path), p);
100 if ((error = namei(&nd)) != 0)
101 return (error);
102 vp = nd.ni_vp;
103 if (SCARG(uap, flags) & MNT_UPDATE) {
104 if ((vp->v_flag & VROOT) == 0) {
105 vput(vp);
106 return (EINVAL);
107 }
108 mp = vp->v_mount;
109 flag = mp->mnt_flag;
110 /*
111 * We only allow the filesystem to be reloaded if it
112 * is currently mounted read-only.
113 */
114 if ((SCARG(uap, flags) & MNT_RELOAD) &&
115 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
116 vput(vp);
117 return (EOPNOTSUPP); /* Needs translation */
118 }
119 mp->mnt_flag |=
120 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
121 /*
122 * Only root, or the user that did the original mount is
123 * permitted to update it.
124 */
125 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
126 (error = suser(p->p_ucred, &p->p_acflag))) {
127 vput(vp);
128 return (error);
129 }
130 /*
131 * Do not allow NFS export by non-root users. Silently
132 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
133 */
134 if (p->p_ucred->cr_uid != 0) {
135 if (SCARG(uap, flags) & MNT_EXPORTED) {
136 vput(vp);
137 return (EPERM);
138 }
139 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
140 }
141 VOP_UNLOCK(vp);
142 goto update;
143 }
144 /*
145 * If the user is not root, ensure that they own the directory
146 * onto which we are attempting to mount.
147 */
148 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
149 (va.va_uid != p->p_ucred->cr_uid &&
150 (error = suser(p->p_ucred, &p->p_acflag)))) {
151 vput(vp);
152 return (error);
153 }
154 /*
155 * Do not allow NFS export by non-root users. Silently
156 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
157 */
158 if (p->p_ucred->cr_uid != 0) {
159 if (SCARG(uap, flags) & MNT_EXPORTED) {
160 vput(vp);
161 return (EPERM);
162 }
163 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
164 }
165 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
166 return (error);
167 if (vp->v_type != VDIR) {
168 vput(vp);
169 return (ENOTDIR);
170 }
171 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
172 if (error) {
173 #if defined(COMPAT_09) || defined(COMPAT_43)
174 /*
175 * Historically filesystem types were identified by number.
176 * If we get an integer for the filesystem type instead of a
177 * string, we check to see if it matches one of the historic
178 * filesystem types.
179 */
180 fsindex = (u_long)SCARG(uap, type);
181 if (fsindex >= nvfssw || vfssw[fsindex] == NULL) {
182 vput(vp);
183 return (ENODEV);
184 }
185 strncpy(fstypename, vfssw[fsindex]->vfs_name, MFSNAMELEN);
186 #else
187 vput(vp);
188 return (error);
189 #endif
190 }
191 #ifdef COMPAT_10
192 /* Accept `ufs' as an alias for `ffs'. */
193 if (!strncmp(fstypename, "ufs", MFSNAMELEN))
194 strncpy(fstypename, "ffs", MFSNAMELEN);
195 #endif
196 for (fsindex = 0; fsindex < nvfssw; fsindex++)
197 if (vfssw[fsindex] != NULL &&
198 !strncmp(vfssw[fsindex]->vfs_name, fstypename, MFSNAMELEN))
199 break;
200 if (fsindex >= nvfssw) {
201 vput(vp);
202 return (ENODEV);
203 }
204 if (vp->v_mountedhere != NULL) {
205 vput(vp);
206 return (EBUSY);
207 }
208
209 /*
210 * Allocate and initialize the file system.
211 */
212 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
213 M_MOUNT, M_WAITOK);
214 bzero((char *)mp, (u_long)sizeof(struct mount));
215 mp->mnt_op = vfssw[fsindex];
216 if ((error = vfs_lock(mp)) != 0) {
217 free((caddr_t)mp, M_MOUNT);
218 vput(vp);
219 return (error);
220 }
221 /* Do this early in case we block later. */
222 vfssw[fsindex]->vfs_refcount++;
223 vp->v_mountedhere = mp;
224 mp->mnt_vnodecovered = vp;
225 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
226 update:
227 /*
228 * Set the mount level flags.
229 */
230 if (SCARG(uap, flags) & MNT_RDONLY)
231 mp->mnt_flag |= MNT_RDONLY;
232 else if (mp->mnt_flag & MNT_RDONLY)
233 mp->mnt_flag |= MNT_WANTRDWR;
234 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
235 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
236 MNT_NOATIME);
237 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
238 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
239 MNT_NOCOREDUMP | MNT_NOATIME);
240 /*
241 * Mount the filesystem.
242 */
243 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
244 if (mp->mnt_flag & MNT_UPDATE) {
245 vrele(vp);
246 if (mp->mnt_flag & MNT_WANTRDWR)
247 mp->mnt_flag &= ~MNT_RDONLY;
248 mp->mnt_flag &=~
249 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
250 if (error)
251 mp->mnt_flag = flag;
252 return (error);
253 }
254 /*
255 * Put the new filesystem on the mount list after root.
256 */
257 cache_purge(vp);
258 if (!error) {
259 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
260 checkdirs(vp);
261 VOP_UNLOCK(vp);
262 vfs_unlock(mp);
263 (void) VFS_STATFS(mp, &mp->mnt_stat, p);
264 error = VFS_START(mp, 0, p);
265 } else {
266 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
267 vfssw[fsindex]->vfs_refcount--;
268 vfs_unlock(mp);
269 free((caddr_t)mp, M_MOUNT);
270 vput(vp);
271 }
272 return (error);
273 }
274
275 /*
276 * Scan all active processes to see if any of them have a current
277 * or root directory onto which the new filesystem has just been
278 * mounted. If so, replace them with the new mount point.
279 */
280 void
281 checkdirs(olddp)
282 struct vnode *olddp;
283 {
284 struct filedesc *fdp;
285 struct vnode *newdp;
286 struct proc *p;
287
288 if (olddp->v_usecount == 1)
289 return;
290 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
291 panic("mount: lost mount");
292 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
293 fdp = p->p_fd;
294 if (fdp->fd_cdir == olddp) {
295 vrele(fdp->fd_cdir);
296 VREF(newdp);
297 fdp->fd_cdir = newdp;
298 }
299 if (fdp->fd_rdir == olddp) {
300 vrele(fdp->fd_rdir);
301 VREF(newdp);
302 fdp->fd_rdir = newdp;
303 }
304 }
305 if (rootvnode == olddp) {
306 vrele(rootvnode);
307 VREF(newdp);
308 rootvnode = newdp;
309 }
310 vput(newdp);
311 }
312
313 /*
314 * Unmount a file system.
315 *
316 * Note: unmount takes a path to the vnode mounted on as argument,
317 * not special file (as before).
318 */
319 /* ARGSUSED */
320 int
321 sys_unmount(p, v, retval)
322 struct proc *p;
323 void *v;
324 register_t *retval;
325 {
326 register struct sys_unmount_args /* {
327 syscallarg(const char *) path;
328 syscallarg(int) flags;
329 } */ *uap = v;
330 register struct vnode *vp;
331 struct mount *mp;
332 int error;
333 struct nameidata nd;
334
335 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
336 SCARG(uap, path), p);
337 if ((error = namei(&nd)) != 0)
338 return (error);
339 vp = nd.ni_vp;
340 mp = vp->v_mount;
341
342 /*
343 * Only root, or the user that did the original mount is
344 * permitted to unmount this filesystem.
345 */
346 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
347 (error = suser(p->p_ucred, &p->p_acflag))) {
348 vput(vp);
349 return (error);
350 }
351
352 /*
353 * Don't allow unmounting the root file system.
354 */
355 if (mp->mnt_flag & MNT_ROOTFS) {
356 vput(vp);
357 return (EINVAL);
358 }
359
360 /*
361 * Must be the root of the filesystem
362 */
363 if ((vp->v_flag & VROOT) == 0) {
364 vput(vp);
365 return (EINVAL);
366 }
367 vput(vp);
368
369 if (vfs_busy(mp))
370 return (EBUSY);
371
372 return (dounmount(mp, SCARG(uap, flags), p));
373 }
374
375 /*
376 * Do the actual file system unmount. File system is assumed to have been
377 * marked busy by the caller.
378 */
379 int
380 dounmount(mp, flags, p)
381 register struct mount *mp;
382 int flags;
383 struct proc *p;
384 {
385 struct vnode *coveredvp;
386 int error;
387
388 coveredvp = mp->mnt_vnodecovered;
389 mp->mnt_flag |= MNT_UNMOUNT;
390 if ((error = vfs_lock(mp)) != 0) {
391 vfs_unbusy(mp);
392 return (error);
393 }
394
395 mp->mnt_flag &=~ MNT_ASYNC;
396 vnode_pager_umount(mp); /* release cached vnodes */
397 cache_purgevfs(mp); /* remove cache entries for this file sys */
398 if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
399 (flags & MNT_FORCE))
400 error = VFS_UNMOUNT(mp, flags, p);
401 mp->mnt_flag &= ~MNT_UNMOUNT;
402 vfs_unbusy(mp);
403 if (error) {
404 vfs_unlock(mp);
405 } else {
406 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
407 if (coveredvp != NULLVP) {
408 vrele(coveredvp);
409 coveredvp->v_mountedhere = (struct mount *)0;
410 }
411 mp->mnt_op->vfs_refcount--;
412 vfs_unlock(mp);
413 if (mp->mnt_vnodelist.lh_first != NULL)
414 panic("unmount: dangling vnode");
415 free((caddr_t)mp, M_MOUNT);
416 }
417 return (error);
418 }
419
420 /*
421 * Sync each mounted filesystem.
422 */
423 #ifdef DEBUG
424 int syncprt = 0;
425 struct ctldebug debug0 = { "syncprt", &syncprt };
426 #endif
427
428 /* ARGSUSED */
429 int
430 sys_sync(p, v, retval)
431 struct proc *p;
432 void *v;
433 register_t *retval;
434 {
435 register struct mount *mp, *nmp;
436 int asyncflag;
437
438 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
439 /*
440 * Get the prev pointer in case we hang on vfs_busy
441 * while we are being unmounted.
442 */
443 nmp = mp->mnt_list.cqe_prev;
444 /*
445 * The lock check below is to avoid races with mount
446 * and unmount.
447 */
448 if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
449 !vfs_busy(mp)) {
450 asyncflag = mp->mnt_flag & MNT_ASYNC;
451 mp->mnt_flag &= ~MNT_ASYNC;
452 vnode_pager_sync(mp);
453 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
454 if (asyncflag)
455 mp->mnt_flag |= MNT_ASYNC;
456 /*
457 * Get the prev pointer again, as the prior filesystem
458 * might have been unmounted while we were sync'ing.
459 */
460 nmp = mp->mnt_list.cqe_prev;
461 vfs_unbusy(mp);
462 }
463 }
464 #ifdef DEBUG
465 if (syncprt)
466 vfs_bufstats();
467 #endif /* DEBUG */
468 return (0);
469 }
470
471 /*
472 * Change filesystem quotas.
473 */
474 /* ARGSUSED */
475 int
476 sys_quotactl(p, v, retval)
477 struct proc *p;
478 void *v;
479 register_t *retval;
480 {
481 register struct sys_quotactl_args /* {
482 syscallarg(const char *) path;
483 syscallarg(int) cmd;
484 syscallarg(int) uid;
485 syscallarg(caddr_t) arg;
486 } */ *uap = v;
487 register struct mount *mp;
488 int error;
489 struct nameidata nd;
490
491 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
492 if ((error = namei(&nd)) != 0)
493 return (error);
494 mp = nd.ni_vp->v_mount;
495 vrele(nd.ni_vp);
496 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
497 SCARG(uap, arg), p));
498 }
499
500 /*
501 * Get filesystem statistics.
502 */
503 /* ARGSUSED */
504 int
505 sys_statfs(p, v, retval)
506 struct proc *p;
507 void *v;
508 register_t *retval;
509 {
510 register struct sys_statfs_args /* {
511 syscallarg(const char *) path;
512 syscallarg(struct statfs *) buf;
513 } */ *uap = v;
514 register struct mount *mp;
515 register struct statfs *sp;
516 int error;
517 struct nameidata nd;
518
519 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
520 if ((error = namei(&nd)) != 0)
521 return (error);
522 mp = nd.ni_vp->v_mount;
523 sp = &mp->mnt_stat;
524 vrele(nd.ni_vp);
525 if ((error = VFS_STATFS(mp, sp, p)) != 0)
526 return (error);
527 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
528 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
529 }
530
531 /*
532 * Get filesystem statistics.
533 */
534 /* ARGSUSED */
535 int
536 sys_fstatfs(p, v, retval)
537 struct proc *p;
538 void *v;
539 register_t *retval;
540 {
541 register struct sys_fstatfs_args /* {
542 syscallarg(int) fd;
543 syscallarg(struct statfs *) buf;
544 } */ *uap = v;
545 struct file *fp;
546 struct mount *mp;
547 register struct statfs *sp;
548 int error;
549
550 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
551 return (error);
552 mp = ((struct vnode *)fp->f_data)->v_mount;
553 sp = &mp->mnt_stat;
554 if ((error = VFS_STATFS(mp, sp, p)) != 0)
555 return (error);
556 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
557 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
558 }
559
560 /*
561 * Get statistics on all filesystems.
562 */
563 int
564 sys_getfsstat(p, v, retval)
565 struct proc *p;
566 void *v;
567 register_t *retval;
568 {
569 register struct sys_getfsstat_args /* {
570 syscallarg(struct statfs *) buf;
571 syscallarg(long) bufsize;
572 syscallarg(int) flags;
573 } */ *uap = v;
574 register struct mount *mp, *nmp;
575 register struct statfs *sp;
576 caddr_t sfsp;
577 long count, maxcount, error;
578
579 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
580 sfsp = (caddr_t)SCARG(uap, buf);
581 for (count = 0,
582 mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
583 nmp = mp->mnt_list.cqe_next;
584 if (sfsp && count < maxcount &&
585 ((mp->mnt_flag & MNT_MLOCK) == 0)) {
586 sp = &mp->mnt_stat;
587 /*
588 * If MNT_NOWAIT is specified, do not refresh the
589 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
590 */
591 if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
592 (SCARG(uap, flags) & MNT_WAIT)) &&
593 (error = VFS_STATFS(mp, sp, p)))
594 continue;
595 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
596 error = copyout(sp, sfsp, sizeof(*sp));
597 if (error)
598 return (error);
599 sfsp += sizeof(*sp);
600 }
601 count++;
602 }
603 if (sfsp && count > maxcount)
604 *retval = maxcount;
605 else
606 *retval = count;
607 return (0);
608 }
609
610 /*
611 * Change current working directory to a given file descriptor.
612 */
613 /* ARGSUSED */
614 int
615 sys_fchdir(p, v, retval)
616 struct proc *p;
617 void *v;
618 register_t *retval;
619 {
620 struct sys_fchdir_args /* {
621 syscallarg(int) fd;
622 } */ *uap = v;
623 register struct filedesc *fdp = p->p_fd;
624 struct vnode *vp, *tdp;
625 struct mount *mp;
626 struct file *fp;
627 int error;
628
629 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
630 return (error);
631 vp = (struct vnode *)fp->f_data;
632 VREF(vp);
633 VOP_LOCK(vp);
634 if (vp->v_type != VDIR)
635 error = ENOTDIR;
636 else
637 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
638 while (!error && (mp = vp->v_mountedhere) != NULL) {
639 if (mp->mnt_flag & MNT_MLOCK) {
640 mp->mnt_flag |= MNT_MWAIT;
641 sleep((caddr_t)mp, PVFS);
642 continue;
643 }
644 if ((error = VFS_ROOT(mp, &tdp)) != 0)
645 break;
646 vput(vp);
647 vp = tdp;
648 }
649 VOP_UNLOCK(vp);
650 if (error) {
651 vrele(vp);
652 return (error);
653 }
654 vrele(fdp->fd_cdir);
655 fdp->fd_cdir = vp;
656 return (0);
657 }
658
659 /*
660 * Change current working directory (``.'').
661 */
662 /* ARGSUSED */
663 int
664 sys_chdir(p, v, retval)
665 struct proc *p;
666 void *v;
667 register_t *retval;
668 {
669 struct sys_chdir_args /* {
670 syscallarg(const char *) path;
671 } */ *uap = v;
672 register struct filedesc *fdp = p->p_fd;
673 int error;
674 struct nameidata nd;
675
676 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
677 SCARG(uap, path), p);
678 if ((error = change_dir(&nd, p)) != 0)
679 return (error);
680 vrele(fdp->fd_cdir);
681 fdp->fd_cdir = nd.ni_vp;
682 return (0);
683 }
684
685 /*
686 * Change notion of root (``/'') directory.
687 */
688 /* ARGSUSED */
689 int
690 sys_chroot(p, v, retval)
691 struct proc *p;
692 void *v;
693 register_t *retval;
694 {
695 struct sys_chroot_args /* {
696 syscallarg(const char *) path;
697 } */ *uap = v;
698 register struct filedesc *fdp = p->p_fd;
699 int error;
700 struct nameidata nd;
701
702 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
703 return (error);
704 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
705 SCARG(uap, path), p);
706 if ((error = change_dir(&nd, p)) != 0)
707 return (error);
708 if (fdp->fd_rdir != NULL)
709 vrele(fdp->fd_rdir);
710 fdp->fd_rdir = nd.ni_vp;
711 return (0);
712 }
713
714 /*
715 * Common routine for chroot and chdir.
716 */
717 static int
718 change_dir(ndp, p)
719 register struct nameidata *ndp;
720 struct proc *p;
721 {
722 struct vnode *vp;
723 int error;
724
725 if ((error = namei(ndp)) != 0)
726 return (error);
727 vp = ndp->ni_vp;
728 if (vp->v_type != VDIR)
729 error = ENOTDIR;
730 else
731 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
732 VOP_UNLOCK(vp);
733 if (error)
734 vrele(vp);
735 return (error);
736 }
737
738 /*
739 * Check permissions, allocate an open file structure,
740 * and call the device open routine if any.
741 */
742 int
743 sys_open(p, v, retval)
744 struct proc *p;
745 void *v;
746 register_t *retval;
747 {
748 register struct sys_open_args /* {
749 syscallarg(const char *) path;
750 syscallarg(int) flags;
751 syscallarg(int) mode;
752 } */ *uap = v;
753 register struct filedesc *fdp = p->p_fd;
754 register struct file *fp;
755 register struct vnode *vp;
756 int flags, cmode;
757 struct file *nfp;
758 int type, indx, error;
759 struct flock lf;
760 struct nameidata nd;
761 extern struct fileops vnops;
762
763 if ((error = falloc(p, &nfp, &indx)) != 0)
764 return (error);
765 fp = nfp;
766 flags = FFLAGS(SCARG(uap, flags));
767 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
768 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
769 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
770 if ((error = vn_open(&nd, flags, cmode)) != 0) {
771 ffree(fp);
772 if ((error == ENODEV || error == ENXIO) &&
773 p->p_dupfd >= 0 && /* XXX from fdopen */
774 (error =
775 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
776 *retval = indx;
777 return (0);
778 }
779 if (error == ERESTART)
780 error = EINTR;
781 fdp->fd_ofiles[indx] = NULL;
782 return (error);
783 }
784 p->p_dupfd = 0;
785 vp = nd.ni_vp;
786 fp->f_flag = flags & FMASK;
787 fp->f_type = DTYPE_VNODE;
788 fp->f_ops = &vnops;
789 fp->f_data = (caddr_t)vp;
790 if (flags & (O_EXLOCK | O_SHLOCK)) {
791 lf.l_whence = SEEK_SET;
792 lf.l_start = 0;
793 lf.l_len = 0;
794 if (flags & O_EXLOCK)
795 lf.l_type = F_WRLCK;
796 else
797 lf.l_type = F_RDLCK;
798 type = F_FLOCK;
799 if ((flags & FNONBLOCK) == 0)
800 type |= F_WAIT;
801 VOP_UNLOCK(vp);
802 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
803 if (error) {
804 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
805 ffree(fp);
806 fdp->fd_ofiles[indx] = NULL;
807 return (error);
808 }
809 VOP_LOCK(vp);
810 fp->f_flag |= FHASLOCK;
811 }
812 VOP_UNLOCK(vp);
813 *retval = indx;
814 return (0);
815 }
816
817 /*
818 * Create a special file.
819 */
820 /* ARGSUSED */
821 int
822 sys_mknod(p, v, retval)
823 struct proc *p;
824 void *v;
825 register_t *retval;
826 {
827 register struct sys_mknod_args /* {
828 syscallarg(const char *) path;
829 syscallarg(int) mode;
830 syscallarg(int) dev;
831 } */ *uap = v;
832 register struct vnode *vp;
833 struct vattr vattr;
834 int error;
835 int whiteout = 0;
836 struct nameidata nd;
837
838 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
839 return (error);
840 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
841 if ((error = namei(&nd)) != 0)
842 return (error);
843 vp = nd.ni_vp;
844 if (vp != NULL)
845 error = EEXIST;
846 else {
847 VATTR_NULL(&vattr);
848 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
849 vattr.va_rdev = SCARG(uap, dev);
850 whiteout = 0;
851
852 switch (SCARG(uap, mode) & S_IFMT) {
853 case S_IFMT: /* used by badsect to flag bad sectors */
854 vattr.va_type = VBAD;
855 break;
856 case S_IFCHR:
857 vattr.va_type = VCHR;
858 break;
859 case S_IFBLK:
860 vattr.va_type = VBLK;
861 break;
862 case S_IFWHT:
863 whiteout = 1;
864 break;
865 default:
866 error = EINVAL;
867 break;
868 }
869 }
870 if (!error) {
871 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
872 if (whiteout) {
873 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
874 if (error)
875 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
876 vput(nd.ni_dvp);
877 } else {
878 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
879 &nd.ni_cnd, &vattr);
880 }
881 } else {
882 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
883 if (nd.ni_dvp == vp)
884 vrele(nd.ni_dvp);
885 else
886 vput(nd.ni_dvp);
887 if (vp)
888 vrele(vp);
889 }
890 return (error);
891 }
892
893 /*
894 * Create a named pipe.
895 */
896 /* ARGSUSED */
897 int
898 sys_mkfifo(p, v, retval)
899 struct proc *p;
900 void *v;
901 register_t *retval;
902 {
903 #ifndef FIFO
904 return (EOPNOTSUPP);
905 #else
906 register struct sys_mkfifo_args /* {
907 syscallarg(const char *) path;
908 syscallarg(int) mode;
909 } */ *uap = v;
910 struct vattr vattr;
911 int error;
912 struct nameidata nd;
913
914 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
915 if ((error = namei(&nd)) != 0)
916 return (error);
917 if (nd.ni_vp != NULL) {
918 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
919 if (nd.ni_dvp == nd.ni_vp)
920 vrele(nd.ni_dvp);
921 else
922 vput(nd.ni_dvp);
923 vrele(nd.ni_vp);
924 return (EEXIST);
925 }
926 VATTR_NULL(&vattr);
927 vattr.va_type = VFIFO;
928 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
929 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
930 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
931 #endif /* FIFO */
932 }
933
934 /*
935 * Make a hard file link.
936 */
937 /* ARGSUSED */
938 int
939 sys_link(p, v, retval)
940 struct proc *p;
941 void *v;
942 register_t *retval;
943 {
944 register struct sys_link_args /* {
945 syscallarg(const char *) path;
946 syscallarg(const char *) link;
947 } */ *uap = v;
948 register struct vnode *vp;
949 struct nameidata nd;
950 int error;
951
952 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
953 if ((error = namei(&nd)) != 0)
954 return (error);
955 vp = nd.ni_vp;
956 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
957 if ((error = namei(&nd)) != 0)
958 goto out;
959 if (nd.ni_vp) {
960 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
961 if (nd.ni_dvp == nd.ni_vp)
962 vrele(nd.ni_dvp);
963 else
964 vput(nd.ni_dvp);
965 vrele(nd.ni_vp);
966 error = EEXIST;
967 goto out;
968 }
969 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
970 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
971 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
972 out:
973 vrele(vp);
974 return (error);
975 }
976
977 /*
978 * Make a symbolic link.
979 */
980 /* ARGSUSED */
981 int
982 sys_symlink(p, v, retval)
983 struct proc *p;
984 void *v;
985 register_t *retval;
986 {
987 register struct sys_symlink_args /* {
988 syscallarg(const char *) path;
989 syscallarg(const char *) link;
990 } */ *uap = v;
991 struct vattr vattr;
992 char *path;
993 int error;
994 struct nameidata nd;
995
996 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
997 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
998 if (error)
999 goto out;
1000 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1001 if ((error = namei(&nd)) != 0)
1002 goto out;
1003 if (nd.ni_vp) {
1004 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1005 if (nd.ni_dvp == nd.ni_vp)
1006 vrele(nd.ni_dvp);
1007 else
1008 vput(nd.ni_dvp);
1009 vrele(nd.ni_vp);
1010 error = EEXIST;
1011 goto out;
1012 }
1013 VATTR_NULL(&vattr);
1014 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1015 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1016 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1017 out:
1018 FREE(path, M_NAMEI);
1019 return (error);
1020 }
1021
1022 /*
1023 * Delete a whiteout from the filesystem.
1024 */
1025 /* ARGSUSED */
1026 int
1027 sys_undelete(p, v, retval)
1028 struct proc *p;
1029 void *v;
1030 register_t *retval;
1031 {
1032 register struct sys_undelete_args /* {
1033 syscallarg(const char *) path;
1034 } */ *uap = v;
1035 int error;
1036 struct nameidata nd;
1037
1038 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1039 SCARG(uap, path), p);
1040 error = namei(&nd);
1041 if (error)
1042 return (error);
1043
1044 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1045 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1046 if (nd.ni_dvp == nd.ni_vp)
1047 vrele(nd.ni_dvp);
1048 else
1049 vput(nd.ni_dvp);
1050 if (nd.ni_vp)
1051 vrele(nd.ni_vp);
1052 return (EEXIST);
1053 }
1054
1055 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1056 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1057 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1058 vput(nd.ni_dvp);
1059 return (error);
1060 }
1061
1062 /*
1063 * Delete a name from the filesystem.
1064 */
1065 /* ARGSUSED */
1066 int
1067 sys_unlink(p, v, retval)
1068 struct proc *p;
1069 void *v;
1070 register_t *retval;
1071 {
1072 struct sys_unlink_args /* {
1073 syscallarg(const char *) path;
1074 } */ *uap = v;
1075 register struct vnode *vp;
1076 int error;
1077 struct nameidata nd;
1078
1079 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1080 SCARG(uap, path), p);
1081 if ((error = namei(&nd)) != 0)
1082 return (error);
1083 vp = nd.ni_vp;
1084
1085 /*
1086 * The root of a mounted filesystem cannot be deleted.
1087 */
1088 if (vp->v_flag & VROOT) {
1089 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1090 if (nd.ni_dvp == vp)
1091 vrele(nd.ni_dvp);
1092 else
1093 vput(nd.ni_dvp);
1094 vput(vp);
1095 error = EBUSY;
1096 goto out;
1097 }
1098
1099 (void)vnode_pager_uncache(vp);
1100
1101 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1102 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1103 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1104 out:
1105 return (error);
1106 }
1107
1108 /*
1109 * Reposition read/write file offset.
1110 */
1111 int
1112 sys_lseek(p, v, retval)
1113 struct proc *p;
1114 void *v;
1115 register_t *retval;
1116 {
1117 register struct sys_lseek_args /* {
1118 syscallarg(int) fd;
1119 syscallarg(int) pad;
1120 syscallarg(off_t) offset;
1121 syscallarg(int) whence;
1122 } */ *uap = v;
1123 struct ucred *cred = p->p_ucred;
1124 register struct filedesc *fdp = p->p_fd;
1125 register struct file *fp;
1126 struct vattr vattr;
1127 int error;
1128
1129 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1130 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1131 return (EBADF);
1132 if (fp->f_type != DTYPE_VNODE
1133 #ifdef FIFO
1134 || ((struct vnode *)fp->f_data)->v_type == VFIFO
1135 #endif
1136 )
1137 return (ESPIPE);
1138 switch (SCARG(uap, whence)) {
1139 case L_INCR:
1140 fp->f_offset += SCARG(uap, offset);
1141 break;
1142 case L_XTND:
1143 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr,
1144 cred, p);
1145 if (error)
1146 return (error);
1147 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1148 break;
1149 case L_SET:
1150 fp->f_offset = SCARG(uap, offset);
1151 break;
1152 default:
1153 return (EINVAL);
1154 }
1155 *(off_t *)retval = fp->f_offset;
1156 return (0);
1157 }
1158
1159 /*
1160 * Check access permissions.
1161 */
1162 int
1163 sys_access(p, v, retval)
1164 struct proc *p;
1165 void *v;
1166 register_t *retval;
1167 {
1168 register struct sys_access_args /* {
1169 syscallarg(const char *) path;
1170 syscallarg(int) flags;
1171 } */ *uap = v;
1172 register struct ucred *cred = p->p_ucred;
1173 register struct vnode *vp;
1174 int error, flags, t_gid, t_uid;
1175 struct nameidata nd;
1176
1177 t_uid = cred->cr_uid;
1178 t_gid = cred->cr_gid;
1179 cred->cr_uid = p->p_cred->p_ruid;
1180 cred->cr_gid = p->p_cred->p_rgid;
1181 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1182 SCARG(uap, path), p);
1183 if ((error = namei(&nd)) != 0)
1184 goto out1;
1185 vp = nd.ni_vp;
1186
1187 /* Flags == 0 means only check for existence. */
1188 if (SCARG(uap, flags)) {
1189 flags = 0;
1190 if (SCARG(uap, flags) & R_OK)
1191 flags |= VREAD;
1192 if (SCARG(uap, flags) & W_OK)
1193 flags |= VWRITE;
1194 if (SCARG(uap, flags) & X_OK)
1195 flags |= VEXEC;
1196 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1197 error = VOP_ACCESS(vp, flags, cred, p);
1198 }
1199 vput(vp);
1200 out1:
1201 cred->cr_uid = t_uid;
1202 cred->cr_gid = t_gid;
1203 return (error);
1204 }
1205
1206 /*
1207 * Get file status; this version follows links.
1208 */
1209 /* ARGSUSED */
1210 int
1211 sys_stat(p, v, retval)
1212 struct proc *p;
1213 void *v;
1214 register_t *retval;
1215 {
1216 register struct sys_stat_args /* {
1217 syscallarg(const char *) path;
1218 syscallarg(struct stat *) ub;
1219 } */ *uap = v;
1220 struct stat sb;
1221 int error;
1222 struct nameidata nd;
1223
1224 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1225 SCARG(uap, path), p);
1226 if ((error = namei(&nd)) != 0)
1227 return (error);
1228 error = vn_stat(nd.ni_vp, &sb, p);
1229 vput(nd.ni_vp);
1230 if (error)
1231 return (error);
1232 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1233 return (error);
1234 }
1235
1236 /*
1237 * Get file status; this version does not follow links.
1238 */
1239 /* ARGSUSED */
1240 int
1241 sys_lstat(p, v, retval)
1242 struct proc *p;
1243 void *v;
1244 register_t *retval;
1245 {
1246 register struct sys_lstat_args /* {
1247 syscallarg(const char *) path;
1248 syscallarg(struct stat *) ub;
1249 } */ *uap = v;
1250 struct stat sb;
1251 int error;
1252 struct nameidata nd;
1253
1254 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1255 SCARG(uap, path), p);
1256 if ((error = namei(&nd)) != 0)
1257 return (error);
1258 error = vn_stat(nd.ni_vp, &sb, p);
1259 vput(nd.ni_vp);
1260 if (error)
1261 return (error);
1262 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1263 return (error);
1264 }
1265
1266 /*
1267 * Get configurable pathname variables.
1268 */
1269 /* ARGSUSED */
1270 int
1271 sys_pathconf(p, v, retval)
1272 struct proc *p;
1273 void *v;
1274 register_t *retval;
1275 {
1276 register struct sys_pathconf_args /* {
1277 syscallarg(const char *) path;
1278 syscallarg(int) name;
1279 } */ *uap = v;
1280 int error;
1281 struct nameidata nd;
1282
1283 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1284 SCARG(uap, path), p);
1285 if ((error = namei(&nd)) != 0)
1286 return (error);
1287 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1288 vput(nd.ni_vp);
1289 return (error);
1290 }
1291
1292 /*
1293 * Return target name of a symbolic link.
1294 */
1295 /* ARGSUSED */
1296 int
1297 sys_readlink(p, v, retval)
1298 struct proc *p;
1299 void *v;
1300 register_t *retval;
1301 {
1302 register struct sys_readlink_args /* {
1303 syscallarg(const char *) path;
1304 syscallarg(char *) buf;
1305 syscallarg(int) count;
1306 } */ *uap = v;
1307 register struct vnode *vp;
1308 struct iovec aiov;
1309 struct uio auio;
1310 int error;
1311 struct nameidata nd;
1312
1313 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1314 SCARG(uap, path), p);
1315 if ((error = namei(&nd)) != 0)
1316 return (error);
1317 vp = nd.ni_vp;
1318 if (vp->v_type != VLNK)
1319 error = EINVAL;
1320 else {
1321 aiov.iov_base = SCARG(uap, buf);
1322 aiov.iov_len = SCARG(uap, count);
1323 auio.uio_iov = &aiov;
1324 auio.uio_iovcnt = 1;
1325 auio.uio_offset = 0;
1326 auio.uio_rw = UIO_READ;
1327 auio.uio_segflg = UIO_USERSPACE;
1328 auio.uio_procp = p;
1329 auio.uio_resid = SCARG(uap, count);
1330 error = VOP_READLINK(vp, &auio, p->p_ucred);
1331 }
1332 vput(vp);
1333 *retval = SCARG(uap, count) - auio.uio_resid;
1334 return (error);
1335 }
1336
1337 /*
1338 * Change flags of a file given a path name.
1339 */
1340 /* ARGSUSED */
1341 int
1342 sys_chflags(p, v, retval)
1343 struct proc *p;
1344 void *v;
1345 register_t *retval;
1346 {
1347 register struct sys_chflags_args /* {
1348 syscallarg(const char *) path;
1349 syscallarg(u_long) flags;
1350 } */ *uap = v;
1351 register struct vnode *vp;
1352 struct vattr vattr;
1353 int error;
1354 struct nameidata nd;
1355
1356 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1357 if ((error = namei(&nd)) != 0)
1358 return (error);
1359 vp = nd.ni_vp;
1360 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1361 VOP_LOCK(vp);
1362 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1363 error = EROFS;
1364 else {
1365 VATTR_NULL(&vattr);
1366 vattr.va_flags = SCARG(uap, flags);
1367 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1368 }
1369 vput(vp);
1370 return (error);
1371 }
1372
1373 /*
1374 * Change flags of a file given a file descriptor.
1375 */
1376 /* ARGSUSED */
1377 int
1378 sys_fchflags(p, v, retval)
1379 struct proc *p;
1380 void *v;
1381 register_t *retval;
1382 {
1383 register struct sys_fchflags_args /* {
1384 syscallarg(int) fd;
1385 syscallarg(u_long) flags;
1386 } */ *uap = v;
1387 struct vattr vattr;
1388 struct vnode *vp;
1389 struct file *fp;
1390 int error;
1391
1392 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1393 return (error);
1394 vp = (struct vnode *)fp->f_data;
1395 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1396 VOP_LOCK(vp);
1397 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1398 error = EROFS;
1399 else {
1400 VATTR_NULL(&vattr);
1401 vattr.va_flags = SCARG(uap, flags);
1402 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1403 }
1404 VOP_UNLOCK(vp);
1405 return (error);
1406 }
1407
1408 /*
1409 * Change mode of a file given path name.
1410 */
1411 /* ARGSUSED */
1412 int
1413 sys_chmod(p, v, retval)
1414 struct proc *p;
1415 void *v;
1416 register_t *retval;
1417 {
1418 register struct sys_chmod_args /* {
1419 syscallarg(const char *) path;
1420 syscallarg(int) mode;
1421 } */ *uap = v;
1422 register struct vnode *vp;
1423 struct vattr vattr;
1424 int error;
1425 struct nameidata nd;
1426
1427 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1428 if ((error = namei(&nd)) != 0)
1429 return (error);
1430 vp = nd.ni_vp;
1431 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1432 VOP_LOCK(vp);
1433 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1434 error = EROFS;
1435 else {
1436 VATTR_NULL(&vattr);
1437 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1438 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1439 }
1440 vput(vp);
1441 return (error);
1442 }
1443
1444 /*
1445 * Change mode of a file given a file descriptor.
1446 */
1447 /* ARGSUSED */
1448 int
1449 sys_fchmod(p, v, retval)
1450 struct proc *p;
1451 void *v;
1452 register_t *retval;
1453 {
1454 register struct sys_fchmod_args /* {
1455 syscallarg(int) fd;
1456 syscallarg(int) mode;
1457 } */ *uap = v;
1458 struct vattr vattr;
1459 struct vnode *vp;
1460 struct file *fp;
1461 int error;
1462
1463 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1464 return (error);
1465 vp = (struct vnode *)fp->f_data;
1466 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1467 VOP_LOCK(vp);
1468 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1469 error = EROFS;
1470 else {
1471 VATTR_NULL(&vattr);
1472 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1473 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1474 }
1475 VOP_UNLOCK(vp);
1476 return (error);
1477 }
1478
1479 /*
1480 * Set ownership given a path name.
1481 */
1482 /* ARGSUSED */
1483 int
1484 sys_chown(p, v, retval)
1485 struct proc *p;
1486 void *v;
1487 register_t *retval;
1488 {
1489 register struct sys_chown_args /* {
1490 syscallarg(const char *) path;
1491 syscallarg(uid_t) uid;
1492 syscallarg(gid_t) gid;
1493 } */ *uap = v;
1494 register struct vnode *vp;
1495 struct vattr vattr;
1496 int error;
1497 struct nameidata nd;
1498
1499 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1500 if ((error = namei(&nd)) != 0)
1501 return (error);
1502 vp = nd.ni_vp;
1503 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1504 VOP_LOCK(vp);
1505 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1506 error = EROFS;
1507 else {
1508 VATTR_NULL(&vattr);
1509 vattr.va_uid = SCARG(uap, uid);
1510 vattr.va_gid = SCARG(uap, gid);
1511 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1512 }
1513 vput(vp);
1514 return (error);
1515 }
1516
1517 /*
1518 * Set ownership given a file descriptor.
1519 */
1520 /* ARGSUSED */
1521 int
1522 sys_fchown(p, v, retval)
1523 struct proc *p;
1524 void *v;
1525 register_t *retval;
1526 {
1527 register struct sys_fchown_args /* {
1528 syscallarg(int) fd;
1529 syscallarg(uid_t) uid;
1530 syscallarg(gid_t) gid;
1531 } */ *uap = v;
1532 register struct vnode *vp;
1533 struct vattr vattr;
1534 int error;
1535 struct file *fp;
1536
1537 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1538 return (error);
1539 vp = (struct vnode *)fp->f_data;
1540 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1541 VOP_LOCK(vp);
1542 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1543 error = EROFS;
1544 else {
1545 VATTR_NULL(&vattr);
1546 vattr.va_uid = SCARG(uap, uid);
1547 vattr.va_gid = SCARG(uap, gid);
1548 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1549 }
1550 VOP_UNLOCK(vp);
1551 return (error);
1552 }
1553
1554 /*
1555 * Set the access and modification times given a path name.
1556 */
1557 /* ARGSUSED */
1558 int
1559 sys_utimes(p, v, retval)
1560 struct proc *p;
1561 void *v;
1562 register_t *retval;
1563 {
1564 register struct sys_utimes_args /* {
1565 syscallarg(const char *) path;
1566 syscallarg(const struct timeval *) tptr;
1567 } */ *uap = v;
1568 register struct vnode *vp;
1569 struct timeval tv[2];
1570 struct vattr vattr;
1571 int error;
1572 struct nameidata nd;
1573
1574 VATTR_NULL(&vattr);
1575 if (SCARG(uap, tptr) == NULL) {
1576 microtime(&tv[0]);
1577 tv[1] = tv[0];
1578 vattr.va_vaflags |= VA_UTIMES_NULL;
1579 } else {
1580 error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
1581 if (error)
1582 return (error);
1583 }
1584 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1585 if ((error = namei(&nd)) != 0)
1586 return (error);
1587 vp = nd.ni_vp;
1588 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1589 VOP_LOCK(vp);
1590 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1591 error = EROFS;
1592 else {
1593 vattr.va_atime.tv_sec = tv[0].tv_sec;
1594 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1595 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1596 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1597 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1598 }
1599 vput(vp);
1600 return (error);
1601 }
1602
1603 /*
1604 * Set the access and modification times given a file descriptor.
1605 */
1606 /* ARGSUSED */
1607 int
1608 sys_futimes(p, v, retval)
1609 struct proc *p;
1610 void *v;
1611 register_t *retval;
1612 {
1613 register struct sys_futimes_args /* {
1614 syscallarg(int) fd;
1615 syscallarg(const struct timeval *) tptr;
1616 } */ *uap = v;
1617 register struct vnode *vp;
1618 struct timeval tv[2];
1619 struct vattr vattr;
1620 int error;
1621 struct file *fp;
1622
1623 VATTR_NULL(&vattr);
1624 if (SCARG(uap, tptr) == NULL) {
1625 microtime(&tv[0]);
1626 tv[1] = tv[0];
1627 vattr.va_vaflags |= VA_UTIMES_NULL;
1628 } else {
1629 error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
1630 if (error)
1631 return (error);
1632 }
1633 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1634 return (error);
1635 vp = (struct vnode *)fp->f_data;
1636 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1637 VOP_LOCK(vp);
1638 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1639 error = EROFS;
1640 else {
1641 vattr.va_atime.tv_sec = tv[0].tv_sec;
1642 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1643 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1644 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1645 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1646 }
1647 VOP_UNLOCK(vp);
1648 return (error);
1649 }
1650
1651 /*
1652 * Truncate a file given its path name.
1653 */
1654 /* ARGSUSED */
1655 int
1656 sys_truncate(p, v, retval)
1657 struct proc *p;
1658 void *v;
1659 register_t *retval;
1660 {
1661 register struct sys_truncate_args /* {
1662 syscallarg(const char *) path;
1663 syscallarg(int) pad;
1664 syscallarg(off_t) length;
1665 } */ *uap = v;
1666 register struct vnode *vp;
1667 struct vattr vattr;
1668 int error;
1669 struct nameidata nd;
1670
1671 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1672 if ((error = namei(&nd)) != 0)
1673 return (error);
1674 vp = nd.ni_vp;
1675 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1676 VOP_LOCK(vp);
1677 if (vp->v_type == VDIR)
1678 error = EISDIR;
1679 else if ((error = vn_writechk(vp)) == 0 &&
1680 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1681 VATTR_NULL(&vattr);
1682 vattr.va_size = SCARG(uap, length);
1683 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1684 }
1685 vput(vp);
1686 return (error);
1687 }
1688
1689 /*
1690 * Truncate a file given a file descriptor.
1691 */
1692 /* ARGSUSED */
1693 int
1694 sys_ftruncate(p, v, retval)
1695 struct proc *p;
1696 void *v;
1697 register_t *retval;
1698 {
1699 register struct sys_ftruncate_args /* {
1700 syscallarg(int) fd;
1701 syscallarg(int) pad;
1702 syscallarg(off_t) length;
1703 } */ *uap = v;
1704 struct vattr vattr;
1705 struct vnode *vp;
1706 struct file *fp;
1707 int error;
1708
1709 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1710 return (error);
1711 if ((fp->f_flag & FWRITE) == 0)
1712 return (EINVAL);
1713 vp = (struct vnode *)fp->f_data;
1714 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1715 VOP_LOCK(vp);
1716 if (vp->v_type == VDIR)
1717 error = EISDIR;
1718 else if ((error = vn_writechk(vp)) == 0) {
1719 VATTR_NULL(&vattr);
1720 vattr.va_size = SCARG(uap, length);
1721 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1722 }
1723 VOP_UNLOCK(vp);
1724 return (error);
1725 }
1726
1727 /*
1728 * Sync an open file.
1729 */
1730 /* ARGSUSED */
1731 int
1732 sys_fsync(p, v, retval)
1733 struct proc *p;
1734 void *v;
1735 register_t *retval;
1736 {
1737 struct sys_fsync_args /* {
1738 syscallarg(int) fd;
1739 } */ *uap = v;
1740 register struct vnode *vp;
1741 struct file *fp;
1742 int error;
1743
1744 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1745 return (error);
1746 vp = (struct vnode *)fp->f_data;
1747 VOP_LOCK(vp);
1748 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1749 VOP_UNLOCK(vp);
1750 return (error);
1751 }
1752
1753 /*
1754 * Rename files. Source and destination must either both be directories,
1755 * or both not be directories. If target is a directory, it must be empty.
1756 */
1757 /* ARGSUSED */
1758 int
1759 sys_rename(p, v, retval)
1760 struct proc *p;
1761 void *v;
1762 register_t *retval;
1763 {
1764 register struct sys_rename_args /* {
1765 syscallarg(const char *) from;
1766 syscallarg(const char *) to;
1767 } */ *uap = v;
1768 register struct vnode *tvp, *fvp, *tdvp;
1769 struct nameidata fromnd, tond;
1770 int error;
1771
1772 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1773 SCARG(uap, from), p);
1774 if ((error = namei(&fromnd)) != 0)
1775 return (error);
1776 fvp = fromnd.ni_vp;
1777 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1778 UIO_USERSPACE, SCARG(uap, to), p);
1779 if ((error = namei(&tond)) != 0) {
1780 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1781 vrele(fromnd.ni_dvp);
1782 vrele(fvp);
1783 goto out1;
1784 }
1785 tdvp = tond.ni_dvp;
1786 tvp = tond.ni_vp;
1787 /*
1788 * If source and destination refer to the same object, do nothing.
1789 */
1790 if (fvp == tvp) {
1791 error = -1;
1792 goto out;
1793 }
1794 if (tvp != NULL) {
1795 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1796 error = ENOTDIR;
1797 goto out;
1798 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1799 error = EISDIR;
1800 goto out;
1801 }
1802 }
1803 if (fvp == tdvp)
1804 error = EINVAL;
1805 out:
1806 if (!error) {
1807 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1808 if (fromnd.ni_dvp != tdvp)
1809 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1810 if (tvp) {
1811 (void)vnode_pager_uncache(tvp);
1812 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1813 }
1814 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1815 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1816 } else {
1817 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1818 if (tdvp == tvp)
1819 vrele(tdvp);
1820 else
1821 vput(tdvp);
1822 if (tvp)
1823 vput(tvp);
1824 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1825 vrele(fromnd.ni_dvp);
1826 vrele(fvp);
1827 }
1828 vrele(tond.ni_startdir);
1829 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1830 out1:
1831 if (fromnd.ni_startdir)
1832 vrele(fromnd.ni_startdir);
1833 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1834 return (error == -1 ? 0 : error);
1835 }
1836
1837 /*
1838 * Make a directory file.
1839 */
1840 /* ARGSUSED */
1841 int
1842 sys_mkdir(p, v, retval)
1843 struct proc *p;
1844 void *v;
1845 register_t *retval;
1846 {
1847 register struct sys_mkdir_args /* {
1848 syscallarg(const char *) path;
1849 syscallarg(int) mode;
1850 } */ *uap = v;
1851 register struct vnode *vp;
1852 struct vattr vattr;
1853 int error;
1854 struct nameidata nd;
1855
1856 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1857 if ((error = namei(&nd)) != 0)
1858 return (error);
1859 vp = nd.ni_vp;
1860 if (vp != NULL) {
1861 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1862 if (nd.ni_dvp == vp)
1863 vrele(nd.ni_dvp);
1864 else
1865 vput(nd.ni_dvp);
1866 vrele(vp);
1867 return (EEXIST);
1868 }
1869 VATTR_NULL(&vattr);
1870 vattr.va_type = VDIR;
1871 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
1872 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1873 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1874 if (!error)
1875 vput(nd.ni_vp);
1876 return (error);
1877 }
1878
1879 /*
1880 * Remove a directory file.
1881 */
1882 /* ARGSUSED */
1883 int
1884 sys_rmdir(p, v, retval)
1885 struct proc *p;
1886 void *v;
1887 register_t *retval;
1888 {
1889 struct sys_rmdir_args /* {
1890 syscallarg(const char *) path;
1891 } */ *uap = v;
1892 register struct vnode *vp;
1893 int error;
1894 struct nameidata nd;
1895
1896 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1897 SCARG(uap, path), p);
1898 if ((error = namei(&nd)) != 0)
1899 return (error);
1900 vp = nd.ni_vp;
1901 if (vp->v_type != VDIR) {
1902 error = ENOTDIR;
1903 goto out;
1904 }
1905 /*
1906 * No rmdir "." please.
1907 */
1908 if (nd.ni_dvp == vp) {
1909 error = EINVAL;
1910 goto out;
1911 }
1912 /*
1913 * The root of a mounted filesystem cannot be deleted.
1914 */
1915 if (vp->v_flag & VROOT)
1916 error = EBUSY;
1917 out:
1918 if (!error) {
1919 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1920 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1921 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1922 } else {
1923 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1924 if (nd.ni_dvp == vp)
1925 vrele(nd.ni_dvp);
1926 else
1927 vput(nd.ni_dvp);
1928 vput(vp);
1929 }
1930 return (error);
1931 }
1932
1933 /*
1934 * Read a block of directory entries in a file system independent format.
1935 */
1936 int
1937 sys_getdirentries(p, v, retval)
1938 struct proc *p;
1939 void *v;
1940 register_t *retval;
1941 {
1942 register struct sys_getdirentries_args /* {
1943 syscallarg(int) fd;
1944 syscallarg(char *) buf;
1945 syscallarg(u_int) count;
1946 syscallarg(long *) basep;
1947 } */ *uap = v;
1948 register struct vnode *vp;
1949 struct file *fp;
1950 struct uio auio;
1951 struct iovec aiov;
1952 long loff;
1953 int error, eofflag;
1954
1955 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1956 return (error);
1957 if ((fp->f_flag & FREAD) == 0)
1958 return (EBADF);
1959 vp = (struct vnode *)fp->f_data;
1960 unionread:
1961 if (vp->v_type != VDIR)
1962 return (EINVAL);
1963 aiov.iov_base = SCARG(uap, buf);
1964 aiov.iov_len = SCARG(uap, count);
1965 auio.uio_iov = &aiov;
1966 auio.uio_iovcnt = 1;
1967 auio.uio_rw = UIO_READ;
1968 auio.uio_segflg = UIO_USERSPACE;
1969 auio.uio_procp = p;
1970 auio.uio_resid = SCARG(uap, count);
1971 VOP_LOCK(vp);
1972 loff = auio.uio_offset = fp->f_offset;
1973 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
1974 fp->f_offset = auio.uio_offset;
1975 VOP_UNLOCK(vp);
1976 if (error)
1977 return (error);
1978
1979 #ifdef UNION
1980 {
1981 extern int (**union_vnodeop_p) __P((void *));
1982 extern struct vnode *union_dircache __P((struct vnode *));
1983
1984 if ((SCARG(uap, count) == auio.uio_resid) &&
1985 (vp->v_op == union_vnodeop_p)) {
1986 struct vnode *lvp;
1987
1988 lvp = union_dircache(vp);
1989 if (lvp != NULLVP) {
1990 struct vattr va;
1991
1992 /*
1993 * If the directory is opaque,
1994 * then don't show lower entries
1995 */
1996 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
1997 if (va.va_flags & OPAQUE) {
1998 vput(lvp);
1999 lvp = NULL;
2000 }
2001 }
2002
2003 if (lvp != NULLVP) {
2004 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2005 VOP_UNLOCK(lvp);
2006
2007 if (error) {
2008 vrele(lvp);
2009 return (error);
2010 }
2011 fp->f_data = (caddr_t) lvp;
2012 fp->f_offset = 0;
2013 error = vn_close(vp, FREAD, fp->f_cred, p);
2014 if (error)
2015 return (error);
2016 vp = lvp;
2017 goto unionread;
2018 }
2019 }
2020 }
2021 #endif /* UNION */
2022
2023 if ((SCARG(uap, count) == auio.uio_resid) &&
2024 (vp->v_flag & VROOT) &&
2025 (vp->v_mount->mnt_flag & MNT_UNION)) {
2026 struct vnode *tvp = vp;
2027 vp = vp->v_mount->mnt_vnodecovered;
2028 VREF(vp);
2029 fp->f_data = (caddr_t) vp;
2030 fp->f_offset = 0;
2031 vrele(tvp);
2032 goto unionread;
2033 }
2034 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
2035 *retval = SCARG(uap, count) - auio.uio_resid;
2036 return (error);
2037 }
2038
2039 /*
2040 * Set the mode mask for creation of filesystem nodes.
2041 */
2042 int
2043 sys_umask(p, v, retval)
2044 struct proc *p;
2045 void *v;
2046 register_t *retval;
2047 {
2048 struct sys_umask_args /* {
2049 syscallarg(int) newmask;
2050 } */ *uap = v;
2051 register struct filedesc *fdp;
2052
2053 fdp = p->p_fd;
2054 *retval = fdp->fd_cmask;
2055 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2056 return (0);
2057 }
2058
2059 /*
2060 * Void all references to file by ripping underlying filesystem
2061 * away from vnode.
2062 */
2063 /* ARGSUSED */
2064 int
2065 sys_revoke(p, v, retval)
2066 struct proc *p;
2067 void *v;
2068 register_t *retval;
2069 {
2070 register struct sys_revoke_args /* {
2071 syscallarg(const char *) path;
2072 } */ *uap = v;
2073 register struct vnode *vp;
2074 struct vattr vattr;
2075 int error;
2076 struct nameidata nd;
2077
2078 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2079 if ((error = namei(&nd)) != 0)
2080 return (error);
2081 vp = nd.ni_vp;
2082 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2083 error = EINVAL;
2084 goto out;
2085 }
2086 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2087 goto out;
2088 if (p->p_ucred->cr_uid != vattr.va_uid &&
2089 (error = suser(p->p_ucred, &p->p_acflag)))
2090 goto out;
2091 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2092 vgoneall(vp);
2093 out:
2094 vrele(vp);
2095 return (error);
2096 }
2097
2098 /*
2099 * Convert a user file descriptor to a kernel file entry.
2100 */
2101 int
2102 getvnode(fdp, fd, fpp)
2103 struct filedesc *fdp;
2104 int fd;
2105 struct file **fpp;
2106 {
2107 struct vnode *vp;
2108 struct file *fp;
2109
2110 if ((u_int)fd >= fdp->fd_nfiles ||
2111 (fp = fdp->fd_ofiles[fd]) == NULL)
2112 return (EBADF);
2113 if (fp->f_type != DTYPE_VNODE)
2114 return (EINVAL);
2115 vp = (struct vnode *)fp->f_data;
2116 if (vp->v_type == VBAD)
2117 return (EBADF);
2118 *fpp = fp;
2119 return (0);
2120 }
2121