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