vfs_syscalls.c revision 1.86 1 /* $NetBSD: vfs_syscalls.c,v 1.86 1997/04/30 19:29:43 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 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *));
64
65 void checkdirs __P((struct vnode *));
66 int dounmount __P((struct mount *, int, struct proc *));
67
68 /*
69 * Virtual File System System Calls
70 */
71
72 /*
73 * Mount a file system.
74 */
75 /* ARGSUSED */
76 int
77 sys_mount(p, v, retval)
78 struct proc *p;
79 void *v;
80 register_t *retval;
81 {
82 register struct sys_mount_args /* {
83 syscallarg(const char *) type;
84 syscallarg(const char *) path;
85 syscallarg(int) flags;
86 syscallarg(void *) data;
87 } */ *uap = v;
88 register struct vnode *vp;
89 register struct mount *mp;
90 int error, flag = 0;
91 u_long fsindex = 0;
92 char fstypename[MFSNAMELEN];
93 struct vattr va;
94 struct nameidata nd;
95
96 /*
97 * Get vnode to be covered
98 */
99 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
100 SCARG(uap, path), p);
101 if ((error = namei(&nd)) != 0)
102 return (error);
103 vp = nd.ni_vp;
104 if (SCARG(uap, flags) & MNT_UPDATE) {
105 if ((vp->v_flag & VROOT) == 0) {
106 vput(vp);
107 return (EINVAL);
108 }
109 mp = vp->v_mount;
110 flag = mp->mnt_flag;
111 /*
112 * We only allow the filesystem to be reloaded if it
113 * is currently mounted read-only.
114 */
115 if ((SCARG(uap, flags) & MNT_RELOAD) &&
116 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
117 vput(vp);
118 return (EOPNOTSUPP); /* Needs translation */
119 }
120 mp->mnt_flag |=
121 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
122 /*
123 * Only root, or the user that did the original mount is
124 * permitted to update it.
125 */
126 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
127 (error = suser(p->p_ucred, &p->p_acflag))) {
128 vput(vp);
129 return (error);
130 }
131 /*
132 * Do not allow NFS export by non-root users. Silently
133 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
134 */
135 if (p->p_ucred->cr_uid != 0) {
136 if (SCARG(uap, flags) & MNT_EXPORTED) {
137 vput(vp);
138 return (EPERM);
139 }
140 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
141 }
142 VOP_UNLOCK(vp);
143 goto update;
144 }
145 /*
146 * If the user is not root, ensure that they own the directory
147 * onto which we are attempting to mount.
148 */
149 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) ||
150 (va.va_uid != p->p_ucred->cr_uid &&
151 (error = suser(p->p_ucred, &p->p_acflag)))) {
152 vput(vp);
153 return (error);
154 }
155 /*
156 * Do not allow NFS export by non-root users. Silently
157 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
158 */
159 if (p->p_ucred->cr_uid != 0) {
160 if (SCARG(uap, flags) & MNT_EXPORTED) {
161 vput(vp);
162 return (EPERM);
163 }
164 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
165 }
166 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
167 return (error);
168 if (vp->v_type != VDIR) {
169 vput(vp);
170 return (ENOTDIR);
171 }
172 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
173 if (error) {
174 #if defined(COMPAT_09) || defined(COMPAT_43)
175 /*
176 * Historically filesystem types were identified by number.
177 * If we get an integer for the filesystem type instead of a
178 * string, we check to see if it matches one of the historic
179 * filesystem types.
180 */
181 fsindex = (u_long)SCARG(uap, type);
182 if (fsindex >= nvfssw || vfssw[fsindex] == NULL) {
183 vput(vp);
184 return (ENODEV);
185 }
186 strncpy(fstypename, vfssw[fsindex]->vfs_name, MFSNAMELEN);
187 #else
188 vput(vp);
189 return (error);
190 #endif
191 }
192 #ifdef COMPAT_10
193 /* Accept `ufs' as an alias for `ffs'. */
194 if (!strncmp(fstypename, "ufs", MFSNAMELEN))
195 strncpy(fstypename, "ffs", MFSNAMELEN);
196 #endif
197 for (fsindex = 0; fsindex < nvfssw; fsindex++)
198 if (vfssw[fsindex] != NULL &&
199 !strncmp(vfssw[fsindex]->vfs_name, fstypename, MFSNAMELEN))
200 break;
201 if (fsindex >= nvfssw) {
202 vput(vp);
203 return (ENODEV);
204 }
205 if (vp->v_mountedhere != NULL) {
206 vput(vp);
207 return (EBUSY);
208 }
209
210 /*
211 * Allocate and initialize the file system.
212 */
213 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
214 M_MOUNT, M_WAITOK);
215 bzero((char *)mp, (u_long)sizeof(struct mount));
216 mp->mnt_op = vfssw[fsindex];
217 if ((error = vfs_lock(mp)) != 0) {
218 free((caddr_t)mp, M_MOUNT);
219 vput(vp);
220 return (error);
221 }
222 /* Do this early in case we block later. */
223 vfssw[fsindex]->vfs_refcount++;
224 vp->v_mountedhere = mp;
225 mp->mnt_vnodecovered = vp;
226 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
227 update:
228 /*
229 * Set the mount level flags.
230 */
231 if (SCARG(uap, flags) & MNT_RDONLY)
232 mp->mnt_flag |= MNT_RDONLY;
233 else if (mp->mnt_flag & MNT_RDONLY)
234 mp->mnt_flag |= MNT_WANTRDWR;
235 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
236 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
237 MNT_NOATIME);
238 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
239 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
240 MNT_NOCOREDUMP | MNT_NOATIME);
241 /*
242 * Mount the filesystem.
243 */
244 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
245 if (mp->mnt_flag & MNT_UPDATE) {
246 vrele(vp);
247 if (mp->mnt_flag & MNT_WANTRDWR)
248 mp->mnt_flag &= ~MNT_RDONLY;
249 mp->mnt_flag &=~
250 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
251 if (error)
252 mp->mnt_flag = flag;
253 return (error);
254 }
255 /*
256 * Put the new filesystem on the mount list after root.
257 */
258 cache_purge(vp);
259 if (!error) {
260 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
261 checkdirs(vp);
262 VOP_UNLOCK(vp);
263 vfs_unlock(mp);
264 (void) VFS_STATFS(mp, &mp->mnt_stat, p);
265 error = VFS_START(mp, 0, p);
266 } else {
267 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
268 vfssw[fsindex]->vfs_refcount--;
269 vfs_unlock(mp);
270 free((caddr_t)mp, M_MOUNT);
271 vput(vp);
272 }
273 return (error);
274 }
275
276 /*
277 * Scan all active processes to see if any of them have a current
278 * or root directory onto which the new filesystem has just been
279 * mounted. If so, replace them with the new mount point.
280 */
281 void
282 checkdirs(olddp)
283 struct vnode *olddp;
284 {
285 struct filedesc *fdp;
286 struct vnode *newdp;
287 struct proc *p;
288
289 if (olddp->v_usecount == 1)
290 return;
291 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
292 panic("mount: lost mount");
293 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
294 fdp = p->p_fd;
295 if (fdp->fd_cdir == olddp) {
296 vrele(fdp->fd_cdir);
297 VREF(newdp);
298 fdp->fd_cdir = newdp;
299 }
300 if (fdp->fd_rdir == olddp) {
301 vrele(fdp->fd_rdir);
302 VREF(newdp);
303 fdp->fd_rdir = newdp;
304 }
305 }
306 if (rootvnode == olddp) {
307 vrele(rootvnode);
308 VREF(newdp);
309 rootvnode = newdp;
310 }
311 vput(newdp);
312 }
313
314 /*
315 * Unmount a file system.
316 *
317 * Note: unmount takes a path to the vnode mounted on as argument,
318 * not special file (as before).
319 */
320 /* ARGSUSED */
321 int
322 sys_unmount(p, v, retval)
323 struct proc *p;
324 void *v;
325 register_t *retval;
326 {
327 register struct sys_unmount_args /* {
328 syscallarg(const char *) path;
329 syscallarg(int) flags;
330 } */ *uap = v;
331 register struct vnode *vp;
332 struct mount *mp;
333 int error;
334 struct nameidata nd;
335
336 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
337 SCARG(uap, path), p);
338 if ((error = namei(&nd)) != 0)
339 return (error);
340 vp = nd.ni_vp;
341 mp = vp->v_mount;
342
343 /*
344 * Only root, or the user that did the original mount is
345 * permitted to unmount this filesystem.
346 */
347 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
348 (error = suser(p->p_ucred, &p->p_acflag))) {
349 vput(vp);
350 return (error);
351 }
352
353 /*
354 * Don't allow unmounting the root file system.
355 */
356 if (mp->mnt_flag & MNT_ROOTFS) {
357 vput(vp);
358 return (EINVAL);
359 }
360
361 /*
362 * Must be the root of the filesystem
363 */
364 if ((vp->v_flag & VROOT) == 0) {
365 vput(vp);
366 return (EINVAL);
367 }
368 vput(vp);
369
370 if (vfs_busy(mp))
371 return (EBUSY);
372
373 return (dounmount(mp, SCARG(uap, flags), p));
374 }
375
376 /*
377 * Do the actual file system unmount. File system is assumed to have been
378 * marked busy by the caller.
379 */
380 int
381 dounmount(mp, flags, p)
382 register struct mount *mp;
383 int flags;
384 struct proc *p;
385 {
386 struct vnode *coveredvp;
387 int error;
388
389 coveredvp = mp->mnt_vnodecovered;
390 mp->mnt_flag |= MNT_UNMOUNT;
391 if ((error = vfs_lock(mp)) != 0) {
392 vfs_unbusy(mp);
393 return (error);
394 }
395
396 mp->mnt_flag &=~ MNT_ASYNC;
397 vnode_pager_umount(mp); /* release cached vnodes */
398 cache_purgevfs(mp); /* remove cache entries for this file sys */
399 if ((error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0 ||
400 (flags & MNT_FORCE))
401 error = VFS_UNMOUNT(mp, flags, p);
402 mp->mnt_flag &= ~MNT_UNMOUNT;
403 vfs_unbusy(mp);
404 if (error) {
405 vfs_unlock(mp);
406 } else {
407 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
408 if (coveredvp != NULLVP) {
409 vrele(coveredvp);
410 coveredvp->v_mountedhere = (struct mount *)0;
411 }
412 mp->mnt_op->vfs_refcount--;
413 vfs_unlock(mp);
414 if (mp->mnt_vnodelist.lh_first != NULL)
415 panic("unmount: dangling vnode");
416 free((caddr_t)mp, M_MOUNT);
417 }
418 return (error);
419 }
420
421 /*
422 * Sync each mounted filesystem.
423 */
424 #ifdef DEBUG
425 int syncprt = 0;
426 struct ctldebug debug0 = { "syncprt", &syncprt };
427 #endif
428
429 /* ARGSUSED */
430 int
431 sys_sync(p, v, retval)
432 struct proc *p;
433 void *v;
434 register_t *retval;
435 {
436 register struct mount *mp, *nmp;
437 int asyncflag;
438
439 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
440 /*
441 * Get the prev pointer in case we hang on vfs_busy
442 * while we are being unmounted.
443 */
444 nmp = mp->mnt_list.cqe_prev;
445 /*
446 * The lock check below is to avoid races with mount
447 * and unmount.
448 */
449 if ((mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY)) == 0 &&
450 !vfs_busy(mp)) {
451 asyncflag = mp->mnt_flag & MNT_ASYNC;
452 mp->mnt_flag &= ~MNT_ASYNC;
453 vnode_pager_sync(mp);
454 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
455 if (asyncflag)
456 mp->mnt_flag |= MNT_ASYNC;
457 /*
458 * Get the prev pointer again, as the prior filesystem
459 * might have been unmounted while we were sync'ing.
460 */
461 nmp = mp->mnt_list.cqe_prev;
462 vfs_unbusy(mp);
463 }
464 }
465 #ifdef DEBUG
466 if (syncprt)
467 vfs_bufstats();
468 #endif /* DEBUG */
469 return (0);
470 }
471
472 /*
473 * Change filesystem quotas.
474 */
475 /* ARGSUSED */
476 int
477 sys_quotactl(p, v, retval)
478 struct proc *p;
479 void *v;
480 register_t *retval;
481 {
482 register struct sys_quotactl_args /* {
483 syscallarg(const char *) path;
484 syscallarg(int) cmd;
485 syscallarg(int) uid;
486 syscallarg(caddr_t) arg;
487 } */ *uap = v;
488 register struct mount *mp;
489 int error;
490 struct nameidata nd;
491
492 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
493 if ((error = namei(&nd)) != 0)
494 return (error);
495 mp = nd.ni_vp->v_mount;
496 vrele(nd.ni_vp);
497 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
498 SCARG(uap, arg), p));
499 }
500
501 /*
502 * Get filesystem statistics.
503 */
504 /* ARGSUSED */
505 int
506 sys_statfs(p, v, retval)
507 struct proc *p;
508 void *v;
509 register_t *retval;
510 {
511 register struct sys_statfs_args /* {
512 syscallarg(const char *) path;
513 syscallarg(struct statfs *) buf;
514 } */ *uap = v;
515 register struct mount *mp;
516 register struct statfs *sp;
517 int error;
518 struct nameidata nd;
519
520 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
521 if ((error = namei(&nd)) != 0)
522 return (error);
523 mp = nd.ni_vp->v_mount;
524 sp = &mp->mnt_stat;
525 vrele(nd.ni_vp);
526 if ((error = VFS_STATFS(mp, sp, p)) != 0)
527 return (error);
528 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
529 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
530 }
531
532 /*
533 * Get filesystem statistics.
534 */
535 /* ARGSUSED */
536 int
537 sys_fstatfs(p, v, retval)
538 struct proc *p;
539 void *v;
540 register_t *retval;
541 {
542 register struct sys_fstatfs_args /* {
543 syscallarg(int) fd;
544 syscallarg(struct statfs *) buf;
545 } */ *uap = v;
546 struct file *fp;
547 struct mount *mp;
548 register struct statfs *sp;
549 int error;
550
551 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
552 return (error);
553 mp = ((struct vnode *)fp->f_data)->v_mount;
554 sp = &mp->mnt_stat;
555 if ((error = VFS_STATFS(mp, sp, p)) != 0)
556 return (error);
557 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
558 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
559 }
560
561 /*
562 * Get statistics on all filesystems.
563 */
564 int
565 sys_getfsstat(p, v, retval)
566 struct proc *p;
567 void *v;
568 register_t *retval;
569 {
570 register struct sys_getfsstat_args /* {
571 syscallarg(struct statfs *) buf;
572 syscallarg(long) bufsize;
573 syscallarg(int) flags;
574 } */ *uap = v;
575 register struct mount *mp, *nmp;
576 register struct statfs *sp;
577 caddr_t sfsp;
578 long count, maxcount, error;
579
580 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
581 sfsp = (caddr_t)SCARG(uap, buf);
582 for (count = 0,
583 mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
584 nmp = mp->mnt_list.cqe_next;
585 if (sfsp && count < maxcount &&
586 ((mp->mnt_flag & MNT_MLOCK) == 0)) {
587 sp = &mp->mnt_stat;
588 /*
589 * If MNT_NOWAIT is specified, do not refresh the
590 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
591 */
592 if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
593 (SCARG(uap, flags) & MNT_WAIT)) &&
594 (error = VFS_STATFS(mp, sp, p)))
595 continue;
596 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
597 error = copyout(sp, sfsp, sizeof(*sp));
598 if (error)
599 return (error);
600 sfsp += sizeof(*sp);
601 }
602 count++;
603 }
604 if (sfsp && count > maxcount)
605 *retval = maxcount;
606 else
607 *retval = count;
608 return (0);
609 }
610
611 /*
612 * Change current working directory to a given file descriptor.
613 */
614 /* ARGSUSED */
615 int
616 sys_fchdir(p, v, retval)
617 struct proc *p;
618 void *v;
619 register_t *retval;
620 {
621 struct sys_fchdir_args /* {
622 syscallarg(int) fd;
623 } */ *uap = v;
624 register struct filedesc *fdp = p->p_fd;
625 struct vnode *vp, *tdp;
626 struct mount *mp;
627 struct file *fp;
628 int error;
629
630 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
631 return (error);
632 vp = (struct vnode *)fp->f_data;
633 VREF(vp);
634 VOP_LOCK(vp);
635 if (vp->v_type != VDIR)
636 error = ENOTDIR;
637 else
638 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
639 while (!error && (mp = vp->v_mountedhere) != NULL) {
640 if (mp->mnt_flag & MNT_MLOCK) {
641 mp->mnt_flag |= MNT_MWAIT;
642 sleep((caddr_t)mp, PVFS);
643 continue;
644 }
645 if ((error = VFS_ROOT(mp, &tdp)) != 0)
646 break;
647 vput(vp);
648 vp = tdp;
649 }
650 VOP_UNLOCK(vp);
651 if (error) {
652 vrele(vp);
653 return (error);
654 }
655 vrele(fdp->fd_cdir);
656 fdp->fd_cdir = vp;
657 return (0);
658 }
659
660 /*
661 * Change current working directory (``.'').
662 */
663 /* ARGSUSED */
664 int
665 sys_chdir(p, v, retval)
666 struct proc *p;
667 void *v;
668 register_t *retval;
669 {
670 struct sys_chdir_args /* {
671 syscallarg(const char *) path;
672 } */ *uap = v;
673 register struct filedesc *fdp = p->p_fd;
674 int error;
675 struct nameidata nd;
676
677 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
678 SCARG(uap, path), p);
679 if ((error = change_dir(&nd, p)) != 0)
680 return (error);
681 vrele(fdp->fd_cdir);
682 fdp->fd_cdir = nd.ni_vp;
683 return (0);
684 }
685
686 /*
687 * Change notion of root (``/'') directory.
688 */
689 /* ARGSUSED */
690 int
691 sys_chroot(p, v, retval)
692 struct proc *p;
693 void *v;
694 register_t *retval;
695 {
696 struct sys_chroot_args /* {
697 syscallarg(const char *) path;
698 } */ *uap = v;
699 register struct filedesc *fdp = p->p_fd;
700 int error;
701 struct nameidata nd;
702
703 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
704 return (error);
705 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
706 SCARG(uap, path), p);
707 if ((error = change_dir(&nd, p)) != 0)
708 return (error);
709 if (fdp->fd_rdir != NULL)
710 vrele(fdp->fd_rdir);
711 fdp->fd_rdir = nd.ni_vp;
712 return (0);
713 }
714
715 /*
716 * Common routine for chroot and chdir.
717 */
718 static int
719 change_dir(ndp, p)
720 register struct nameidata *ndp;
721 struct proc *p;
722 {
723 struct vnode *vp;
724 int error;
725
726 if ((error = namei(ndp)) != 0)
727 return (error);
728 vp = ndp->ni_vp;
729 if (vp->v_type != VDIR)
730 error = ENOTDIR;
731 else
732 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
733 VOP_UNLOCK(vp);
734 if (error)
735 vrele(vp);
736 return (error);
737 }
738
739 /*
740 * Check permissions, allocate an open file structure,
741 * and call the device open routine if any.
742 */
743 int
744 sys_open(p, v, retval)
745 struct proc *p;
746 void *v;
747 register_t *retval;
748 {
749 register struct sys_open_args /* {
750 syscallarg(const char *) path;
751 syscallarg(int) flags;
752 syscallarg(int) mode;
753 } */ *uap = v;
754 register struct filedesc *fdp = p->p_fd;
755 register struct file *fp;
756 register struct vnode *vp;
757 int flags, cmode;
758 struct file *nfp;
759 int type, indx, error;
760 struct flock lf;
761 struct nameidata nd;
762 extern struct fileops vnops;
763
764 if ((error = falloc(p, &nfp, &indx)) != 0)
765 return (error);
766 fp = nfp;
767 flags = FFLAGS(SCARG(uap, flags));
768 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
769 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
770 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
771 if ((error = vn_open(&nd, flags, cmode)) != 0) {
772 ffree(fp);
773 if ((error == ENODEV || error == ENXIO) &&
774 p->p_dupfd >= 0 && /* XXX from fdopen */
775 (error =
776 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
777 *retval = indx;
778 return (0);
779 }
780 if (error == ERESTART)
781 error = EINTR;
782 fdp->fd_ofiles[indx] = NULL;
783 return (error);
784 }
785 p->p_dupfd = 0;
786 vp = nd.ni_vp;
787 fp->f_flag = flags & FMASK;
788 fp->f_type = DTYPE_VNODE;
789 fp->f_ops = &vnops;
790 fp->f_data = (caddr_t)vp;
791 if (flags & (O_EXLOCK | O_SHLOCK)) {
792 lf.l_whence = SEEK_SET;
793 lf.l_start = 0;
794 lf.l_len = 0;
795 if (flags & O_EXLOCK)
796 lf.l_type = F_WRLCK;
797 else
798 lf.l_type = F_RDLCK;
799 type = F_FLOCK;
800 if ((flags & FNONBLOCK) == 0)
801 type |= F_WAIT;
802 VOP_UNLOCK(vp);
803 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
804 if (error) {
805 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
806 ffree(fp);
807 fdp->fd_ofiles[indx] = NULL;
808 return (error);
809 }
810 VOP_LOCK(vp);
811 fp->f_flag |= FHASLOCK;
812 }
813 VOP_UNLOCK(vp);
814 *retval = indx;
815 return (0);
816 }
817
818 /*
819 * Create a special file.
820 */
821 /* ARGSUSED */
822 int
823 sys_mknod(p, v, retval)
824 struct proc *p;
825 void *v;
826 register_t *retval;
827 {
828 register struct sys_mknod_args /* {
829 syscallarg(const char *) path;
830 syscallarg(int) mode;
831 syscallarg(int) dev;
832 } */ *uap = v;
833 register struct vnode *vp;
834 struct vattr vattr;
835 int error;
836 int whiteout = 0;
837 struct nameidata nd;
838
839 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
840 return (error);
841 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
842 if ((error = namei(&nd)) != 0)
843 return (error);
844 vp = nd.ni_vp;
845 if (vp != NULL)
846 error = EEXIST;
847 else {
848 VATTR_NULL(&vattr);
849 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
850 vattr.va_rdev = SCARG(uap, dev);
851 whiteout = 0;
852
853 switch (SCARG(uap, mode) & S_IFMT) {
854 case S_IFMT: /* used by badsect to flag bad sectors */
855 vattr.va_type = VBAD;
856 break;
857 case S_IFCHR:
858 vattr.va_type = VCHR;
859 break;
860 case S_IFBLK:
861 vattr.va_type = VBLK;
862 break;
863 case S_IFWHT:
864 whiteout = 1;
865 break;
866 default:
867 error = EINVAL;
868 break;
869 }
870 }
871 if (!error) {
872 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
873 if (whiteout) {
874 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
875 if (error)
876 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
877 vput(nd.ni_dvp);
878 } else {
879 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
880 &nd.ni_cnd, &vattr);
881 }
882 } else {
883 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
884 if (nd.ni_dvp == vp)
885 vrele(nd.ni_dvp);
886 else
887 vput(nd.ni_dvp);
888 if (vp)
889 vrele(vp);
890 }
891 return (error);
892 }
893
894 /*
895 * Create a named pipe.
896 */
897 /* ARGSUSED */
898 int
899 sys_mkfifo(p, v, retval)
900 struct proc *p;
901 void *v;
902 register_t *retval;
903 {
904 #ifndef FIFO
905 return (EOPNOTSUPP);
906 #else
907 register struct sys_mkfifo_args /* {
908 syscallarg(const char *) path;
909 syscallarg(int) mode;
910 } */ *uap = v;
911 struct vattr vattr;
912 int error;
913 struct nameidata nd;
914
915 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
916 if ((error = namei(&nd)) != 0)
917 return (error);
918 if (nd.ni_vp != NULL) {
919 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
920 if (nd.ni_dvp == nd.ni_vp)
921 vrele(nd.ni_dvp);
922 else
923 vput(nd.ni_dvp);
924 vrele(nd.ni_vp);
925 return (EEXIST);
926 }
927 VATTR_NULL(&vattr);
928 vattr.va_type = VFIFO;
929 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
930 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
931 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
932 #endif /* FIFO */
933 }
934
935 /*
936 * Make a hard file link.
937 */
938 /* ARGSUSED */
939 int
940 sys_link(p, v, retval)
941 struct proc *p;
942 void *v;
943 register_t *retval;
944 {
945 register struct sys_link_args /* {
946 syscallarg(const char *) path;
947 syscallarg(const char *) link;
948 } */ *uap = v;
949 register struct vnode *vp;
950 struct nameidata nd;
951 int error;
952
953 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
954 if ((error = namei(&nd)) != 0)
955 return (error);
956 vp = nd.ni_vp;
957 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
958 if ((error = namei(&nd)) != 0)
959 goto out;
960 if (nd.ni_vp) {
961 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
962 if (nd.ni_dvp == nd.ni_vp)
963 vrele(nd.ni_dvp);
964 else
965 vput(nd.ni_dvp);
966 vrele(nd.ni_vp);
967 error = EEXIST;
968 goto out;
969 }
970 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
971 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
972 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
973 out:
974 vrele(vp);
975 return (error);
976 }
977
978 /*
979 * Make a symbolic link.
980 */
981 /* ARGSUSED */
982 int
983 sys_symlink(p, v, retval)
984 struct proc *p;
985 void *v;
986 register_t *retval;
987 {
988 register struct sys_symlink_args /* {
989 syscallarg(const char *) path;
990 syscallarg(const char *) link;
991 } */ *uap = v;
992 struct vattr vattr;
993 char *path;
994 int error;
995 struct nameidata nd;
996
997 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
998 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
999 if (error)
1000 goto out;
1001 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1002 if ((error = namei(&nd)) != 0)
1003 goto out;
1004 if (nd.ni_vp) {
1005 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1006 if (nd.ni_dvp == nd.ni_vp)
1007 vrele(nd.ni_dvp);
1008 else
1009 vput(nd.ni_dvp);
1010 vrele(nd.ni_vp);
1011 error = EEXIST;
1012 goto out;
1013 }
1014 VATTR_NULL(&vattr);
1015 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1016 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1017 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1018 out:
1019 FREE(path, M_NAMEI);
1020 return (error);
1021 }
1022
1023 /*
1024 * Delete a whiteout from the filesystem.
1025 */
1026 /* ARGSUSED */
1027 int
1028 sys_undelete(p, v, retval)
1029 struct proc *p;
1030 void *v;
1031 register_t *retval;
1032 {
1033 register struct sys_undelete_args /* {
1034 syscallarg(const char *) path;
1035 } */ *uap = v;
1036 int error;
1037 struct nameidata nd;
1038
1039 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1040 SCARG(uap, path), p);
1041 error = namei(&nd);
1042 if (error)
1043 return (error);
1044
1045 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1046 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1047 if (nd.ni_dvp == nd.ni_vp)
1048 vrele(nd.ni_dvp);
1049 else
1050 vput(nd.ni_dvp);
1051 if (nd.ni_vp)
1052 vrele(nd.ni_vp);
1053 return (EEXIST);
1054 }
1055
1056 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1057 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1058 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1059 vput(nd.ni_dvp);
1060 return (error);
1061 }
1062
1063 /*
1064 * Delete a name from the filesystem.
1065 */
1066 /* ARGSUSED */
1067 int
1068 sys_unlink(p, v, retval)
1069 struct proc *p;
1070 void *v;
1071 register_t *retval;
1072 {
1073 struct sys_unlink_args /* {
1074 syscallarg(const char *) path;
1075 } */ *uap = v;
1076 register struct vnode *vp;
1077 int error;
1078 struct nameidata nd;
1079
1080 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1081 SCARG(uap, path), p);
1082 if ((error = namei(&nd)) != 0)
1083 return (error);
1084 vp = nd.ni_vp;
1085
1086 /*
1087 * The root of a mounted filesystem cannot be deleted.
1088 */
1089 if (vp->v_flag & VROOT) {
1090 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1091 if (nd.ni_dvp == vp)
1092 vrele(nd.ni_dvp);
1093 else
1094 vput(nd.ni_dvp);
1095 vput(vp);
1096 error = EBUSY;
1097 goto out;
1098 }
1099
1100 (void)vnode_pager_uncache(vp);
1101
1102 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1103 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1104 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1105 out:
1106 return (error);
1107 }
1108
1109 /*
1110 * Reposition read/write file offset.
1111 */
1112 int
1113 sys_lseek(p, v, retval)
1114 struct proc *p;
1115 void *v;
1116 register_t *retval;
1117 {
1118 register struct sys_lseek_args /* {
1119 syscallarg(int) fd;
1120 syscallarg(int) pad;
1121 syscallarg(off_t) offset;
1122 syscallarg(int) whence;
1123 } */ *uap = v;
1124 struct ucred *cred = p->p_ucred;
1125 register struct filedesc *fdp = p->p_fd;
1126 register struct file *fp;
1127 struct vnode *vp;
1128 struct vattr vattr;
1129 register off_t newoff;
1130 int error;
1131
1132 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1133 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1134 return (EBADF);
1135
1136 vp = (struct vnode *)fp->f_data;
1137 if (fp->f_type != DTYPE_VNODE
1138 #ifdef FIFO
1139 || vp->v_type == VFIFO
1140 #endif
1141 )
1142 return (ESPIPE);
1143
1144 switch (SCARG(uap, whence)) {
1145 case L_INCR:
1146 newoff = fp->f_offset + SCARG(uap, offset);
1147 break;
1148 case L_XTND:
1149 error = VOP_GETATTR(vp, &vattr, 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 int error;
1503 struct nameidata nd;
1504
1505 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1506 if ((error = namei(&nd)) != 0)
1507 return (error);
1508
1509 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p);
1510
1511 vrele(nd.ni_vp);
1512 return (error);
1513 }
1514
1515 /*
1516 * Set ownership given a file descriptor.
1517 */
1518 /* ARGSUSED */
1519 int
1520 sys_fchown(p, v, retval)
1521 struct proc *p;
1522 void *v;
1523 register_t *retval;
1524 {
1525 register struct sys_fchown_args /* {
1526 syscallarg(int) fd;
1527 syscallarg(uid_t) uid;
1528 syscallarg(gid_t) gid;
1529 } */ *uap = v;
1530 int error;
1531 struct file *fp;
1532
1533 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1534 return (error);
1535
1536 return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
1537 SCARG(uap, gid), p));
1538 }
1539
1540 /*
1541 * Common routine to set ownership given a vnode.
1542 */
1543 static int
1544 change_owner(vp, uid, gid, p)
1545 register struct vnode *vp;
1546 uid_t uid;
1547 gid_t gid;
1548 struct proc *p;
1549 {
1550 struct vattr vattr;
1551 mode_t newmode = VNOVAL;
1552 int error;
1553
1554 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1555 VOP_LOCK(vp);
1556 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1557 error = EROFS;
1558 goto out;
1559 }
1560 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)))
1561 goto out;
1562
1563 /* Clear (VSUID | VSGID) bits: alter va_mode only if necessary. */
1564 if (vattr.va_mode & (VSUID | VSGID))
1565 newmode = vattr.va_mode & ~(VSUID | VSGID);
1566
1567 VATTR_NULL(&vattr);
1568 vattr.va_uid = uid;
1569 vattr.va_gid = gid;
1570 vattr.va_mode = newmode;
1571 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1572
1573 out:
1574 VOP_UNLOCK(vp);
1575 return (error);
1576 }
1577
1578 /*
1579 * Set the access and modification times given a path name.
1580 */
1581 /* ARGSUSED */
1582 int
1583 sys_utimes(p, v, retval)
1584 struct proc *p;
1585 void *v;
1586 register_t *retval;
1587 {
1588 register struct sys_utimes_args /* {
1589 syscallarg(const char *) path;
1590 syscallarg(const struct timeval *) tptr;
1591 } */ *uap = v;
1592 register struct vnode *vp;
1593 struct timeval tv[2];
1594 struct vattr vattr;
1595 int error;
1596 struct nameidata nd;
1597
1598 VATTR_NULL(&vattr);
1599 if (SCARG(uap, tptr) == NULL) {
1600 microtime(&tv[0]);
1601 tv[1] = tv[0];
1602 vattr.va_vaflags |= VA_UTIMES_NULL;
1603 } else {
1604 error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
1605 if (error)
1606 return (error);
1607 }
1608 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1609 if ((error = namei(&nd)) != 0)
1610 return (error);
1611 vp = nd.ni_vp;
1612 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1613 VOP_LOCK(vp);
1614 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1615 error = EROFS;
1616 else {
1617 vattr.va_atime.tv_sec = tv[0].tv_sec;
1618 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1619 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1620 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1621 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1622 }
1623 vput(vp);
1624 return (error);
1625 }
1626
1627 /*
1628 * Set the access and modification times given a file descriptor.
1629 */
1630 /* ARGSUSED */
1631 int
1632 sys_futimes(p, v, retval)
1633 struct proc *p;
1634 void *v;
1635 register_t *retval;
1636 {
1637 register struct sys_futimes_args /* {
1638 syscallarg(int) fd;
1639 syscallarg(const struct timeval *) tptr;
1640 } */ *uap = v;
1641 register struct vnode *vp;
1642 struct timeval tv[2];
1643 struct vattr vattr;
1644 int error;
1645 struct file *fp;
1646
1647 VATTR_NULL(&vattr);
1648 if (SCARG(uap, tptr) == NULL) {
1649 microtime(&tv[0]);
1650 tv[1] = tv[0];
1651 vattr.va_vaflags |= VA_UTIMES_NULL;
1652 } else {
1653 error = copyin(SCARG(uap, tptr), tv, sizeof (tv));
1654 if (error)
1655 return (error);
1656 }
1657 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1658 return (error);
1659 vp = (struct vnode *)fp->f_data;
1660 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1661 VOP_LOCK(vp);
1662 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1663 error = EROFS;
1664 else {
1665 vattr.va_atime.tv_sec = tv[0].tv_sec;
1666 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1667 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1668 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1669 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1670 }
1671 VOP_UNLOCK(vp);
1672 return (error);
1673 }
1674
1675 /*
1676 * Truncate a file given its path name.
1677 */
1678 /* ARGSUSED */
1679 int
1680 sys_truncate(p, v, retval)
1681 struct proc *p;
1682 void *v;
1683 register_t *retval;
1684 {
1685 register struct sys_truncate_args /* {
1686 syscallarg(const char *) path;
1687 syscallarg(int) pad;
1688 syscallarg(off_t) length;
1689 } */ *uap = v;
1690 register struct vnode *vp;
1691 struct vattr vattr;
1692 int error;
1693 struct nameidata nd;
1694
1695 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1696 if ((error = namei(&nd)) != 0)
1697 return (error);
1698 vp = nd.ni_vp;
1699 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1700 VOP_LOCK(vp);
1701 if (vp->v_type == VDIR)
1702 error = EISDIR;
1703 else if ((error = vn_writechk(vp)) == 0 &&
1704 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1705 VATTR_NULL(&vattr);
1706 vattr.va_size = SCARG(uap, length);
1707 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1708 }
1709 vput(vp);
1710 return (error);
1711 }
1712
1713 /*
1714 * Truncate a file given a file descriptor.
1715 */
1716 /* ARGSUSED */
1717 int
1718 sys_ftruncate(p, v, retval)
1719 struct proc *p;
1720 void *v;
1721 register_t *retval;
1722 {
1723 register struct sys_ftruncate_args /* {
1724 syscallarg(int) fd;
1725 syscallarg(int) pad;
1726 syscallarg(off_t) length;
1727 } */ *uap = v;
1728 struct vattr vattr;
1729 struct vnode *vp;
1730 struct file *fp;
1731 int error;
1732
1733 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1734 return (error);
1735 if ((fp->f_flag & FWRITE) == 0)
1736 return (EINVAL);
1737 vp = (struct vnode *)fp->f_data;
1738 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1739 VOP_LOCK(vp);
1740 if (vp->v_type == VDIR)
1741 error = EISDIR;
1742 else if ((error = vn_writechk(vp)) == 0) {
1743 VATTR_NULL(&vattr);
1744 vattr.va_size = SCARG(uap, length);
1745 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1746 }
1747 VOP_UNLOCK(vp);
1748 return (error);
1749 }
1750
1751 /*
1752 * Sync an open file.
1753 */
1754 /* ARGSUSED */
1755 int
1756 sys_fsync(p, v, retval)
1757 struct proc *p;
1758 void *v;
1759 register_t *retval;
1760 {
1761 struct sys_fsync_args /* {
1762 syscallarg(int) fd;
1763 } */ *uap = v;
1764 register struct vnode *vp;
1765 struct file *fp;
1766 int error;
1767
1768 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1769 return (error);
1770 vp = (struct vnode *)fp->f_data;
1771 VOP_LOCK(vp);
1772 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1773 VOP_UNLOCK(vp);
1774 return (error);
1775 }
1776
1777 /*
1778 * Rename files. Source and destination must either both be directories,
1779 * or both not be directories. If target is a directory, it must be empty.
1780 */
1781 /* ARGSUSED */
1782 int
1783 sys_rename(p, v, retval)
1784 struct proc *p;
1785 void *v;
1786 register_t *retval;
1787 {
1788 register struct sys_rename_args /* {
1789 syscallarg(const char *) from;
1790 syscallarg(const char *) to;
1791 } */ *uap = v;
1792 register struct vnode *tvp, *fvp, *tdvp;
1793 struct nameidata fromnd, tond;
1794 int error;
1795
1796 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1797 SCARG(uap, from), p);
1798 if ((error = namei(&fromnd)) != 0)
1799 return (error);
1800 fvp = fromnd.ni_vp;
1801 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1802 UIO_USERSPACE, SCARG(uap, to), p);
1803 if ((error = namei(&tond)) != 0) {
1804 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1805 vrele(fromnd.ni_dvp);
1806 vrele(fvp);
1807 goto out1;
1808 }
1809 tdvp = tond.ni_dvp;
1810 tvp = tond.ni_vp;
1811 if (tvp != NULL) {
1812 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1813 error = ENOTDIR;
1814 goto out;
1815 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1816 error = EISDIR;
1817 goto out;
1818 }
1819 }
1820 if (fvp == tdvp)
1821 error = EINVAL;
1822 /*
1823 * If source is the same as the destination (that is the
1824 * same inode number with the same name in the same directory),
1825 * then there is nothing to do.
1826 */
1827 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1828 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1829 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1830 fromnd.ni_cnd.cn_namelen))
1831 error = -1;
1832 out:
1833 if (!error) {
1834 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1835 if (fromnd.ni_dvp != tdvp)
1836 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1837 if (tvp) {
1838 (void)vnode_pager_uncache(tvp);
1839 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1840 }
1841 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1842 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1843 } else {
1844 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1845 if (tdvp == tvp)
1846 vrele(tdvp);
1847 else
1848 vput(tdvp);
1849 if (tvp)
1850 vput(tvp);
1851 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1852 vrele(fromnd.ni_dvp);
1853 vrele(fvp);
1854 }
1855 vrele(tond.ni_startdir);
1856 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1857 out1:
1858 if (fromnd.ni_startdir)
1859 vrele(fromnd.ni_startdir);
1860 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1861 return (error == -1 ? 0 : error);
1862 }
1863
1864 /*
1865 * Make a directory file.
1866 */
1867 /* ARGSUSED */
1868 int
1869 sys_mkdir(p, v, retval)
1870 struct proc *p;
1871 void *v;
1872 register_t *retval;
1873 {
1874 register struct sys_mkdir_args /* {
1875 syscallarg(const char *) path;
1876 syscallarg(int) mode;
1877 } */ *uap = v;
1878 register struct vnode *vp;
1879 struct vattr vattr;
1880 int error;
1881 struct nameidata nd;
1882
1883 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1884 if ((error = namei(&nd)) != 0)
1885 return (error);
1886 vp = nd.ni_vp;
1887 if (vp != NULL) {
1888 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1889 if (nd.ni_dvp == vp)
1890 vrele(nd.ni_dvp);
1891 else
1892 vput(nd.ni_dvp);
1893 vrele(vp);
1894 return (EEXIST);
1895 }
1896 VATTR_NULL(&vattr);
1897 vattr.va_type = VDIR;
1898 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
1899 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1900 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1901 if (!error)
1902 vput(nd.ni_vp);
1903 return (error);
1904 }
1905
1906 /*
1907 * Remove a directory file.
1908 */
1909 /* ARGSUSED */
1910 int
1911 sys_rmdir(p, v, retval)
1912 struct proc *p;
1913 void *v;
1914 register_t *retval;
1915 {
1916 struct sys_rmdir_args /* {
1917 syscallarg(const char *) path;
1918 } */ *uap = v;
1919 register struct vnode *vp;
1920 int error;
1921 struct nameidata nd;
1922
1923 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1924 SCARG(uap, path), p);
1925 if ((error = namei(&nd)) != 0)
1926 return (error);
1927 vp = nd.ni_vp;
1928 if (vp->v_type != VDIR) {
1929 error = ENOTDIR;
1930 goto out;
1931 }
1932 /*
1933 * No rmdir "." please.
1934 */
1935 if (nd.ni_dvp == vp) {
1936 error = EINVAL;
1937 goto out;
1938 }
1939 /*
1940 * The root of a mounted filesystem cannot be deleted.
1941 */
1942 if (vp->v_flag & VROOT)
1943 error = EBUSY;
1944 out:
1945 if (!error) {
1946 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1947 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1948 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1949 } else {
1950 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1951 if (nd.ni_dvp == vp)
1952 vrele(nd.ni_dvp);
1953 else
1954 vput(nd.ni_dvp);
1955 vput(vp);
1956 }
1957 return (error);
1958 }
1959
1960 /*
1961 * Read a block of directory entries in a file system independent format.
1962 */
1963 int
1964 sys_getdirentries(p, v, retval)
1965 struct proc *p;
1966 void *v;
1967 register_t *retval;
1968 {
1969 register struct sys_getdirentries_args /* {
1970 syscallarg(int) fd;
1971 syscallarg(char *) buf;
1972 syscallarg(u_int) count;
1973 syscallarg(long *) basep;
1974 } */ *uap = v;
1975 register struct vnode *vp;
1976 struct file *fp;
1977 struct uio auio;
1978 struct iovec aiov;
1979 long loff;
1980 int error, eofflag;
1981
1982 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1983 return (error);
1984 if ((fp->f_flag & FREAD) == 0)
1985 return (EBADF);
1986 vp = (struct vnode *)fp->f_data;
1987 unionread:
1988 if (vp->v_type != VDIR)
1989 return (EINVAL);
1990 aiov.iov_base = SCARG(uap, buf);
1991 aiov.iov_len = SCARG(uap, count);
1992 auio.uio_iov = &aiov;
1993 auio.uio_iovcnt = 1;
1994 auio.uio_rw = UIO_READ;
1995 auio.uio_segflg = UIO_USERSPACE;
1996 auio.uio_procp = p;
1997 auio.uio_resid = SCARG(uap, count);
1998 VOP_LOCK(vp);
1999 loff = auio.uio_offset = fp->f_offset;
2000 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
2001 fp->f_offset = auio.uio_offset;
2002 VOP_UNLOCK(vp);
2003 if (error)
2004 return (error);
2005
2006 #ifdef UNION
2007 {
2008 extern int (**union_vnodeop_p) __P((void *));
2009 extern struct vnode *union_dircache __P((struct vnode *));
2010
2011 if ((SCARG(uap, count) == auio.uio_resid) &&
2012 (vp->v_op == union_vnodeop_p)) {
2013 struct vnode *lvp;
2014
2015 lvp = union_dircache(vp);
2016 if (lvp != NULLVP) {
2017 struct vattr va;
2018
2019 /*
2020 * If the directory is opaque,
2021 * then don't show lower entries
2022 */
2023 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2024 if (va.va_flags & OPAQUE) {
2025 vput(lvp);
2026 lvp = NULL;
2027 }
2028 }
2029
2030 if (lvp != NULLVP) {
2031 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2032 VOP_UNLOCK(lvp);
2033
2034 if (error) {
2035 vrele(lvp);
2036 return (error);
2037 }
2038 fp->f_data = (caddr_t) lvp;
2039 fp->f_offset = 0;
2040 error = vn_close(vp, FREAD, fp->f_cred, p);
2041 if (error)
2042 return (error);
2043 vp = lvp;
2044 goto unionread;
2045 }
2046 }
2047 }
2048 #endif /* UNION */
2049
2050 if ((SCARG(uap, count) == auio.uio_resid) &&
2051 (vp->v_flag & VROOT) &&
2052 (vp->v_mount->mnt_flag & MNT_UNION)) {
2053 struct vnode *tvp = vp;
2054 vp = vp->v_mount->mnt_vnodecovered;
2055 VREF(vp);
2056 fp->f_data = (caddr_t) vp;
2057 fp->f_offset = 0;
2058 vrele(tvp);
2059 goto unionread;
2060 }
2061 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
2062 *retval = SCARG(uap, count) - auio.uio_resid;
2063 return (error);
2064 }
2065
2066 /*
2067 * Set the mode mask for creation of filesystem nodes.
2068 */
2069 int
2070 sys_umask(p, v, retval)
2071 struct proc *p;
2072 void *v;
2073 register_t *retval;
2074 {
2075 struct sys_umask_args /* {
2076 syscallarg(int) newmask;
2077 } */ *uap = v;
2078 register struct filedesc *fdp;
2079
2080 fdp = p->p_fd;
2081 *retval = fdp->fd_cmask;
2082 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2083 return (0);
2084 }
2085
2086 /*
2087 * Void all references to file by ripping underlying filesystem
2088 * away from vnode.
2089 */
2090 /* ARGSUSED */
2091 int
2092 sys_revoke(p, v, retval)
2093 struct proc *p;
2094 void *v;
2095 register_t *retval;
2096 {
2097 register struct sys_revoke_args /* {
2098 syscallarg(const char *) path;
2099 } */ *uap = v;
2100 register struct vnode *vp;
2101 struct vattr vattr;
2102 int error;
2103 struct nameidata nd;
2104
2105 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2106 if ((error = namei(&nd)) != 0)
2107 return (error);
2108 vp = nd.ni_vp;
2109 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2110 error = EINVAL;
2111 goto out;
2112 }
2113 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2114 goto out;
2115 if (p->p_ucred->cr_uid != vattr.va_uid &&
2116 (error = suser(p->p_ucred, &p->p_acflag)))
2117 goto out;
2118 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2119 vgoneall(vp);
2120 out:
2121 vrele(vp);
2122 return (error);
2123 }
2124
2125 /*
2126 * Convert a user file descriptor to a kernel file entry.
2127 */
2128 int
2129 getvnode(fdp, fd, fpp)
2130 struct filedesc *fdp;
2131 int fd;
2132 struct file **fpp;
2133 {
2134 struct vnode *vp;
2135 struct file *fp;
2136
2137 if ((u_int)fd >= fdp->fd_nfiles ||
2138 (fp = fdp->fd_ofiles[fd]) == NULL)
2139 return (EBADF);
2140 if (fp->f_type != DTYPE_VNODE)
2141 return (EINVAL);
2142 vp = (struct vnode *)fp->f_data;
2143 if (vp->v_type == VBAD)
2144 return (EBADF);
2145 *fpp = fp;
2146 return (0);
2147 }
2148