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