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