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