vfs_syscalls.c revision 1.133.4.5 1 /* $NetBSD: vfs_syscalls.c,v 1.133.4.5 1999/08/02 22:19:15 thorpej 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_compat_netbsd.h"
44 #include "opt_compat_43.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/filedesc.h>
50 #include <sys/kernel.h>
51 #include <sys/file.h>
52 #include <sys/stat.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/proc.h>
56 #include <sys/uio.h>
57 #include <sys/malloc.h>
58 #include <sys/dirent.h>
59
60 #include <sys/syscallargs.h>
61
62 #include <vm/vm.h>
63 #include <sys/sysctl.h>
64
65 #include <uvm/uvm_extern.h>
66
67 static int change_dir __P((struct nameidata *, struct proc *));
68 static int change_mode __P((struct vnode *, int, struct proc *p));
69 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
70 int));
71 static int change_utimes __P((struct vnode *vp, const struct timeval *,
72 struct proc *p));
73 static int rename_files __P((const char *, const char *, struct proc *, int));
74
75 void checkdirs __P((struct vnode *));
76 int dounmount __P((struct mount *, int, struct proc *));
77
78 /*
79 * Virtual File System System Calls
80 */
81
82 /*
83 * Mount a file system.
84 */
85
86 /*
87 * This table is used to maintain compatibility with 4.3BSD
88 * and NetBSD 0.9 mount syscalls. Note, the order is important!
89 *
90 * Also note that not all of these had actual numbers in 4.3BSD
91 * or NetBSD 0.9!
92 */
93 const char *mountcompatnames[] = {
94 NULL, /* 0 = MOUNT_NONE */
95 MOUNT_FFS, /* 1 */
96 MOUNT_NFS, /* 2 */
97 MOUNT_MFS, /* 3 */
98 MOUNT_MSDOS, /* 4 */
99 MOUNT_LFS, /* 5 */
100 NULL, /* 6 = MOUNT_LOFS */
101 MOUNT_FDESC, /* 7 */
102 MOUNT_PORTAL, /* 8 */
103 MOUNT_NULL, /* 9 */
104 MOUNT_UMAP, /* 10 */
105 MOUNT_KERNFS, /* 11 */
106 MOUNT_PROCFS, /* 12 */
107 MOUNT_AFS, /* 13 */
108 MOUNT_CD9660, /* 14 = MOUNT_ISOFS */
109 MOUNT_UNION, /* 15 */
110 MOUNT_ADOSFS, /* 16 */
111 MOUNT_EXT2FS, /* 17 */
112 MOUNT_CODA, /* 18 */
113 MOUNT_FILECORE, /* 19 */
114 MOUNT_NTFS, /* 20 */
115 };
116 const int nmountcompatnames = sizeof(mountcompatnames) /
117 sizeof(mountcompatnames[0]);
118
119 /* ARGSUSED */
120 int
121 sys_mount(p, v, retval)
122 struct proc *p;
123 void *v;
124 register_t *retval;
125 {
126 register struct sys_mount_args /* {
127 syscallarg(const char *) type;
128 syscallarg(const char *) path;
129 syscallarg(int) flags;
130 syscallarg(void *) data;
131 } */ *uap = v;
132 struct vnode *vp;
133 struct mount *mp;
134 int error, flag = 0;
135 char fstypename[MFSNAMELEN];
136 struct vattr va;
137 struct nameidata nd;
138 struct vfsops *vfs;
139
140 /*
141 * Get vnode to be covered
142 */
143 NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
144 SCARG(uap, path), p);
145 if ((error = namei(&nd)) != 0)
146 return (error);
147 vp = nd.ni_vp;
148 /*
149 * A lookup in VFS_MOUNT might result in an attempt to
150 * lock this vnode again, so make the lock resursive.
151 */
152 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_SETRECURSE);
153 if (SCARG(uap, flags) & MNT_UPDATE) {
154 if ((vp->v_flag & VROOT) == 0) {
155 vput(vp);
156 return (EINVAL);
157 }
158 mp = vp->v_mount;
159 flag = mp->mnt_flag;
160 vfs = mp->mnt_op;
161 /*
162 * We only allow the filesystem to be reloaded if it
163 * is currently mounted read-only.
164 */
165 if ((SCARG(uap, flags) & MNT_RELOAD) &&
166 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
167 vput(vp);
168 return (EOPNOTSUPP); /* Needs translation */
169 }
170 /*
171 * In "highly secure" mode, don't let the caller do anything
172 * but downgrade a filesystem from read-write to read-only.
173 * (see also below; MNT_UPDATE is required.)
174 */
175 if (securelevel >= 2 &&
176 (SCARG(uap, flags) !=
177 (mp->mnt_flag | MNT_RDONLY |
178 MNT_RELOAD | MNT_FORCE | MNT_UPDATE))) {
179 vput(vp);
180 return (EPERM);
181 }
182 mp->mnt_flag |=
183 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
184 /*
185 * Only root, or the user that did the original mount is
186 * permitted to update it.
187 */
188 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
189 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
190 vput(vp);
191 return (error);
192 }
193 /*
194 * Do not allow NFS export by non-root users. For non-root
195 * users, silently enforce MNT_NOSUID and MNT_NODEV, and
196 * MNT_NOEXEC if mount point is already MNT_NOEXEC.
197 */
198 if (p->p_ucred->cr_uid != 0) {
199 if (SCARG(uap, flags) & MNT_EXPORTED) {
200 vput(vp);
201 return (EPERM);
202 }
203 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
204 if (flag & MNT_NOEXEC)
205 SCARG(uap, flags) |= MNT_NOEXEC;
206 }
207 if (vfs_busy(mp, LK_NOWAIT, 0)) {
208 vput(vp);
209 return (EPERM);
210 }
211 VOP_UNLOCK(vp, 0);
212 goto update;
213 } else {
214 if (securelevel >= 2)
215 return (EPERM);
216 }
217 /*
218 * If the user is not root, ensure that they own the directory
219 * onto which we are attempting to mount.
220 */
221 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
222 (va.va_uid != p->p_ucred->cr_uid &&
223 (error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
224 vput(vp);
225 return (error);
226 }
227 /*
228 * Do not allow NFS export by non-root users. For non-root users,
229 * silently enforce MNT_NOSUID and MNT_NODEV, and MNT_NOEXEC if the
230 * mount point is already MNT_NOEXEC.
231 */
232 if (p->p_ucred->cr_uid != 0) {
233 if (SCARG(uap, flags) & MNT_EXPORTED) {
234 vput(vp);
235 return (EPERM);
236 }
237 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
238 if (vp->v_mount->mnt_flag & MNT_NOEXEC)
239 SCARG(uap, flags) |= MNT_NOEXEC;
240 }
241 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
242 return (error);
243 if (vp->v_type != VDIR) {
244 vput(vp);
245 return (ENOTDIR);
246 }
247 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
248 if (error) {
249 #if defined(COMPAT_09) || defined(COMPAT_43)
250 /*
251 * Historically filesystem types were identified by number.
252 * If we get an integer for the filesystem type instead of a
253 * string, we check to see if it matches one of the historic
254 * filesystem types.
255 */
256 u_long fsindex = (u_long)SCARG(uap, type);
257 if (fsindex >= nmountcompatnames ||
258 mountcompatnames[fsindex] == NULL) {
259 vput(vp);
260 return (ENODEV);
261 }
262 strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
263 #else
264 vput(vp);
265 return (error);
266 #endif
267 }
268 #ifdef COMPAT_10
269 /* Accept `ufs' as an alias for `ffs'. */
270 if (!strncmp(fstypename, "ufs", MFSNAMELEN))
271 strncpy(fstypename, "ffs", MFSNAMELEN);
272 #endif
273 if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
274 vput(vp);
275 return (ENODEV);
276 }
277 if (vp->v_mountedhere != NULL) {
278 vput(vp);
279 return (EBUSY);
280 }
281
282 /*
283 * Allocate and initialize the file system.
284 */
285 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
286 M_MOUNT, M_WAITOK);
287 memset((char *)mp, 0, (u_long)sizeof(struct mount));
288 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
289 (void)vfs_busy(mp, LK_NOWAIT, 0);
290 mp->mnt_op = vfs;
291 vfs->vfs_refcount++;
292 mp->mnt_vnodecovered = vp;
293 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
294 update:
295 /*
296 * Set the mount level flags.
297 */
298 if (SCARG(uap, flags) & MNT_RDONLY)
299 mp->mnt_flag |= MNT_RDONLY;
300 else if (mp->mnt_flag & MNT_RDONLY)
301 mp->mnt_flag |= MNT_WANTRDWR;
302 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
303 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
304 MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
305 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
306 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
307 MNT_NOCOREDUMP | MNT_NOATIME | MNT_NODEVMTIME | MNT_SYMPERM);
308 /*
309 * Mount the filesystem.
310 */
311 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
312 if (mp->mnt_flag & MNT_UPDATE) {
313 vrele(vp);
314 if (mp->mnt_flag & MNT_WANTRDWR)
315 mp->mnt_flag &= ~MNT_RDONLY;
316 mp->mnt_flag &=~
317 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
318 if (error)
319 mp->mnt_flag = flag;
320 vfs_unbusy(mp);
321 return (error);
322 }
323 /*
324 * Put the new filesystem on the mount list after root.
325 */
326 cache_purge(vp);
327 if (!error) {
328 vp->v_mountedhere = mp;
329 simple_lock(&mountlist_slock);
330 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
331 simple_unlock(&mountlist_slock);
332 checkdirs(vp);
333 VOP_UNLOCK(vp, 0);
334 vfs_unbusy(mp);
335 (void) VFS_STATFS(mp, &mp->mnt_stat, p);
336 if ((error = VFS_START(mp, 0, p)))
337 vrele(vp);
338 } else {
339 vp->v_mountedhere = (struct mount *)0;
340 vfs->vfs_refcount--;
341 vfs_unbusy(mp);
342 free((caddr_t)mp, M_MOUNT);
343 vput(vp);
344 }
345 return (error);
346 }
347
348 /*
349 * Scan all active processes to see if any of them have a current
350 * or root directory onto which the new filesystem has just been
351 * mounted. If so, replace them with the new mount point.
352 */
353 void
354 checkdirs(olddp)
355 struct vnode *olddp;
356 {
357 struct cwdinfo *cwdi;
358 struct vnode *newdp;
359 struct proc *p;
360
361 if (olddp->v_usecount == 1)
362 return;
363 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
364 panic("mount: lost mount");
365 proclist_lock_read();
366 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
367 cwdi = p->p_cwdi;
368 if (cwdi->cwdi_cdir == olddp) {
369 vrele(cwdi->cwdi_cdir);
370 VREF(newdp);
371 cwdi->cwdi_cdir = newdp;
372 }
373 if (cwdi->cwdi_rdir == olddp) {
374 vrele(cwdi->cwdi_rdir);
375 VREF(newdp);
376 cwdi->cwdi_rdir = newdp;
377 }
378 }
379 proclist_unlock_read();
380 if (rootvnode == olddp) {
381 vrele(rootvnode);
382 VREF(newdp);
383 rootvnode = newdp;
384 }
385 vput(newdp);
386 }
387
388 /*
389 * Unmount a file system.
390 *
391 * Note: unmount takes a path to the vnode mounted on as argument,
392 * not special file (as before).
393 */
394 /* ARGSUSED */
395 int
396 sys_unmount(p, v, retval)
397 struct proc *p;
398 void *v;
399 register_t *retval;
400 {
401 register struct sys_unmount_args /* {
402 syscallarg(const char *) path;
403 syscallarg(int) flags;
404 } */ *uap = v;
405 register struct vnode *vp;
406 struct mount *mp;
407 int error;
408 struct nameidata nd;
409
410 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
411 SCARG(uap, path), p);
412 if ((error = namei(&nd)) != 0)
413 return (error);
414 vp = nd.ni_vp;
415 mp = vp->v_mount;
416
417 /*
418 * Only root, or the user that did the original mount is
419 * permitted to unmount this filesystem.
420 */
421 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
422 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
423 vput(vp);
424 return (error);
425 }
426
427 /*
428 * Don't allow unmounting the root file system.
429 */
430 if (mp->mnt_flag & MNT_ROOTFS) {
431 vput(vp);
432 return (EINVAL);
433 }
434
435 /*
436 * Must be the root of the filesystem
437 */
438 if ((vp->v_flag & VROOT) == 0) {
439 vput(vp);
440 return (EINVAL);
441 }
442 vput(vp);
443
444 if (vfs_busy(mp, 0, 0))
445 return (EBUSY);
446
447 return (dounmount(mp, SCARG(uap, flags), p));
448 }
449
450 /*
451 * Do the actual file system unmount. File system is assumed to have been
452 * marked busy by the caller.
453 */
454 int
455 dounmount(mp, flags, p)
456 register struct mount *mp;
457 int flags;
458 struct proc *p;
459 {
460 struct vnode *coveredvp;
461 int error;
462 int async;
463
464 simple_lock(&mountlist_slock);
465 mp->mnt_flag |= MNT_UNMOUNT;
466 vfs_unbusy(mp);
467 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
468 if (mp->mnt_flag & MNT_EXPUBLIC)
469 vfs_setpublicfs(NULL, NULL, NULL);
470 async = mp->mnt_flag & MNT_ASYNC;
471 mp->mnt_flag &=~ MNT_ASYNC;
472 cache_purgevfs(mp); /* remove cache entries for this file sys */
473 if (((mp->mnt_flag & MNT_RDONLY) ||
474 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
475 (flags & MNT_FORCE))
476 error = VFS_UNMOUNT(mp, flags, p);
477 simple_lock(&mountlist_slock);
478 if (error) {
479 mp->mnt_flag &= ~MNT_UNMOUNT;
480 mp->mnt_flag |= async;
481 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
482 &mountlist_slock);
483 while(mp->mnt_wcnt > 0) {
484 wakeup((caddr_t)mp);
485 sleep(&mp->mnt_wcnt, PVFS);
486 }
487 return (error);
488 }
489 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
490 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
491 coveredvp->v_mountedhere = NULL;
492 vrele(coveredvp);
493 }
494 mp->mnt_op->vfs_refcount--;
495 if (mp->mnt_vnodelist.lh_first != NULL)
496 panic("unmount: dangling vnode");
497 mp->mnt_flag |= MNT_GONE;
498 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
499 while(mp->mnt_wcnt > 0) {
500 wakeup((caddr_t)mp);
501 sleep(&mp->mnt_wcnt, PVFS);
502 }
503 free((caddr_t)mp, M_MOUNT);
504 return (0);
505 }
506
507 /*
508 * Sync each mounted filesystem.
509 */
510 #ifdef DEBUG
511 int syncprt = 0;
512 struct ctldebug debug0 = { "syncprt", &syncprt };
513 #endif
514
515 /* ARGSUSED */
516 int
517 sys_sync(p, v, retval)
518 struct proc *p;
519 void *v;
520 register_t *retval;
521 {
522 register struct mount *mp, *nmp;
523 int asyncflag;
524
525 simple_lock(&mountlist_slock);
526 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
527 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
528 nmp = mp->mnt_list.cqe_prev;
529 continue;
530 }
531 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
532 asyncflag = mp->mnt_flag & MNT_ASYNC;
533 mp->mnt_flag &= ~MNT_ASYNC;
534 uvm_vnp_sync(mp);
535 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
536 if (asyncflag)
537 mp->mnt_flag |= MNT_ASYNC;
538 }
539 simple_lock(&mountlist_slock);
540 nmp = mp->mnt_list.cqe_prev;
541 vfs_unbusy(mp);
542
543 }
544 simple_unlock(&mountlist_slock);
545 #ifdef DEBUG
546 if (syncprt)
547 vfs_bufstats();
548 #endif /* DEBUG */
549 return (0);
550 }
551
552 /*
553 * Change filesystem quotas.
554 */
555 /* ARGSUSED */
556 int
557 sys_quotactl(p, v, retval)
558 struct proc *p;
559 void *v;
560 register_t *retval;
561 {
562 register struct sys_quotactl_args /* {
563 syscallarg(const char *) path;
564 syscallarg(int) cmd;
565 syscallarg(int) uid;
566 syscallarg(caddr_t) arg;
567 } */ *uap = v;
568 register struct mount *mp;
569 int error;
570 struct nameidata nd;
571
572 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
573 if ((error = namei(&nd)) != 0)
574 return (error);
575 mp = nd.ni_vp->v_mount;
576 vrele(nd.ni_vp);
577 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
578 SCARG(uap, arg), p));
579 }
580
581 /*
582 * Get filesystem statistics.
583 */
584 /* ARGSUSED */
585 int
586 sys_statfs(p, v, retval)
587 struct proc *p;
588 void *v;
589 register_t *retval;
590 {
591 register struct sys_statfs_args /* {
592 syscallarg(const char *) path;
593 syscallarg(struct statfs *) buf;
594 } */ *uap = v;
595 register struct mount *mp;
596 register struct statfs *sp;
597 int error;
598 struct nameidata nd;
599
600 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
601 if ((error = namei(&nd)) != 0)
602 return (error);
603 mp = nd.ni_vp->v_mount;
604 sp = &mp->mnt_stat;
605 vrele(nd.ni_vp);
606 if ((error = VFS_STATFS(mp, sp, p)) != 0)
607 return (error);
608 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
609 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
610 }
611
612 /*
613 * Get filesystem statistics.
614 */
615 /* ARGSUSED */
616 int
617 sys_fstatfs(p, v, retval)
618 struct proc *p;
619 void *v;
620 register_t *retval;
621 {
622 register struct sys_fstatfs_args /* {
623 syscallarg(int) fd;
624 syscallarg(struct statfs *) buf;
625 } */ *uap = v;
626 struct file *fp;
627 struct mount *mp;
628 register struct statfs *sp;
629 int error;
630
631 /* getvnode() will use the descriptor for us */
632 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
633 return (error);
634 mp = ((struct vnode *)fp->f_data)->v_mount;
635 sp = &mp->mnt_stat;
636 if ((error = VFS_STATFS(mp, sp, p)) != 0)
637 goto out;
638 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
639 error = copyout(sp, SCARG(uap, buf), sizeof(*sp));
640 out:
641 FILE_UNUSE(fp, p);
642 return (error);
643 }
644
645 /*
646 * Get statistics on all filesystems.
647 */
648 int
649 sys_getfsstat(p, v, retval)
650 struct proc *p;
651 void *v;
652 register_t *retval;
653 {
654 register struct sys_getfsstat_args /* {
655 syscallarg(struct statfs *) buf;
656 syscallarg(long) bufsize;
657 syscallarg(int) flags;
658 } */ *uap = v;
659 register struct mount *mp, *nmp;
660 register struct statfs *sp;
661 caddr_t sfsp;
662 long count, maxcount, error;
663
664 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
665 sfsp = (caddr_t)SCARG(uap, buf);
666 simple_lock(&mountlist_slock);
667 count = 0;
668 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
669 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
670 nmp = mp->mnt_list.cqe_next;
671 continue;
672 }
673 if (sfsp && count < maxcount) {
674 sp = &mp->mnt_stat;
675 /*
676 * If MNT_NOWAIT is specified, do not refresh the
677 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
678 */
679 if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
680 (SCARG(uap, flags) & MNT_WAIT)) &&
681 (error = VFS_STATFS(mp, sp, p)) != 0) {
682 simple_lock(&mountlist_slock);
683 nmp = mp->mnt_list.cqe_next;
684 vfs_unbusy(mp);
685 continue;
686 }
687 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
688 error = copyout(sp, sfsp, sizeof(*sp));
689 if (error) {
690 vfs_unbusy(mp);
691 return (error);
692 }
693 sfsp += sizeof(*sp);
694 }
695 count++;
696 simple_lock(&mountlist_slock);
697 nmp = mp->mnt_list.cqe_next;
698 vfs_unbusy(mp);
699 }
700 simple_unlock(&mountlist_slock);
701 if (sfsp && count > maxcount)
702 *retval = maxcount;
703 else
704 *retval = count;
705 return (0);
706 }
707
708 /*
709 * Change current working directory to a given file descriptor.
710 */
711 /* ARGSUSED */
712 int
713 sys_fchdir(p, v, retval)
714 struct proc *p;
715 void *v;
716 register_t *retval;
717 {
718 struct sys_fchdir_args /* {
719 syscallarg(int) fd;
720 } */ *uap = v;
721 struct filedesc *fdp = p->p_fd;
722 struct cwdinfo *cwdi = p->p_cwdi;
723 struct vnode *vp, *tdp;
724 struct mount *mp;
725 struct file *fp;
726 int error;
727
728 /* getvnode() will use the descriptor for us */
729 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
730 return (error);
731 vp = (struct vnode *)fp->f_data;
732
733 VREF(vp);
734 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
735 if (vp->v_type != VDIR)
736 error = ENOTDIR;
737 else
738 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
739 while (!error && (mp = vp->v_mountedhere) != NULL) {
740 if (vfs_busy(mp, 0, 0))
741 continue;
742 error = VFS_ROOT(mp, &tdp);
743 vfs_unbusy(mp);
744 if (error)
745 break;
746 vput(vp);
747 vp = tdp;
748 }
749 if (error) {
750 vput(vp);
751 goto out;
752 }
753 VOP_UNLOCK(vp, 0);
754
755 /*
756 * Disallow changing to a directory not under the process's
757 * current root directory (if there is one).
758 */
759 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, p)) {
760 vrele(vp);
761 error = EPERM; /* operation not permitted */
762 goto out;
763 }
764
765 vrele(cwdi->cwdi_cdir);
766 cwdi->cwdi_cdir = vp;
767 out:
768 FILE_UNUSE(fp, p);
769 return (error);
770 }
771
772 /*
773 * Change this process's notion of the root directory to a given file descriptor.
774 */
775
776 int
777 sys_fchroot(p, v, retval)
778 struct proc *p;
779 void *v;
780 register_t *retval;
781 {
782 struct sys_fchroot_args *uap = v;
783 struct filedesc *fdp = p->p_fd;
784 struct cwdinfo *cwdi = p->p_cwdi;
785 struct vnode *vp;
786 struct file *fp;
787 int error;
788
789 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
790 return error;
791 /* getvnode() will use the descriptor for us */
792 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
793 return error;
794 vp = (struct vnode *) fp->f_data;
795 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
796 if (vp->v_type != VDIR)
797 error = ENOTDIR;
798 else
799 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
800 VOP_UNLOCK(vp, 0);
801 if (error)
802 goto out;
803 VREF(vp);
804
805 /*
806 * Prevent escaping from chroot by putting the root under
807 * the working directory. Silently chdir to / if we aren't
808 * already there.
809 */
810 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
811 /*
812 * XXX would be more failsafe to change directory to a
813 * deadfs node here instead
814 */
815 vrele(cwdi->cwdi_cdir);
816 VREF(vp);
817 cwdi->cwdi_cdir = vp;
818 }
819
820 if (cwdi->cwdi_rdir != NULL)
821 vrele(cwdi->cwdi_rdir);
822 cwdi->cwdi_rdir = vp;
823 out:
824 FILE_UNUSE(fp, p);
825 return (error);
826 }
827
828
829
830 /*
831 * Change current working directory (``.'').
832 */
833 /* ARGSUSED */
834 int
835 sys_chdir(p, v, retval)
836 struct proc *p;
837 void *v;
838 register_t *retval;
839 {
840 struct sys_chdir_args /* {
841 syscallarg(const char *) path;
842 } */ *uap = v;
843 struct cwdinfo *cwdi = p->p_cwdi;
844 int error;
845 struct nameidata nd;
846
847 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
848 SCARG(uap, path), p);
849 if ((error = change_dir(&nd, p)) != 0)
850 return (error);
851 vrele(cwdi->cwdi_cdir);
852 cwdi->cwdi_cdir = nd.ni_vp;
853 return (0);
854 }
855
856 /*
857 * Change notion of root (``/'') directory.
858 */
859 /* ARGSUSED */
860 int
861 sys_chroot(p, v, retval)
862 struct proc *p;
863 void *v;
864 register_t *retval;
865 {
866 struct sys_chroot_args /* {
867 syscallarg(const char *) path;
868 } */ *uap = v;
869 struct cwdinfo *cwdi = p->p_cwdi;
870 struct vnode *vp;
871 int error;
872 struct nameidata nd;
873
874 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
875 return (error);
876 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
877 SCARG(uap, path), p);
878 if ((error = change_dir(&nd, p)) != 0)
879 return (error);
880 if (cwdi->cwdi_rdir != NULL)
881 vrele(cwdi->cwdi_rdir);
882 vp = nd.ni_vp;
883 cwdi->cwdi_rdir = vp;
884
885 /*
886 * Prevent escaping from chroot by putting the root under
887 * the working directory. Silently chdir to / if we aren't
888 * already there.
889 */
890 if (!vn_isunder(cwdi->cwdi_cdir, vp, p)) {
891 /*
892 * XXX would be more failsafe to change directory to a
893 * deadfs node here instead
894 */
895 vrele(cwdi->cwdi_cdir);
896 VREF(vp);
897 cwdi->cwdi_cdir = vp;
898 }
899
900 return (0);
901 }
902
903 /*
904 * Common routine for chroot and chdir.
905 */
906 static int
907 change_dir(ndp, p)
908 register struct nameidata *ndp;
909 struct proc *p;
910 {
911 struct vnode *vp;
912 int error;
913
914 if ((error = namei(ndp)) != 0)
915 return (error);
916 vp = ndp->ni_vp;
917 if (vp->v_type != VDIR)
918 error = ENOTDIR;
919 else
920 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
921
922 if (error)
923 vput(vp);
924 else
925 VOP_UNLOCK(vp, 0);
926 return (error);
927 }
928
929 /*
930 * Check permissions, allocate an open file structure,
931 * and call the device open routine if any.
932 */
933 int
934 sys_open(p, v, retval)
935 struct proc *p;
936 void *v;
937 register_t *retval;
938 {
939 register struct sys_open_args /* {
940 syscallarg(const char *) path;
941 syscallarg(int) flags;
942 syscallarg(int) mode;
943 } */ *uap = v;
944 struct cwdinfo *cwdi = p->p_cwdi;
945 struct filedesc *fdp = p->p_fd;
946 struct file *fp;
947 struct vnode *vp;
948 int flags, cmode;
949 int type, indx, error;
950 struct flock lf;
951 struct nameidata nd;
952 extern struct fileops vnops;
953
954 flags = FFLAGS(SCARG(uap, flags));
955 if ((flags & (FREAD | FWRITE)) == 0)
956 return (EINVAL);
957 /* falloc() will use the file descriptor for us */
958 if ((error = falloc(p, &fp, &indx)) != 0)
959 return (error);
960 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
961 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
962 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
963 if ((error = vn_open(&nd, flags, cmode)) != 0) {
964 FILE_UNUSE(fp, p);
965 ffree(fp);
966 if ((error == ENODEV || error == ENXIO) &&
967 p->p_dupfd >= 0 && /* XXX from fdopen */
968 (error =
969 dupfdopen(p, indx, p->p_dupfd, flags, error)) == 0) {
970 *retval = indx;
971 return (0);
972 }
973 if (error == ERESTART)
974 error = EINTR;
975 fdp->fd_ofiles[indx] = NULL;
976 return (error);
977 }
978 p->p_dupfd = 0;
979 vp = nd.ni_vp;
980 fp->f_flag = flags & FMASK;
981 fp->f_type = DTYPE_VNODE;
982 fp->f_ops = &vnops;
983 fp->f_data = (caddr_t)vp;
984 if (flags & (O_EXLOCK | O_SHLOCK)) {
985 lf.l_whence = SEEK_SET;
986 lf.l_start = 0;
987 lf.l_len = 0;
988 if (flags & O_EXLOCK)
989 lf.l_type = F_WRLCK;
990 else
991 lf.l_type = F_RDLCK;
992 type = F_FLOCK;
993 if ((flags & FNONBLOCK) == 0)
994 type |= F_WAIT;
995 VOP_UNLOCK(vp, 0);
996 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
997 if (error) {
998 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
999 FILE_UNUSE(fp, p);
1000 ffree(fp);
1001 fdp->fd_ofiles[indx] = NULL;
1002 return (error);
1003 }
1004 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1005 fp->f_flag |= FHASLOCK;
1006 }
1007 VOP_UNLOCK(vp, 0);
1008 *retval = indx;
1009 FILE_UNUSE(fp, p);
1010 return (0);
1011 }
1012
1013 /*
1014 * Get file handle system call
1015 */
1016 int
1017 sys_getfh(p, v, retval)
1018 struct proc *p;
1019 register void *v;
1020 register_t *retval;
1021 {
1022 register struct sys_getfh_args /* {
1023 syscallarg(char *) fname;
1024 syscallarg(fhandle_t *) fhp;
1025 } */ *uap = v;
1026 register struct vnode *vp;
1027 fhandle_t fh;
1028 int error;
1029 struct nameidata nd;
1030
1031 /*
1032 * Must be super user
1033 */
1034 error = suser(p->p_ucred, &p->p_acflag);
1035 if (error)
1036 return (error);
1037 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1038 SCARG(uap, fname), p);
1039 error = namei(&nd);
1040 if (error)
1041 return (error);
1042 vp = nd.ni_vp;
1043 memset((caddr_t)&fh, 0, sizeof(fh));
1044 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1045 error = VFS_VPTOFH(vp, &fh.fh_fid);
1046 vput(vp);
1047 if (error)
1048 return (error);
1049 error = copyout((caddr_t)&fh, (caddr_t)SCARG(uap, fhp), sizeof (fh));
1050 return (error);
1051 }
1052
1053 /*
1054 * Open a file given a file handle.
1055 *
1056 * Check permissions, allocate an open file structure,
1057 * and call the device open routine if any.
1058 */
1059 int
1060 sys_fhopen(p, v, retval)
1061 struct proc *p;
1062 void *v;
1063 register_t *retval;
1064 {
1065 register struct sys_fhopen_args /* {
1066 syscallarg(const fhandle_t *) fhp;
1067 syscallarg(int) flags;
1068 } */ *uap = v;
1069 struct filedesc *fdp = p->p_fd;
1070 struct file *fp;
1071 struct vnode *vp = NULL;
1072 struct mount *mp;
1073 struct ucred *cred = p->p_ucred;
1074 int flags;
1075 struct file *nfp;
1076 int type, indx, error=0;
1077 struct flock lf;
1078 struct vattr va;
1079 fhandle_t fh;
1080 extern struct fileops vnops;
1081
1082 /*
1083 * Must be super user
1084 */
1085 if ((error = suser(p->p_ucred, &p->p_acflag)))
1086 return (error);
1087
1088 flags = FFLAGS(SCARG(uap, flags));
1089 if ((flags & (FREAD | FWRITE)) == 0)
1090 return (EINVAL);
1091 if ((flags & O_CREAT))
1092 return (EINVAL);
1093 /* falloc() will use the file descriptor for us */
1094 if ((error = falloc(p, &nfp, &indx)) != 0)
1095 return (error);
1096 fp = nfp;
1097 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1098 goto bad;
1099
1100 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
1101 error = ESTALE;
1102 goto bad;
1103 }
1104
1105 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
1106 vp = NULL; /* most likely unnecessary sanity for bad: */
1107 goto bad;
1108 }
1109
1110 /* Now do an effective vn_open */
1111
1112 if (vp->v_type == VSOCK) {
1113 error = EOPNOTSUPP;
1114 goto bad;
1115 }
1116 if (flags & FREAD) {
1117 if ((error = VOP_ACCESS(vp, VREAD, cred, p)) != 0)
1118 goto bad;
1119 }
1120 if (flags & (FWRITE | O_TRUNC)) {
1121 if (vp->v_type == VDIR) {
1122 error = EISDIR;
1123 goto bad;
1124 }
1125 if ((error = vn_writechk(vp)) != 0 ||
1126 (error = VOP_ACCESS(vp, VWRITE, cred, p)) != 0)
1127 goto bad;
1128 }
1129 if (flags & O_TRUNC) {
1130 VOP_UNLOCK(vp, 0); /* XXX */
1131 VOP_LEASE(vp, p, cred, LEASE_WRITE);
1132 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1133 VATTR_NULL(&va);
1134 va.va_size = 0;
1135 if ((error = VOP_SETATTR(vp, &va, cred, p)) != 0)
1136 goto bad;
1137 }
1138 if ((error = VOP_OPEN(vp, flags, cred, p)) != 0)
1139 goto bad;
1140 if (vp->v_type == VREG &&
1141 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
1142 error = EIO;
1143 goto bad;
1144 }
1145 if (flags & FWRITE)
1146 vp->v_writecount++;
1147
1148 /* done with modified vn_open, now finish what sys_open does. */
1149
1150 fp->f_flag = flags & FMASK;
1151 fp->f_type = DTYPE_VNODE;
1152 fp->f_ops = &vnops;
1153 fp->f_data = (caddr_t)vp;
1154 if (flags & (O_EXLOCK | O_SHLOCK)) {
1155 lf.l_whence = SEEK_SET;
1156 lf.l_start = 0;
1157 lf.l_len = 0;
1158 if (flags & O_EXLOCK)
1159 lf.l_type = F_WRLCK;
1160 else
1161 lf.l_type = F_RDLCK;
1162 type = F_FLOCK;
1163 if ((flags & FNONBLOCK) == 0)
1164 type |= F_WAIT;
1165 VOP_UNLOCK(vp, 0);
1166 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
1167 if (error) {
1168 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1169 FILE_UNUSE(fp, p);
1170 ffree(fp);
1171 fdp->fd_ofiles[indx] = NULL;
1172 return (error);
1173 }
1174 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1175 fp->f_flag |= FHASLOCK;
1176 }
1177 VOP_UNLOCK(vp, 0);
1178 *retval = indx;
1179 FILE_UNUSE(fp, p);
1180 return (0);
1181
1182 bad:
1183 FILE_UNUSE(fp, p);
1184 ffree(fp);
1185 fdp->fd_ofiles[indx] = NULL;
1186 if (vp != NULL)
1187 vput(vp);
1188 return (error);
1189 }
1190
1191 /* ARGSUSED */
1192 int
1193 sys_fhstat(p, v, retval)
1194 struct proc *p;
1195 void *v;
1196 register_t *retval;
1197 {
1198 register struct sys_fhstat_args /* {
1199 syscallarg(const fhandle_t *) fhp;
1200 syscallarg(struct stat *) sb;
1201 } */ *uap = v;
1202 struct stat sb;
1203 int error;
1204 fhandle_t fh;
1205 struct mount *mp;
1206 struct vnode *vp;
1207
1208 /*
1209 * Must be super user
1210 */
1211 if ((error = suser(p->p_ucred, &p->p_acflag)))
1212 return (error);
1213
1214 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1215 return (error);
1216
1217 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1218 return (ESTALE);
1219 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1220 return (error);
1221 error = vn_stat(vp, &sb, p);
1222 vput(vp);
1223 if (error)
1224 return (error);
1225 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
1226 return (error);
1227 }
1228
1229 /* ARGSUSED */
1230 int
1231 sys_fhstatfs(p, v, retval)
1232 struct proc *p;
1233 void *v;
1234 register_t *retval;
1235 {
1236 register struct sys_fhstatfs_args /*
1237 syscallarg(const fhandle_t *) fhp;
1238 syscallarg(struct statfs *) buf;
1239 } */ *uap = v;
1240 struct statfs sp;
1241 fhandle_t fh;
1242 struct mount *mp;
1243 struct vnode *vp;
1244 int error;
1245
1246 /*
1247 * Must be super user
1248 */
1249 if ((error = suser(p->p_ucred, &p->p_acflag)))
1250 return (error);
1251
1252 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1253 return (error);
1254
1255 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1256 return (ESTALE);
1257 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1258 return (error);
1259 mp = vp->v_mount;
1260 vput(vp);
1261 if ((error = VFS_STATFS(mp, &sp, p)) != 0)
1262 return (error);
1263 sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1264 return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
1265 }
1266
1267 /*
1268 * Create a special file.
1269 */
1270 /* ARGSUSED */
1271 int
1272 sys_mknod(p, v, retval)
1273 struct proc *p;
1274 void *v;
1275 register_t *retval;
1276 {
1277 register struct sys_mknod_args /* {
1278 syscallarg(const char *) path;
1279 syscallarg(int) mode;
1280 syscallarg(int) dev;
1281 } */ *uap = v;
1282 register struct vnode *vp;
1283 struct vattr vattr;
1284 int error;
1285 int whiteout = 0;
1286 struct nameidata nd;
1287
1288 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1289 return (error);
1290 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1291 if ((error = namei(&nd)) != 0)
1292 return (error);
1293 vp = nd.ni_vp;
1294 if (vp != NULL)
1295 error = EEXIST;
1296 else {
1297 VATTR_NULL(&vattr);
1298 vattr.va_mode =
1299 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1300 vattr.va_rdev = SCARG(uap, dev);
1301 whiteout = 0;
1302
1303 switch (SCARG(uap, mode) & S_IFMT) {
1304 case S_IFMT: /* used by badsect to flag bad sectors */
1305 vattr.va_type = VBAD;
1306 break;
1307 case S_IFCHR:
1308 vattr.va_type = VCHR;
1309 break;
1310 case S_IFBLK:
1311 vattr.va_type = VBLK;
1312 break;
1313 case S_IFWHT:
1314 whiteout = 1;
1315 break;
1316 default:
1317 error = EINVAL;
1318 break;
1319 }
1320 }
1321 if (!error) {
1322 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1323 if (whiteout) {
1324 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1325 if (error)
1326 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1327 vput(nd.ni_dvp);
1328 } else {
1329 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1330 &nd.ni_cnd, &vattr);
1331 }
1332 } else {
1333 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1334 if (nd.ni_dvp == vp)
1335 vrele(nd.ni_dvp);
1336 else
1337 vput(nd.ni_dvp);
1338 if (vp)
1339 vrele(vp);
1340 }
1341 return (error);
1342 }
1343
1344 /*
1345 * Create a named pipe.
1346 */
1347 /* ARGSUSED */
1348 int
1349 sys_mkfifo(p, v, retval)
1350 struct proc *p;
1351 void *v;
1352 register_t *retval;
1353 {
1354 register struct sys_mkfifo_args /* {
1355 syscallarg(const char *) path;
1356 syscallarg(int) mode;
1357 } */ *uap = v;
1358 struct vattr vattr;
1359 int error;
1360 struct nameidata nd;
1361
1362 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1363 if ((error = namei(&nd)) != 0)
1364 return (error);
1365 if (nd.ni_vp != NULL) {
1366 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1367 if (nd.ni_dvp == nd.ni_vp)
1368 vrele(nd.ni_dvp);
1369 else
1370 vput(nd.ni_dvp);
1371 vrele(nd.ni_vp);
1372 return (EEXIST);
1373 }
1374 VATTR_NULL(&vattr);
1375 vattr.va_type = VFIFO;
1376 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1377 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1378 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
1379 }
1380
1381 /*
1382 * Make a hard file link.
1383 */
1384 /* ARGSUSED */
1385 int
1386 sys_link(p, v, retval)
1387 struct proc *p;
1388 void *v;
1389 register_t *retval;
1390 {
1391 register struct sys_link_args /* {
1392 syscallarg(const char *) path;
1393 syscallarg(const char *) link;
1394 } */ *uap = v;
1395 register struct vnode *vp;
1396 struct nameidata nd;
1397 int error;
1398
1399 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1400 if ((error = namei(&nd)) != 0)
1401 return (error);
1402 vp = nd.ni_vp;
1403 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1404 if ((error = namei(&nd)) != 0)
1405 goto out;
1406 if (nd.ni_vp) {
1407 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1408 if (nd.ni_dvp == nd.ni_vp)
1409 vrele(nd.ni_dvp);
1410 else
1411 vput(nd.ni_dvp);
1412 vrele(nd.ni_vp);
1413 error = EEXIST;
1414 goto out;
1415 }
1416 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1417 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1418 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1419 out:
1420 vrele(vp);
1421 return (error);
1422 }
1423
1424 /*
1425 * Make a symbolic link.
1426 */
1427 /* ARGSUSED */
1428 int
1429 sys_symlink(p, v, retval)
1430 struct proc *p;
1431 void *v;
1432 register_t *retval;
1433 {
1434 register struct sys_symlink_args /* {
1435 syscallarg(const char *) path;
1436 syscallarg(const char *) link;
1437 } */ *uap = v;
1438 struct vattr vattr;
1439 char *path;
1440 int error;
1441 struct nameidata nd;
1442
1443 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1444 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1445 if (error)
1446 goto out;
1447 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1448 if ((error = namei(&nd)) != 0)
1449 goto out;
1450 if (nd.ni_vp) {
1451 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1452 if (nd.ni_dvp == nd.ni_vp)
1453 vrele(nd.ni_dvp);
1454 else
1455 vput(nd.ni_dvp);
1456 vrele(nd.ni_vp);
1457 error = EEXIST;
1458 goto out;
1459 }
1460 VATTR_NULL(&vattr);
1461 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1462 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1463 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1464 out:
1465 FREE(path, M_NAMEI);
1466 return (error);
1467 }
1468
1469 /*
1470 * Delete a whiteout from the filesystem.
1471 */
1472 /* ARGSUSED */
1473 int
1474 sys_undelete(p, v, retval)
1475 struct proc *p;
1476 void *v;
1477 register_t *retval;
1478 {
1479 register struct sys_undelete_args /* {
1480 syscallarg(const char *) path;
1481 } */ *uap = v;
1482 int error;
1483 struct nameidata nd;
1484
1485 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1486 SCARG(uap, path), p);
1487 error = namei(&nd);
1488 if (error)
1489 return (error);
1490
1491 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1492 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1493 if (nd.ni_dvp == nd.ni_vp)
1494 vrele(nd.ni_dvp);
1495 else
1496 vput(nd.ni_dvp);
1497 if (nd.ni_vp)
1498 vrele(nd.ni_vp);
1499 return (EEXIST);
1500 }
1501
1502 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1503 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1504 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1505 vput(nd.ni_dvp);
1506 return (error);
1507 }
1508
1509 /*
1510 * Delete a name from the filesystem.
1511 */
1512 /* ARGSUSED */
1513 int
1514 sys_unlink(p, v, retval)
1515 struct proc *p;
1516 void *v;
1517 register_t *retval;
1518 {
1519 struct sys_unlink_args /* {
1520 syscallarg(const char *) path;
1521 } */ *uap = v;
1522 register struct vnode *vp;
1523 int error;
1524 struct nameidata nd;
1525
1526 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1527 SCARG(uap, path), p);
1528 if ((error = namei(&nd)) != 0)
1529 return (error);
1530 vp = nd.ni_vp;
1531
1532 /*
1533 * The root of a mounted filesystem cannot be deleted.
1534 */
1535 if (vp->v_flag & VROOT) {
1536 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1537 if (nd.ni_dvp == vp)
1538 vrele(nd.ni_dvp);
1539 else
1540 vput(nd.ni_dvp);
1541 vput(vp);
1542 error = EBUSY;
1543 goto out;
1544 }
1545
1546 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1547 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1548 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1549 out:
1550 return (error);
1551 }
1552
1553 /*
1554 * Reposition read/write file offset.
1555 */
1556 int
1557 sys_lseek(p, v, retval)
1558 struct proc *p;
1559 void *v;
1560 register_t *retval;
1561 {
1562 register struct sys_lseek_args /* {
1563 syscallarg(int) fd;
1564 syscallarg(int) pad;
1565 syscallarg(off_t) offset;
1566 syscallarg(int) whence;
1567 } */ *uap = v;
1568 struct ucred *cred = p->p_ucred;
1569 register struct filedesc *fdp = p->p_fd;
1570 register struct file *fp;
1571 struct vnode *vp;
1572 struct vattr vattr;
1573 register off_t newoff;
1574 int error;
1575
1576 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1577 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
1578 (fp->f_iflags & FIF_WANTCLOSE) != 0)
1579 return (EBADF);
1580
1581 FILE_USE(fp);
1582
1583 vp = (struct vnode *)fp->f_data;
1584 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1585 error = ESPIPE;
1586 goto out;
1587 }
1588
1589 switch (SCARG(uap, whence)) {
1590 case SEEK_CUR:
1591 newoff = fp->f_offset + SCARG(uap, offset);
1592 break;
1593 case SEEK_END:
1594 error = VOP_GETATTR(vp, &vattr, cred, p);
1595 if (error)
1596 goto out;
1597 newoff = SCARG(uap, offset) + vattr.va_size;
1598 break;
1599 case SEEK_SET:
1600 newoff = SCARG(uap, offset);
1601 break;
1602 default:
1603 error = EINVAL;
1604 goto out;
1605 }
1606 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1607 goto out;
1608
1609 *(off_t *)retval = fp->f_offset = newoff;
1610 out:
1611 FILE_UNUSE(fp, p);
1612 return (error);
1613 }
1614
1615 /*
1616 * Positional read system call.
1617 */
1618 int
1619 sys_pread(p, v, retval)
1620 struct proc *p;
1621 void *v;
1622 register_t *retval;
1623 {
1624 struct sys_pread_args /* {
1625 syscallarg(int) fd;
1626 syscallarg(void *) buf;
1627 syscallarg(size_t) nbyte;
1628 syscallarg(off_t) offset;
1629 } */ *uap = v;
1630 struct filedesc *fdp = p->p_fd;
1631 struct file *fp;
1632 struct vnode *vp;
1633 off_t offset;
1634 int error, fd = SCARG(uap, fd);
1635
1636 if ((u_int)fd >= fdp->fd_nfiles ||
1637 (fp = fdp->fd_ofiles[fd]) == NULL ||
1638 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1639 (fp->f_flag & FREAD) == 0)
1640 return (EBADF);
1641
1642 FILE_USE(fp);
1643
1644 vp = (struct vnode *)fp->f_data;
1645 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1646 error = ESPIPE;
1647 goto out;
1648 }
1649
1650 offset = SCARG(uap, offset);
1651
1652 /*
1653 * XXX This works because no file systems actually
1654 * XXX take any action on the seek operation.
1655 */
1656 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1657 goto out;
1658
1659 /* dofileread() will unuse the descriptor for us */
1660 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1661 &offset, 0, retval));
1662
1663 out:
1664 FILE_UNUSE(fp, p);
1665 return (error);
1666 }
1667
1668 /*
1669 * Positional scatter read system call.
1670 */
1671 int
1672 sys_preadv(p, v, retval)
1673 struct proc *p;
1674 void *v;
1675 register_t *retval;
1676 {
1677 struct sys_preadv_args /* {
1678 syscallarg(int) fd;
1679 syscallarg(const struct iovec *) iovp;
1680 syscallarg(int) iovcnt;
1681 syscallarg(off_t) offset;
1682 } */ *uap = v;
1683 struct filedesc *fdp = p->p_fd;
1684 struct file *fp;
1685 struct vnode *vp;
1686 off_t offset;
1687 int error, fd = SCARG(uap, fd);
1688
1689 if ((u_int)fd >= fdp->fd_nfiles ||
1690 (fp = fdp->fd_ofiles[fd]) == NULL ||
1691 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1692 (fp->f_flag & FREAD) == 0)
1693 return (EBADF);
1694
1695 FILE_USE(fp);
1696
1697 vp = (struct vnode *)fp->f_data;
1698 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1699 error = ESPIPE;
1700 goto out;
1701 }
1702
1703 offset = SCARG(uap, offset);
1704
1705 /*
1706 * XXX This works because no file systems actually
1707 * XXX take any action on the seek operation.
1708 */
1709 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1710 goto out;
1711
1712 /* dofilereadv() will unuse the descriptor for us */
1713 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1714 &offset, 0, retval));
1715
1716 out:
1717 FILE_UNUSE(fp, p);
1718 return (error);
1719 }
1720
1721 /*
1722 * Positional write system call.
1723 */
1724 int
1725 sys_pwrite(p, v, retval)
1726 struct proc *p;
1727 void *v;
1728 register_t *retval;
1729 {
1730 struct sys_pwrite_args /* {
1731 syscallarg(int) fd;
1732 syscallarg(const void *) buf;
1733 syscallarg(size_t) nbyte;
1734 syscallarg(off_t) offset;
1735 } */ *uap = v;
1736 struct filedesc *fdp = p->p_fd;
1737 struct file *fp;
1738 struct vnode *vp;
1739 off_t offset;
1740 int error, fd = SCARG(uap, fd);
1741
1742 if ((u_int)fd >= fdp->fd_nfiles ||
1743 (fp = fdp->fd_ofiles[fd]) == NULL ||
1744 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1745 (fp->f_flag & FWRITE) == 0)
1746 return (EBADF);
1747
1748 FILE_USE(fp);
1749
1750 vp = (struct vnode *)fp->f_data;
1751 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1752 error = ESPIPE;
1753 goto out;
1754 }
1755
1756 offset = SCARG(uap, offset);
1757
1758 /*
1759 * XXX This works because no file systems actually
1760 * XXX take any action on the seek operation.
1761 */
1762 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1763 goto out;
1764
1765 /* dofilewrite() will unuse the descriptor for us */
1766 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1767 &offset, 0, retval));
1768
1769 out:
1770 FILE_UNUSE(fp, p);
1771 return (error);
1772 }
1773
1774 /*
1775 * Positional gather write system call.
1776 */
1777 int
1778 sys_pwritev(p, v, retval)
1779 struct proc *p;
1780 void *v;
1781 register_t *retval;
1782 {
1783 struct sys_pwritev_args /* {
1784 syscallarg(int) fd;
1785 syscallarg(const struct iovec *) iovp;
1786 syscallarg(int) iovcnt;
1787 syscallarg(off_t) offset;
1788 } */ *uap = v;
1789 struct filedesc *fdp = p->p_fd;
1790 struct file *fp;
1791 struct vnode *vp;
1792 off_t offset;
1793 int error, fd = SCARG(uap, fd);
1794
1795 if ((u_int)fd >= fdp->fd_nfiles ||
1796 (fp = fdp->fd_ofiles[fd]) == NULL ||
1797 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1798 (fp->f_flag & FWRITE) == 0)
1799 return (EBADF);
1800
1801 FILE_USE(fp);
1802
1803 vp = (struct vnode *)fp->f_data;
1804 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1805 error = ESPIPE;
1806 goto out;
1807 }
1808
1809 offset = SCARG(uap, offset);
1810
1811 /*
1812 * XXX This works because no file systems actually
1813 * XXX take any action on the seek operation.
1814 */
1815 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1816 goto out;
1817
1818 /* dofilewritev() will unuse the descriptor for us */
1819 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1820 &offset, 0, retval));
1821
1822 out:
1823 FILE_UNUSE(fp, p);
1824 return (error);
1825 }
1826
1827 /*
1828 * Check access permissions.
1829 */
1830 int
1831 sys_access(p, v, retval)
1832 struct proc *p;
1833 void *v;
1834 register_t *retval;
1835 {
1836 register struct sys_access_args /* {
1837 syscallarg(const char *) path;
1838 syscallarg(int) flags;
1839 } */ *uap = v;
1840 register struct ucred *cred = p->p_ucred;
1841 register struct vnode *vp;
1842 int error, flags, t_gid, t_uid;
1843 struct nameidata nd;
1844
1845 t_uid = cred->cr_uid;
1846 t_gid = cred->cr_gid;
1847 cred->cr_uid = p->p_cred->p_ruid;
1848 cred->cr_gid = p->p_cred->p_rgid;
1849 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1850 SCARG(uap, path), p);
1851 if ((error = namei(&nd)) != 0)
1852 goto out1;
1853 vp = nd.ni_vp;
1854
1855 /* Flags == 0 means only check for existence. */
1856 if (SCARG(uap, flags)) {
1857 flags = 0;
1858 if (SCARG(uap, flags) & R_OK)
1859 flags |= VREAD;
1860 if (SCARG(uap, flags) & W_OK)
1861 flags |= VWRITE;
1862 if (SCARG(uap, flags) & X_OK)
1863 flags |= VEXEC;
1864
1865 error = VOP_ACCESS(vp, flags, cred, p);
1866 if (!error && (flags & VWRITE))
1867 error = vn_writechk(vp);
1868 }
1869 vput(vp);
1870 out1:
1871 cred->cr_uid = t_uid;
1872 cred->cr_gid = t_gid;
1873 return (error);
1874 }
1875
1876 /*
1877 * Get file status; this version follows links.
1878 */
1879 /* ARGSUSED */
1880 int
1881 sys___stat13(p, v, retval)
1882 struct proc *p;
1883 void *v;
1884 register_t *retval;
1885 {
1886 register struct sys___stat13_args /* {
1887 syscallarg(const char *) path;
1888 syscallarg(struct stat *) ub;
1889 } */ *uap = v;
1890 struct stat sb;
1891 int error;
1892 struct nameidata nd;
1893
1894 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1895 SCARG(uap, path), p);
1896 if ((error = namei(&nd)) != 0)
1897 return (error);
1898 error = vn_stat(nd.ni_vp, &sb, p);
1899 vput(nd.ni_vp);
1900 if (error)
1901 return (error);
1902 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1903 return (error);
1904 }
1905
1906 /*
1907 * Get file status; this version does not follow links.
1908 */
1909 /* ARGSUSED */
1910 int
1911 sys___lstat13(p, v, retval)
1912 struct proc *p;
1913 void *v;
1914 register_t *retval;
1915 {
1916 register struct sys___lstat13_args /* {
1917 syscallarg(const char *) path;
1918 syscallarg(struct stat *) ub;
1919 } */ *uap = v;
1920 struct stat sb;
1921 int error;
1922 struct nameidata nd;
1923
1924 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1925 SCARG(uap, path), p);
1926 if ((error = namei(&nd)) != 0)
1927 return (error);
1928 error = vn_stat(nd.ni_vp, &sb, p);
1929 vput(nd.ni_vp);
1930 if (error)
1931 return (error);
1932 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1933 return (error);
1934 }
1935
1936 /*
1937 * Get configurable pathname variables.
1938 */
1939 /* ARGSUSED */
1940 int
1941 sys_pathconf(p, v, retval)
1942 struct proc *p;
1943 void *v;
1944 register_t *retval;
1945 {
1946 register struct sys_pathconf_args /* {
1947 syscallarg(const char *) path;
1948 syscallarg(int) name;
1949 } */ *uap = v;
1950 int error;
1951 struct nameidata nd;
1952
1953 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1954 SCARG(uap, path), p);
1955 if ((error = namei(&nd)) != 0)
1956 return (error);
1957 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1958 vput(nd.ni_vp);
1959 return (error);
1960 }
1961
1962 /*
1963 * Return target name of a symbolic link.
1964 */
1965 /* ARGSUSED */
1966 int
1967 sys_readlink(p, v, retval)
1968 struct proc *p;
1969 void *v;
1970 register_t *retval;
1971 {
1972 register struct sys_readlink_args /* {
1973 syscallarg(const char *) path;
1974 syscallarg(char *) buf;
1975 syscallarg(size_t) count;
1976 } */ *uap = v;
1977 register struct vnode *vp;
1978 struct iovec aiov;
1979 struct uio auio;
1980 int error;
1981 struct nameidata nd;
1982
1983 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1984 SCARG(uap, path), p);
1985 if ((error = namei(&nd)) != 0)
1986 return (error);
1987 vp = nd.ni_vp;
1988 if (vp->v_type != VLNK)
1989 error = EINVAL;
1990 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
1991 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
1992 aiov.iov_base = SCARG(uap, buf);
1993 aiov.iov_len = SCARG(uap, count);
1994 auio.uio_iov = &aiov;
1995 auio.uio_iovcnt = 1;
1996 auio.uio_offset = 0;
1997 auio.uio_rw = UIO_READ;
1998 auio.uio_segflg = UIO_USERSPACE;
1999 auio.uio_procp = p;
2000 auio.uio_resid = SCARG(uap, count);
2001 error = VOP_READLINK(vp, &auio, p->p_ucred);
2002 }
2003 vput(vp);
2004 *retval = SCARG(uap, count) - auio.uio_resid;
2005 return (error);
2006 }
2007
2008 /*
2009 * Change flags of a file given a path name.
2010 */
2011 /* ARGSUSED */
2012 int
2013 sys_chflags(p, v, retval)
2014 struct proc *p;
2015 void *v;
2016 register_t *retval;
2017 {
2018 register struct sys_chflags_args /* {
2019 syscallarg(const char *) path;
2020 syscallarg(u_long) flags;
2021 } */ *uap = v;
2022 register struct vnode *vp;
2023 struct vattr vattr;
2024 int error;
2025 struct nameidata nd;
2026
2027 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2028 if ((error = namei(&nd)) != 0)
2029 return (error);
2030 vp = nd.ni_vp;
2031 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2032 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2033 /* Non-superusers cannot change the flags on devices, even if they
2034 own them. */
2035 if (suser(p->p_ucred, &p->p_acflag)) {
2036 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2037 goto out;
2038 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2039 error = EINVAL;
2040 goto out;
2041 }
2042 }
2043 VATTR_NULL(&vattr);
2044 vattr.va_flags = SCARG(uap, flags);
2045 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2046 out:
2047 vput(vp);
2048 return (error);
2049 }
2050
2051 /*
2052 * Change flags of a file given a file descriptor.
2053 */
2054 /* ARGSUSED */
2055 int
2056 sys_fchflags(p, v, retval)
2057 struct proc *p;
2058 void *v;
2059 register_t *retval;
2060 {
2061 register struct sys_fchflags_args /* {
2062 syscallarg(int) fd;
2063 syscallarg(u_long) flags;
2064 } */ *uap = v;
2065 struct vattr vattr;
2066 struct vnode *vp;
2067 struct file *fp;
2068 int error;
2069
2070 /* getvnode() will use the descriptor for us */
2071 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2072 return (error);
2073 vp = (struct vnode *)fp->f_data;
2074 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2075 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2076 /* Non-superusers cannot change the flags on devices, even if they
2077 own them. */
2078 if (suser(p->p_ucred, &p->p_acflag)) {
2079 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p))
2080 != 0)
2081 goto out;
2082 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2083 error = EINVAL;
2084 goto out;
2085 }
2086 }
2087 VATTR_NULL(&vattr);
2088 vattr.va_flags = SCARG(uap, flags);
2089 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2090 out:
2091 VOP_UNLOCK(vp, 0);
2092 FILE_UNUSE(fp, p);
2093 return (error);
2094 }
2095
2096 /*
2097 * Change mode of a file given path name; this version follows links.
2098 */
2099 /* ARGSUSED */
2100 int
2101 sys_chmod(p, v, retval)
2102 struct proc *p;
2103 void *v;
2104 register_t *retval;
2105 {
2106 register struct sys_chmod_args /* {
2107 syscallarg(const char *) path;
2108 syscallarg(int) mode;
2109 } */ *uap = v;
2110 int error;
2111 struct nameidata nd;
2112
2113 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2114 if ((error = namei(&nd)) != 0)
2115 return (error);
2116
2117 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2118
2119 vrele(nd.ni_vp);
2120 return (error);
2121 }
2122
2123 /*
2124 * Change mode of a file given a file descriptor.
2125 */
2126 /* ARGSUSED */
2127 int
2128 sys_fchmod(p, v, retval)
2129 struct proc *p;
2130 void *v;
2131 register_t *retval;
2132 {
2133 register struct sys_fchmod_args /* {
2134 syscallarg(int) fd;
2135 syscallarg(int) mode;
2136 } */ *uap = v;
2137 struct file *fp;
2138 int error;
2139
2140 /* getvnode() will use the descriptor for us */
2141 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2142 return (error);
2143
2144 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2145 FILE_UNUSE(fp, p);
2146 return (error);
2147 }
2148
2149 /*
2150 * Change mode of a file given path name; this version does not follow links.
2151 */
2152 /* ARGSUSED */
2153 int
2154 sys_lchmod(p, v, retval)
2155 struct proc *p;
2156 void *v;
2157 register_t *retval;
2158 {
2159 register struct sys_lchmod_args /* {
2160 syscallarg(const char *) path;
2161 syscallarg(int) mode;
2162 } */ *uap = v;
2163 int error;
2164 struct nameidata nd;
2165
2166 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2167 if ((error = namei(&nd)) != 0)
2168 return (error);
2169
2170 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2171
2172 vrele(nd.ni_vp);
2173 return (error);
2174 }
2175
2176 /*
2177 * Common routine to set mode given a vnode.
2178 */
2179 static int
2180 change_mode(vp, mode, p)
2181 struct vnode *vp;
2182 int mode;
2183 struct proc *p;
2184 {
2185 struct vattr vattr;
2186 int error;
2187
2188 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2189 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2190 VATTR_NULL(&vattr);
2191 vattr.va_mode = mode & ALLPERMS;
2192 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2193 VOP_UNLOCK(vp, 0);
2194 return (error);
2195 }
2196
2197 /*
2198 * Set ownership given a path name; this version follows links.
2199 */
2200 /* ARGSUSED */
2201 int
2202 sys_chown(p, v, retval)
2203 struct proc *p;
2204 void *v;
2205 register_t *retval;
2206 {
2207 register struct sys_chown_args /* {
2208 syscallarg(const char *) path;
2209 syscallarg(uid_t) uid;
2210 syscallarg(gid_t) gid;
2211 } */ *uap = v;
2212 int error;
2213 struct nameidata nd;
2214
2215 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2216 if ((error = namei(&nd)) != 0)
2217 return (error);
2218
2219 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2220
2221 vrele(nd.ni_vp);
2222 return (error);
2223 }
2224
2225 /*
2226 * Set ownership given a path name; this version follows links.
2227 * Provides POSIX semantics.
2228 */
2229 /* ARGSUSED */
2230 int
2231 sys___posix_chown(p, v, retval)
2232 struct proc *p;
2233 void *v;
2234 register_t *retval;
2235 {
2236 register struct sys_chown_args /* {
2237 syscallarg(const char *) path;
2238 syscallarg(uid_t) uid;
2239 syscallarg(gid_t) gid;
2240 } */ *uap = v;
2241 int error;
2242 struct nameidata nd;
2243
2244 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2245 if ((error = namei(&nd)) != 0)
2246 return (error);
2247
2248 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2249
2250 vrele(nd.ni_vp);
2251 return (error);
2252 }
2253
2254 /*
2255 * Set ownership given a file descriptor.
2256 */
2257 /* ARGSUSED */
2258 int
2259 sys_fchown(p, v, retval)
2260 struct proc *p;
2261 void *v;
2262 register_t *retval;
2263 {
2264 register struct sys_fchown_args /* {
2265 syscallarg(int) fd;
2266 syscallarg(uid_t) uid;
2267 syscallarg(gid_t) gid;
2268 } */ *uap = v;
2269 int error;
2270 struct file *fp;
2271
2272 /* getvnode() will use the descriptor for us */
2273 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2274 return (error);
2275
2276 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2277 SCARG(uap, gid), p, 0);
2278 FILE_UNUSE(fp, p);
2279 return (error);
2280 }
2281
2282 /*
2283 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2284 */
2285 /* ARGSUSED */
2286 int
2287 sys___posix_fchown(p, v, retval)
2288 struct proc *p;
2289 void *v;
2290 register_t *retval;
2291 {
2292 register struct sys_fchown_args /* {
2293 syscallarg(int) fd;
2294 syscallarg(uid_t) uid;
2295 syscallarg(gid_t) gid;
2296 } */ *uap = v;
2297 int error;
2298 struct file *fp;
2299
2300 /* getvnode() will use the descriptor for us */
2301 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2302 return (error);
2303
2304 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2305 SCARG(uap, gid), p, 1);
2306 FILE_UNUSE(fp, p);
2307 return (error);
2308 }
2309
2310 /*
2311 * Set ownership given a path name; this version does not follow links.
2312 */
2313 /* ARGSUSED */
2314 int
2315 sys_lchown(p, v, retval)
2316 struct proc *p;
2317 void *v;
2318 register_t *retval;
2319 {
2320 register struct sys_lchown_args /* {
2321 syscallarg(const char *) path;
2322 syscallarg(uid_t) uid;
2323 syscallarg(gid_t) gid;
2324 } */ *uap = v;
2325 int error;
2326 struct nameidata nd;
2327
2328 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2329 if ((error = namei(&nd)) != 0)
2330 return (error);
2331
2332 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2333
2334 vrele(nd.ni_vp);
2335 return (error);
2336 }
2337
2338 /*
2339 * Set ownership given a path name; this version does not follow links.
2340 * Provides POSIX/XPG semantics.
2341 */
2342 /* ARGSUSED */
2343 int
2344 sys___posix_lchown(p, v, retval)
2345 struct proc *p;
2346 void *v;
2347 register_t *retval;
2348 {
2349 register struct sys_lchown_args /* {
2350 syscallarg(const char *) path;
2351 syscallarg(uid_t) uid;
2352 syscallarg(gid_t) gid;
2353 } */ *uap = v;
2354 int error;
2355 struct nameidata nd;
2356
2357 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2358 if ((error = namei(&nd)) != 0)
2359 return (error);
2360
2361 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2362
2363 vrele(nd.ni_vp);
2364 return (error);
2365 }
2366
2367 /*
2368 * Common routine to set ownership given a vnode.
2369 */
2370 static int
2371 change_owner(vp, uid, gid, p, posix_semantics)
2372 register struct vnode *vp;
2373 uid_t uid;
2374 gid_t gid;
2375 struct proc *p;
2376 int posix_semantics;
2377 {
2378 struct vattr vattr;
2379 mode_t newmode;
2380 int error;
2381
2382 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2383 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2384 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2385 goto out;
2386
2387 #define CHANGED(x) ((x) != -1)
2388 newmode = vattr.va_mode;
2389 if (posix_semantics) {
2390 /*
2391 * POSIX/XPG semantics: if the caller is not the super-user,
2392 * clear set-user-id and set-group-id bits. Both POSIX and
2393 * the XPG consider the behaviour for calls by the super-user
2394 * implementation-defined; we leave the set-user-id and set-
2395 * group-id settings intact in that case.
2396 */
2397 if (suser(p->p_ucred, NULL) != 0)
2398 newmode &= ~(S_ISUID | S_ISGID);
2399 } else {
2400 /*
2401 * NetBSD semantics: when changing owner and/or group,
2402 * clear the respective bit(s).
2403 */
2404 if (CHANGED(uid))
2405 newmode &= ~S_ISUID;
2406 if (CHANGED(gid))
2407 newmode &= ~S_ISGID;
2408 }
2409 /* Update va_mode iff altered. */
2410 if (vattr.va_mode == newmode)
2411 newmode = VNOVAL;
2412
2413 VATTR_NULL(&vattr);
2414 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
2415 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
2416 vattr.va_mode = newmode;
2417 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2418 #undef CHANGED
2419
2420 out:
2421 VOP_UNLOCK(vp, 0);
2422 return (error);
2423 }
2424
2425 /*
2426 * Set the access and modification times given a path name; this
2427 * version follows links.
2428 */
2429 /* ARGSUSED */
2430 int
2431 sys_utimes(p, v, retval)
2432 struct proc *p;
2433 void *v;
2434 register_t *retval;
2435 {
2436 register struct sys_utimes_args /* {
2437 syscallarg(const char *) path;
2438 syscallarg(const struct timeval *) tptr;
2439 } */ *uap = v;
2440 int error;
2441 struct nameidata nd;
2442
2443 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2444 if ((error = namei(&nd)) != 0)
2445 return (error);
2446
2447 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2448
2449 vrele(nd.ni_vp);
2450 return (error);
2451 }
2452
2453 /*
2454 * Set the access and modification times given a file descriptor.
2455 */
2456 /* ARGSUSED */
2457 int
2458 sys_futimes(p, v, retval)
2459 struct proc *p;
2460 void *v;
2461 register_t *retval;
2462 {
2463 register struct sys_futimes_args /* {
2464 syscallarg(int) fd;
2465 syscallarg(const struct timeval *) tptr;
2466 } */ *uap = v;
2467 int error;
2468 struct file *fp;
2469
2470 /* getvnode() will use the descriptor for us */
2471 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2472 return (error);
2473
2474 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2475 FILE_UNUSE(fp, p);
2476 return (error);
2477 }
2478
2479 /*
2480 * Set the access and modification times given a path name; this
2481 * version does not follow links.
2482 */
2483 /* ARGSUSED */
2484 int
2485 sys_lutimes(p, v, retval)
2486 struct proc *p;
2487 void *v;
2488 register_t *retval;
2489 {
2490 register struct sys_lutimes_args /* {
2491 syscallarg(const char *) path;
2492 syscallarg(const struct timeval *) tptr;
2493 } */ *uap = v;
2494 int error;
2495 struct nameidata nd;
2496
2497 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2498 if ((error = namei(&nd)) != 0)
2499 return (error);
2500
2501 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2502
2503 vrele(nd.ni_vp);
2504 return (error);
2505 }
2506
2507 /*
2508 * Common routine to set access and modification times given a vnode.
2509 */
2510 static int
2511 change_utimes(vp, tptr, p)
2512 struct vnode *vp;
2513 const struct timeval *tptr;
2514 struct proc *p;
2515 {
2516 struct timeval tv[2];
2517 struct vattr vattr;
2518 int error;
2519
2520 VATTR_NULL(&vattr);
2521 if (tptr == NULL) {
2522 microtime(&tv[0]);
2523 tv[1] = tv[0];
2524 vattr.va_vaflags |= VA_UTIMES_NULL;
2525 } else {
2526 error = copyin(tptr, tv, sizeof(tv));
2527 if (error)
2528 return (error);
2529 }
2530 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2531 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2532 vattr.va_atime.tv_sec = tv[0].tv_sec;
2533 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2534 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2535 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2536 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2537 VOP_UNLOCK(vp, 0);
2538 return (error);
2539 }
2540
2541 /*
2542 * Truncate a file given its path name.
2543 */
2544 /* ARGSUSED */
2545 int
2546 sys_truncate(p, v, retval)
2547 struct proc *p;
2548 void *v;
2549 register_t *retval;
2550 {
2551 register struct sys_truncate_args /* {
2552 syscallarg(const char *) path;
2553 syscallarg(int) pad;
2554 syscallarg(off_t) length;
2555 } */ *uap = v;
2556 register struct vnode *vp;
2557 struct vattr vattr;
2558 int error;
2559 struct nameidata nd;
2560
2561 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2562 if ((error = namei(&nd)) != 0)
2563 return (error);
2564 vp = nd.ni_vp;
2565 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2566 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2567 if (vp->v_type == VDIR)
2568 error = EISDIR;
2569 else if ((error = vn_writechk(vp)) == 0 &&
2570 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2571 VATTR_NULL(&vattr);
2572 vattr.va_size = SCARG(uap, length);
2573 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2574 }
2575 vput(vp);
2576 return (error);
2577 }
2578
2579 /*
2580 * Truncate a file given a file descriptor.
2581 */
2582 /* ARGSUSED */
2583 int
2584 sys_ftruncate(p, v, retval)
2585 struct proc *p;
2586 void *v;
2587 register_t *retval;
2588 {
2589 register struct sys_ftruncate_args /* {
2590 syscallarg(int) fd;
2591 syscallarg(int) pad;
2592 syscallarg(off_t) length;
2593 } */ *uap = v;
2594 struct vattr vattr;
2595 struct vnode *vp;
2596 struct file *fp;
2597 int error;
2598
2599 /* getvnode() will use the descriptor for us */
2600 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2601 return (error);
2602 if ((fp->f_flag & FWRITE) == 0) {
2603 error = EINVAL;
2604 goto out;
2605 }
2606 vp = (struct vnode *)fp->f_data;
2607 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2608 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2609 if (vp->v_type == VDIR)
2610 error = EISDIR;
2611 else if ((error = vn_writechk(vp)) == 0) {
2612 VATTR_NULL(&vattr);
2613 vattr.va_size = SCARG(uap, length);
2614 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2615 }
2616 VOP_UNLOCK(vp, 0);
2617 out:
2618 FILE_UNUSE(fp, p);
2619 return (error);
2620 }
2621
2622 /*
2623 * Sync an open file.
2624 */
2625 /* ARGSUSED */
2626 int
2627 sys_fsync(p, v, retval)
2628 struct proc *p;
2629 void *v;
2630 register_t *retval;
2631 {
2632 struct sys_fsync_args /* {
2633 syscallarg(int) fd;
2634 } */ *uap = v;
2635 register struct vnode *vp;
2636 struct file *fp;
2637 int error;
2638
2639 /* getvnode() will use the descriptor for us */
2640 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2641 return (error);
2642 vp = (struct vnode *)fp->f_data;
2643 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2644 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
2645 VOP_UNLOCK(vp, 0);
2646 FILE_UNUSE(fp, p);
2647 return (error);
2648 }
2649
2650 /*
2651 * Sync the data of an open file.
2652 */
2653 /* ARGSUSED */
2654 int
2655 sys_fdatasync(p, v, retval)
2656 struct proc *p;
2657 void *v;
2658 register_t *retval;
2659 {
2660 struct sys_fdatasync_args /* {
2661 syscallarg(int) fd;
2662 } */ *uap = v;
2663 struct vnode *vp;
2664 struct file *fp;
2665 int error;
2666
2667 /* getvnode() will use the descriptor for us */
2668 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2669 return (error);
2670 vp = (struct vnode *)fp->f_data;
2671 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2672 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
2673 VOP_UNLOCK(vp, 0);
2674 FILE_UNUSE(fp, p);
2675 return (error);
2676 }
2677
2678 /*
2679 * Rename files, (standard) BSD semantics frontend.
2680 */
2681 /* ARGSUSED */
2682 int
2683 sys_rename(p, v, retval)
2684 struct proc *p;
2685 void *v;
2686 register_t *retval;
2687 {
2688 register struct sys_rename_args /* {
2689 syscallarg(const char *) from;
2690 syscallarg(const char *) to;
2691 } */ *uap = v;
2692
2693 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2694 }
2695
2696 /*
2697 * Rename files, POSIX semantics frontend.
2698 */
2699 /* ARGSUSED */
2700 int
2701 sys___posix_rename(p, v, retval)
2702 struct proc *p;
2703 void *v;
2704 register_t *retval;
2705 {
2706 register struct sys___posix_rename_args /* {
2707 syscallarg(const char *) from;
2708 syscallarg(const char *) to;
2709 } */ *uap = v;
2710
2711 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2712 }
2713
2714 /*
2715 * Rename files. Source and destination must either both be directories,
2716 * or both not be directories. If target is a directory, it must be empty.
2717 * If `from' and `to' refer to the same object, the value of the `retain'
2718 * argument is used to determine whether `from' will be
2719 *
2720 * (retain == 0) deleted unless `from' and `to' refer to the same
2721 * object in the file system's name space (BSD).
2722 * (retain == 1) always retained (POSIX).
2723 */
2724 static int
2725 rename_files(from, to, p, retain)
2726 const char *from, *to;
2727 struct proc *p;
2728 int retain;
2729 {
2730 register struct vnode *tvp, *fvp, *tdvp;
2731 struct nameidata fromnd, tond;
2732 int error;
2733
2734 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2735 from, p);
2736 if ((error = namei(&fromnd)) != 0)
2737 return (error);
2738 fvp = fromnd.ni_vp;
2739 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2740 UIO_USERSPACE, to, p);
2741 if ((error = namei(&tond)) != 0) {
2742 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2743 vrele(fromnd.ni_dvp);
2744 vrele(fvp);
2745 goto out1;
2746 }
2747 tdvp = tond.ni_dvp;
2748 tvp = tond.ni_vp;
2749
2750 if (tvp != NULL) {
2751 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2752 error = ENOTDIR;
2753 goto out;
2754 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2755 error = EISDIR;
2756 goto out;
2757 }
2758 }
2759
2760 if (fvp == tdvp)
2761 error = EINVAL;
2762
2763 /*
2764 * Source and destination refer to the same object.
2765 */
2766 if (fvp == tvp) {
2767 if (retain)
2768 error = -1;
2769 else if (fromnd.ni_dvp == tdvp &&
2770 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2771 !memcmp(fromnd.ni_cnd.cn_nameptr,
2772 tond.ni_cnd.cn_nameptr,
2773 fromnd.ni_cnd.cn_namelen))
2774 error = -1;
2775 }
2776
2777 out:
2778 if (!error) {
2779 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2780 if (fromnd.ni_dvp != tdvp)
2781 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2782 if (tvp) {
2783 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2784 }
2785 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2786 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2787 } else {
2788 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2789 if (tdvp == tvp)
2790 vrele(tdvp);
2791 else
2792 vput(tdvp);
2793 if (tvp)
2794 vput(tvp);
2795 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2796 vrele(fromnd.ni_dvp);
2797 vrele(fvp);
2798 }
2799 vrele(tond.ni_startdir);
2800 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2801 out1:
2802 if (fromnd.ni_startdir)
2803 vrele(fromnd.ni_startdir);
2804 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2805 return (error == -1 ? 0 : error);
2806 }
2807
2808 /*
2809 * Make a directory file.
2810 */
2811 /* ARGSUSED */
2812 int
2813 sys_mkdir(p, v, retval)
2814 struct proc *p;
2815 void *v;
2816 register_t *retval;
2817 {
2818 register struct sys_mkdir_args /* {
2819 syscallarg(const char *) path;
2820 syscallarg(int) mode;
2821 } */ *uap = v;
2822 register struct vnode *vp;
2823 struct vattr vattr;
2824 int error;
2825 struct nameidata nd;
2826
2827 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2828 if ((error = namei(&nd)) != 0)
2829 return (error);
2830 vp = nd.ni_vp;
2831 if (vp != NULL) {
2832 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2833 if (nd.ni_dvp == vp)
2834 vrele(nd.ni_dvp);
2835 else
2836 vput(nd.ni_dvp);
2837 vrele(vp);
2838 return (EEXIST);
2839 }
2840 VATTR_NULL(&vattr);
2841 vattr.va_type = VDIR;
2842 vattr.va_mode =
2843 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
2844 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2845 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2846 if (!error)
2847 vput(nd.ni_vp);
2848 return (error);
2849 }
2850
2851 /*
2852 * Remove a directory file.
2853 */
2854 /* ARGSUSED */
2855 int
2856 sys_rmdir(p, v, retval)
2857 struct proc *p;
2858 void *v;
2859 register_t *retval;
2860 {
2861 struct sys_rmdir_args /* {
2862 syscallarg(const char *) path;
2863 } */ *uap = v;
2864 register struct vnode *vp;
2865 int error;
2866 struct nameidata nd;
2867
2868 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2869 SCARG(uap, path), p);
2870 if ((error = namei(&nd)) != 0)
2871 return (error);
2872 vp = nd.ni_vp;
2873 if (vp->v_type != VDIR) {
2874 error = ENOTDIR;
2875 goto out;
2876 }
2877 /*
2878 * No rmdir "." please.
2879 */
2880 if (nd.ni_dvp == vp) {
2881 error = EINVAL;
2882 goto out;
2883 }
2884 /*
2885 * The root of a mounted filesystem cannot be deleted.
2886 */
2887 if (vp->v_flag & VROOT)
2888 error = EBUSY;
2889 out:
2890 if (!error) {
2891 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2892 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2893 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2894 } else {
2895 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2896 if (nd.ni_dvp == vp)
2897 vrele(nd.ni_dvp);
2898 else
2899 vput(nd.ni_dvp);
2900 vput(vp);
2901 }
2902 return (error);
2903 }
2904
2905 /*
2906 * Read a block of directory entries in a file system independent format.
2907 */
2908 int
2909 sys_getdents(p, v, retval)
2910 struct proc *p;
2911 void *v;
2912 register_t *retval;
2913 {
2914 register struct sys_getdents_args /* {
2915 syscallarg(int) fd;
2916 syscallarg(char *) buf;
2917 syscallarg(size_t) count;
2918 } */ *uap = v;
2919 struct file *fp;
2920 int error, done;
2921
2922 /* getvnode() will use the descriptor for us */
2923 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2924 return (error);
2925 if ((fp->f_flag & FREAD) == 0) {
2926 error = EBADF;
2927 goto out;
2928 }
2929 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2930 SCARG(uap, count), &done, p, 0, 0);
2931 *retval = done;
2932 out:
2933 FILE_UNUSE(fp, p);
2934 return (error);
2935 }
2936
2937 /*
2938 * Set the mode mask for creation of filesystem nodes.
2939 */
2940 int
2941 sys_umask(p, v, retval)
2942 struct proc *p;
2943 void *v;
2944 register_t *retval;
2945 {
2946 struct sys_umask_args /* {
2947 syscallarg(mode_t) newmask;
2948 } */ *uap = v;
2949 struct cwdinfo *cwdi;
2950
2951 cwdi = p->p_cwdi;
2952 *retval = cwdi->cwdi_cmask;
2953 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
2954 return (0);
2955 }
2956
2957 /*
2958 * Void all references to file by ripping underlying filesystem
2959 * away from vnode.
2960 */
2961 /* ARGSUSED */
2962 int
2963 sys_revoke(p, v, retval)
2964 struct proc *p;
2965 void *v;
2966 register_t *retval;
2967 {
2968 register struct sys_revoke_args /* {
2969 syscallarg(const char *) path;
2970 } */ *uap = v;
2971 register struct vnode *vp;
2972 struct vattr vattr;
2973 int error;
2974 struct nameidata nd;
2975
2976 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2977 if ((error = namei(&nd)) != 0)
2978 return (error);
2979 vp = nd.ni_vp;
2980 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2981 goto out;
2982 if (p->p_ucred->cr_uid != vattr.va_uid &&
2983 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2984 goto out;
2985 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
2986 VOP_REVOKE(vp, REVOKEALL);
2987 out:
2988 vrele(vp);
2989 return (error);
2990 }
2991
2992 /*
2993 * Convert a user file descriptor to a kernel file entry.
2994 */
2995 int
2996 getvnode(fdp, fd, fpp)
2997 struct filedesc *fdp;
2998 int fd;
2999 struct file **fpp;
3000 {
3001 struct vnode *vp;
3002 struct file *fp;
3003
3004 if ((u_int)fd >= fdp->fd_nfiles ||
3005 (fp = fdp->fd_ofiles[fd]) == NULL ||
3006 (fp->f_iflags & FIF_WANTCLOSE) != 0)
3007 return (EBADF);
3008
3009 FILE_USE(fp);
3010
3011 if (fp->f_type != DTYPE_VNODE) {
3012 FILE_UNUSE(fp, NULL);
3013 return (EINVAL);
3014 }
3015
3016 vp = (struct vnode *)fp->f_data;
3017 if (vp->v_type == VBAD) {
3018 FILE_UNUSE(fp, NULL);
3019 return (EBADF);
3020 }
3021
3022 *fpp = fp;
3023 return (0);
3024 }
3025