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