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