vfs_syscalls.c revision 1.119 1 /* $NetBSD: vfs_syscalls.c,v 1.119 1998/06/24 20:58:46 sommerfe 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 register struct sys_mkfifo_args /* {
973 syscallarg(const char *) path;
974 syscallarg(int) mode;
975 } */ *uap = v;
976 struct vattr vattr;
977 int error;
978 struct nameidata nd;
979
980 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
981 if ((error = namei(&nd)) != 0)
982 return (error);
983 if (nd.ni_vp != NULL) {
984 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
985 if (nd.ni_dvp == nd.ni_vp)
986 vrele(nd.ni_dvp);
987 else
988 vput(nd.ni_dvp);
989 vrele(nd.ni_vp);
990 return (EEXIST);
991 }
992 VATTR_NULL(&vattr);
993 vattr.va_type = VFIFO;
994 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
995 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
996 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
997 }
998
999 /*
1000 * Make a hard file link.
1001 */
1002 /* ARGSUSED */
1003 int
1004 sys_link(p, v, retval)
1005 struct proc *p;
1006 void *v;
1007 register_t *retval;
1008 {
1009 register struct sys_link_args /* {
1010 syscallarg(const char *) path;
1011 syscallarg(const char *) link;
1012 } */ *uap = v;
1013 register struct vnode *vp;
1014 struct nameidata nd;
1015 int error;
1016
1017 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1018 if ((error = namei(&nd)) != 0)
1019 return (error);
1020 vp = nd.ni_vp;
1021 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1022 if ((error = namei(&nd)) != 0)
1023 goto out;
1024 if (nd.ni_vp) {
1025 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1026 if (nd.ni_dvp == nd.ni_vp)
1027 vrele(nd.ni_dvp);
1028 else
1029 vput(nd.ni_dvp);
1030 vrele(nd.ni_vp);
1031 error = EEXIST;
1032 goto out;
1033 }
1034 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1035 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1036 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1037 out:
1038 vrele(vp);
1039 return (error);
1040 }
1041
1042 /*
1043 * Make a symbolic link.
1044 */
1045 /* ARGSUSED */
1046 int
1047 sys_symlink(p, v, retval)
1048 struct proc *p;
1049 void *v;
1050 register_t *retval;
1051 {
1052 register struct sys_symlink_args /* {
1053 syscallarg(const char *) path;
1054 syscallarg(const char *) link;
1055 } */ *uap = v;
1056 struct vattr vattr;
1057 char *path;
1058 int error;
1059 struct nameidata nd;
1060
1061 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1062 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1063 if (error)
1064 goto out;
1065 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1066 if ((error = namei(&nd)) != 0)
1067 goto out;
1068 if (nd.ni_vp) {
1069 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1070 if (nd.ni_dvp == nd.ni_vp)
1071 vrele(nd.ni_dvp);
1072 else
1073 vput(nd.ni_dvp);
1074 vrele(nd.ni_vp);
1075 error = EEXIST;
1076 goto out;
1077 }
1078 VATTR_NULL(&vattr);
1079 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1080 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1081 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1082 out:
1083 FREE(path, M_NAMEI);
1084 return (error);
1085 }
1086
1087 /*
1088 * Delete a whiteout from the filesystem.
1089 */
1090 /* ARGSUSED */
1091 int
1092 sys_undelete(p, v, retval)
1093 struct proc *p;
1094 void *v;
1095 register_t *retval;
1096 {
1097 register struct sys_undelete_args /* {
1098 syscallarg(const char *) path;
1099 } */ *uap = v;
1100 int error;
1101 struct nameidata nd;
1102
1103 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1104 SCARG(uap, path), p);
1105 error = namei(&nd);
1106 if (error)
1107 return (error);
1108
1109 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1110 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1111 if (nd.ni_dvp == nd.ni_vp)
1112 vrele(nd.ni_dvp);
1113 else
1114 vput(nd.ni_dvp);
1115 if (nd.ni_vp)
1116 vrele(nd.ni_vp);
1117 return (EEXIST);
1118 }
1119
1120 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1121 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1122 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1123 vput(nd.ni_dvp);
1124 return (error);
1125 }
1126
1127 /*
1128 * Delete a name from the filesystem.
1129 */
1130 /* ARGSUSED */
1131 int
1132 sys_unlink(p, v, retval)
1133 struct proc *p;
1134 void *v;
1135 register_t *retval;
1136 {
1137 struct sys_unlink_args /* {
1138 syscallarg(const char *) path;
1139 } */ *uap = v;
1140 register struct vnode *vp;
1141 int error;
1142 struct nameidata nd;
1143
1144 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1145 SCARG(uap, path), p);
1146 if ((error = namei(&nd)) != 0)
1147 return (error);
1148 vp = nd.ni_vp;
1149
1150 /*
1151 * The root of a mounted filesystem cannot be deleted.
1152 */
1153 if (vp->v_flag & VROOT) {
1154 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1155 if (nd.ni_dvp == vp)
1156 vrele(nd.ni_dvp);
1157 else
1158 vput(nd.ni_dvp);
1159 vput(vp);
1160 error = EBUSY;
1161 goto out;
1162 }
1163
1164 #if defined(UVM)
1165 (void)uvm_vnp_uncache(vp);
1166 #else
1167 (void)vnode_pager_uncache(vp);
1168 #endif
1169
1170 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1171 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1172 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1173 out:
1174 return (error);
1175 }
1176
1177 /*
1178 * Reposition read/write file offset.
1179 */
1180 int
1181 sys_lseek(p, v, retval)
1182 struct proc *p;
1183 void *v;
1184 register_t *retval;
1185 {
1186 register struct sys_lseek_args /* {
1187 syscallarg(int) fd;
1188 syscallarg(int) pad;
1189 syscallarg(off_t) offset;
1190 syscallarg(int) whence;
1191 } */ *uap = v;
1192 struct ucred *cred = p->p_ucred;
1193 register struct filedesc *fdp = p->p_fd;
1194 register struct file *fp;
1195 struct vnode *vp;
1196 struct vattr vattr;
1197 register off_t newoff;
1198 int error;
1199
1200 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1201 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1202 return (EBADF);
1203
1204 vp = (struct vnode *)fp->f_data;
1205 if (fp->f_type != DTYPE_VNODE
1206 || vp->v_type == VFIFO)
1207 return (ESPIPE);
1208
1209 switch (SCARG(uap, whence)) {
1210 case SEEK_CUR:
1211 newoff = fp->f_offset + SCARG(uap, offset);
1212 break;
1213 case SEEK_END:
1214 error = VOP_GETATTR(vp, &vattr, cred, p);
1215 if (error)
1216 return (error);
1217 newoff = SCARG(uap, offset) + vattr.va_size;
1218 break;
1219 case SEEK_SET:
1220 newoff = SCARG(uap, offset);
1221 break;
1222 default:
1223 return (EINVAL);
1224 }
1225 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1226 return (error);
1227
1228 *(off_t *)retval = fp->f_offset = newoff;
1229 return (0);
1230 }
1231
1232 /*
1233 * Check access permissions.
1234 */
1235 int
1236 sys_access(p, v, retval)
1237 struct proc *p;
1238 void *v;
1239 register_t *retval;
1240 {
1241 register struct sys_access_args /* {
1242 syscallarg(const char *) path;
1243 syscallarg(int) flags;
1244 } */ *uap = v;
1245 register struct ucred *cred = p->p_ucred;
1246 register struct vnode *vp;
1247 int error, flags, t_gid, t_uid;
1248 struct nameidata nd;
1249
1250 t_uid = cred->cr_uid;
1251 t_gid = cred->cr_gid;
1252 cred->cr_uid = p->p_cred->p_ruid;
1253 cred->cr_gid = p->p_cred->p_rgid;
1254 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1255 SCARG(uap, path), p);
1256 if ((error = namei(&nd)) != 0)
1257 goto out1;
1258 vp = nd.ni_vp;
1259
1260 /* Flags == 0 means only check for existence. */
1261 if (SCARG(uap, flags)) {
1262 flags = 0;
1263 if (SCARG(uap, flags) & R_OK)
1264 flags |= VREAD;
1265 if (SCARG(uap, flags) & W_OK)
1266 flags |= VWRITE;
1267 if (SCARG(uap, flags) & X_OK)
1268 flags |= VEXEC;
1269 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1270 error = VOP_ACCESS(vp, flags, cred, p);
1271 }
1272 vput(vp);
1273 out1:
1274 cred->cr_uid = t_uid;
1275 cred->cr_gid = t_gid;
1276 return (error);
1277 }
1278
1279 /*
1280 * Get file status; this version follows links.
1281 */
1282 /* ARGSUSED */
1283 int
1284 sys___stat13(p, v, retval)
1285 struct proc *p;
1286 void *v;
1287 register_t *retval;
1288 {
1289 register struct sys___stat13_args /* {
1290 syscallarg(const char *) path;
1291 syscallarg(struct stat *) ub;
1292 } */ *uap = v;
1293 struct stat sb;
1294 int error;
1295 struct nameidata nd;
1296
1297 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1298 SCARG(uap, path), p);
1299 if ((error = namei(&nd)) != 0)
1300 return (error);
1301 error = vn_stat(nd.ni_vp, &sb, p);
1302 vput(nd.ni_vp);
1303 if (error)
1304 return (error);
1305 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1306 return (error);
1307 }
1308
1309 /*
1310 * Get file status; this version does not follow links.
1311 */
1312 /* ARGSUSED */
1313 int
1314 sys___lstat13(p, v, retval)
1315 struct proc *p;
1316 void *v;
1317 register_t *retval;
1318 {
1319 register struct sys___lstat13_args /* {
1320 syscallarg(const char *) path;
1321 syscallarg(struct stat *) ub;
1322 } */ *uap = v;
1323 struct stat sb;
1324 int error;
1325 struct nameidata nd;
1326
1327 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1328 SCARG(uap, path), p);
1329 if ((error = namei(&nd)) != 0)
1330 return (error);
1331 error = vn_stat(nd.ni_vp, &sb, p);
1332 vput(nd.ni_vp);
1333 if (error)
1334 return (error);
1335 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1336 return (error);
1337 }
1338
1339 /*
1340 * Get configurable pathname variables.
1341 */
1342 /* ARGSUSED */
1343 int
1344 sys_pathconf(p, v, retval)
1345 struct proc *p;
1346 void *v;
1347 register_t *retval;
1348 {
1349 register struct sys_pathconf_args /* {
1350 syscallarg(const char *) path;
1351 syscallarg(int) name;
1352 } */ *uap = v;
1353 int error;
1354 struct nameidata nd;
1355
1356 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1357 SCARG(uap, path), p);
1358 if ((error = namei(&nd)) != 0)
1359 return (error);
1360 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1361 vput(nd.ni_vp);
1362 return (error);
1363 }
1364
1365 /*
1366 * Return target name of a symbolic link.
1367 */
1368 /* ARGSUSED */
1369 int
1370 sys_readlink(p, v, retval)
1371 struct proc *p;
1372 void *v;
1373 register_t *retval;
1374 {
1375 register struct sys_readlink_args /* {
1376 syscallarg(const char *) path;
1377 syscallarg(char *) buf;
1378 syscallarg(size_t) count;
1379 } */ *uap = v;
1380 register struct vnode *vp;
1381 struct iovec aiov;
1382 struct uio auio;
1383 int error;
1384 struct nameidata nd;
1385
1386 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1387 SCARG(uap, path), p);
1388 if ((error = namei(&nd)) != 0)
1389 return (error);
1390 vp = nd.ni_vp;
1391 if (vp->v_type != VLNK)
1392 error = EINVAL;
1393 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
1394 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
1395 aiov.iov_base = SCARG(uap, buf);
1396 aiov.iov_len = SCARG(uap, count);
1397 auio.uio_iov = &aiov;
1398 auio.uio_iovcnt = 1;
1399 auio.uio_offset = 0;
1400 auio.uio_rw = UIO_READ;
1401 auio.uio_segflg = UIO_USERSPACE;
1402 auio.uio_procp = p;
1403 auio.uio_resid = SCARG(uap, count);
1404 error = VOP_READLINK(vp, &auio, p->p_ucred);
1405 }
1406 vput(vp);
1407 *retval = SCARG(uap, count) - auio.uio_resid;
1408 return (error);
1409 }
1410
1411 /*
1412 * Change flags of a file given a path name.
1413 */
1414 /* ARGSUSED */
1415 int
1416 sys_chflags(p, v, retval)
1417 struct proc *p;
1418 void *v;
1419 register_t *retval;
1420 {
1421 register struct sys_chflags_args /* {
1422 syscallarg(const char *) path;
1423 syscallarg(u_long) flags;
1424 } */ *uap = v;
1425 register struct vnode *vp;
1426 struct vattr vattr;
1427 int error;
1428 struct nameidata nd;
1429
1430 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1431 if ((error = namei(&nd)) != 0)
1432 return (error);
1433 vp = nd.ni_vp;
1434 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1435 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1436 VATTR_NULL(&vattr);
1437 vattr.va_flags = SCARG(uap, flags);
1438 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1439 vput(vp);
1440 return (error);
1441 }
1442
1443 /*
1444 * Change flags of a file given a file descriptor.
1445 */
1446 /* ARGSUSED */
1447 int
1448 sys_fchflags(p, v, retval)
1449 struct proc *p;
1450 void *v;
1451 register_t *retval;
1452 {
1453 register struct sys_fchflags_args /* {
1454 syscallarg(int) fd;
1455 syscallarg(u_long) flags;
1456 } */ *uap = v;
1457 struct vattr vattr;
1458 struct vnode *vp;
1459 struct file *fp;
1460 int error;
1461
1462 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1463 return (error);
1464 vp = (struct vnode *)fp->f_data;
1465 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1466 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1467 VATTR_NULL(&vattr);
1468 vattr.va_flags = SCARG(uap, flags);
1469 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1470 VOP_UNLOCK(vp, 0);
1471 return (error);
1472 }
1473
1474 /*
1475 * Change mode of a file given path name; this version follows links.
1476 */
1477 /* ARGSUSED */
1478 int
1479 sys_chmod(p, v, retval)
1480 struct proc *p;
1481 void *v;
1482 register_t *retval;
1483 {
1484 register struct sys_chmod_args /* {
1485 syscallarg(const char *) path;
1486 syscallarg(int) mode;
1487 } */ *uap = v;
1488 int error;
1489 struct nameidata nd;
1490
1491 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1492 if ((error = namei(&nd)) != 0)
1493 return (error);
1494
1495 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
1496
1497 vrele(nd.ni_vp);
1498 return (error);
1499 }
1500
1501 /*
1502 * Change mode of a file given a file descriptor.
1503 */
1504 /* ARGSUSED */
1505 int
1506 sys_fchmod(p, v, retval)
1507 struct proc *p;
1508 void *v;
1509 register_t *retval;
1510 {
1511 register struct sys_fchmod_args /* {
1512 syscallarg(int) fd;
1513 syscallarg(int) mode;
1514 } */ *uap = v;
1515 struct file *fp;
1516 int error;
1517
1518 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1519 return (error);
1520
1521 return (change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p));
1522 }
1523
1524 /*
1525 * Change mode of a file given path name; this version does not follow links.
1526 */
1527 /* ARGSUSED */
1528 int
1529 sys_lchmod(p, v, retval)
1530 struct proc *p;
1531 void *v;
1532 register_t *retval;
1533 {
1534 register struct sys_lchmod_args /* {
1535 syscallarg(const char *) path;
1536 syscallarg(int) mode;
1537 } */ *uap = v;
1538 int error;
1539 struct nameidata nd;
1540
1541 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1542 if ((error = namei(&nd)) != 0)
1543 return (error);
1544
1545 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
1546
1547 vrele(nd.ni_vp);
1548 return (error);
1549 }
1550
1551 /*
1552 * Common routine to set mode given a vnode.
1553 */
1554 static int
1555 change_mode(vp, mode, p)
1556 struct vnode *vp;
1557 int mode;
1558 struct proc *p;
1559 {
1560 struct vattr vattr;
1561 int error;
1562
1563 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1564 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1565 VATTR_NULL(&vattr);
1566 vattr.va_mode = mode & ALLPERMS;
1567 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1568 VOP_UNLOCK(vp, 0);
1569 return (error);
1570 }
1571
1572 /*
1573 * Set ownership given a path name; this version follows links.
1574 */
1575 /* ARGSUSED */
1576 int
1577 sys_chown(p, v, retval)
1578 struct proc *p;
1579 void *v;
1580 register_t *retval;
1581 {
1582 register struct sys_chown_args /* {
1583 syscallarg(const char *) path;
1584 syscallarg(uid_t) uid;
1585 syscallarg(gid_t) gid;
1586 } */ *uap = v;
1587 int error;
1588 struct nameidata nd;
1589
1590 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1591 if ((error = namei(&nd)) != 0)
1592 return (error);
1593
1594 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
1595
1596 vrele(nd.ni_vp);
1597 return (error);
1598 }
1599
1600 /*
1601 * Set ownership given a path name; this version follows links.
1602 * Provides POSIX semantics.
1603 */
1604 /* ARGSUSED */
1605 int
1606 sys___posix_chown(p, v, retval)
1607 struct proc *p;
1608 void *v;
1609 register_t *retval;
1610 {
1611 register struct sys_chown_args /* {
1612 syscallarg(const char *) path;
1613 syscallarg(uid_t) uid;
1614 syscallarg(gid_t) gid;
1615 } */ *uap = v;
1616 int error;
1617 struct nameidata nd;
1618
1619 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1620 if ((error = namei(&nd)) != 0)
1621 return (error);
1622
1623 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
1624
1625 vrele(nd.ni_vp);
1626 return (error);
1627 }
1628
1629 /*
1630 * Set ownership given a file descriptor.
1631 */
1632 /* ARGSUSED */
1633 int
1634 sys_fchown(p, v, retval)
1635 struct proc *p;
1636 void *v;
1637 register_t *retval;
1638 {
1639 register struct sys_fchown_args /* {
1640 syscallarg(int) fd;
1641 syscallarg(uid_t) uid;
1642 syscallarg(gid_t) gid;
1643 } */ *uap = v;
1644 int error;
1645 struct file *fp;
1646
1647 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1648 return (error);
1649
1650 return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
1651 SCARG(uap, gid), p, 0));
1652 }
1653
1654 /*
1655 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
1656 */
1657 /* ARGSUSED */
1658 int
1659 sys___posix_fchown(p, v, retval)
1660 struct proc *p;
1661 void *v;
1662 register_t *retval;
1663 {
1664 register struct sys_fchown_args /* {
1665 syscallarg(int) fd;
1666 syscallarg(uid_t) uid;
1667 syscallarg(gid_t) gid;
1668 } */ *uap = v;
1669 int error;
1670 struct file *fp;
1671
1672 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1673 return (error);
1674
1675 return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
1676 SCARG(uap, gid), p, 1));
1677 }
1678
1679 /*
1680 * Set ownership given a path name; this version does not follow links.
1681 */
1682 /* ARGSUSED */
1683 int
1684 sys_lchown(p, v, retval)
1685 struct proc *p;
1686 void *v;
1687 register_t *retval;
1688 {
1689 register struct sys_lchown_args /* {
1690 syscallarg(const char *) path;
1691 syscallarg(uid_t) uid;
1692 syscallarg(gid_t) gid;
1693 } */ *uap = v;
1694 int error;
1695 struct nameidata nd;
1696
1697 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1698 if ((error = namei(&nd)) != 0)
1699 return (error);
1700
1701 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
1702
1703 vrele(nd.ni_vp);
1704 return (error);
1705 }
1706
1707 /*
1708 * Set ownership given a path name; this version does not follow links.
1709 * Provides POSIX/XPG semantics.
1710 */
1711 /* ARGSUSED */
1712 int
1713 sys___posix_lchown(p, v, retval)
1714 struct proc *p;
1715 void *v;
1716 register_t *retval;
1717 {
1718 register struct sys_lchown_args /* {
1719 syscallarg(const char *) path;
1720 syscallarg(uid_t) uid;
1721 syscallarg(gid_t) gid;
1722 } */ *uap = v;
1723 int error;
1724 struct nameidata nd;
1725
1726 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1727 if ((error = namei(&nd)) != 0)
1728 return (error);
1729
1730 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
1731
1732 vrele(nd.ni_vp);
1733 return (error);
1734 }
1735
1736 /*
1737 * Common routine to set ownership given a vnode.
1738 */
1739 static int
1740 change_owner(vp, uid, gid, p, posix_semantics)
1741 register struct vnode *vp;
1742 uid_t uid;
1743 gid_t gid;
1744 struct proc *p;
1745 int posix_semantics;
1746 {
1747 struct vattr vattr;
1748 mode_t newmode;
1749 int error;
1750
1751 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1752 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1753 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
1754 goto out;
1755
1756 #define CHANGED(x) ((x) != -1)
1757 newmode = vattr.va_mode;
1758 if (posix_semantics) {
1759 /*
1760 * POSIX/XPG semantics: if the caller is not the super-user,
1761 * clear set-user-id and set-group-id bits. Both POSIX and
1762 * the XPG consider the behaviour for calls by the super-user
1763 * implementation-defined; we leave the set-user-id and set-
1764 * group-id settings intact in that case.
1765 */
1766 if (suser(p->p_ucred, NULL) != 0)
1767 newmode &= ~(S_ISUID | S_ISGID);
1768 } else {
1769 /*
1770 * NetBSD semantics: when changing owner and/or group,
1771 * clear the respective bit(s).
1772 */
1773 if (CHANGED(uid))
1774 newmode &= ~S_ISUID;
1775 if (CHANGED(gid))
1776 newmode &= ~S_ISGID;
1777 }
1778 /* Update va_mode iff altered. */
1779 if (vattr.va_mode == newmode)
1780 newmode = VNOVAL;
1781
1782 VATTR_NULL(&vattr);
1783 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
1784 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
1785 vattr.va_mode = newmode;
1786 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1787 #undef CHANGED
1788
1789 out:
1790 VOP_UNLOCK(vp, 0);
1791 return (error);
1792 }
1793
1794 /*
1795 * Set the access and modification times given a path name; this
1796 * version follows links.
1797 */
1798 /* ARGSUSED */
1799 int
1800 sys_utimes(p, v, retval)
1801 struct proc *p;
1802 void *v;
1803 register_t *retval;
1804 {
1805 register struct sys_utimes_args /* {
1806 syscallarg(const char *) path;
1807 syscallarg(const struct timeval *) tptr;
1808 } */ *uap = v;
1809 int error;
1810 struct nameidata nd;
1811
1812 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1813 if ((error = namei(&nd)) != 0)
1814 return (error);
1815
1816 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1817
1818 vrele(nd.ni_vp);
1819 return (error);
1820 }
1821
1822 /*
1823 * Set the access and modification times given a file descriptor.
1824 */
1825 /* ARGSUSED */
1826 int
1827 sys_futimes(p, v, retval)
1828 struct proc *p;
1829 void *v;
1830 register_t *retval;
1831 {
1832 register struct sys_futimes_args /* {
1833 syscallarg(int) fd;
1834 syscallarg(const struct timeval *) tptr;
1835 } */ *uap = v;
1836 int error;
1837 struct file *fp;
1838
1839 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1840 return (error);
1841
1842 return (change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr),
1843 p));
1844 }
1845
1846 /*
1847 * Set the access and modification times given a path name; this
1848 * version does not follow links.
1849 */
1850 /* ARGSUSED */
1851 int
1852 sys_lutimes(p, v, retval)
1853 struct proc *p;
1854 void *v;
1855 register_t *retval;
1856 {
1857 register struct sys_lutimes_args /* {
1858 syscallarg(const char *) path;
1859 syscallarg(const struct timeval *) tptr;
1860 } */ *uap = v;
1861 int error;
1862 struct nameidata nd;
1863
1864 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1865 if ((error = namei(&nd)) != 0)
1866 return (error);
1867
1868 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1869
1870 vrele(nd.ni_vp);
1871 return (error);
1872 }
1873
1874 /*
1875 * Common routine to set access and modification times given a vnode.
1876 */
1877 static int
1878 change_utimes(vp, tptr, p)
1879 struct vnode *vp;
1880 const struct timeval *tptr;
1881 struct proc *p;
1882 {
1883 struct timeval tv[2];
1884 struct vattr vattr;
1885 int error;
1886
1887 VATTR_NULL(&vattr);
1888 if (tptr == NULL) {
1889 microtime(&tv[0]);
1890 tv[1] = tv[0];
1891 vattr.va_vaflags |= VA_UTIMES_NULL;
1892 } else {
1893 error = copyin(tptr, tv, sizeof (tv));
1894 if (error)
1895 return (error);
1896 }
1897 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1898 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1899 vattr.va_atime.tv_sec = tv[0].tv_sec;
1900 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1901 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1902 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1903 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1904 VOP_UNLOCK(vp, 0);
1905 return (error);
1906 }
1907
1908 /*
1909 * Truncate a file given its path name.
1910 */
1911 /* ARGSUSED */
1912 int
1913 sys_truncate(p, v, retval)
1914 struct proc *p;
1915 void *v;
1916 register_t *retval;
1917 {
1918 register struct sys_truncate_args /* {
1919 syscallarg(const char *) path;
1920 syscallarg(int) pad;
1921 syscallarg(off_t) length;
1922 } */ *uap = v;
1923 register struct vnode *vp;
1924 struct vattr vattr;
1925 int error;
1926 struct nameidata nd;
1927
1928 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1929 if ((error = namei(&nd)) != 0)
1930 return (error);
1931 vp = nd.ni_vp;
1932 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1933 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1934 if (vp->v_type == VDIR)
1935 error = EISDIR;
1936 else if ((error = vn_writechk(vp)) == 0 &&
1937 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1938 VATTR_NULL(&vattr);
1939 vattr.va_size = SCARG(uap, length);
1940 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1941 }
1942 vput(vp);
1943 return (error);
1944 }
1945
1946 /*
1947 * Truncate a file given a file descriptor.
1948 */
1949 /* ARGSUSED */
1950 int
1951 sys_ftruncate(p, v, retval)
1952 struct proc *p;
1953 void *v;
1954 register_t *retval;
1955 {
1956 register struct sys_ftruncate_args /* {
1957 syscallarg(int) fd;
1958 syscallarg(int) pad;
1959 syscallarg(off_t) length;
1960 } */ *uap = v;
1961 struct vattr vattr;
1962 struct vnode *vp;
1963 struct file *fp;
1964 int error;
1965
1966 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1967 return (error);
1968 if ((fp->f_flag & FWRITE) == 0)
1969 return (EINVAL);
1970 vp = (struct vnode *)fp->f_data;
1971 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1972 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1973 if (vp->v_type == VDIR)
1974 error = EISDIR;
1975 else if ((error = vn_writechk(vp)) == 0) {
1976 VATTR_NULL(&vattr);
1977 vattr.va_size = SCARG(uap, length);
1978 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1979 }
1980 VOP_UNLOCK(vp, 0);
1981 return (error);
1982 }
1983
1984 /*
1985 * Sync an open file.
1986 */
1987 /* ARGSUSED */
1988 int
1989 sys_fsync(p, v, retval)
1990 struct proc *p;
1991 void *v;
1992 register_t *retval;
1993 {
1994 struct sys_fsync_args /* {
1995 syscallarg(int) fd;
1996 } */ *uap = v;
1997 register struct vnode *vp;
1998 struct file *fp;
1999 int error;
2000
2001 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2002 return (error);
2003 vp = (struct vnode *)fp->f_data;
2004 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2005 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
2006 VOP_UNLOCK(vp, 0);
2007 return (error);
2008 }
2009
2010 /*
2011 * Sync the data of an open file.
2012 */
2013 /* ARGSUSED */
2014 int
2015 sys_fdatasync(p, v, retval)
2016 struct proc *p;
2017 void *v;
2018 register_t *retval;
2019 {
2020 struct sys_fdatasync_args /* {
2021 syscallarg(int) fd;
2022 } */ *uap = v;
2023 struct vnode *vp;
2024 struct file *fp;
2025 int error;
2026
2027 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2028 return (error);
2029 vp = (struct vnode *)fp->f_data;
2030 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2031 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
2032 VOP_UNLOCK(vp, 0);
2033 return (error);
2034 }
2035
2036 /*
2037 * Rename files, (standard) BSD semantics frontend.
2038 */
2039 /* ARGSUSED */
2040 int
2041 sys_rename(p, v, retval)
2042 struct proc *p;
2043 void *v;
2044 register_t *retval;
2045 {
2046 register struct sys_rename_args /* {
2047 syscallarg(const char *) from;
2048 syscallarg(const char *) to;
2049 } */ *uap = v;
2050
2051 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2052 }
2053
2054 /*
2055 * Rename files, POSIX semantics frontend.
2056 */
2057 /* ARGSUSED */
2058 int
2059 sys___posix_rename(p, v, retval)
2060 struct proc *p;
2061 void *v;
2062 register_t *retval;
2063 {
2064 register struct sys___posix_rename_args /* {
2065 syscallarg(const char *) from;
2066 syscallarg(const char *) to;
2067 } */ *uap = v;
2068
2069 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2070 }
2071
2072 /*
2073 * Rename files. Source and destination must either both be directories,
2074 * or both not be directories. If target is a directory, it must be empty.
2075 * If `from' and `to' refer to the same object, the value of the `retain'
2076 * argument is used to determine whether `from' will be
2077 *
2078 * (retain == 0) deleted unless `from' and `to' refer to the same
2079 * object in the file system's name space (BSD).
2080 * (retain == 1) always retained (POSIX).
2081 */
2082 static int
2083 rename_files(from, to, p, retain)
2084 const char *from, *to;
2085 struct proc *p;
2086 int retain;
2087 {
2088 register struct vnode *tvp, *fvp, *tdvp;
2089 struct nameidata fromnd, tond;
2090 int error;
2091
2092 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2093 from, p);
2094 if ((error = namei(&fromnd)) != 0)
2095 return (error);
2096 fvp = fromnd.ni_vp;
2097 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2098 UIO_USERSPACE, to, p);
2099 if ((error = namei(&tond)) != 0) {
2100 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2101 vrele(fromnd.ni_dvp);
2102 vrele(fvp);
2103 goto out1;
2104 }
2105 tdvp = tond.ni_dvp;
2106 tvp = tond.ni_vp;
2107
2108 if (tvp != NULL) {
2109 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2110 error = ENOTDIR;
2111 goto out;
2112 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2113 error = EISDIR;
2114 goto out;
2115 }
2116 }
2117
2118 if (fvp == tdvp)
2119 error = EINVAL;
2120
2121 /*
2122 * Source and destination refer to the same object.
2123 */
2124 if (fvp == tvp) {
2125 if (retain)
2126 error = -1;
2127 else if (fromnd.ni_dvp == tdvp &&
2128 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2129 !bcmp(fromnd.ni_cnd.cn_nameptr,
2130 tond.ni_cnd.cn_nameptr,
2131 fromnd.ni_cnd.cn_namelen))
2132 error = -1;
2133 }
2134
2135 out:
2136 if (!error) {
2137 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2138 if (fromnd.ni_dvp != tdvp)
2139 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2140 if (tvp) {
2141 #if defined(UVM)
2142 (void)uvm_vnp_uncache(tvp);
2143 #else
2144 (void)vnode_pager_uncache(tvp);
2145 #endif
2146 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2147 }
2148 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2149 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2150 } else {
2151 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2152 if (tdvp == tvp)
2153 vrele(tdvp);
2154 else
2155 vput(tdvp);
2156 if (tvp)
2157 vput(tvp);
2158 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2159 vrele(fromnd.ni_dvp);
2160 vrele(fvp);
2161 }
2162 vrele(tond.ni_startdir);
2163 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2164 out1:
2165 if (fromnd.ni_startdir)
2166 vrele(fromnd.ni_startdir);
2167 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2168 return (error == -1 ? 0 : error);
2169 }
2170
2171 /*
2172 * Make a directory file.
2173 */
2174 /* ARGSUSED */
2175 int
2176 sys_mkdir(p, v, retval)
2177 struct proc *p;
2178 void *v;
2179 register_t *retval;
2180 {
2181 register struct sys_mkdir_args /* {
2182 syscallarg(const char *) path;
2183 syscallarg(int) mode;
2184 } */ *uap = v;
2185 register struct vnode *vp;
2186 struct vattr vattr;
2187 int error;
2188 struct nameidata nd;
2189
2190 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2191 if ((error = namei(&nd)) != 0)
2192 return (error);
2193 vp = nd.ni_vp;
2194 if (vp != NULL) {
2195 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2196 if (nd.ni_dvp == vp)
2197 vrele(nd.ni_dvp);
2198 else
2199 vput(nd.ni_dvp);
2200 vrele(vp);
2201 return (EEXIST);
2202 }
2203 VATTR_NULL(&vattr);
2204 vattr.va_type = VDIR;
2205 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2206 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2207 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2208 if (!error)
2209 vput(nd.ni_vp);
2210 return (error);
2211 }
2212
2213 /*
2214 * Remove a directory file.
2215 */
2216 /* ARGSUSED */
2217 int
2218 sys_rmdir(p, v, retval)
2219 struct proc *p;
2220 void *v;
2221 register_t *retval;
2222 {
2223 struct sys_rmdir_args /* {
2224 syscallarg(const char *) path;
2225 } */ *uap = v;
2226 register struct vnode *vp;
2227 int error;
2228 struct nameidata nd;
2229
2230 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2231 SCARG(uap, path), p);
2232 if ((error = namei(&nd)) != 0)
2233 return (error);
2234 vp = nd.ni_vp;
2235 if (vp->v_type != VDIR) {
2236 error = ENOTDIR;
2237 goto out;
2238 }
2239 /*
2240 * No rmdir "." please.
2241 */
2242 if (nd.ni_dvp == vp) {
2243 error = EINVAL;
2244 goto out;
2245 }
2246 /*
2247 * The root of a mounted filesystem cannot be deleted.
2248 */
2249 if (vp->v_flag & VROOT)
2250 error = EBUSY;
2251 out:
2252 if (!error) {
2253 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2254 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2255 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2256 } else {
2257 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2258 if (nd.ni_dvp == vp)
2259 vrele(nd.ni_dvp);
2260 else
2261 vput(nd.ni_dvp);
2262 vput(vp);
2263 }
2264 return (error);
2265 }
2266
2267 /*
2268 * Read a block of directory entries in a file system independent format.
2269 */
2270 int
2271 sys_getdents(p, v, retval)
2272 struct proc *p;
2273 void *v;
2274 register_t *retval;
2275 {
2276 register struct sys_getdents_args /* {
2277 syscallarg(int) fd;
2278 syscallarg(char *) buf;
2279 syscallarg(size_t) count;
2280 } */ *uap = v;
2281 struct file *fp;
2282 int error, done;
2283
2284 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2285 return (error);
2286 if ((fp->f_flag & FREAD) == 0)
2287 return (EBADF);
2288 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2289 SCARG(uap, count), &done, p, 0, 0);
2290 *retval = done;
2291 return (error);
2292 }
2293
2294 /*
2295 * Set the mode mask for creation of filesystem nodes.
2296 */
2297 int
2298 sys_umask(p, v, retval)
2299 struct proc *p;
2300 void *v;
2301 register_t *retval;
2302 {
2303 struct sys_umask_args /* {
2304 syscallarg(mode_t) newmask;
2305 } */ *uap = v;
2306 register struct filedesc *fdp;
2307
2308 fdp = p->p_fd;
2309 *retval = fdp->fd_cmask;
2310 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2311 return (0);
2312 }
2313
2314 /*
2315 * Void all references to file by ripping underlying filesystem
2316 * away from vnode.
2317 */
2318 /* ARGSUSED */
2319 int
2320 sys_revoke(p, v, retval)
2321 struct proc *p;
2322 void *v;
2323 register_t *retval;
2324 {
2325 register struct sys_revoke_args /* {
2326 syscallarg(const char *) path;
2327 } */ *uap = v;
2328 register struct vnode *vp;
2329 struct vattr vattr;
2330 int error;
2331 struct nameidata nd;
2332
2333 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2334 if ((error = namei(&nd)) != 0)
2335 return (error);
2336 vp = nd.ni_vp;
2337 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2338 goto out;
2339 if (p->p_ucred->cr_uid != vattr.va_uid &&
2340 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2341 goto out;
2342 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2343 VOP_REVOKE(vp, REVOKEALL);
2344 out:
2345 vrele(vp);
2346 return (error);
2347 }
2348
2349 /*
2350 * Convert a user file descriptor to a kernel file entry.
2351 */
2352 int
2353 getvnode(fdp, fd, fpp)
2354 struct filedesc *fdp;
2355 int fd;
2356 struct file **fpp;
2357 {
2358 struct vnode *vp;
2359 struct file *fp;
2360
2361 if ((u_int)fd >= fdp->fd_nfiles ||
2362 (fp = fdp->fd_ofiles[fd]) == NULL)
2363 return (EBADF);
2364 if (fp->f_type != DTYPE_VNODE)
2365 return (EINVAL);
2366 vp = (struct vnode *)fp->f_data;
2367 if (vp->v_type == VBAD)
2368 return (EBADF);
2369 *fpp = fp;
2370 return (0);
2371 }
2372