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