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