vfs_syscalls.c revision 1.84 1 /* $NetBSD: vfs_syscalls.c,v 1.84 1997/04/11 22:03:58 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 vnode *vp;
1127 struct vattr vattr;
1128 register off_t newoff;
1129 int error;
1130
1131 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1132 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1133 return (EBADF);
1134
1135 vp = (struct vnode *)fp->f_data;
1136 if (fp->f_type != DTYPE_VNODE
1137 #ifdef FIFO
1138 || vp->v_type == VFIFO
1139 #endif
1140 )
1141 return (ESPIPE);
1142
1143 switch (SCARG(uap, whence)) {
1144 case L_INCR:
1145 newoff = fp->f_offset + SCARG(uap, offset);
1146 break;
1147 case L_XTND:
1148 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr,
1149 cred, p);
1150 if (error)
1151 return (error);
1152 newoff = SCARG(uap, offset) + vattr.va_size;
1153 break;
1154 case L_SET:
1155 newoff = SCARG(uap, offset);
1156 break;
1157 default:
1158 return (EINVAL);
1159 }
1160 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1161 return (error);
1162
1163 *(off_t *)retval = fp->f_offset = newoff;
1164 return (0);
1165 }
1166
1167 /*
1168 * Check access permissions.
1169 */
1170 int
1171 sys_access(p, v, retval)
1172 struct proc *p;
1173 void *v;
1174 register_t *retval;
1175 {
1176 register struct sys_access_args /* {
1177 syscallarg(const char *) path;
1178 syscallarg(int) flags;
1179 } */ *uap = v;
1180 register struct ucred *cred = p->p_ucred;
1181 register struct vnode *vp;
1182 int error, flags, t_gid, t_uid;
1183 struct nameidata nd;
1184
1185 t_uid = cred->cr_uid;
1186 t_gid = cred->cr_gid;
1187 cred->cr_uid = p->p_cred->p_ruid;
1188 cred->cr_gid = p->p_cred->p_rgid;
1189 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1190 SCARG(uap, path), p);
1191 if ((error = namei(&nd)) != 0)
1192 goto out1;
1193 vp = nd.ni_vp;
1194
1195 /* Flags == 0 means only check for existence. */
1196 if (SCARG(uap, flags)) {
1197 flags = 0;
1198 if (SCARG(uap, flags) & R_OK)
1199 flags |= VREAD;
1200 if (SCARG(uap, flags) & W_OK)
1201 flags |= VWRITE;
1202 if (SCARG(uap, flags) & X_OK)
1203 flags |= VEXEC;
1204 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1205 error = VOP_ACCESS(vp, flags, cred, p);
1206 }
1207 vput(vp);
1208 out1:
1209 cred->cr_uid = t_uid;
1210 cred->cr_gid = t_gid;
1211 return (error);
1212 }
1213
1214 /*
1215 * Get file status; this version follows links.
1216 */
1217 /* ARGSUSED */
1218 int
1219 sys_stat(p, v, retval)
1220 struct proc *p;
1221 void *v;
1222 register_t *retval;
1223 {
1224 register struct sys_stat_args /* {
1225 syscallarg(const char *) path;
1226 syscallarg(struct stat *) ub;
1227 } */ *uap = v;
1228 struct stat sb;
1229 int error;
1230 struct nameidata nd;
1231
1232 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1233 SCARG(uap, path), p);
1234 if ((error = namei(&nd)) != 0)
1235 return (error);
1236 error = vn_stat(nd.ni_vp, &sb, p);
1237 vput(nd.ni_vp);
1238 if (error)
1239 return (error);
1240 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1241 return (error);
1242 }
1243
1244 /*
1245 * Get file status; this version does not follow links.
1246 */
1247 /* ARGSUSED */
1248 int
1249 sys_lstat(p, v, retval)
1250 struct proc *p;
1251 void *v;
1252 register_t *retval;
1253 {
1254 register struct sys_lstat_args /* {
1255 syscallarg(const char *) path;
1256 syscallarg(struct stat *) ub;
1257 } */ *uap = v;
1258 struct stat sb;
1259 int error;
1260 struct nameidata nd;
1261
1262 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1263 SCARG(uap, path), p);
1264 if ((error = namei(&nd)) != 0)
1265 return (error);
1266 error = vn_stat(nd.ni_vp, &sb, p);
1267 vput(nd.ni_vp);
1268 if (error)
1269 return (error);
1270 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1271 return (error);
1272 }
1273
1274 /*
1275 * Get configurable pathname variables.
1276 */
1277 /* ARGSUSED */
1278 int
1279 sys_pathconf(p, v, retval)
1280 struct proc *p;
1281 void *v;
1282 register_t *retval;
1283 {
1284 register struct sys_pathconf_args /* {
1285 syscallarg(const char *) path;
1286 syscallarg(int) name;
1287 } */ *uap = v;
1288 int error;
1289 struct nameidata nd;
1290
1291 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1292 SCARG(uap, path), p);
1293 if ((error = namei(&nd)) != 0)
1294 return (error);
1295 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1296 vput(nd.ni_vp);
1297 return (error);
1298 }
1299
1300 /*
1301 * Return target name of a symbolic link.
1302 */
1303 /* ARGSUSED */
1304 int
1305 sys_readlink(p, v, retval)
1306 struct proc *p;
1307 void *v;
1308 register_t *retval;
1309 {
1310 register struct sys_readlink_args /* {
1311 syscallarg(const char *) path;
1312 syscallarg(char *) buf;
1313 syscallarg(int) count;
1314 } */ *uap = v;
1315 register struct vnode *vp;
1316 struct iovec aiov;
1317 struct uio auio;
1318 int error;
1319 struct nameidata nd;
1320
1321 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1322 SCARG(uap, path), p);
1323 if ((error = namei(&nd)) != 0)
1324 return (error);
1325 vp = nd.ni_vp;
1326 if (vp->v_type != VLNK)
1327 error = EINVAL;
1328 else {
1329 aiov.iov_base = SCARG(uap, buf);
1330 aiov.iov_len = SCARG(uap, count);
1331 auio.uio_iov = &aiov;
1332 auio.uio_iovcnt = 1;
1333 auio.uio_offset = 0;
1334 auio.uio_rw = UIO_READ;
1335 auio.uio_segflg = UIO_USERSPACE;
1336 auio.uio_procp = p;
1337 auio.uio_resid = SCARG(uap, count);
1338 error = VOP_READLINK(vp, &auio, p->p_ucred);
1339 }
1340 vput(vp);
1341 *retval = SCARG(uap, count) - auio.uio_resid;
1342 return (error);
1343 }
1344
1345 /*
1346 * Change flags of a file given a path name.
1347 */
1348 /* ARGSUSED */
1349 int
1350 sys_chflags(p, v, retval)
1351 struct proc *p;
1352 void *v;
1353 register_t *retval;
1354 {
1355 register struct sys_chflags_args /* {
1356 syscallarg(const char *) path;
1357 syscallarg(u_long) flags;
1358 } */ *uap = v;
1359 register struct vnode *vp;
1360 struct vattr vattr;
1361 int error;
1362 struct nameidata nd;
1363
1364 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1365 if ((error = namei(&nd)) != 0)
1366 return (error);
1367 vp = nd.ni_vp;
1368 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1369 VOP_LOCK(vp);
1370 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1371 error = EROFS;
1372 else {
1373 VATTR_NULL(&vattr);
1374 vattr.va_flags = SCARG(uap, flags);
1375 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1376 }
1377 vput(vp);
1378 return (error);
1379 }
1380
1381 /*
1382 * Change flags of a file given a file descriptor.
1383 */
1384 /* ARGSUSED */
1385 int
1386 sys_fchflags(p, v, retval)
1387 struct proc *p;
1388 void *v;
1389 register_t *retval;
1390 {
1391 register struct sys_fchflags_args /* {
1392 syscallarg(int) fd;
1393 syscallarg(u_long) flags;
1394 } */ *uap = v;
1395 struct vattr vattr;
1396 struct vnode *vp;
1397 struct file *fp;
1398 int error;
1399
1400 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1401 return (error);
1402 vp = (struct vnode *)fp->f_data;
1403 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1404 VOP_LOCK(vp);
1405 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1406 error = EROFS;
1407 else {
1408 VATTR_NULL(&vattr);
1409 vattr.va_flags = SCARG(uap, flags);
1410 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1411 }
1412 VOP_UNLOCK(vp);
1413 return (error);
1414 }
1415
1416 /*
1417 * Change mode of a file given path name.
1418 */
1419 /* ARGSUSED */
1420 int
1421 sys_chmod(p, v, retval)
1422 struct proc *p;
1423 void *v;
1424 register_t *retval;
1425 {
1426 register struct sys_chmod_args /* {
1427 syscallarg(const char *) path;
1428 syscallarg(int) mode;
1429 } */ *uap = v;
1430 register struct vnode *vp;
1431 struct vattr vattr;
1432 int error;
1433 struct nameidata nd;
1434
1435 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1436 if ((error = namei(&nd)) != 0)
1437 return (error);
1438 vp = nd.ni_vp;
1439 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1440 VOP_LOCK(vp);
1441 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1442 error = EROFS;
1443 else {
1444 VATTR_NULL(&vattr);
1445 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1446 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1447 }
1448 vput(vp);
1449 return (error);
1450 }
1451
1452 /*
1453 * Change mode of a file given a file descriptor.
1454 */
1455 /* ARGSUSED */
1456 int
1457 sys_fchmod(p, v, retval)
1458 struct proc *p;
1459 void *v;
1460 register_t *retval;
1461 {
1462 register struct sys_fchmod_args /* {
1463 syscallarg(int) fd;
1464 syscallarg(int) mode;
1465 } */ *uap = v;
1466 struct vattr vattr;
1467 struct vnode *vp;
1468 struct file *fp;
1469 int error;
1470
1471 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1472 return (error);
1473 vp = (struct vnode *)fp->f_data;
1474 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1475 VOP_LOCK(vp);
1476 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1477 error = EROFS;
1478 else {
1479 VATTR_NULL(&vattr);
1480 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1481 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1482 }
1483 VOP_UNLOCK(vp);
1484 return (error);
1485 }
1486
1487 /*
1488 * Set ownership given a path name.
1489 */
1490 /* ARGSUSED */
1491 int
1492 sys_chown(p, v, retval)
1493 struct proc *p;
1494 void *v;
1495 register_t *retval;
1496 {
1497 register struct sys_chown_args /* {
1498 syscallarg(const char *) path;
1499 syscallarg(uid_t) uid;
1500 syscallarg(gid_t) gid;
1501 } */ *uap = v;
1502 register struct vnode *vp;
1503 struct vattr vattr;
1504 int error;
1505 struct nameidata nd;
1506
1507 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1508 if ((error = namei(&nd)) != 0)
1509 return (error);
1510 vp = nd.ni_vp;
1511 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1512 VOP_LOCK(vp);
1513 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1514 error = EROFS;
1515 else {
1516 VATTR_NULL(&vattr);
1517 vattr.va_uid = SCARG(uap, uid);
1518 vattr.va_gid = SCARG(uap, gid);
1519 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1520 }
1521 vput(vp);
1522 return (error);
1523 }
1524
1525 /*
1526 * Set ownership given a file descriptor.
1527 */
1528 /* ARGSUSED */
1529 int
1530 sys_fchown(p, v, retval)
1531 struct proc *p;
1532 void *v;
1533 register_t *retval;
1534 {
1535 register struct sys_fchown_args /* {
1536 syscallarg(int) fd;
1537 syscallarg(uid_t) uid;
1538 syscallarg(gid_t) gid;
1539 } */ *uap = v;
1540 register struct vnode *vp;
1541 struct vattr vattr;
1542 int error;
1543 struct file *fp;
1544
1545 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1546 return (error);
1547 vp = (struct vnode *)fp->f_data;
1548 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1549 VOP_LOCK(vp);
1550 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1551 error = EROFS;
1552 else {
1553 VATTR_NULL(&vattr);
1554 vattr.va_uid = SCARG(uap, uid);
1555 vattr.va_gid = SCARG(uap, gid);
1556 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1557 }
1558 VOP_UNLOCK(vp);
1559 return (error);
1560 }
1561
1562 /*
1563 * Set the access and modification times given a path name.
1564 */
1565 /* ARGSUSED */
1566 int
1567 sys_utimes(p, v, retval)
1568 struct proc *p;
1569 void *v;
1570 register_t *retval;
1571 {
1572 register struct sys_utimes_args /* {
1573 syscallarg(const char *) path;
1574 syscallarg(const struct timeval *) tptr;
1575 } */ *uap = v;
1576 register struct vnode *vp;
1577 struct timeval tv[2];
1578 struct vattr vattr;
1579 int error;
1580 struct nameidata nd;
1581
1582 VATTR_NULL(&vattr);
1583 if (SCARG(uap, tptr) == NULL) {
1584 microtime(&tv[0]);
1585 tv[1] = tv[0];
1586 vattr.va_vaflags |= VA_UTIMES_NULL;
1587 } else {
1588 error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
1589 if (error)
1590 return (error);
1591 }
1592 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1593 if ((error = namei(&nd)) != 0)
1594 return (error);
1595 vp = nd.ni_vp;
1596 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1597 VOP_LOCK(vp);
1598 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1599 error = EROFS;
1600 else {
1601 vattr.va_atime.tv_sec = tv[0].tv_sec;
1602 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1603 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1604 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1605 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1606 }
1607 vput(vp);
1608 return (error);
1609 }
1610
1611 /*
1612 * Set the access and modification times given a file descriptor.
1613 */
1614 /* ARGSUSED */
1615 int
1616 sys_futimes(p, v, retval)
1617 struct proc *p;
1618 void *v;
1619 register_t *retval;
1620 {
1621 register struct sys_futimes_args /* {
1622 syscallarg(int) fd;
1623 syscallarg(const struct timeval *) tptr;
1624 } */ *uap = v;
1625 register struct vnode *vp;
1626 struct timeval tv[2];
1627 struct vattr vattr;
1628 int error;
1629 struct file *fp;
1630
1631 VATTR_NULL(&vattr);
1632 if (SCARG(uap, tptr) == NULL) {
1633 microtime(&tv[0]);
1634 tv[1] = tv[0];
1635 vattr.va_vaflags |= VA_UTIMES_NULL;
1636 } else {
1637 error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
1638 if (error)
1639 return (error);
1640 }
1641 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1642 return (error);
1643 vp = (struct vnode *)fp->f_data;
1644 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1645 VOP_LOCK(vp);
1646 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1647 error = EROFS;
1648 else {
1649 vattr.va_atime.tv_sec = tv[0].tv_sec;
1650 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1651 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1652 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1653 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1654 }
1655 VOP_UNLOCK(vp);
1656 return (error);
1657 }
1658
1659 /*
1660 * Truncate a file given its path name.
1661 */
1662 /* ARGSUSED */
1663 int
1664 sys_truncate(p, v, retval)
1665 struct proc *p;
1666 void *v;
1667 register_t *retval;
1668 {
1669 register struct sys_truncate_args /* {
1670 syscallarg(const char *) path;
1671 syscallarg(int) pad;
1672 syscallarg(off_t) length;
1673 } */ *uap = v;
1674 register struct vnode *vp;
1675 struct vattr vattr;
1676 int error;
1677 struct nameidata nd;
1678
1679 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1680 if ((error = namei(&nd)) != 0)
1681 return (error);
1682 vp = nd.ni_vp;
1683 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1684 VOP_LOCK(vp);
1685 if (vp->v_type == VDIR)
1686 error = EISDIR;
1687 else if ((error = vn_writechk(vp)) == 0 &&
1688 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1689 VATTR_NULL(&vattr);
1690 vattr.va_size = SCARG(uap, length);
1691 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1692 }
1693 vput(vp);
1694 return (error);
1695 }
1696
1697 /*
1698 * Truncate a file given a file descriptor.
1699 */
1700 /* ARGSUSED */
1701 int
1702 sys_ftruncate(p, v, retval)
1703 struct proc *p;
1704 void *v;
1705 register_t *retval;
1706 {
1707 register struct sys_ftruncate_args /* {
1708 syscallarg(int) fd;
1709 syscallarg(int) pad;
1710 syscallarg(off_t) length;
1711 } */ *uap = v;
1712 struct vattr vattr;
1713 struct vnode *vp;
1714 struct file *fp;
1715 int error;
1716
1717 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1718 return (error);
1719 if ((fp->f_flag & FWRITE) == 0)
1720 return (EINVAL);
1721 vp = (struct vnode *)fp->f_data;
1722 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1723 VOP_LOCK(vp);
1724 if (vp->v_type == VDIR)
1725 error = EISDIR;
1726 else if ((error = vn_writechk(vp)) == 0) {
1727 VATTR_NULL(&vattr);
1728 vattr.va_size = SCARG(uap, length);
1729 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1730 }
1731 VOP_UNLOCK(vp);
1732 return (error);
1733 }
1734
1735 /*
1736 * Sync an open file.
1737 */
1738 /* ARGSUSED */
1739 int
1740 sys_fsync(p, v, retval)
1741 struct proc *p;
1742 void *v;
1743 register_t *retval;
1744 {
1745 struct sys_fsync_args /* {
1746 syscallarg(int) fd;
1747 } */ *uap = v;
1748 register struct vnode *vp;
1749 struct file *fp;
1750 int error;
1751
1752 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1753 return (error);
1754 vp = (struct vnode *)fp->f_data;
1755 VOP_LOCK(vp);
1756 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1757 VOP_UNLOCK(vp);
1758 return (error);
1759 }
1760
1761 /*
1762 * Rename files. Source and destination must either both be directories,
1763 * or both not be directories. If target is a directory, it must be empty.
1764 */
1765 /* ARGSUSED */
1766 int
1767 sys_rename(p, v, retval)
1768 struct proc *p;
1769 void *v;
1770 register_t *retval;
1771 {
1772 register struct sys_rename_args /* {
1773 syscallarg(const char *) from;
1774 syscallarg(const char *) to;
1775 } */ *uap = v;
1776 register struct vnode *tvp, *fvp, *tdvp;
1777 struct nameidata fromnd, tond;
1778 int error;
1779
1780 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1781 SCARG(uap, from), p);
1782 if ((error = namei(&fromnd)) != 0)
1783 return (error);
1784 fvp = fromnd.ni_vp;
1785 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1786 UIO_USERSPACE, SCARG(uap, to), p);
1787 if ((error = namei(&tond)) != 0) {
1788 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1789 vrele(fromnd.ni_dvp);
1790 vrele(fvp);
1791 goto out1;
1792 }
1793 tdvp = tond.ni_dvp;
1794 tvp = tond.ni_vp;
1795 if (tvp != NULL) {
1796 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1797 error = ENOTDIR;
1798 goto out;
1799 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1800 error = EISDIR;
1801 goto out;
1802 }
1803 }
1804 if (fvp == tdvp)
1805 error = EINVAL;
1806 /*
1807 * If source is the same as the destination (that is the
1808 * same inode number with the same name in the same directory),
1809 * then there is nothing to do.
1810 */
1811 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1812 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1813 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1814 fromnd.ni_cnd.cn_namelen))
1815 error = -1;
1816 out:
1817 if (!error) {
1818 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1819 if (fromnd.ni_dvp != tdvp)
1820 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1821 if (tvp) {
1822 (void)vnode_pager_uncache(tvp);
1823 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1824 }
1825 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1826 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1827 } else {
1828 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1829 if (tdvp == tvp)
1830 vrele(tdvp);
1831 else
1832 vput(tdvp);
1833 if (tvp)
1834 vput(tvp);
1835 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1836 vrele(fromnd.ni_dvp);
1837 vrele(fvp);
1838 }
1839 vrele(tond.ni_startdir);
1840 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1841 out1:
1842 if (fromnd.ni_startdir)
1843 vrele(fromnd.ni_startdir);
1844 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1845 return (error == -1 ? 0 : error);
1846 }
1847
1848 /*
1849 * Make a directory file.
1850 */
1851 /* ARGSUSED */
1852 int
1853 sys_mkdir(p, v, retval)
1854 struct proc *p;
1855 void *v;
1856 register_t *retval;
1857 {
1858 register struct sys_mkdir_args /* {
1859 syscallarg(const char *) path;
1860 syscallarg(int) mode;
1861 } */ *uap = v;
1862 register struct vnode *vp;
1863 struct vattr vattr;
1864 int error;
1865 struct nameidata nd;
1866
1867 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1868 if ((error = namei(&nd)) != 0)
1869 return (error);
1870 vp = nd.ni_vp;
1871 if (vp != NULL) {
1872 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1873 if (nd.ni_dvp == vp)
1874 vrele(nd.ni_dvp);
1875 else
1876 vput(nd.ni_dvp);
1877 vrele(vp);
1878 return (EEXIST);
1879 }
1880 VATTR_NULL(&vattr);
1881 vattr.va_type = VDIR;
1882 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
1883 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1884 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1885 if (!error)
1886 vput(nd.ni_vp);
1887 return (error);
1888 }
1889
1890 /*
1891 * Remove a directory file.
1892 */
1893 /* ARGSUSED */
1894 int
1895 sys_rmdir(p, v, retval)
1896 struct proc *p;
1897 void *v;
1898 register_t *retval;
1899 {
1900 struct sys_rmdir_args /* {
1901 syscallarg(const char *) path;
1902 } */ *uap = v;
1903 register struct vnode *vp;
1904 int error;
1905 struct nameidata nd;
1906
1907 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1908 SCARG(uap, path), p);
1909 if ((error = namei(&nd)) != 0)
1910 return (error);
1911 vp = nd.ni_vp;
1912 if (vp->v_type != VDIR) {
1913 error = ENOTDIR;
1914 goto out;
1915 }
1916 /*
1917 * No rmdir "." please.
1918 */
1919 if (nd.ni_dvp == vp) {
1920 error = EINVAL;
1921 goto out;
1922 }
1923 /*
1924 * The root of a mounted filesystem cannot be deleted.
1925 */
1926 if (vp->v_flag & VROOT)
1927 error = EBUSY;
1928 out:
1929 if (!error) {
1930 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1931 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1932 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1933 } else {
1934 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1935 if (nd.ni_dvp == vp)
1936 vrele(nd.ni_dvp);
1937 else
1938 vput(nd.ni_dvp);
1939 vput(vp);
1940 }
1941 return (error);
1942 }
1943
1944 /*
1945 * Read a block of directory entries in a file system independent format.
1946 */
1947 int
1948 sys_getdirentries(p, v, retval)
1949 struct proc *p;
1950 void *v;
1951 register_t *retval;
1952 {
1953 register struct sys_getdirentries_args /* {
1954 syscallarg(int) fd;
1955 syscallarg(char *) buf;
1956 syscallarg(u_int) count;
1957 syscallarg(long *) basep;
1958 } */ *uap = v;
1959 register struct vnode *vp;
1960 struct file *fp;
1961 struct uio auio;
1962 struct iovec aiov;
1963 long loff;
1964 int error, eofflag;
1965
1966 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1967 return (error);
1968 if ((fp->f_flag & FREAD) == 0)
1969 return (EBADF);
1970 vp = (struct vnode *)fp->f_data;
1971 unionread:
1972 if (vp->v_type != VDIR)
1973 return (EINVAL);
1974 aiov.iov_base = SCARG(uap, buf);
1975 aiov.iov_len = SCARG(uap, count);
1976 auio.uio_iov = &aiov;
1977 auio.uio_iovcnt = 1;
1978 auio.uio_rw = UIO_READ;
1979 auio.uio_segflg = UIO_USERSPACE;
1980 auio.uio_procp = p;
1981 auio.uio_resid = SCARG(uap, count);
1982 VOP_LOCK(vp);
1983 loff = auio.uio_offset = fp->f_offset;
1984 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
1985 fp->f_offset = auio.uio_offset;
1986 VOP_UNLOCK(vp);
1987 if (error)
1988 return (error);
1989
1990 #ifdef UNION
1991 {
1992 extern int (**union_vnodeop_p) __P((void *));
1993 extern struct vnode *union_dircache __P((struct vnode *));
1994
1995 if ((SCARG(uap, count) == auio.uio_resid) &&
1996 (vp->v_op == union_vnodeop_p)) {
1997 struct vnode *lvp;
1998
1999 lvp = union_dircache(vp);
2000 if (lvp != NULLVP) {
2001 struct vattr va;
2002
2003 /*
2004 * If the directory is opaque,
2005 * then don't show lower entries
2006 */
2007 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2008 if (va.va_flags & OPAQUE) {
2009 vput(lvp);
2010 lvp = NULL;
2011 }
2012 }
2013
2014 if (lvp != NULLVP) {
2015 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2016 VOP_UNLOCK(lvp);
2017
2018 if (error) {
2019 vrele(lvp);
2020 return (error);
2021 }
2022 fp->f_data = (caddr_t) lvp;
2023 fp->f_offset = 0;
2024 error = vn_close(vp, FREAD, fp->f_cred, p);
2025 if (error)
2026 return (error);
2027 vp = lvp;
2028 goto unionread;
2029 }
2030 }
2031 }
2032 #endif /* UNION */
2033
2034 if ((SCARG(uap, count) == auio.uio_resid) &&
2035 (vp->v_flag & VROOT) &&
2036 (vp->v_mount->mnt_flag & MNT_UNION)) {
2037 struct vnode *tvp = vp;
2038 vp = vp->v_mount->mnt_vnodecovered;
2039 VREF(vp);
2040 fp->f_data = (caddr_t) vp;
2041 fp->f_offset = 0;
2042 vrele(tvp);
2043 goto unionread;
2044 }
2045 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
2046 *retval = SCARG(uap, count) - auio.uio_resid;
2047 return (error);
2048 }
2049
2050 /*
2051 * Set the mode mask for creation of filesystem nodes.
2052 */
2053 int
2054 sys_umask(p, v, retval)
2055 struct proc *p;
2056 void *v;
2057 register_t *retval;
2058 {
2059 struct sys_umask_args /* {
2060 syscallarg(int) newmask;
2061 } */ *uap = v;
2062 register struct filedesc *fdp;
2063
2064 fdp = p->p_fd;
2065 *retval = fdp->fd_cmask;
2066 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2067 return (0);
2068 }
2069
2070 /*
2071 * Void all references to file by ripping underlying filesystem
2072 * away from vnode.
2073 */
2074 /* ARGSUSED */
2075 int
2076 sys_revoke(p, v, retval)
2077 struct proc *p;
2078 void *v;
2079 register_t *retval;
2080 {
2081 register struct sys_revoke_args /* {
2082 syscallarg(const char *) path;
2083 } */ *uap = v;
2084 register struct vnode *vp;
2085 struct vattr vattr;
2086 int error;
2087 struct nameidata nd;
2088
2089 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2090 if ((error = namei(&nd)) != 0)
2091 return (error);
2092 vp = nd.ni_vp;
2093 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2094 error = EINVAL;
2095 goto out;
2096 }
2097 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2098 goto out;
2099 if (p->p_ucred->cr_uid != vattr.va_uid &&
2100 (error = suser(p->p_ucred, &p->p_acflag)))
2101 goto out;
2102 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2103 vgoneall(vp);
2104 out:
2105 vrele(vp);
2106 return (error);
2107 }
2108
2109 /*
2110 * Convert a user file descriptor to a kernel file entry.
2111 */
2112 int
2113 getvnode(fdp, fd, fpp)
2114 struct filedesc *fdp;
2115 int fd;
2116 struct file **fpp;
2117 {
2118 struct vnode *vp;
2119 struct file *fp;
2120
2121 if ((u_int)fd >= fdp->fd_nfiles ||
2122 (fp = fdp->fd_ofiles[fd]) == NULL)
2123 return (EBADF);
2124 if (fp->f_type != DTYPE_VNODE)
2125 return (EINVAL);
2126 vp = (struct vnode *)fp->f_data;
2127 if (vp->v_type == VBAD)
2128 return (EBADF);
2129 *fpp = fp;
2130 return (0);
2131 }
2132