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