vfs_syscalls.c revision 1.67 1 /* $NetBSD: vfs_syscalls.c,v 1.67 1996/02/09 15:39:12 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 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
951 if ((error = namei(&nd)) != 0)
952 goto out;
953 if (nd.ni_vp) {
954 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
955 if (nd.ni_dvp == nd.ni_vp)
956 vrele(nd.ni_dvp);
957 else
958 vput(nd.ni_dvp);
959 vrele(nd.ni_vp);
960 error = EEXIST;
961 goto out;
962 }
963 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
964 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
965 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
966 out:
967 vrele(vp);
968 return (error);
969 }
970
971 /*
972 * Make a symbolic link.
973 */
974 /* ARGSUSED */
975 int
976 sys_symlink(p, v, retval)
977 struct proc *p;
978 void *v;
979 register_t *retval;
980 {
981 register struct sys_symlink_args /* {
982 syscallarg(char *) path;
983 syscallarg(char *) link;
984 } */ *uap = v;
985 struct vattr vattr;
986 char *path;
987 int error;
988 struct nameidata nd;
989
990 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
991 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
992 if (error)
993 goto out;
994 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
995 if ((error = namei(&nd)) != 0)
996 goto out;
997 if (nd.ni_vp) {
998 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
999 if (nd.ni_dvp == nd.ni_vp)
1000 vrele(nd.ni_dvp);
1001 else
1002 vput(nd.ni_dvp);
1003 vrele(nd.ni_vp);
1004 error = EEXIST;
1005 goto out;
1006 }
1007 VATTR_NULL(&vattr);
1008 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1009 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1010 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1011 out:
1012 FREE(path, M_NAMEI);
1013 return (error);
1014 }
1015
1016 /*
1017 * Delete a whiteout from the filesystem.
1018 */
1019 /* ARGSUSED */
1020 int
1021 sys_undelete(p, v, retval)
1022 struct proc *p;
1023 void *v;
1024 register_t *retval;
1025 {
1026 register struct sys_undelete_args /* {
1027 syscallarg(char *) path;
1028 } */ *uap = v;
1029 int error;
1030 struct nameidata nd;
1031
1032 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1033 SCARG(uap, path), p);
1034 error = namei(&nd);
1035 if (error)
1036 return (error);
1037
1038 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1039 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1040 if (nd.ni_dvp == nd.ni_vp)
1041 vrele(nd.ni_dvp);
1042 else
1043 vput(nd.ni_dvp);
1044 if (nd.ni_vp)
1045 vrele(nd.ni_vp);
1046 return (EEXIST);
1047 }
1048
1049 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1050 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1051 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1052 vput(nd.ni_dvp);
1053 return (error);
1054 }
1055
1056 /*
1057 * Delete a name from the filesystem.
1058 */
1059 /* ARGSUSED */
1060 int
1061 sys_unlink(p, v, retval)
1062 struct proc *p;
1063 void *v;
1064 register_t *retval;
1065 {
1066 struct sys_unlink_args /* {
1067 syscallarg(char *) path;
1068 } */ *uap = v;
1069 register struct vnode *vp;
1070 int error;
1071 struct nameidata nd;
1072
1073 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1074 SCARG(uap, path), p);
1075 if ((error = namei(&nd)) != 0)
1076 return (error);
1077 vp = nd.ni_vp;
1078
1079 /*
1080 * The root of a mounted filesystem cannot be deleted.
1081 */
1082 if (vp->v_flag & VROOT) {
1083 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1084 if (nd.ni_dvp == vp)
1085 vrele(nd.ni_dvp);
1086 else
1087 vput(nd.ni_dvp);
1088 vput(vp);
1089 error = EBUSY;
1090 goto out;
1091 }
1092
1093 if (vp->v_flag & VTEXT)
1094 (void)vnode_pager_uncache(vp);
1095 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1096 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1097 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1098 out:
1099 return (error);
1100 }
1101
1102 /*
1103 * Reposition read/write file offset.
1104 */
1105 int
1106 sys_lseek(p, v, retval)
1107 struct proc *p;
1108 void *v;
1109 register_t *retval;
1110 {
1111 register struct sys_lseek_args /* {
1112 syscallarg(int) fd;
1113 syscallarg(int) pad;
1114 syscallarg(off_t) offset;
1115 syscallarg(int) whence;
1116 } */ *uap = v;
1117 struct ucred *cred = p->p_ucred;
1118 register struct filedesc *fdp = p->p_fd;
1119 register struct file *fp;
1120 struct vattr vattr;
1121 int error;
1122
1123 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1124 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1125 return (EBADF);
1126 if (fp->f_type != DTYPE_VNODE)
1127 return (ESPIPE);
1128 switch (SCARG(uap, whence)) {
1129 case L_INCR:
1130 fp->f_offset += SCARG(uap, offset);
1131 break;
1132 case L_XTND:
1133 error = VOP_GETATTR((struct vnode *)fp->f_data, &vattr,
1134 cred, p);
1135 if (error)
1136 return (error);
1137 fp->f_offset = SCARG(uap, offset) + vattr.va_size;
1138 break;
1139 case L_SET:
1140 fp->f_offset = SCARG(uap, offset);
1141 break;
1142 default:
1143 return (EINVAL);
1144 }
1145 *(off_t *)retval = fp->f_offset;
1146 return (0);
1147 }
1148
1149 /*
1150 * Check access permissions.
1151 */
1152 int
1153 sys_access(p, v, retval)
1154 struct proc *p;
1155 void *v;
1156 register_t *retval;
1157 {
1158 register struct sys_access_args /* {
1159 syscallarg(char *) path;
1160 syscallarg(int) flags;
1161 } */ *uap = v;
1162 register struct ucred *cred = p->p_ucred;
1163 register struct vnode *vp;
1164 int error, flags, t_gid, t_uid;
1165 struct nameidata nd;
1166
1167 t_uid = cred->cr_uid;
1168 t_gid = cred->cr_gid;
1169 cred->cr_uid = p->p_cred->p_ruid;
1170 cred->cr_gid = p->p_cred->p_rgid;
1171 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1172 SCARG(uap, path), p);
1173 if ((error = namei(&nd)) != 0)
1174 goto out1;
1175 vp = nd.ni_vp;
1176
1177 /* Flags == 0 means only check for existence. */
1178 if (SCARG(uap, flags)) {
1179 flags = 0;
1180 if (SCARG(uap, flags) & R_OK)
1181 flags |= VREAD;
1182 if (SCARG(uap, flags) & W_OK)
1183 flags |= VWRITE;
1184 if (SCARG(uap, flags) & X_OK)
1185 flags |= VEXEC;
1186 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1187 error = VOP_ACCESS(vp, flags, cred, p);
1188 }
1189 vput(vp);
1190 out1:
1191 cred->cr_uid = t_uid;
1192 cred->cr_gid = t_gid;
1193 return (error);
1194 }
1195
1196 /*
1197 * Get file status; this version follows links.
1198 */
1199 /* ARGSUSED */
1200 int
1201 sys_stat(p, v, retval)
1202 struct proc *p;
1203 void *v;
1204 register_t *retval;
1205 {
1206 register struct sys_stat_args /* {
1207 syscallarg(char *) path;
1208 syscallarg(struct stat *) ub;
1209 } */ *uap = v;
1210 struct stat sb;
1211 int error;
1212 struct nameidata nd;
1213
1214 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1215 SCARG(uap, path), p);
1216 if ((error = namei(&nd)) != 0)
1217 return (error);
1218 error = vn_stat(nd.ni_vp, &sb, p);
1219 vput(nd.ni_vp);
1220 if (error)
1221 return (error);
1222 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1223 return (error);
1224 }
1225
1226 /*
1227 * Get file status; this version does not follow links.
1228 */
1229 /* ARGSUSED */
1230 int
1231 sys_lstat(p, v, retval)
1232 struct proc *p;
1233 void *v;
1234 register_t *retval;
1235 {
1236 register struct sys_lstat_args /* {
1237 syscallarg(char *) path;
1238 syscallarg(struct stat *) ub;
1239 } */ *uap = v;
1240 struct stat sb;
1241 int error;
1242 struct nameidata nd;
1243
1244 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1245 SCARG(uap, path), p);
1246 if ((error = namei(&nd)) != 0)
1247 return (error);
1248 error = vn_stat(nd.ni_vp, &sb, p);
1249 vput(nd.ni_vp);
1250 if (error)
1251 return (error);
1252 error = copyout((caddr_t)&sb, (caddr_t)SCARG(uap, ub), sizeof (sb));
1253 return (error);
1254 }
1255
1256 /*
1257 * Get configurable pathname variables.
1258 */
1259 /* ARGSUSED */
1260 int
1261 sys_pathconf(p, v, retval)
1262 struct proc *p;
1263 void *v;
1264 register_t *retval;
1265 {
1266 register struct sys_pathconf_args /* {
1267 syscallarg(char *) path;
1268 syscallarg(int) name;
1269 } */ *uap = v;
1270 int error;
1271 struct nameidata nd;
1272
1273 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1274 SCARG(uap, path), p);
1275 if ((error = namei(&nd)) != 0)
1276 return (error);
1277 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1278 vput(nd.ni_vp);
1279 return (error);
1280 }
1281
1282 /*
1283 * Return target name of a symbolic link.
1284 */
1285 /* ARGSUSED */
1286 int
1287 sys_readlink(p, v, retval)
1288 struct proc *p;
1289 void *v;
1290 register_t *retval;
1291 {
1292 register struct sys_readlink_args /* {
1293 syscallarg(char *) path;
1294 syscallarg(char *) buf;
1295 syscallarg(int) count;
1296 } */ *uap = v;
1297 register struct vnode *vp;
1298 struct iovec aiov;
1299 struct uio auio;
1300 int error;
1301 struct nameidata nd;
1302
1303 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1304 SCARG(uap, path), p);
1305 if ((error = namei(&nd)) != 0)
1306 return (error);
1307 vp = nd.ni_vp;
1308 if (vp->v_type != VLNK)
1309 error = EINVAL;
1310 else {
1311 aiov.iov_base = SCARG(uap, buf);
1312 aiov.iov_len = SCARG(uap, count);
1313 auio.uio_iov = &aiov;
1314 auio.uio_iovcnt = 1;
1315 auio.uio_offset = 0;
1316 auio.uio_rw = UIO_READ;
1317 auio.uio_segflg = UIO_USERSPACE;
1318 auio.uio_procp = p;
1319 auio.uio_resid = SCARG(uap, count);
1320 error = VOP_READLINK(vp, &auio, p->p_ucred);
1321 }
1322 vput(vp);
1323 *retval = SCARG(uap, count) - auio.uio_resid;
1324 return (error);
1325 }
1326
1327 /*
1328 * Change flags of a file given a path name.
1329 */
1330 /* ARGSUSED */
1331 int
1332 sys_chflags(p, v, retval)
1333 struct proc *p;
1334 void *v;
1335 register_t *retval;
1336 {
1337 register struct sys_chflags_args /* {
1338 syscallarg(char *) path;
1339 syscallarg(int) flags;
1340 } */ *uap = v;
1341 register struct vnode *vp;
1342 struct vattr vattr;
1343 int error;
1344 struct nameidata nd;
1345
1346 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1347 if ((error = namei(&nd)) != 0)
1348 return (error);
1349 vp = nd.ni_vp;
1350 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1351 VOP_LOCK(vp);
1352 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1353 error = EROFS;
1354 else {
1355 VATTR_NULL(&vattr);
1356 vattr.va_flags = SCARG(uap, flags);
1357 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1358 }
1359 vput(vp);
1360 return (error);
1361 }
1362
1363 /*
1364 * Change flags of a file given a file descriptor.
1365 */
1366 /* ARGSUSED */
1367 int
1368 sys_fchflags(p, v, retval)
1369 struct proc *p;
1370 void *v;
1371 register_t *retval;
1372 {
1373 register struct sys_fchflags_args /* {
1374 syscallarg(int) fd;
1375 syscallarg(int) flags;
1376 } */ *uap = v;
1377 struct vattr vattr;
1378 struct vnode *vp;
1379 struct file *fp;
1380 int error;
1381
1382 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1383 return (error);
1384 vp = (struct vnode *)fp->f_data;
1385 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1386 VOP_LOCK(vp);
1387 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1388 error = EROFS;
1389 else {
1390 VATTR_NULL(&vattr);
1391 vattr.va_flags = SCARG(uap, flags);
1392 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1393 }
1394 VOP_UNLOCK(vp);
1395 return (error);
1396 }
1397
1398 /*
1399 * Change mode of a file given path name.
1400 */
1401 /* ARGSUSED */
1402 int
1403 sys_chmod(p, v, retval)
1404 struct proc *p;
1405 void *v;
1406 register_t *retval;
1407 {
1408 register struct sys_chmod_args /* {
1409 syscallarg(char *) path;
1410 syscallarg(int) mode;
1411 } */ *uap = v;
1412 register struct vnode *vp;
1413 struct vattr vattr;
1414 int error;
1415 struct nameidata nd;
1416
1417 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1418 if ((error = namei(&nd)) != 0)
1419 return (error);
1420 vp = nd.ni_vp;
1421 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1422 VOP_LOCK(vp);
1423 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1424 error = EROFS;
1425 else {
1426 VATTR_NULL(&vattr);
1427 vattr.va_mode = SCARG(uap, mode) & ALLPERMS;
1428 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1429 }
1430 vput(vp);
1431 return (error);
1432 }
1433
1434 /*
1435 * Change mode of a file given a file descriptor.
1436 */
1437 /* ARGSUSED */
1438 int
1439 sys_fchmod(p, v, retval)
1440 struct proc *p;
1441 void *v;
1442 register_t *retval;
1443 {
1444 register struct sys_fchmod_args /* {
1445 syscallarg(int) fd;
1446 syscallarg(int) mode;
1447 } */ *uap = v;
1448 struct vattr vattr;
1449 struct vnode *vp;
1450 struct file *fp;
1451 int error;
1452
1453 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1454 return (error);
1455 vp = (struct vnode *)fp->f_data;
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 int
1474 sys_chown(p, v, retval)
1475 struct proc *p;
1476 void *v;
1477 register_t *retval;
1478 {
1479 register struct sys_chown_args /* {
1480 syscallarg(char *) path;
1481 syscallarg(int) uid;
1482 syscallarg(int) gid;
1483 } */ *uap = v;
1484 register struct vnode *vp;
1485 struct vattr vattr;
1486 int error;
1487 struct nameidata nd;
1488
1489 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1490 if ((error = namei(&nd)) != 0)
1491 return (error);
1492 vp = nd.ni_vp;
1493 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1494 VOP_LOCK(vp);
1495 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1496 error = EROFS;
1497 else {
1498 VATTR_NULL(&vattr);
1499 vattr.va_uid = SCARG(uap, uid);
1500 vattr.va_gid = SCARG(uap, gid);
1501 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1502 }
1503 vput(vp);
1504 return (error);
1505 }
1506
1507 /*
1508 * Set ownership given a file descriptor.
1509 */
1510 /* ARGSUSED */
1511 int
1512 sys_fchown(p, v, retval)
1513 struct proc *p;
1514 void *v;
1515 register_t *retval;
1516 {
1517 register struct sys_fchown_args /* {
1518 syscallarg(int) fd;
1519 syscallarg(int) uid;
1520 syscallarg(int) gid;
1521 } */ *uap = v;
1522 struct vattr vattr;
1523 struct vnode *vp;
1524 struct file *fp;
1525 int error;
1526
1527 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1528 return (error);
1529 vp = (struct vnode *)fp->f_data;
1530 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1531 VOP_LOCK(vp);
1532 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1533 error = EROFS;
1534 else {
1535 VATTR_NULL(&vattr);
1536 vattr.va_uid = SCARG(uap, uid);
1537 vattr.va_gid = SCARG(uap, gid);
1538 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1539 }
1540 VOP_UNLOCK(vp);
1541 return (error);
1542 }
1543
1544 /*
1545 * Set the access and modification times of a file.
1546 */
1547 /* ARGSUSED */
1548 int
1549 sys_utimes(p, v, retval)
1550 struct proc *p;
1551 void *v;
1552 register_t *retval;
1553 {
1554 register struct sys_utimes_args /* {
1555 syscallarg(char *) path;
1556 syscallarg(struct timeval *) tptr;
1557 } */ *uap = v;
1558 register struct vnode *vp;
1559 struct timeval tv[2];
1560 struct vattr vattr;
1561 int error;
1562 struct nameidata nd;
1563
1564 VATTR_NULL(&vattr);
1565 if (SCARG(uap, tptr) == NULL) {
1566 microtime(&tv[0]);
1567 tv[1] = tv[0];
1568 vattr.va_vaflags |= VA_UTIMES_NULL;
1569 }
1570 else {
1571 error = copyin((caddr_t)SCARG(uap, tptr), (caddr_t)tv,
1572 sizeof (tv));
1573 if (error)
1574 return (error);
1575 }
1576 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1577 if ((error = namei(&nd)) != 0)
1578 return (error);
1579 vp = nd.ni_vp;
1580 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1581 VOP_LOCK(vp);
1582 if (vp->v_mount->mnt_flag & MNT_RDONLY)
1583 error = EROFS;
1584 else {
1585 vattr.va_atime.tv_sec = tv[0].tv_sec;
1586 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1587 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1588 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1589 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1590 }
1591 vput(vp);
1592 return (error);
1593 }
1594
1595 /*
1596 * Truncate a file given its path name.
1597 */
1598 /* ARGSUSED */
1599 int
1600 sys_truncate(p, v, retval)
1601 struct proc *p;
1602 void *v;
1603 register_t *retval;
1604 {
1605 register struct sys_truncate_args /* {
1606 syscallarg(char *) path;
1607 syscallarg(int) pad;
1608 syscallarg(off_t) length;
1609 } */ *uap = v;
1610 register struct vnode *vp;
1611 struct vattr vattr;
1612 int error;
1613 struct nameidata nd;
1614
1615 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1616 if ((error = namei(&nd)) != 0)
1617 return (error);
1618 vp = nd.ni_vp;
1619 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1620 VOP_LOCK(vp);
1621 if (vp->v_type == VDIR)
1622 error = EISDIR;
1623 else if ((error = vn_writechk(vp)) == 0 &&
1624 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1625 VATTR_NULL(&vattr);
1626 vattr.va_size = SCARG(uap, length);
1627 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1628 }
1629 vput(vp);
1630 return (error);
1631 }
1632
1633 /*
1634 * Truncate a file given a file descriptor.
1635 */
1636 /* ARGSUSED */
1637 int
1638 sys_ftruncate(p, v, retval)
1639 struct proc *p;
1640 void *v;
1641 register_t *retval;
1642 {
1643 register struct sys_ftruncate_args /* {
1644 syscallarg(int) fd;
1645 syscallarg(int) pad;
1646 syscallarg(off_t) length;
1647 } */ *uap = v;
1648 struct vattr vattr;
1649 struct vnode *vp;
1650 struct file *fp;
1651 int error;
1652
1653 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1654 return (error);
1655 if ((fp->f_flag & FWRITE) == 0)
1656 return (EINVAL);
1657 vp = (struct vnode *)fp->f_data;
1658 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1659 VOP_LOCK(vp);
1660 if (vp->v_type == VDIR)
1661 error = EISDIR;
1662 else if ((error = vn_writechk(vp)) == 0) {
1663 VATTR_NULL(&vattr);
1664 vattr.va_size = SCARG(uap, length);
1665 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1666 }
1667 VOP_UNLOCK(vp);
1668 return (error);
1669 }
1670
1671 /*
1672 * Sync an open file.
1673 */
1674 /* ARGSUSED */
1675 int
1676 sys_fsync(p, v, retval)
1677 struct proc *p;
1678 void *v;
1679 register_t *retval;
1680 {
1681 struct sys_fsync_args /* {
1682 syscallarg(int) fd;
1683 } */ *uap = v;
1684 register struct vnode *vp;
1685 struct file *fp;
1686 int error;
1687
1688 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1689 return (error);
1690 vp = (struct vnode *)fp->f_data;
1691 VOP_LOCK(vp);
1692 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
1693 VOP_UNLOCK(vp);
1694 return (error);
1695 }
1696
1697 /*
1698 * Rename files. Source and destination must either both be directories,
1699 * or both not be directories. If target is a directory, it must be empty.
1700 */
1701 /* ARGSUSED */
1702 int
1703 sys_rename(p, v, retval)
1704 struct proc *p;
1705 void *v;
1706 register_t *retval;
1707 {
1708 register struct sys_rename_args /* {
1709 syscallarg(char *) from;
1710 syscallarg(char *) to;
1711 } */ *uap = v;
1712 register struct vnode *tvp, *fvp, *tdvp;
1713 struct nameidata fromnd, tond;
1714 int error;
1715
1716 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
1717 SCARG(uap, from), p);
1718 if ((error = namei(&fromnd)) != 0)
1719 return (error);
1720 fvp = fromnd.ni_vp;
1721 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
1722 UIO_USERSPACE, SCARG(uap, to), p);
1723 if ((error = namei(&tond)) != 0) {
1724 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1725 vrele(fromnd.ni_dvp);
1726 vrele(fvp);
1727 goto out1;
1728 }
1729 tdvp = tond.ni_dvp;
1730 tvp = tond.ni_vp;
1731 if (tvp != NULL) {
1732 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
1733 error = ENOTDIR;
1734 goto out;
1735 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
1736 error = EISDIR;
1737 goto out;
1738 }
1739 }
1740 if (fvp == tdvp)
1741 error = EINVAL;
1742 /*
1743 * If source is the same as the destination (that is the
1744 * same inode number with the same name in the same directory),
1745 * then there is nothing to do.
1746 */
1747 if (fvp == tvp && fromnd.ni_dvp == tdvp &&
1748 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
1749 !bcmp(fromnd.ni_cnd.cn_nameptr, tond.ni_cnd.cn_nameptr,
1750 fromnd.ni_cnd.cn_namelen))
1751 error = -1;
1752 out:
1753 if (!error) {
1754 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
1755 if (fromnd.ni_dvp != tdvp)
1756 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1757 if (tvp)
1758 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
1759 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
1760 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
1761 } else {
1762 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
1763 if (tdvp == tvp)
1764 vrele(tdvp);
1765 else
1766 vput(tdvp);
1767 if (tvp)
1768 vput(tvp);
1769 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
1770 vrele(fromnd.ni_dvp);
1771 vrele(fvp);
1772 }
1773 vrele(tond.ni_startdir);
1774 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
1775 out1:
1776 if (fromnd.ni_startdir)
1777 vrele(fromnd.ni_startdir);
1778 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
1779 if (error == -1)
1780 return (0);
1781 return (error);
1782 }
1783
1784 /*
1785 * Make a directory file.
1786 */
1787 /* ARGSUSED */
1788 int
1789 sys_mkdir(p, v, retval)
1790 struct proc *p;
1791 void *v;
1792 register_t *retval;
1793 {
1794 register struct sys_mkdir_args /* {
1795 syscallarg(char *) path;
1796 syscallarg(int) mode;
1797 } */ *uap = v;
1798 register struct vnode *vp;
1799 struct vattr vattr;
1800 int error;
1801 struct nameidata nd;
1802
1803 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1804 if ((error = namei(&nd)) != 0)
1805 return (error);
1806 vp = nd.ni_vp;
1807 if (vp != NULL) {
1808 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1809 if (nd.ni_dvp == vp)
1810 vrele(nd.ni_dvp);
1811 else
1812 vput(nd.ni_dvp);
1813 vrele(vp);
1814 return (EEXIST);
1815 }
1816 VATTR_NULL(&vattr);
1817 vattr.va_type = VDIR;
1818 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
1819 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1820 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1821 if (!error)
1822 vput(nd.ni_vp);
1823 return (error);
1824 }
1825
1826 /*
1827 * Remove a directory file.
1828 */
1829 /* ARGSUSED */
1830 int
1831 sys_rmdir(p, v, retval)
1832 struct proc *p;
1833 void *v;
1834 register_t *retval;
1835 {
1836 struct sys_rmdir_args /* {
1837 syscallarg(char *) path;
1838 } */ *uap = v;
1839 register struct vnode *vp;
1840 int error;
1841 struct nameidata nd;
1842
1843 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1844 SCARG(uap, path), p);
1845 if ((error = namei(&nd)) != 0)
1846 return (error);
1847 vp = nd.ni_vp;
1848 if (vp->v_type != VDIR) {
1849 error = ENOTDIR;
1850 goto out;
1851 }
1852 /*
1853 * No rmdir "." please.
1854 */
1855 if (nd.ni_dvp == vp) {
1856 error = EINVAL;
1857 goto out;
1858 }
1859 /*
1860 * The root of a mounted filesystem cannot be deleted.
1861 */
1862 if (vp->v_flag & VROOT)
1863 error = EBUSY;
1864 out:
1865 if (!error) {
1866 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1867 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1868 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1869 } else {
1870 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1871 if (nd.ni_dvp == vp)
1872 vrele(nd.ni_dvp);
1873 else
1874 vput(nd.ni_dvp);
1875 vput(vp);
1876 }
1877 return (error);
1878 }
1879
1880 /*
1881 * Read a block of directory entries in a file system independent format.
1882 */
1883 int
1884 sys_getdirentries(p, v, retval)
1885 struct proc *p;
1886 void *v;
1887 register_t *retval;
1888 {
1889 register struct sys_getdirentries_args /* {
1890 syscallarg(int) fd;
1891 syscallarg(char *) buf;
1892 syscallarg(u_int) count;
1893 syscallarg(long *) basep;
1894 } */ *uap = v;
1895 register struct vnode *vp;
1896 struct file *fp;
1897 struct uio auio;
1898 struct iovec aiov;
1899 long loff;
1900 int error, eofflag;
1901
1902 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1903 return (error);
1904 if ((fp->f_flag & FREAD) == 0)
1905 return (EBADF);
1906 vp = (struct vnode *)fp->f_data;
1907 unionread:
1908 if (vp->v_type != VDIR)
1909 return (EINVAL);
1910 aiov.iov_base = SCARG(uap, buf);
1911 aiov.iov_len = SCARG(uap, count);
1912 auio.uio_iov = &aiov;
1913 auio.uio_iovcnt = 1;
1914 auio.uio_rw = UIO_READ;
1915 auio.uio_segflg = UIO_USERSPACE;
1916 auio.uio_procp = p;
1917 auio.uio_resid = SCARG(uap, count);
1918 VOP_LOCK(vp);
1919 loff = auio.uio_offset = fp->f_offset;
1920 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
1921 fp->f_offset = auio.uio_offset;
1922 VOP_UNLOCK(vp);
1923 if (error)
1924 return (error);
1925
1926 #ifdef UNION
1927 {
1928 extern int (**union_vnodeop_p) __P((void *));
1929 extern struct vnode *union_dircache __P((struct vnode *));
1930
1931 if ((SCARG(uap, count) == auio.uio_resid) &&
1932 (vp->v_op == union_vnodeop_p)) {
1933 struct vnode *lvp;
1934
1935 lvp = union_dircache(vp);
1936 if (lvp != NULLVP) {
1937 struct vattr va;
1938
1939 /*
1940 * If the directory is opaque,
1941 * then don't show lower entries
1942 */
1943 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
1944 if (va.va_flags & OPAQUE) {
1945 vput(lvp);
1946 lvp = NULL;
1947 }
1948 }
1949
1950 if (lvp != NULLVP) {
1951 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
1952 VOP_UNLOCK(lvp);
1953
1954 if (error) {
1955 vrele(lvp);
1956 return (error);
1957 }
1958 fp->f_data = (caddr_t) lvp;
1959 fp->f_offset = 0;
1960 error = vn_close(vp, FREAD, fp->f_cred, p);
1961 if (error)
1962 return (error);
1963 vp = lvp;
1964 goto unionread;
1965 }
1966 }
1967 }
1968 #endif /* UNION */
1969
1970 if ((SCARG(uap, count) == auio.uio_resid) &&
1971 (vp->v_flag & VROOT) &&
1972 (vp->v_mount->mnt_flag & MNT_UNION)) {
1973 struct vnode *tvp = vp;
1974 vp = vp->v_mount->mnt_vnodecovered;
1975 VREF(vp);
1976 fp->f_data = (caddr_t) vp;
1977 fp->f_offset = 0;
1978 vrele(tvp);
1979 goto unionread;
1980 }
1981 error = copyout((caddr_t)&loff, (caddr_t)SCARG(uap, basep),
1982 sizeof(long));
1983 *retval = SCARG(uap, count) - auio.uio_resid;
1984 return (error);
1985 }
1986
1987 /*
1988 * Set the mode mask for creation of filesystem nodes.
1989 */
1990 int
1991 sys_umask(p, v, retval)
1992 struct proc *p;
1993 void *v;
1994 register_t *retval;
1995 {
1996 struct sys_umask_args /* {
1997 syscallarg(int) newmask;
1998 } */ *uap = v;
1999 register struct filedesc *fdp;
2000
2001 fdp = p->p_fd;
2002 *retval = fdp->fd_cmask;
2003 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2004 return (0);
2005 }
2006
2007 /*
2008 * Void all references to file by ripping underlying filesystem
2009 * away from vnode.
2010 */
2011 /* ARGSUSED */
2012 int
2013 sys_revoke(p, v, retval)
2014 struct proc *p;
2015 void *v;
2016 register_t *retval;
2017 {
2018 register struct sys_revoke_args /* {
2019 syscallarg(char *) path;
2020 } */ *uap = v;
2021 register struct vnode *vp;
2022 struct vattr vattr;
2023 int error;
2024 struct nameidata nd;
2025
2026 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2027 if ((error = namei(&nd)) != 0)
2028 return (error);
2029 vp = nd.ni_vp;
2030 if (vp->v_type != VCHR && vp->v_type != VBLK) {
2031 error = EINVAL;
2032 goto out;
2033 }
2034 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2035 goto out;
2036 if (p->p_ucred->cr_uid != vattr.va_uid &&
2037 (error = suser(p->p_ucred, &p->p_acflag)))
2038 goto out;
2039 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2040 vgoneall(vp);
2041 out:
2042 vrele(vp);
2043 return (error);
2044 }
2045
2046 /*
2047 * Convert a user file descriptor to a kernel file entry.
2048 */
2049 int
2050 getvnode(fdp, fd, fpp)
2051 struct filedesc *fdp;
2052 int fd;
2053 struct file **fpp;
2054 {
2055 struct vnode *vp;
2056 struct file *fp;
2057
2058 if ((u_int)fd >= fdp->fd_nfiles ||
2059 (fp = fdp->fd_ofiles[fd]) == NULL)
2060 return (EBADF);
2061 if (fp->f_type != DTYPE_VNODE)
2062 return (EINVAL);
2063 vp = (struct vnode *)fp->f_data;
2064 if (vp->v_type == VBAD)
2065 return (EBADF);
2066 *fpp = fp;
2067 return (0);
2068 }
2069