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