vfs_syscalls.c revision 1.98 1 /* $NetBSD: vfs_syscalls.c,v 1.98 1997/10/03 14:44:26 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; this version follows links.
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 * Change mode of a file given path name; this version does not follow links.
1476 */
1477 /* ARGSUSED */
1478 int
1479 sys_lchmod(p, v, retval)
1480 struct proc *p;
1481 void *v;
1482 register_t *retval;
1483 {
1484 register struct sys_lchmod_args /* {
1485 syscallarg(const char *) path;
1486 syscallarg(int) mode;
1487 } */ *uap = v;
1488 int error;
1489 struct nameidata nd;
1490
1491 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1492 if ((error = namei(&nd)) != 0)
1493 return (error);
1494
1495 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
1496
1497 vrele(nd.ni_vp);
1498 return (error);
1499 }
1500
1501 /*
1502 * Common routine to set mode given a vnode.
1503 */
1504 static int
1505 change_mode(vp, mode, p)
1506 struct vnode *vp;
1507 int mode;
1508 struct proc *p;
1509 {
1510 struct vattr vattr;
1511 int error;
1512
1513 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1514 VOP_LOCK(vp);
1515 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1516 error = EROFS;
1517 else {
1518 VATTR_NULL(&vattr);
1519 vattr.va_mode = mode & ALLPERMS;
1520 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1521 }
1522 VOP_UNLOCK(vp);
1523 return (error);
1524 }
1525
1526 /*
1527 * Set ownership given a path name; this version follows links.
1528 */
1529 /* ARGSUSED */
1530 int
1531 sys_chown(p, v, retval)
1532 struct proc *p;
1533 void *v;
1534 register_t *retval;
1535 {
1536 register struct sys_chown_args /* {
1537 syscallarg(const char *) path;
1538 syscallarg(uid_t) uid;
1539 syscallarg(gid_t) gid;
1540 } */ *uap = v;
1541 int error;
1542 struct nameidata nd;
1543
1544 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1545 if ((error = namei(&nd)) != 0)
1546 return (error);
1547
1548 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p);
1549
1550 vrele(nd.ni_vp);
1551 return (error);
1552 }
1553
1554 /*
1555 * Set ownership given a file descriptor.
1556 */
1557 /* ARGSUSED */
1558 int
1559 sys_fchown(p, v, retval)
1560 struct proc *p;
1561 void *v;
1562 register_t *retval;
1563 {
1564 register struct sys_fchown_args /* {
1565 syscallarg(int) fd;
1566 syscallarg(uid_t) uid;
1567 syscallarg(gid_t) gid;
1568 } */ *uap = v;
1569 int error;
1570 struct file *fp;
1571
1572 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1573 return (error);
1574
1575 return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
1576 SCARG(uap, gid), p));
1577 }
1578
1579 /*
1580 * Set ownership given a path name; this version does not follow links.
1581 */
1582 /* ARGSUSED */
1583 int
1584 sys_lchown(p, v, retval)
1585 struct proc *p;
1586 void *v;
1587 register_t *retval;
1588 {
1589 register struct sys_lchown_args /* {
1590 syscallarg(const char *) path;
1591 syscallarg(uid_t) uid;
1592 syscallarg(gid_t) gid;
1593 } */ *uap = v;
1594 int error;
1595 struct nameidata nd;
1596
1597 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1598 if ((error = namei(&nd)) != 0)
1599 return (error);
1600
1601 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p);
1602
1603 vrele(nd.ni_vp);
1604 return (error);
1605 }
1606
1607 /*
1608 * Common routine to set ownership given a vnode.
1609 */
1610 static int
1611 change_owner(vp, uid, gid, p)
1612 register struct vnode *vp;
1613 uid_t uid;
1614 gid_t gid;
1615 struct proc *p;
1616 {
1617 struct vattr vattr;
1618 mode_t newmode = VNOVAL;
1619 int error;
1620
1621 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1622 VOP_LOCK(vp);
1623 if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1624 error = EROFS;
1625 goto out;
1626 }
1627 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
1628 goto out;
1629
1630 /* Clear (S_ISUID | S_ISGID) bits: alter va_mode only if necessary. */
1631 if (vattr.va_mode & (S_ISUID | S_ISGID))
1632 newmode = vattr.va_mode & ~(S_ISUID | S_ISGID);
1633
1634 VATTR_NULL(&vattr);
1635 vattr.va_uid = uid;
1636 vattr.va_gid = gid;
1637 vattr.va_mode = newmode;
1638 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1639
1640 out:
1641 VOP_UNLOCK(vp);
1642 return (error);
1643 }
1644
1645 /*
1646 * Set the access and modification times given a path name; this
1647 * version follows links.
1648 */
1649 /* ARGSUSED */
1650 int
1651 sys_utimes(p, v, retval)
1652 struct proc *p;
1653 void *v;
1654 register_t *retval;
1655 {
1656 register struct sys_utimes_args /* {
1657 syscallarg(const char *) path;
1658 syscallarg(const struct timeval *) tptr;
1659 } */ *uap = v;
1660 int error;
1661 struct nameidata nd;
1662
1663 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1664 if ((error = namei(&nd)) != 0)
1665 return (error);
1666
1667 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1668
1669 vrele(nd.ni_vp);
1670 return (error);
1671 }
1672
1673 /*
1674 * Set the access and modification times given a file descriptor.
1675 */
1676 /* ARGSUSED */
1677 int
1678 sys_futimes(p, v, retval)
1679 struct proc *p;
1680 void *v;
1681 register_t *retval;
1682 {
1683 register struct sys_futimes_args /* {
1684 syscallarg(int) fd;
1685 syscallarg(const struct timeval *) tptr;
1686 } */ *uap = v;
1687 int error;
1688 struct file *fp;
1689
1690 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1691 return (error);
1692
1693 return (change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr),
1694 p));
1695 }
1696
1697 /*
1698 * Set the access and modification times given a path name; this
1699 * version does not follow links.
1700 */
1701 /* ARGSUSED */
1702 int
1703 sys_lutimes(p, v, retval)
1704 struct proc *p;
1705 void *v;
1706 register_t *retval;
1707 {
1708 register struct sys_lutimes_args /* {
1709 syscallarg(const char *) path;
1710 syscallarg(const struct timeval *) tptr;
1711 } */ *uap = v;
1712 int error;
1713 struct nameidata nd;
1714
1715 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1716 if ((error = namei(&nd)) != 0)
1717 return (error);
1718
1719 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1720
1721 vrele(nd.ni_vp);
1722 return (error);
1723 }
1724
1725 /*
1726 * Common routine to set access and modification times given a vnode.
1727 */
1728 static int
1729 change_utimes(vp, tptr, p)
1730 struct vnode *vp;
1731 const struct timeval *tptr;
1732 struct proc *p;
1733 {
1734 struct timeval tv[2];
1735 struct vattr vattr;
1736 int error;
1737
1738 VATTR_NULL(&vattr);
1739 if (tptr == NULL) {
1740 microtime(&tv[0]);
1741 tv[1] = tv[0];
1742 vattr.va_vaflags |= VA_UTIMES_NULL;
1743 } else {
1744 error = copyin(tptr, tv, sizeof (tv));
1745 if (error)
1746 return (error);
1747 }
1748 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1749 VOP_LOCK(vp);
1750 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1751 error = EROFS;
1752 else {
1753 vattr.va_atime.tv_sec = tv[0].tv_sec;
1754 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1755 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1756 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1757 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1758 }
1759 VOP_UNLOCK(vp);
1760 return (error);
1761 }
1762
1763 /*
1764 * Truncate a file given its path name.
1765 */
1766 /* ARGSUSED */
1767 int
1768 sys_truncate(p, v, retval)
1769 struct proc *p;
1770 void *v;
1771 register_t *retval;
1772 {
1773 register struct sys_truncate_args /* {
1774 syscallarg(const char *) path;
1775 syscallarg(int) pad;
1776 syscallarg(off_t) length;
1777 } */ *uap = v;
1778 register struct vnode *vp;
1779 struct vattr vattr;
1780 int error;
1781 struct nameidata nd;
1782
1783 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1784 if ((error = namei(&nd)) != 0)
1785 return (error);
1786 vp = nd.ni_vp;
1787 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1788 VOP_LOCK(vp);
1789 if (vp->v_type == VDIR)
1790 error = EISDIR;
1791 else if ((error = vn_writechk(vp)) == 0 &&
1792 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1793 VATTR_NULL(&vattr);
1794 vattr.va_size = SCARG(uap, length);
1795 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1796 }
1797 vput(vp);
1798 return (error);
1799 }
1800
1801 /*
1802 * Truncate a file given a file descriptor.
1803 */
1804 /* ARGSUSED */
1805 int
1806 sys_ftruncate(p, v, retval)
1807 struct proc *p;
1808 void *v;
1809 register_t *retval;
1810 {
1811 register struct sys_ftruncate_args /* {
1812 syscallarg(int) fd;
1813 syscallarg(int) pad;
1814 syscallarg(off_t) length;
1815 } */ *uap = v;
1816 struct vattr vattr;
1817 struct vnode *vp;
1818 struct file *fp;
1819 int error;
1820
1821 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1822 return (error);
1823 if ((fp->f_flag & FWRITE) == 0)
1824 return (EINVAL);
1825 vp = (struct vnode *)fp->f_data;
1826 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1827 VOP_LOCK(vp);
1828 if (vp->v_type == VDIR)
1829 error = EISDIR;
1830 else if ((error = vn_writechk(vp)) == 0) {
1831 VATTR_NULL(&vattr);
1832 vattr.va_size = SCARG(uap, length);
1833 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1834 }
1835 VOP_UNLOCK(vp);
1836 return (error);
1837 }
1838
1839 /*
1840 * Sync an open file.
1841 */
1842 /* ARGSUSED */
1843 int
1844 sys_fsync(p, v, retval)
1845 struct proc *p;
1846 void *v;
1847 register_t *retval;
1848 {
1849 struct sys_fsync_args /* {
1850 syscallarg(int) fd;
1851 } */ *uap = v;
1852 register struct vnode *vp;
1853 struct file *fp;
1854 int error;
1855
1856 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1857 return (error);
1858 vp = (struct vnode *)fp->f_data;
1859 VOP_LOCK(vp);
1860 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1861 VOP_UNLOCK(vp);
1862 return (error);
1863 }
1864
1865 /*
1866 * Rename files, (standard) BSD semantics frontend.
1867 */
1868 /* ARGSUSED */
1869 int
1870 sys_rename(p, v, retval)
1871 struct proc *p;
1872 void *v;
1873 register_t *retval;
1874 {
1875 register struct sys_rename_args /* {
1876 syscallarg(const char *) from;
1877 syscallarg(const char *) to;
1878 } */ *uap = v;
1879
1880 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
1881 }
1882
1883 /*
1884 * Rename files, POSIX semantics frontend.
1885 */
1886 /* ARGSUSED */
1887 int
1888 sys_posix_rename(p, v, retval)
1889 struct proc *p;
1890 void *v;
1891 register_t *retval;
1892 {
1893 register struct sys_posix_rename_args /* {
1894 syscallarg(const char *) from;
1895 syscallarg(const char *) to;
1896 } */ *uap = v;
1897
1898 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
1899 }
1900
1901 /*
1902 * Rename files. Source and destination must either both be directories,
1903 * or both not be directories. If target is a directory, it must be empty.
1904 * If `from' and `to' refer to the same object, the value of the `retain'
1905 * argument is used to determine whether `from' will be
1906 *
1907 * (retain == 0) deleted unless `from' and `to' refer to the same
1908 * object in the file system's name space (BSD).
1909 * (retain == 1) always retained (POSIX).
1910 */
1911 static int
1912 rename_files(from, to, p, retain)
1913 const char *from, *to;
1914 struct proc *p;
1915 int retain;
1916 {
1917 register struct vnode *tvp, *fvp, *tdvp;
1918 struct nameidata fromnd, tond;
1919 int error;
1920
1921 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1922 from, p);
1923 if ((error = namei(&fromnd)) != 0)
1924 return (error);
1925 fvp = fromnd.ni_vp;
1926 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1927 UIO_USERSPACE, to, p);
1928 if ((error = namei(&tond)) != 0) {
1929 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1930 vrele(fromnd.ni_dvp);
1931 vrele(fvp);
1932 goto out1;
1933 }
1934 tdvp = tond.ni_dvp;
1935 tvp = tond.ni_vp;
1936
1937 if (tvp != NULL) {
1938 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1939 error = ENOTDIR;
1940 goto out;
1941 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1942 error = EISDIR;
1943 goto out;
1944 }
1945 }
1946
1947 if (fvp == tdvp)
1948 error = EINVAL;
1949
1950 /*
1951 * Source and destination refer to the same object.
1952 */
1953 if (fvp == tvp) {
1954 if (retain)
1955 error = -1;
1956 else if (fromnd.ni_dvp == tdvp &&
1957 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1958 !bcmp(fromnd.ni_cnd.cn_nameptr,
1959 tond.ni_cnd.cn_nameptr,
1960 fromnd.ni_cnd.cn_namelen))
1961 error = -1;
1962 }
1963
1964 out:
1965 if (!error) {
1966 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1967 if (fromnd.ni_dvp != tdvp)
1968 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1969 if (tvp) {
1970 (void)vnode_pager_uncache(tvp);
1971 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1972 }
1973 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1974 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1975 } else {
1976 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1977 if (tdvp == tvp)
1978 vrele(tdvp);
1979 else
1980 vput(tdvp);
1981 if (tvp)
1982 vput(tvp);
1983 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1984 vrele(fromnd.ni_dvp);
1985 vrele(fvp);
1986 }
1987 vrele(tond.ni_startdir);
1988 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1989 out1:
1990 if (fromnd.ni_startdir)
1991 vrele(fromnd.ni_startdir);
1992 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1993 return (error == -1 ? 0 : error);
1994 }
1995
1996 /*
1997 * Make a directory file.
1998 */
1999 /* ARGSUSED */
2000 int
2001 sys_mkdir(p, v, retval)
2002 struct proc *p;
2003 void *v;
2004 register_t *retval;
2005 {
2006 register struct sys_mkdir_args /* {
2007 syscallarg(const char *) path;
2008 syscallarg(int) mode;
2009 } */ *uap = v;
2010 register struct vnode *vp;
2011 struct vattr vattr;
2012 int error;
2013 struct nameidata nd;
2014
2015 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2016 if ((error = namei(&nd)) != 0)
2017 return (error);
2018 vp = nd.ni_vp;
2019 if (vp != NULL) {
2020 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2021 if (nd.ni_dvp == vp)
2022 vrele(nd.ni_dvp);
2023 else
2024 vput(nd.ni_dvp);
2025 vrele(vp);
2026 return (EEXIST);
2027 }
2028 VATTR_NULL(&vattr);
2029 vattr.va_type = VDIR;
2030 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2031 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2032 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2033 if (!error)
2034 vput(nd.ni_vp);
2035 return (error);
2036 }
2037
2038 /*
2039 * Remove a directory file.
2040 */
2041 /* ARGSUSED */
2042 int
2043 sys_rmdir(p, v, retval)
2044 struct proc *p;
2045 void *v;
2046 register_t *retval;
2047 {
2048 struct sys_rmdir_args /* {
2049 syscallarg(const char *) path;
2050 } */ *uap = v;
2051 register struct vnode *vp;
2052 int error;
2053 struct nameidata nd;
2054
2055 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2056 SCARG(uap, path), p);
2057 if ((error = namei(&nd)) != 0)
2058 return (error);
2059 vp = nd.ni_vp;
2060 if (vp->v_type != VDIR) {
2061 error = ENOTDIR;
2062 goto out;
2063 }
2064 /*
2065 * No rmdir "." please.
2066 */
2067 if (nd.ni_dvp == vp) {
2068 error = EINVAL;
2069 goto out;
2070 }
2071 /*
2072 * The root of a mounted filesystem cannot be deleted.
2073 */
2074 if (vp->v_flag & VROOT)
2075 error = EBUSY;
2076 out:
2077 if (!error) {
2078 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2079 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2080 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2081 } else {
2082 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2083 if (nd.ni_dvp == vp)
2084 vrele(nd.ni_dvp);
2085 else
2086 vput(nd.ni_dvp);
2087 vput(vp);
2088 }
2089 return (error);
2090 }
2091
2092 /*
2093 * Read a block of directory entries in a file system independent format.
2094 */
2095 int
2096 sys_getdirentries(p, v, retval)
2097 struct proc *p;
2098 void *v;
2099 register_t *retval;
2100 {
2101 register struct sys_getdirentries_args /* {
2102 syscallarg(int) fd;
2103 syscallarg(char *) buf;
2104 syscallarg(u_int) count;
2105 syscallarg(long *) basep;
2106 } */ *uap = v;
2107 register struct vnode *vp;
2108 struct file *fp;
2109 struct uio auio;
2110 struct iovec aiov;
2111 long loff;
2112 int error, eofflag;
2113
2114 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2115 return (error);
2116 if ((fp->f_flag & FREAD) == 0)
2117 return (EBADF);
2118 vp = (struct vnode *)fp->f_data;
2119 unionread:
2120 if (vp->v_type != VDIR)
2121 return (EINVAL);
2122 aiov.iov_base = SCARG(uap, buf);
2123 aiov.iov_len = SCARG(uap, count);
2124 auio.uio_iov = &aiov;
2125 auio.uio_iovcnt = 1;
2126 auio.uio_rw = UIO_READ;
2127 auio.uio_segflg = UIO_USERSPACE;
2128 auio.uio_procp = p;
2129 auio.uio_resid = SCARG(uap, count);
2130 VOP_LOCK(vp);
2131 loff = auio.uio_offset = fp->f_offset;
2132 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
2133 fp->f_offset = auio.uio_offset;
2134 VOP_UNLOCK(vp);
2135 if (error)
2136 return (error);
2137
2138 #ifdef UNION
2139 {
2140 extern int (**union_vnodeop_p) __P((void *));
2141 extern struct vnode *union_dircache __P((struct vnode *));
2142
2143 if ((SCARG(uap, count) == auio.uio_resid) &&
2144 (vp->v_op == union_vnodeop_p)) {
2145 struct vnode *lvp;
2146
2147 lvp = union_dircache(vp);
2148 if (lvp != NULLVP) {
2149 struct vattr va;
2150
2151 /*
2152 * If the directory is opaque,
2153 * then don't show lower entries
2154 */
2155 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
2156 if (va.va_flags & OPAQUE) {
2157 vput(lvp);
2158 lvp = NULL;
2159 }
2160 }
2161
2162 if (lvp != NULLVP) {
2163 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
2164 VOP_UNLOCK(lvp);
2165
2166 if (error) {
2167 vrele(lvp);
2168 return (error);
2169 }
2170 fp->f_data = (caddr_t) lvp;
2171 fp->f_offset = 0;
2172 error = vn_close(vp, FREAD, fp->f_cred, p);
2173 if (error)
2174 return (error);
2175 vp = lvp;
2176 goto unionread;
2177 }
2178 }
2179 }
2180 #endif /* UNION */
2181
2182 if ((SCARG(uap, count) == auio.uio_resid) &&
2183 (vp->v_flag & VROOT) &&
2184 (vp->v_mount->mnt_flag & MNT_UNION)) {
2185 struct vnode *tvp = vp;
2186 vp = vp->v_mount->mnt_vnodecovered;
2187 VREF(vp);
2188 fp->f_data = (caddr_t) vp;
2189 fp->f_offset = 0;
2190 vrele(tvp);
2191 goto unionread;
2192 }
2193 error = copyout(&loff, SCARG(uap, basep), sizeof(long));
2194 *retval = SCARG(uap, count) - auio.uio_resid;
2195 return (error);
2196 }
2197
2198 /*
2199 * Set the mode mask for creation of filesystem nodes.
2200 */
2201 int
2202 sys_umask(p, v, retval)
2203 struct proc *p;
2204 void *v;
2205 register_t *retval;
2206 {
2207 struct sys_umask_args /* {
2208 syscallarg(int) newmask;
2209 } */ *uap = v;
2210 register struct filedesc *fdp;
2211
2212 fdp = p->p_fd;
2213 *retval = fdp->fd_cmask;
2214 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2215 return (0);
2216 }
2217
2218 /*
2219 * Void all references to file by ripping underlying filesystem
2220 * away from vnode.
2221 */
2222 /* ARGSUSED */
2223 int
2224 sys_revoke(p, v, retval)
2225 struct proc *p;
2226 void *v;
2227 register_t *retval;
2228 {
2229 register struct sys_revoke_args /* {
2230 syscallarg(const char *) path;
2231 } */ *uap = v;
2232 register struct vnode *vp;
2233 struct vattr vattr;
2234 int error;
2235 struct nameidata nd;
2236
2237 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2238 if ((error = namei(&nd)) != 0)
2239 return (error);
2240 vp = nd.ni_vp;
2241 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2242 error = EINVAL;
2243 goto out;
2244 }
2245 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2246 goto out;
2247 if (p->p_ucred->cr_uid != vattr.va_uid &&
2248 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2249 goto out;
2250 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2251 vgoneall(vp);
2252 out:
2253 vrele(vp);
2254 return (error);
2255 }
2256
2257 /*
2258 * Convert a user file descriptor to a kernel file entry.
2259 */
2260 int
2261 getvnode(fdp, fd, fpp)
2262 struct filedesc *fdp;
2263 int fd;
2264 struct file **fpp;
2265 {
2266 struct vnode *vp;
2267 struct file *fp;
2268
2269 if ((u_int)fd >= fdp->fd_nfiles ||
2270 (fp = fdp->fd_ofiles[fd]) == NULL)
2271 return (EBADF);
2272 if (fp->f_type != DTYPE_VNODE)
2273 return (EINVAL);
2274 vp = (struct vnode *)fp->f_data;
2275 if (vp->v_type == VBAD)
2276 return (EBADF);
2277 *fpp = fp;
2278 return (0);
2279 }
2280