vfs_syscalls.c revision 1.113 1 /* $NetBSD: vfs_syscalls.c,v 1.113 1998/03/01 02:22:36 fvdl 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(int) 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 /*
1765 * If we don't own the file, are trying to change the owner
1766 * of the file, or are not a member of the target group,
1767 * the caller must be superuser or the call fails.
1768 */
1769 if ((p->p_ucred->cr_uid != vattr.va_uid ||
1770 (CHANGED(uid) && uid != vattr.va_uid) ||
1771 (CHANGED(gid) && !groupmember(gid, p->p_ucred))) &&
1772 ((error = suser(p->p_ucred, &p->p_acflag)) != 0))
1773 goto out;
1774
1775 newmode = vattr.va_mode;
1776 if (posix_semantics) {
1777 /*
1778 * POSIX/XPG semantics: clear set-user-id and set-group-id
1779 * bits, unless the caller is the super-user.
1780 */
1781 if (suser(p->p_ucred, NULL) != 0)
1782 newmode &= ~(S_ISUID | S_ISGID);
1783 } else {
1784 /*
1785 * NetBSD semantics: when changing owner and/or group,
1786 * clear the respective bit(s).
1787 */
1788 if (CHANGED(uid))
1789 newmode &= ~S_ISUID;
1790 if (CHANGED(gid))
1791 newmode &= ~S_ISGID;
1792 }
1793 /* Update va_mode iff altered. */
1794 if (vattr.va_mode == newmode)
1795 newmode = VNOVAL;
1796
1797 VATTR_NULL(&vattr);
1798 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
1799 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
1800 vattr.va_mode = newmode;
1801 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1802 #undef CHANGED
1803
1804 out:
1805 VOP_UNLOCK(vp, 0);
1806 return (error);
1807 }
1808
1809 /*
1810 * Set the access and modification times given a path name; this
1811 * version follows links.
1812 */
1813 /* ARGSUSED */
1814 int
1815 sys_utimes(p, v, retval)
1816 struct proc *p;
1817 void *v;
1818 register_t *retval;
1819 {
1820 register struct sys_utimes_args /* {
1821 syscallarg(const char *) path;
1822 syscallarg(const struct timeval *) tptr;
1823 } */ *uap = v;
1824 int error;
1825 struct nameidata nd;
1826
1827 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1828 if ((error = namei(&nd)) != 0)
1829 return (error);
1830
1831 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1832
1833 vrele(nd.ni_vp);
1834 return (error);
1835 }
1836
1837 /*
1838 * Set the access and modification times given a file descriptor.
1839 */
1840 /* ARGSUSED */
1841 int
1842 sys_futimes(p, v, retval)
1843 struct proc *p;
1844 void *v;
1845 register_t *retval;
1846 {
1847 register struct sys_futimes_args /* {
1848 syscallarg(int) fd;
1849 syscallarg(const struct timeval *) tptr;
1850 } */ *uap = v;
1851 int error;
1852 struct file *fp;
1853
1854 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1855 return (error);
1856
1857 return (change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr),
1858 p));
1859 }
1860
1861 /*
1862 * Set the access and modification times given a path name; this
1863 * version does not follow links.
1864 */
1865 /* ARGSUSED */
1866 int
1867 sys_lutimes(p, v, retval)
1868 struct proc *p;
1869 void *v;
1870 register_t *retval;
1871 {
1872 register struct sys_lutimes_args /* {
1873 syscallarg(const char *) path;
1874 syscallarg(const struct timeval *) tptr;
1875 } */ *uap = v;
1876 int error;
1877 struct nameidata nd;
1878
1879 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1880 if ((error = namei(&nd)) != 0)
1881 return (error);
1882
1883 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1884
1885 vrele(nd.ni_vp);
1886 return (error);
1887 }
1888
1889 /*
1890 * Common routine to set access and modification times given a vnode.
1891 */
1892 static int
1893 change_utimes(vp, tptr, p)
1894 struct vnode *vp;
1895 const struct timeval *tptr;
1896 struct proc *p;
1897 {
1898 struct timeval tv[2];
1899 struct vattr vattr;
1900 int error;
1901
1902 VATTR_NULL(&vattr);
1903 if (tptr == NULL) {
1904 microtime(&tv[0]);
1905 tv[1] = tv[0];
1906 vattr.va_vaflags |= VA_UTIMES_NULL;
1907 } else {
1908 error = copyin(tptr, tv, sizeof (tv));
1909 if (error)
1910 return (error);
1911 }
1912 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1913 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1914 vattr.va_atime.tv_sec = tv[0].tv_sec;
1915 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
1916 vattr.va_mtime.tv_sec = tv[1].tv_sec;
1917 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
1918 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1919 VOP_UNLOCK(vp, 0);
1920 return (error);
1921 }
1922
1923 /*
1924 * Truncate a file given its path name.
1925 */
1926 /* ARGSUSED */
1927 int
1928 sys_truncate(p, v, retval)
1929 struct proc *p;
1930 void *v;
1931 register_t *retval;
1932 {
1933 register struct sys_truncate_args /* {
1934 syscallarg(const char *) path;
1935 syscallarg(int) pad;
1936 syscallarg(off_t) length;
1937 } */ *uap = v;
1938 register struct vnode *vp;
1939 struct vattr vattr;
1940 int error;
1941 struct nameidata nd;
1942
1943 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1944 if ((error = namei(&nd)) != 0)
1945 return (error);
1946 vp = nd.ni_vp;
1947 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1948 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1949 if (vp->v_type == VDIR)
1950 error = EISDIR;
1951 else if ((error = vn_writechk(vp)) == 0 &&
1952 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
1953 VATTR_NULL(&vattr);
1954 vattr.va_size = SCARG(uap, length);
1955 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1956 }
1957 vput(vp);
1958 return (error);
1959 }
1960
1961 /*
1962 * Truncate a file given a file descriptor.
1963 */
1964 /* ARGSUSED */
1965 int
1966 sys_ftruncate(p, v, retval)
1967 struct proc *p;
1968 void *v;
1969 register_t *retval;
1970 {
1971 register struct sys_ftruncate_args /* {
1972 syscallarg(int) fd;
1973 syscallarg(int) pad;
1974 syscallarg(off_t) length;
1975 } */ *uap = v;
1976 struct vattr vattr;
1977 struct vnode *vp;
1978 struct file *fp;
1979 int error;
1980
1981 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1982 return (error);
1983 if ((fp->f_flag & FWRITE) == 0)
1984 return (EINVAL);
1985 vp = (struct vnode *)fp->f_data;
1986 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1987 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1988 if (vp->v_type == VDIR)
1989 error = EISDIR;
1990 else if ((error = vn_writechk(vp)) == 0) {
1991 VATTR_NULL(&vattr);
1992 vattr.va_size = SCARG(uap, length);
1993 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
1994 }
1995 VOP_UNLOCK(vp, 0);
1996 return (error);
1997 }
1998
1999 /*
2000 * Sync an open file.
2001 */
2002 /* ARGSUSED */
2003 int
2004 sys_fsync(p, v, retval)
2005 struct proc *p;
2006 void *v;
2007 register_t *retval;
2008 {
2009 struct sys_fsync_args /* {
2010 syscallarg(int) fd;
2011 } */ *uap = v;
2012 register struct vnode *vp;
2013 struct file *fp;
2014 int error;
2015
2016 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2017 return (error);
2018 vp = (struct vnode *)fp->f_data;
2019 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2020 error = VOP_FSYNC(vp, fp->f_cred, MNT_WAIT, p);
2021 VOP_UNLOCK(vp, 0);
2022 return (error);
2023 }
2024
2025 /*
2026 * Rename files, (standard) BSD semantics frontend.
2027 */
2028 /* ARGSUSED */
2029 int
2030 sys_rename(p, v, retval)
2031 struct proc *p;
2032 void *v;
2033 register_t *retval;
2034 {
2035 register struct sys_rename_args /* {
2036 syscallarg(const char *) from;
2037 syscallarg(const char *) to;
2038 } */ *uap = v;
2039
2040 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2041 }
2042
2043 /*
2044 * Rename files, POSIX semantics frontend.
2045 */
2046 /* ARGSUSED */
2047 int
2048 sys___posix_rename(p, v, retval)
2049 struct proc *p;
2050 void *v;
2051 register_t *retval;
2052 {
2053 register struct sys___posix_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, 1));
2059 }
2060
2061 /*
2062 * Rename files. Source and destination must either both be directories,
2063 * or both not be directories. If target is a directory, it must be empty.
2064 * If `from' and `to' refer to the same object, the value of the `retain'
2065 * argument is used to determine whether `from' will be
2066 *
2067 * (retain == 0) deleted unless `from' and `to' refer to the same
2068 * object in the file system's name space (BSD).
2069 * (retain == 1) always retained (POSIX).
2070 */
2071 static int
2072 rename_files(from, to, p, retain)
2073 const char *from, *to;
2074 struct proc *p;
2075 int retain;
2076 {
2077 register struct vnode *tvp, *fvp, *tdvp;
2078 struct nameidata fromnd, tond;
2079 int error;
2080
2081 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2082 from, p);
2083 if ((error = namei(&fromnd)) != 0)
2084 return (error);
2085 fvp = fromnd.ni_vp;
2086 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2087 UIO_USERSPACE, to, p);
2088 if ((error = namei(&tond)) != 0) {
2089 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2090 vrele(fromnd.ni_dvp);
2091 vrele(fvp);
2092 goto out1;
2093 }
2094 tdvp = tond.ni_dvp;
2095 tvp = tond.ni_vp;
2096
2097 if (tvp != NULL) {
2098 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2099 error = ENOTDIR;
2100 goto out;
2101 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2102 error = EISDIR;
2103 goto out;
2104 }
2105 }
2106
2107 if (fvp == tdvp)
2108 error = EINVAL;
2109
2110 /*
2111 * Source and destination refer to the same object.
2112 */
2113 if (fvp == tvp) {
2114 if (retain)
2115 error = -1;
2116 else if (fromnd.ni_dvp == tdvp &&
2117 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2118 !bcmp(fromnd.ni_cnd.cn_nameptr,
2119 tond.ni_cnd.cn_nameptr,
2120 fromnd.ni_cnd.cn_namelen))
2121 error = -1;
2122 }
2123
2124 out:
2125 if (!error) {
2126 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2127 if (fromnd.ni_dvp != tdvp)
2128 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2129 if (tvp) {
2130 #if defined(UVM)
2131 (void)uvm_vnp_uncache(tvp);
2132 #else
2133 (void)vnode_pager_uncache(tvp);
2134 #endif
2135 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2136 }
2137 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2138 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2139 } else {
2140 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2141 if (tdvp == tvp)
2142 vrele(tdvp);
2143 else
2144 vput(tdvp);
2145 if (tvp)
2146 vput(tvp);
2147 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2148 vrele(fromnd.ni_dvp);
2149 vrele(fvp);
2150 }
2151 vrele(tond.ni_startdir);
2152 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2153 out1:
2154 if (fromnd.ni_startdir)
2155 vrele(fromnd.ni_startdir);
2156 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2157 return (error == -1 ? 0 : error);
2158 }
2159
2160 /*
2161 * Make a directory file.
2162 */
2163 /* ARGSUSED */
2164 int
2165 sys_mkdir(p, v, retval)
2166 struct proc *p;
2167 void *v;
2168 register_t *retval;
2169 {
2170 register struct sys_mkdir_args /* {
2171 syscallarg(const char *) path;
2172 syscallarg(int) mode;
2173 } */ *uap = v;
2174 register struct vnode *vp;
2175 struct vattr vattr;
2176 int error;
2177 struct nameidata nd;
2178
2179 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2180 if ((error = namei(&nd)) != 0)
2181 return (error);
2182 vp = nd.ni_vp;
2183 if (vp != NULL) {
2184 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2185 if (nd.ni_dvp == vp)
2186 vrele(nd.ni_dvp);
2187 else
2188 vput(nd.ni_dvp);
2189 vrele(vp);
2190 return (EEXIST);
2191 }
2192 VATTR_NULL(&vattr);
2193 vattr.va_type = VDIR;
2194 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2195 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2196 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2197 if (!error)
2198 vput(nd.ni_vp);
2199 return (error);
2200 }
2201
2202 /*
2203 * Remove a directory file.
2204 */
2205 /* ARGSUSED */
2206 int
2207 sys_rmdir(p, v, retval)
2208 struct proc *p;
2209 void *v;
2210 register_t *retval;
2211 {
2212 struct sys_rmdir_args /* {
2213 syscallarg(const char *) path;
2214 } */ *uap = v;
2215 register struct vnode *vp;
2216 int error;
2217 struct nameidata nd;
2218
2219 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2220 SCARG(uap, path), p);
2221 if ((error = namei(&nd)) != 0)
2222 return (error);
2223 vp = nd.ni_vp;
2224 if (vp->v_type != VDIR) {
2225 error = ENOTDIR;
2226 goto out;
2227 }
2228 /*
2229 * No rmdir "." please.
2230 */
2231 if (nd.ni_dvp == vp) {
2232 error = EINVAL;
2233 goto out;
2234 }
2235 /*
2236 * The root of a mounted filesystem cannot be deleted.
2237 */
2238 if (vp->v_flag & VROOT)
2239 error = EBUSY;
2240 out:
2241 if (!error) {
2242 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2243 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2244 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2245 } else {
2246 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2247 if (nd.ni_dvp == vp)
2248 vrele(nd.ni_dvp);
2249 else
2250 vput(nd.ni_dvp);
2251 vput(vp);
2252 }
2253 return (error);
2254 }
2255
2256 /*
2257 * Read a block of directory entries in a file system independent format.
2258 */
2259 int
2260 sys_getdents(p, v, retval)
2261 struct proc *p;
2262 void *v;
2263 register_t *retval;
2264 {
2265 register struct sys_getdents_args /* {
2266 syscallarg(int) fd;
2267 syscallarg(char *) buf;
2268 syscallarg(size_t) count;
2269 } */ *uap = v;
2270 struct file *fp;
2271 int error, done;
2272
2273 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2274 return (error);
2275 if ((fp->f_flag & FREAD) == 0)
2276 return (EBADF);
2277 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2278 SCARG(uap, count), &done, p, 0, 0);
2279 *retval = done;
2280 return (error);
2281 }
2282
2283 /*
2284 * Set the mode mask for creation of filesystem nodes.
2285 */
2286 int
2287 sys_umask(p, v, retval)
2288 struct proc *p;
2289 void *v;
2290 register_t *retval;
2291 {
2292 struct sys_umask_args /* {
2293 syscallarg(mode_t) newmask;
2294 } */ *uap = v;
2295 register struct filedesc *fdp;
2296
2297 fdp = p->p_fd;
2298 *retval = fdp->fd_cmask;
2299 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2300 return (0);
2301 }
2302
2303 /*
2304 * Void all references to file by ripping underlying filesystem
2305 * away from vnode.
2306 */
2307 /* ARGSUSED */
2308 int
2309 sys_revoke(p, v, retval)
2310 struct proc *p;
2311 void *v;
2312 register_t *retval;
2313 {
2314 register struct sys_revoke_args /* {
2315 syscallarg(const char *) path;
2316 } */ *uap = v;
2317 register struct vnode *vp;
2318 struct vattr vattr;
2319 int error;
2320 struct nameidata nd;
2321
2322 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2323 if ((error = namei(&nd)) != 0)
2324 return (error);
2325 vp = nd.ni_vp;
2326 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2327 goto out;
2328 if (p->p_ucred->cr_uid != vattr.va_uid &&
2329 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2330 goto out;
2331 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2332 VOP_REVOKE(vp, REVOKEALL);
2333 out:
2334 vrele(vp);
2335 return (error);
2336 }
2337
2338 /*
2339 * Convert a user file descriptor to a kernel file entry.
2340 */
2341 int
2342 getvnode(fdp, fd, fpp)
2343 struct filedesc *fdp;
2344 int fd;
2345 struct file **fpp;
2346 {
2347 struct vnode *vp;
2348 struct file *fp;
2349
2350 if ((u_int)fd >= fdp->fd_nfiles ||
2351 (fp = fdp->fd_ofiles[fd]) == NULL)
2352 return (EBADF);
2353 if (fp->f_type != DTYPE_VNODE)
2354 return (EINVAL);
2355 vp = (struct vnode *)fp->f_data;
2356 if (vp->v_type == VBAD)
2357 return (EBADF);
2358 *fpp = fp;
2359 return (0);
2360 }
2361