vfs_syscalls.c revision 1.144 1 /* $NetBSD: vfs_syscalls.c,v 1.144 1999/07/25 06:30:35 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 (flags & FWRITE)
1141 vp->v_writecount++;
1142
1143 /* done with modified vn_open, now finish what sys_open does. */
1144
1145 fp->f_flag = flags & FMASK;
1146 fp->f_type = DTYPE_VNODE;
1147 fp->f_ops = &vnops;
1148 fp->f_data = (caddr_t)vp;
1149 if (flags & (O_EXLOCK | O_SHLOCK)) {
1150 lf.l_whence = SEEK_SET;
1151 lf.l_start = 0;
1152 lf.l_len = 0;
1153 if (flags & O_EXLOCK)
1154 lf.l_type = F_WRLCK;
1155 else
1156 lf.l_type = F_RDLCK;
1157 type = F_FLOCK;
1158 if ((flags & FNONBLOCK) == 0)
1159 type |= F_WAIT;
1160 VOP_UNLOCK(vp, 0);
1161 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
1162 if (error) {
1163 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
1164 FILE_UNUSE(fp, p);
1165 ffree(fp);
1166 fdp->fd_ofiles[indx] = NULL;
1167 return (error);
1168 }
1169 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1170 fp->f_flag |= FHASLOCK;
1171 }
1172 VOP_UNLOCK(vp, 0);
1173 *retval = indx;
1174 FILE_UNUSE(fp, p);
1175 return (0);
1176
1177 bad:
1178 FILE_UNUSE(fp, p);
1179 ffree(fp);
1180 fdp->fd_ofiles[indx] = NULL;
1181 if (vp != NULL)
1182 vput(vp);
1183 return (error);
1184 }
1185
1186 /* ARGSUSED */
1187 int
1188 sys_fhstat(p, v, retval)
1189 struct proc *p;
1190 void *v;
1191 register_t *retval;
1192 {
1193 register struct sys_fhstat_args /* {
1194 syscallarg(const fhandle_t *) fhp;
1195 syscallarg(struct stat *) sb;
1196 } */ *uap = v;
1197 struct stat sb;
1198 int error;
1199 fhandle_t fh;
1200 struct mount *mp;
1201 struct vnode *vp;
1202
1203 /*
1204 * Must be super user
1205 */
1206 if ((error = suser(p->p_ucred, &p->p_acflag)))
1207 return (error);
1208
1209 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1210 return (error);
1211
1212 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1213 return (ESTALE);
1214 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1215 return (error);
1216 error = vn_stat(vp, &sb, p);
1217 vput(vp);
1218 if (error)
1219 return (error);
1220 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
1221 return (error);
1222 }
1223
1224 /* ARGSUSED */
1225 int
1226 sys_fhstatfs(p, v, retval)
1227 struct proc *p;
1228 void *v;
1229 register_t *retval;
1230 {
1231 register struct sys_fhstatfs_args /*
1232 syscallarg(const fhandle_t *) fhp;
1233 syscallarg(struct statfs *) buf;
1234 } */ *uap = v;
1235 struct statfs sp;
1236 fhandle_t fh;
1237 struct mount *mp;
1238 struct vnode *vp;
1239 int error;
1240
1241 /*
1242 * Must be super user
1243 */
1244 if ((error = suser(p->p_ucred, &p->p_acflag)))
1245 return (error);
1246
1247 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1248 return (error);
1249
1250 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1251 return (ESTALE);
1252 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1253 return (error);
1254 mp = vp->v_mount;
1255 vput(vp);
1256 if ((error = VFS_STATFS(mp, &sp, p)) != 0)
1257 return (error);
1258 sp.f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
1259 return (copyout(&sp, SCARG(uap, buf), sizeof(sp)));
1260 }
1261
1262 /*
1263 * Create a special file.
1264 */
1265 /* ARGSUSED */
1266 int
1267 sys_mknod(p, v, retval)
1268 struct proc *p;
1269 void *v;
1270 register_t *retval;
1271 {
1272 register struct sys_mknod_args /* {
1273 syscallarg(const char *) path;
1274 syscallarg(int) mode;
1275 syscallarg(int) dev;
1276 } */ *uap = v;
1277 register struct vnode *vp;
1278 struct vattr vattr;
1279 int error;
1280 int whiteout = 0;
1281 struct nameidata nd;
1282
1283 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1284 return (error);
1285 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1286 if ((error = namei(&nd)) != 0)
1287 return (error);
1288 vp = nd.ni_vp;
1289 if (vp != NULL)
1290 error = EEXIST;
1291 else {
1292 VATTR_NULL(&vattr);
1293 vattr.va_mode =
1294 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1295 vattr.va_rdev = SCARG(uap, dev);
1296 whiteout = 0;
1297
1298 switch (SCARG(uap, mode) & S_IFMT) {
1299 case S_IFMT: /* used by badsect to flag bad sectors */
1300 vattr.va_type = VBAD;
1301 break;
1302 case S_IFCHR:
1303 vattr.va_type = VCHR;
1304 break;
1305 case S_IFBLK:
1306 vattr.va_type = VBLK;
1307 break;
1308 case S_IFWHT:
1309 whiteout = 1;
1310 break;
1311 default:
1312 error = EINVAL;
1313 break;
1314 }
1315 }
1316 if (!error) {
1317 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1318 if (whiteout) {
1319 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1320 if (error)
1321 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1322 vput(nd.ni_dvp);
1323 } else {
1324 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1325 &nd.ni_cnd, &vattr);
1326 }
1327 } else {
1328 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1329 if (nd.ni_dvp == vp)
1330 vrele(nd.ni_dvp);
1331 else
1332 vput(nd.ni_dvp);
1333 if (vp)
1334 vrele(vp);
1335 }
1336 return (error);
1337 }
1338
1339 /*
1340 * Create a named pipe.
1341 */
1342 /* ARGSUSED */
1343 int
1344 sys_mkfifo(p, v, retval)
1345 struct proc *p;
1346 void *v;
1347 register_t *retval;
1348 {
1349 register struct sys_mkfifo_args /* {
1350 syscallarg(const char *) path;
1351 syscallarg(int) mode;
1352 } */ *uap = v;
1353 struct vattr vattr;
1354 int error;
1355 struct nameidata nd;
1356
1357 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
1358 if ((error = namei(&nd)) != 0)
1359 return (error);
1360 if (nd.ni_vp != NULL) {
1361 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1362 if (nd.ni_dvp == nd.ni_vp)
1363 vrele(nd.ni_dvp);
1364 else
1365 vput(nd.ni_dvp);
1366 vrele(nd.ni_vp);
1367 return (EEXIST);
1368 }
1369 VATTR_NULL(&vattr);
1370 vattr.va_type = VFIFO;
1371 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1372 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1373 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
1374 }
1375
1376 /*
1377 * Make a hard file link.
1378 */
1379 /* ARGSUSED */
1380 int
1381 sys_link(p, v, retval)
1382 struct proc *p;
1383 void *v;
1384 register_t *retval;
1385 {
1386 register struct sys_link_args /* {
1387 syscallarg(const char *) path;
1388 syscallarg(const char *) link;
1389 } */ *uap = v;
1390 register struct vnode *vp;
1391 struct nameidata nd;
1392 int error;
1393
1394 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1395 if ((error = namei(&nd)) != 0)
1396 return (error);
1397 vp = nd.ni_vp;
1398 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1399 if ((error = namei(&nd)) != 0)
1400 goto out;
1401 if (nd.ni_vp) {
1402 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1403 if (nd.ni_dvp == nd.ni_vp)
1404 vrele(nd.ni_dvp);
1405 else
1406 vput(nd.ni_dvp);
1407 vrele(nd.ni_vp);
1408 error = EEXIST;
1409 goto out;
1410 }
1411 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1412 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1413 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1414 out:
1415 vrele(vp);
1416 return (error);
1417 }
1418
1419 /*
1420 * Make a symbolic link.
1421 */
1422 /* ARGSUSED */
1423 int
1424 sys_symlink(p, v, retval)
1425 struct proc *p;
1426 void *v;
1427 register_t *retval;
1428 {
1429 register struct sys_symlink_args /* {
1430 syscallarg(const char *) path;
1431 syscallarg(const char *) link;
1432 } */ *uap = v;
1433 struct vattr vattr;
1434 char *path;
1435 int error;
1436 struct nameidata nd;
1437
1438 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1439 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1440 if (error)
1441 goto out;
1442 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1443 if ((error = namei(&nd)) != 0)
1444 goto out;
1445 if (nd.ni_vp) {
1446 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1447 if (nd.ni_dvp == nd.ni_vp)
1448 vrele(nd.ni_dvp);
1449 else
1450 vput(nd.ni_dvp);
1451 vrele(nd.ni_vp);
1452 error = EEXIST;
1453 goto out;
1454 }
1455 VATTR_NULL(&vattr);
1456 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1457 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1458 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1459 out:
1460 FREE(path, M_NAMEI);
1461 return (error);
1462 }
1463
1464 /*
1465 * Delete a whiteout from the filesystem.
1466 */
1467 /* ARGSUSED */
1468 int
1469 sys_undelete(p, v, retval)
1470 struct proc *p;
1471 void *v;
1472 register_t *retval;
1473 {
1474 register struct sys_undelete_args /* {
1475 syscallarg(const char *) path;
1476 } */ *uap = v;
1477 int error;
1478 struct nameidata nd;
1479
1480 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1481 SCARG(uap, path), p);
1482 error = namei(&nd);
1483 if (error)
1484 return (error);
1485
1486 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1487 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1488 if (nd.ni_dvp == nd.ni_vp)
1489 vrele(nd.ni_dvp);
1490 else
1491 vput(nd.ni_dvp);
1492 if (nd.ni_vp)
1493 vrele(nd.ni_vp);
1494 return (EEXIST);
1495 }
1496
1497 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1498 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1499 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1500 vput(nd.ni_dvp);
1501 return (error);
1502 }
1503
1504 /*
1505 * Delete a name from the filesystem.
1506 */
1507 /* ARGSUSED */
1508 int
1509 sys_unlink(p, v, retval)
1510 struct proc *p;
1511 void *v;
1512 register_t *retval;
1513 {
1514 struct sys_unlink_args /* {
1515 syscallarg(const char *) path;
1516 } */ *uap = v;
1517 register struct vnode *vp;
1518 int error;
1519 struct nameidata nd;
1520
1521 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1522 SCARG(uap, path), p);
1523 if ((error = namei(&nd)) != 0)
1524 return (error);
1525 vp = nd.ni_vp;
1526
1527 /*
1528 * The root of a mounted filesystem cannot be deleted.
1529 */
1530 if (vp->v_flag & VROOT) {
1531 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1532 if (nd.ni_dvp == vp)
1533 vrele(nd.ni_dvp);
1534 else
1535 vput(nd.ni_dvp);
1536 vput(vp);
1537 error = EBUSY;
1538 goto out;
1539 }
1540
1541 (void)uvm_vnp_uncache(vp);
1542
1543 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1544 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1545 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1546 out:
1547 return (error);
1548 }
1549
1550 /*
1551 * Reposition read/write file offset.
1552 */
1553 int
1554 sys_lseek(p, v, retval)
1555 struct proc *p;
1556 void *v;
1557 register_t *retval;
1558 {
1559 register struct sys_lseek_args /* {
1560 syscallarg(int) fd;
1561 syscallarg(int) pad;
1562 syscallarg(off_t) offset;
1563 syscallarg(int) whence;
1564 } */ *uap = v;
1565 struct ucred *cred = p->p_ucred;
1566 register struct filedesc *fdp = p->p_fd;
1567 register struct file *fp;
1568 struct vnode *vp;
1569 struct vattr vattr;
1570 register off_t newoff;
1571 int error;
1572
1573 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1574 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
1575 (fp->f_iflags & FIF_WANTCLOSE) != 0)
1576 return (EBADF);
1577
1578 FILE_USE(fp);
1579
1580 vp = (struct vnode *)fp->f_data;
1581 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1582 error = ESPIPE;
1583 goto out;
1584 }
1585
1586 switch (SCARG(uap, whence)) {
1587 case SEEK_CUR:
1588 newoff = fp->f_offset + SCARG(uap, offset);
1589 break;
1590 case SEEK_END:
1591 error = VOP_GETATTR(vp, &vattr, cred, p);
1592 if (error)
1593 goto out;
1594 newoff = SCARG(uap, offset) + vattr.va_size;
1595 break;
1596 case SEEK_SET:
1597 newoff = SCARG(uap, offset);
1598 break;
1599 default:
1600 error = EINVAL;
1601 goto out;
1602 }
1603 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1604 goto out;
1605
1606 *(off_t *)retval = fp->f_offset = newoff;
1607 out:
1608 FILE_UNUSE(fp, p);
1609 return (error);
1610 }
1611
1612 /*
1613 * Positional read system call.
1614 */
1615 int
1616 sys_pread(p, v, retval)
1617 struct proc *p;
1618 void *v;
1619 register_t *retval;
1620 {
1621 struct sys_pread_args /* {
1622 syscallarg(int) fd;
1623 syscallarg(void *) buf;
1624 syscallarg(size_t) nbyte;
1625 syscallarg(off_t) offset;
1626 } */ *uap = v;
1627 struct filedesc *fdp = p->p_fd;
1628 struct file *fp;
1629 struct vnode *vp;
1630 off_t offset;
1631 int error, fd = SCARG(uap, fd);
1632
1633 if ((u_int)fd >= fdp->fd_nfiles ||
1634 (fp = fdp->fd_ofiles[fd]) == NULL ||
1635 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1636 (fp->f_flag & FREAD) == 0)
1637 return (EBADF);
1638
1639 FILE_USE(fp);
1640
1641 vp = (struct vnode *)fp->f_data;
1642 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1643 error = ESPIPE;
1644 goto out;
1645 }
1646
1647 offset = SCARG(uap, offset);
1648
1649 /*
1650 * XXX This works because no file systems actually
1651 * XXX take any action on the seek operation.
1652 */
1653 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1654 goto out;
1655
1656 /* dofileread() will unuse the descriptor for us */
1657 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1658 &offset, 0, retval));
1659
1660 out:
1661 FILE_UNUSE(fp, p);
1662 return (error);
1663 }
1664
1665 /*
1666 * Positional scatter read system call.
1667 */
1668 int
1669 sys_preadv(p, v, retval)
1670 struct proc *p;
1671 void *v;
1672 register_t *retval;
1673 {
1674 struct sys_preadv_args /* {
1675 syscallarg(int) fd;
1676 syscallarg(const struct iovec *) iovp;
1677 syscallarg(int) iovcnt;
1678 syscallarg(off_t) offset;
1679 } */ *uap = v;
1680 struct filedesc *fdp = p->p_fd;
1681 struct file *fp;
1682 struct vnode *vp;
1683 off_t offset;
1684 int error, fd = SCARG(uap, fd);
1685
1686 if ((u_int)fd >= fdp->fd_nfiles ||
1687 (fp = fdp->fd_ofiles[fd]) == NULL ||
1688 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1689 (fp->f_flag & FREAD) == 0)
1690 return (EBADF);
1691
1692 FILE_USE(fp);
1693
1694 vp = (struct vnode *)fp->f_data;
1695 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1696 error = ESPIPE;
1697 goto out;
1698 }
1699
1700 offset = SCARG(uap, offset);
1701
1702 /*
1703 * XXX This works because no file systems actually
1704 * XXX take any action on the seek operation.
1705 */
1706 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1707 goto out;
1708
1709 /* dofilereadv() will unuse the descriptor for us */
1710 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1711 &offset, 0, retval));
1712
1713 out:
1714 FILE_UNUSE(fp, p);
1715 return (error);
1716 }
1717
1718 /*
1719 * Positional write system call.
1720 */
1721 int
1722 sys_pwrite(p, v, retval)
1723 struct proc *p;
1724 void *v;
1725 register_t *retval;
1726 {
1727 struct sys_pwrite_args /* {
1728 syscallarg(int) fd;
1729 syscallarg(const void *) buf;
1730 syscallarg(size_t) nbyte;
1731 syscallarg(off_t) offset;
1732 } */ *uap = v;
1733 struct filedesc *fdp = p->p_fd;
1734 struct file *fp;
1735 struct vnode *vp;
1736 off_t offset;
1737 int error, fd = SCARG(uap, fd);
1738
1739 if ((u_int)fd >= fdp->fd_nfiles ||
1740 (fp = fdp->fd_ofiles[fd]) == NULL ||
1741 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1742 (fp->f_flag & FWRITE) == 0)
1743 return (EBADF);
1744
1745 FILE_USE(fp);
1746
1747 vp = (struct vnode *)fp->f_data;
1748 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1749 error = ESPIPE;
1750 goto out;
1751 }
1752
1753 offset = SCARG(uap, offset);
1754
1755 /*
1756 * XXX This works because no file systems actually
1757 * XXX take any action on the seek operation.
1758 */
1759 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1760 goto out;
1761
1762 /* dofilewrite() will unuse the descriptor for us */
1763 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1764 &offset, 0, retval));
1765
1766 out:
1767 FILE_UNUSE(fp, p);
1768 return (error);
1769 }
1770
1771 /*
1772 * Positional gather write system call.
1773 */
1774 int
1775 sys_pwritev(p, v, retval)
1776 struct proc *p;
1777 void *v;
1778 register_t *retval;
1779 {
1780 struct sys_pwritev_args /* {
1781 syscallarg(int) fd;
1782 syscallarg(const struct iovec *) iovp;
1783 syscallarg(int) iovcnt;
1784 syscallarg(off_t) offset;
1785 } */ *uap = v;
1786 struct filedesc *fdp = p->p_fd;
1787 struct file *fp;
1788 struct vnode *vp;
1789 off_t offset;
1790 int error, fd = SCARG(uap, fd);
1791
1792 if ((u_int)fd >= fdp->fd_nfiles ||
1793 (fp = fdp->fd_ofiles[fd]) == NULL ||
1794 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1795 (fp->f_flag & FWRITE) == 0)
1796 return (EBADF);
1797
1798 FILE_USE(fp);
1799
1800 vp = (struct vnode *)fp->f_data;
1801 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1802 error = ESPIPE;
1803 goto out;
1804 }
1805
1806 offset = SCARG(uap, offset);
1807
1808 /*
1809 * XXX This works because no file systems actually
1810 * XXX take any action on the seek operation.
1811 */
1812 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1813 goto out;
1814
1815 /* dofilewritev() will unuse the descriptor for us */
1816 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1817 &offset, 0, retval));
1818
1819 out:
1820 FILE_UNUSE(fp, p);
1821 return (error);
1822 }
1823
1824 /*
1825 * Check access permissions.
1826 */
1827 int
1828 sys_access(p, v, retval)
1829 struct proc *p;
1830 void *v;
1831 register_t *retval;
1832 {
1833 register struct sys_access_args /* {
1834 syscallarg(const char *) path;
1835 syscallarg(int) flags;
1836 } */ *uap = v;
1837 register struct ucred *cred = p->p_ucred;
1838 register struct vnode *vp;
1839 int error, flags, t_gid, t_uid;
1840 struct nameidata nd;
1841
1842 t_uid = cred->cr_uid;
1843 t_gid = cred->cr_gid;
1844 cred->cr_uid = p->p_cred->p_ruid;
1845 cred->cr_gid = p->p_cred->p_rgid;
1846 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1847 SCARG(uap, path), p);
1848 if ((error = namei(&nd)) != 0)
1849 goto out1;
1850 vp = nd.ni_vp;
1851
1852 /* Flags == 0 means only check for existence. */
1853 if (SCARG(uap, flags)) {
1854 flags = 0;
1855 if (SCARG(uap, flags) & R_OK)
1856 flags |= VREAD;
1857 if (SCARG(uap, flags) & W_OK)
1858 flags |= VWRITE;
1859 if (SCARG(uap, flags) & X_OK)
1860 flags |= VEXEC;
1861
1862 error = VOP_ACCESS(vp, flags, cred, p);
1863 if (!error && (flags & VWRITE))
1864 error = vn_writechk(vp);
1865 }
1866 vput(vp);
1867 out1:
1868 cred->cr_uid = t_uid;
1869 cred->cr_gid = t_gid;
1870 return (error);
1871 }
1872
1873 /*
1874 * Get file status; this version follows links.
1875 */
1876 /* ARGSUSED */
1877 int
1878 sys___stat13(p, v, retval)
1879 struct proc *p;
1880 void *v;
1881 register_t *retval;
1882 {
1883 register struct sys___stat13_args /* {
1884 syscallarg(const char *) path;
1885 syscallarg(struct stat *) ub;
1886 } */ *uap = v;
1887 struct stat sb;
1888 int error;
1889 struct nameidata nd;
1890
1891 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1892 SCARG(uap, path), p);
1893 if ((error = namei(&nd)) != 0)
1894 return (error);
1895 error = vn_stat(nd.ni_vp, &sb, p);
1896 vput(nd.ni_vp);
1897 if (error)
1898 return (error);
1899 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1900 return (error);
1901 }
1902
1903 /*
1904 * Get file status; this version does not follow links.
1905 */
1906 /* ARGSUSED */
1907 int
1908 sys___lstat13(p, v, retval)
1909 struct proc *p;
1910 void *v;
1911 register_t *retval;
1912 {
1913 register struct sys___lstat13_args /* {
1914 syscallarg(const char *) path;
1915 syscallarg(struct stat *) ub;
1916 } */ *uap = v;
1917 struct stat sb;
1918 int error;
1919 struct nameidata nd;
1920
1921 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1922 SCARG(uap, path), p);
1923 if ((error = namei(&nd)) != 0)
1924 return (error);
1925 error = vn_stat(nd.ni_vp, &sb, p);
1926 vput(nd.ni_vp);
1927 if (error)
1928 return (error);
1929 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1930 return (error);
1931 }
1932
1933 /*
1934 * Get configurable pathname variables.
1935 */
1936 /* ARGSUSED */
1937 int
1938 sys_pathconf(p, v, retval)
1939 struct proc *p;
1940 void *v;
1941 register_t *retval;
1942 {
1943 register struct sys_pathconf_args /* {
1944 syscallarg(const char *) path;
1945 syscallarg(int) name;
1946 } */ *uap = v;
1947 int error;
1948 struct nameidata nd;
1949
1950 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1951 SCARG(uap, path), p);
1952 if ((error = namei(&nd)) != 0)
1953 return (error);
1954 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1955 vput(nd.ni_vp);
1956 return (error);
1957 }
1958
1959 /*
1960 * Return target name of a symbolic link.
1961 */
1962 /* ARGSUSED */
1963 int
1964 sys_readlink(p, v, retval)
1965 struct proc *p;
1966 void *v;
1967 register_t *retval;
1968 {
1969 register struct sys_readlink_args /* {
1970 syscallarg(const char *) path;
1971 syscallarg(char *) buf;
1972 syscallarg(size_t) count;
1973 } */ *uap = v;
1974 register struct vnode *vp;
1975 struct iovec aiov;
1976 struct uio auio;
1977 int error;
1978 struct nameidata nd;
1979
1980 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1981 SCARG(uap, path), p);
1982 if ((error = namei(&nd)) != 0)
1983 return (error);
1984 vp = nd.ni_vp;
1985 if (vp->v_type != VLNK)
1986 error = EINVAL;
1987 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
1988 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
1989 aiov.iov_base = SCARG(uap, buf);
1990 aiov.iov_len = SCARG(uap, count);
1991 auio.uio_iov = &aiov;
1992 auio.uio_iovcnt = 1;
1993 auio.uio_offset = 0;
1994 auio.uio_rw = UIO_READ;
1995 auio.uio_segflg = UIO_USERSPACE;
1996 auio.uio_procp = p;
1997 auio.uio_resid = SCARG(uap, count);
1998 error = VOP_READLINK(vp, &auio, p->p_ucred);
1999 }
2000 vput(vp);
2001 *retval = SCARG(uap, count) - auio.uio_resid;
2002 return (error);
2003 }
2004
2005 /*
2006 * Change flags of a file given a path name.
2007 */
2008 /* ARGSUSED */
2009 int
2010 sys_chflags(p, v, retval)
2011 struct proc *p;
2012 void *v;
2013 register_t *retval;
2014 {
2015 register struct sys_chflags_args /* {
2016 syscallarg(const char *) path;
2017 syscallarg(u_long) flags;
2018 } */ *uap = v;
2019 register struct vnode *vp;
2020 struct vattr vattr;
2021 int error;
2022 struct nameidata nd;
2023
2024 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2025 if ((error = namei(&nd)) != 0)
2026 return (error);
2027 vp = nd.ni_vp;
2028 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2029 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2030 VATTR_NULL(&vattr);
2031 vattr.va_flags = SCARG(uap, flags);
2032 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2033 vput(vp);
2034 return (error);
2035 }
2036
2037 /*
2038 * Change flags of a file given a file descriptor.
2039 */
2040 /* ARGSUSED */
2041 int
2042 sys_fchflags(p, v, retval)
2043 struct proc *p;
2044 void *v;
2045 register_t *retval;
2046 {
2047 register struct sys_fchflags_args /* {
2048 syscallarg(int) fd;
2049 syscallarg(u_long) flags;
2050 } */ *uap = v;
2051 struct vattr vattr;
2052 struct vnode *vp;
2053 struct file *fp;
2054 int error;
2055
2056 /* getvnode() will use the descriptor for us */
2057 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2058 return (error);
2059 vp = (struct vnode *)fp->f_data;
2060 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2061 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2062 VATTR_NULL(&vattr);
2063 vattr.va_flags = SCARG(uap, flags);
2064 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2065 VOP_UNLOCK(vp, 0);
2066 FILE_UNUSE(fp, p);
2067 return (error);
2068 }
2069
2070 /*
2071 * Change mode of a file given path name; this version follows links.
2072 */
2073 /* ARGSUSED */
2074 int
2075 sys_chmod(p, v, retval)
2076 struct proc *p;
2077 void *v;
2078 register_t *retval;
2079 {
2080 register struct sys_chmod_args /* {
2081 syscallarg(const char *) path;
2082 syscallarg(int) mode;
2083 } */ *uap = v;
2084 int error;
2085 struct nameidata nd;
2086
2087 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2088 if ((error = namei(&nd)) != 0)
2089 return (error);
2090
2091 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2092
2093 vrele(nd.ni_vp);
2094 return (error);
2095 }
2096
2097 /*
2098 * Change mode of a file given a file descriptor.
2099 */
2100 /* ARGSUSED */
2101 int
2102 sys_fchmod(p, v, retval)
2103 struct proc *p;
2104 void *v;
2105 register_t *retval;
2106 {
2107 register struct sys_fchmod_args /* {
2108 syscallarg(int) fd;
2109 syscallarg(int) mode;
2110 } */ *uap = v;
2111 struct file *fp;
2112 int error;
2113
2114 /* getvnode() will use the descriptor for us */
2115 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2116 return (error);
2117
2118 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2119 FILE_UNUSE(fp, p);
2120 return (error);
2121 }
2122
2123 /*
2124 * Change mode of a file given path name; this version does not follow links.
2125 */
2126 /* ARGSUSED */
2127 int
2128 sys_lchmod(p, v, retval)
2129 struct proc *p;
2130 void *v;
2131 register_t *retval;
2132 {
2133 register struct sys_lchmod_args /* {
2134 syscallarg(const char *) path;
2135 syscallarg(int) mode;
2136 } */ *uap = v;
2137 int error;
2138 struct nameidata nd;
2139
2140 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2141 if ((error = namei(&nd)) != 0)
2142 return (error);
2143
2144 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2145
2146 vrele(nd.ni_vp);
2147 return (error);
2148 }
2149
2150 /*
2151 * Common routine to set mode given a vnode.
2152 */
2153 static int
2154 change_mode(vp, mode, p)
2155 struct vnode *vp;
2156 int mode;
2157 struct proc *p;
2158 {
2159 struct vattr vattr;
2160 int error;
2161
2162 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2163 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2164 VATTR_NULL(&vattr);
2165 vattr.va_mode = mode & ALLPERMS;
2166 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2167 VOP_UNLOCK(vp, 0);
2168 return (error);
2169 }
2170
2171 /*
2172 * Set ownership given a path name; this version follows links.
2173 */
2174 /* ARGSUSED */
2175 int
2176 sys_chown(p, v, retval)
2177 struct proc *p;
2178 void *v;
2179 register_t *retval;
2180 {
2181 register struct sys_chown_args /* {
2182 syscallarg(const char *) path;
2183 syscallarg(uid_t) uid;
2184 syscallarg(gid_t) gid;
2185 } */ *uap = v;
2186 int error;
2187 struct nameidata nd;
2188
2189 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2190 if ((error = namei(&nd)) != 0)
2191 return (error);
2192
2193 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2194
2195 vrele(nd.ni_vp);
2196 return (error);
2197 }
2198
2199 /*
2200 * Set ownership given a path name; this version follows links.
2201 * Provides POSIX semantics.
2202 */
2203 /* ARGSUSED */
2204 int
2205 sys___posix_chown(p, v, retval)
2206 struct proc *p;
2207 void *v;
2208 register_t *retval;
2209 {
2210 register struct sys_chown_args /* {
2211 syscallarg(const char *) path;
2212 syscallarg(uid_t) uid;
2213 syscallarg(gid_t) gid;
2214 } */ *uap = v;
2215 int error;
2216 struct nameidata nd;
2217
2218 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2219 if ((error = namei(&nd)) != 0)
2220 return (error);
2221
2222 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2223
2224 vrele(nd.ni_vp);
2225 return (error);
2226 }
2227
2228 /*
2229 * Set ownership given a file descriptor.
2230 */
2231 /* ARGSUSED */
2232 int
2233 sys_fchown(p, v, retval)
2234 struct proc *p;
2235 void *v;
2236 register_t *retval;
2237 {
2238 register struct sys_fchown_args /* {
2239 syscallarg(int) fd;
2240 syscallarg(uid_t) uid;
2241 syscallarg(gid_t) gid;
2242 } */ *uap = v;
2243 int error;
2244 struct file *fp;
2245
2246 /* getvnode() will use the descriptor for us */
2247 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2248 return (error);
2249
2250 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2251 SCARG(uap, gid), p, 0);
2252 FILE_UNUSE(fp, p);
2253 return (error);
2254 }
2255
2256 /*
2257 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2258 */
2259 /* ARGSUSED */
2260 int
2261 sys___posix_fchown(p, v, retval)
2262 struct proc *p;
2263 void *v;
2264 register_t *retval;
2265 {
2266 register struct sys_fchown_args /* {
2267 syscallarg(int) fd;
2268 syscallarg(uid_t) uid;
2269 syscallarg(gid_t) gid;
2270 } */ *uap = v;
2271 int error;
2272 struct file *fp;
2273
2274 /* getvnode() will use the descriptor for us */
2275 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2276 return (error);
2277
2278 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2279 SCARG(uap, gid), p, 1);
2280 FILE_UNUSE(fp, p);
2281 return (error);
2282 }
2283
2284 /*
2285 * Set ownership given a path name; this version does not follow links.
2286 */
2287 /* ARGSUSED */
2288 int
2289 sys_lchown(p, v, retval)
2290 struct proc *p;
2291 void *v;
2292 register_t *retval;
2293 {
2294 register struct sys_lchown_args /* {
2295 syscallarg(const char *) path;
2296 syscallarg(uid_t) uid;
2297 syscallarg(gid_t) gid;
2298 } */ *uap = v;
2299 int error;
2300 struct nameidata nd;
2301
2302 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2303 if ((error = namei(&nd)) != 0)
2304 return (error);
2305
2306 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2307
2308 vrele(nd.ni_vp);
2309 return (error);
2310 }
2311
2312 /*
2313 * Set ownership given a path name; this version does not follow links.
2314 * Provides POSIX/XPG semantics.
2315 */
2316 /* ARGSUSED */
2317 int
2318 sys___posix_lchown(p, v, retval)
2319 struct proc *p;
2320 void *v;
2321 register_t *retval;
2322 {
2323 register struct sys_lchown_args /* {
2324 syscallarg(const char *) path;
2325 syscallarg(uid_t) uid;
2326 syscallarg(gid_t) gid;
2327 } */ *uap = v;
2328 int error;
2329 struct nameidata nd;
2330
2331 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2332 if ((error = namei(&nd)) != 0)
2333 return (error);
2334
2335 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2336
2337 vrele(nd.ni_vp);
2338 return (error);
2339 }
2340
2341 /*
2342 * Common routine to set ownership given a vnode.
2343 */
2344 static int
2345 change_owner(vp, uid, gid, p, posix_semantics)
2346 register struct vnode *vp;
2347 uid_t uid;
2348 gid_t gid;
2349 struct proc *p;
2350 int posix_semantics;
2351 {
2352 struct vattr vattr;
2353 mode_t newmode;
2354 int error;
2355
2356 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2357 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2358 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2359 goto out;
2360
2361 #define CHANGED(x) ((x) != -1)
2362 newmode = vattr.va_mode;
2363 if (posix_semantics) {
2364 /*
2365 * POSIX/XPG semantics: if the caller is not the super-user,
2366 * clear set-user-id and set-group-id bits. Both POSIX and
2367 * the XPG consider the behaviour for calls by the super-user
2368 * implementation-defined; we leave the set-user-id and set-
2369 * group-id settings intact in that case.
2370 */
2371 if (suser(p->p_ucred, NULL) != 0)
2372 newmode &= ~(S_ISUID | S_ISGID);
2373 } else {
2374 /*
2375 * NetBSD semantics: when changing owner and/or group,
2376 * clear the respective bit(s).
2377 */
2378 if (CHANGED(uid))
2379 newmode &= ~S_ISUID;
2380 if (CHANGED(gid))
2381 newmode &= ~S_ISGID;
2382 }
2383 /* Update va_mode iff altered. */
2384 if (vattr.va_mode == newmode)
2385 newmode = VNOVAL;
2386
2387 VATTR_NULL(&vattr);
2388 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
2389 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
2390 vattr.va_mode = newmode;
2391 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2392 #undef CHANGED
2393
2394 out:
2395 VOP_UNLOCK(vp, 0);
2396 return (error);
2397 }
2398
2399 /*
2400 * Set the access and modification times given a path name; this
2401 * version follows links.
2402 */
2403 /* ARGSUSED */
2404 int
2405 sys_utimes(p, v, retval)
2406 struct proc *p;
2407 void *v;
2408 register_t *retval;
2409 {
2410 register struct sys_utimes_args /* {
2411 syscallarg(const char *) path;
2412 syscallarg(const struct timeval *) tptr;
2413 } */ *uap = v;
2414 int error;
2415 struct nameidata nd;
2416
2417 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2418 if ((error = namei(&nd)) != 0)
2419 return (error);
2420
2421 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2422
2423 vrele(nd.ni_vp);
2424 return (error);
2425 }
2426
2427 /*
2428 * Set the access and modification times given a file descriptor.
2429 */
2430 /* ARGSUSED */
2431 int
2432 sys_futimes(p, v, retval)
2433 struct proc *p;
2434 void *v;
2435 register_t *retval;
2436 {
2437 register struct sys_futimes_args /* {
2438 syscallarg(int) fd;
2439 syscallarg(const struct timeval *) tptr;
2440 } */ *uap = v;
2441 int error;
2442 struct file *fp;
2443
2444 /* getvnode() will use the descriptor for us */
2445 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2446 return (error);
2447
2448 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2449 FILE_UNUSE(fp, p);
2450 return (error);
2451 }
2452
2453 /*
2454 * Set the access and modification times given a path name; this
2455 * version does not follow links.
2456 */
2457 /* ARGSUSED */
2458 int
2459 sys_lutimes(p, v, retval)
2460 struct proc *p;
2461 void *v;
2462 register_t *retval;
2463 {
2464 register struct sys_lutimes_args /* {
2465 syscallarg(const char *) path;
2466 syscallarg(const struct timeval *) tptr;
2467 } */ *uap = v;
2468 int error;
2469 struct nameidata nd;
2470
2471 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2472 if ((error = namei(&nd)) != 0)
2473 return (error);
2474
2475 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2476
2477 vrele(nd.ni_vp);
2478 return (error);
2479 }
2480
2481 /*
2482 * Common routine to set access and modification times given a vnode.
2483 */
2484 static int
2485 change_utimes(vp, tptr, p)
2486 struct vnode *vp;
2487 const struct timeval *tptr;
2488 struct proc *p;
2489 {
2490 struct timeval tv[2];
2491 struct vattr vattr;
2492 int error;
2493
2494 VATTR_NULL(&vattr);
2495 if (tptr == NULL) {
2496 microtime(&tv[0]);
2497 tv[1] = tv[0];
2498 vattr.va_vaflags |= VA_UTIMES_NULL;
2499 } else {
2500 error = copyin(tptr, tv, sizeof(tv));
2501 if (error)
2502 return (error);
2503 }
2504 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2505 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2506 vattr.va_atime.tv_sec = tv[0].tv_sec;
2507 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2508 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2509 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2510 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2511 VOP_UNLOCK(vp, 0);
2512 return (error);
2513 }
2514
2515 /*
2516 * Truncate a file given its path name.
2517 */
2518 /* ARGSUSED */
2519 int
2520 sys_truncate(p, v, retval)
2521 struct proc *p;
2522 void *v;
2523 register_t *retval;
2524 {
2525 register struct sys_truncate_args /* {
2526 syscallarg(const char *) path;
2527 syscallarg(int) pad;
2528 syscallarg(off_t) length;
2529 } */ *uap = v;
2530 register struct vnode *vp;
2531 struct vattr vattr;
2532 int error;
2533 struct nameidata nd;
2534
2535 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2536 if ((error = namei(&nd)) != 0)
2537 return (error);
2538 vp = nd.ni_vp;
2539 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2540 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2541 if (vp->v_type == VDIR)
2542 error = EISDIR;
2543 else if ((error = vn_writechk(vp)) == 0 &&
2544 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2545 VATTR_NULL(&vattr);
2546 vattr.va_size = SCARG(uap, length);
2547 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2548 }
2549 vput(vp);
2550 return (error);
2551 }
2552
2553 /*
2554 * Truncate a file given a file descriptor.
2555 */
2556 /* ARGSUSED */
2557 int
2558 sys_ftruncate(p, v, retval)
2559 struct proc *p;
2560 void *v;
2561 register_t *retval;
2562 {
2563 register struct sys_ftruncate_args /* {
2564 syscallarg(int) fd;
2565 syscallarg(int) pad;
2566 syscallarg(off_t) length;
2567 } */ *uap = v;
2568 struct vattr vattr;
2569 struct vnode *vp;
2570 struct file *fp;
2571 int error;
2572
2573 /* getvnode() will use the descriptor for us */
2574 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2575 return (error);
2576 if ((fp->f_flag & FWRITE) == 0) {
2577 error = EINVAL;
2578 goto out;
2579 }
2580 vp = (struct vnode *)fp->f_data;
2581 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2582 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2583 if (vp->v_type == VDIR)
2584 error = EISDIR;
2585 else if ((error = vn_writechk(vp)) == 0) {
2586 VATTR_NULL(&vattr);
2587 vattr.va_size = SCARG(uap, length);
2588 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2589 }
2590 VOP_UNLOCK(vp, 0);
2591 out:
2592 FILE_UNUSE(fp, p);
2593 return (error);
2594 }
2595
2596 /*
2597 * Sync an open file.
2598 */
2599 /* ARGSUSED */
2600 int
2601 sys_fsync(p, v, retval)
2602 struct proc *p;
2603 void *v;
2604 register_t *retval;
2605 {
2606 struct sys_fsync_args /* {
2607 syscallarg(int) fd;
2608 } */ *uap = v;
2609 register struct vnode *vp;
2610 struct file *fp;
2611 int error;
2612
2613 /* getvnode() will use the descriptor for us */
2614 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2615 return (error);
2616 vp = (struct vnode *)fp->f_data;
2617 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2618 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
2619 VOP_UNLOCK(vp, 0);
2620 FILE_UNUSE(fp, p);
2621 return (error);
2622 }
2623
2624 /*
2625 * Sync the data of an open file.
2626 */
2627 /* ARGSUSED */
2628 int
2629 sys_fdatasync(p, v, retval)
2630 struct proc *p;
2631 void *v;
2632 register_t *retval;
2633 {
2634 struct sys_fdatasync_args /* {
2635 syscallarg(int) fd;
2636 } */ *uap = v;
2637 struct vnode *vp;
2638 struct file *fp;
2639 int error;
2640
2641 /* getvnode() will use the descriptor for us */
2642 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2643 return (error);
2644 vp = (struct vnode *)fp->f_data;
2645 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2646 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
2647 VOP_UNLOCK(vp, 0);
2648 FILE_UNUSE(fp, p);
2649 return (error);
2650 }
2651
2652 /*
2653 * Rename files, (standard) BSD semantics frontend.
2654 */
2655 /* ARGSUSED */
2656 int
2657 sys_rename(p, v, retval)
2658 struct proc *p;
2659 void *v;
2660 register_t *retval;
2661 {
2662 register struct sys_rename_args /* {
2663 syscallarg(const char *) from;
2664 syscallarg(const char *) to;
2665 } */ *uap = v;
2666
2667 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2668 }
2669
2670 /*
2671 * Rename files, POSIX semantics frontend.
2672 */
2673 /* ARGSUSED */
2674 int
2675 sys___posix_rename(p, v, retval)
2676 struct proc *p;
2677 void *v;
2678 register_t *retval;
2679 {
2680 register struct sys___posix_rename_args /* {
2681 syscallarg(const char *) from;
2682 syscallarg(const char *) to;
2683 } */ *uap = v;
2684
2685 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2686 }
2687
2688 /*
2689 * Rename files. Source and destination must either both be directories,
2690 * or both not be directories. If target is a directory, it must be empty.
2691 * If `from' and `to' refer to the same object, the value of the `retain'
2692 * argument is used to determine whether `from' will be
2693 *
2694 * (retain == 0) deleted unless `from' and `to' refer to the same
2695 * object in the file system's name space (BSD).
2696 * (retain == 1) always retained (POSIX).
2697 */
2698 static int
2699 rename_files(from, to, p, retain)
2700 const char *from, *to;
2701 struct proc *p;
2702 int retain;
2703 {
2704 register struct vnode *tvp, *fvp, *tdvp;
2705 struct nameidata fromnd, tond;
2706 int error;
2707
2708 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2709 from, p);
2710 if ((error = namei(&fromnd)) != 0)
2711 return (error);
2712 fvp = fromnd.ni_vp;
2713 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2714 UIO_USERSPACE, to, p);
2715 if ((error = namei(&tond)) != 0) {
2716 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2717 vrele(fromnd.ni_dvp);
2718 vrele(fvp);
2719 goto out1;
2720 }
2721 tdvp = tond.ni_dvp;
2722 tvp = tond.ni_vp;
2723
2724 if (tvp != NULL) {
2725 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2726 error = ENOTDIR;
2727 goto out;
2728 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2729 error = EISDIR;
2730 goto out;
2731 }
2732 }
2733
2734 if (fvp == tdvp)
2735 error = EINVAL;
2736
2737 /*
2738 * Source and destination refer to the same object.
2739 */
2740 if (fvp == tvp) {
2741 if (retain)
2742 error = -1;
2743 else if (fromnd.ni_dvp == tdvp &&
2744 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2745 !memcmp(fromnd.ni_cnd.cn_nameptr,
2746 tond.ni_cnd.cn_nameptr,
2747 fromnd.ni_cnd.cn_namelen))
2748 error = -1;
2749 }
2750
2751 out:
2752 if (!error) {
2753 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2754 if (fromnd.ni_dvp != tdvp)
2755 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2756 if (tvp) {
2757 (void)uvm_vnp_uncache(tvp);
2758 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2759 }
2760 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2761 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2762 } else {
2763 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2764 if (tdvp == tvp)
2765 vrele(tdvp);
2766 else
2767 vput(tdvp);
2768 if (tvp)
2769 vput(tvp);
2770 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2771 vrele(fromnd.ni_dvp);
2772 vrele(fvp);
2773 }
2774 vrele(tond.ni_startdir);
2775 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2776 out1:
2777 if (fromnd.ni_startdir)
2778 vrele(fromnd.ni_startdir);
2779 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2780 return (error == -1 ? 0 : error);
2781 }
2782
2783 /*
2784 * Make a directory file.
2785 */
2786 /* ARGSUSED */
2787 int
2788 sys_mkdir(p, v, retval)
2789 struct proc *p;
2790 void *v;
2791 register_t *retval;
2792 {
2793 register struct sys_mkdir_args /* {
2794 syscallarg(const char *) path;
2795 syscallarg(int) mode;
2796 } */ *uap = v;
2797 register struct vnode *vp;
2798 struct vattr vattr;
2799 int error;
2800 struct nameidata nd;
2801
2802 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2803 if ((error = namei(&nd)) != 0)
2804 return (error);
2805 vp = nd.ni_vp;
2806 if (vp != NULL) {
2807 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2808 if (nd.ni_dvp == vp)
2809 vrele(nd.ni_dvp);
2810 else
2811 vput(nd.ni_dvp);
2812 vrele(vp);
2813 return (EEXIST);
2814 }
2815 VATTR_NULL(&vattr);
2816 vattr.va_type = VDIR;
2817 vattr.va_mode =
2818 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
2819 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2820 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2821 if (!error)
2822 vput(nd.ni_vp);
2823 return (error);
2824 }
2825
2826 /*
2827 * Remove a directory file.
2828 */
2829 /* ARGSUSED */
2830 int
2831 sys_rmdir(p, v, retval)
2832 struct proc *p;
2833 void *v;
2834 register_t *retval;
2835 {
2836 struct sys_rmdir_args /* {
2837 syscallarg(const char *) path;
2838 } */ *uap = v;
2839 register struct vnode *vp;
2840 int error;
2841 struct nameidata nd;
2842
2843 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2844 SCARG(uap, path), p);
2845 if ((error = namei(&nd)) != 0)
2846 return (error);
2847 vp = nd.ni_vp;
2848 if (vp->v_type != VDIR) {
2849 error = ENOTDIR;
2850 goto out;
2851 }
2852 /*
2853 * No rmdir "." please.
2854 */
2855 if (nd.ni_dvp == vp) {
2856 error = EINVAL;
2857 goto out;
2858 }
2859 /*
2860 * The root of a mounted filesystem cannot be deleted.
2861 */
2862 if (vp->v_flag & VROOT)
2863 error = EBUSY;
2864 out:
2865 if (!error) {
2866 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2867 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2868 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2869 } else {
2870 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2871 if (nd.ni_dvp == vp)
2872 vrele(nd.ni_dvp);
2873 else
2874 vput(nd.ni_dvp);
2875 vput(vp);
2876 }
2877 return (error);
2878 }
2879
2880 /*
2881 * Read a block of directory entries in a file system independent format.
2882 */
2883 int
2884 sys_getdents(p, v, retval)
2885 struct proc *p;
2886 void *v;
2887 register_t *retval;
2888 {
2889 register struct sys_getdents_args /* {
2890 syscallarg(int) fd;
2891 syscallarg(char *) buf;
2892 syscallarg(size_t) count;
2893 } */ *uap = v;
2894 struct file *fp;
2895 int error, done;
2896
2897 /* getvnode() will use the descriptor for us */
2898 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2899 return (error);
2900 if ((fp->f_flag & FREAD) == 0) {
2901 error = EBADF;
2902 goto out;
2903 }
2904 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2905 SCARG(uap, count), &done, p, 0, 0);
2906 *retval = done;
2907 out:
2908 FILE_UNUSE(fp, p);
2909 return (error);
2910 }
2911
2912 /*
2913 * Set the mode mask for creation of filesystem nodes.
2914 */
2915 int
2916 sys_umask(p, v, retval)
2917 struct proc *p;
2918 void *v;
2919 register_t *retval;
2920 {
2921 struct sys_umask_args /* {
2922 syscallarg(mode_t) newmask;
2923 } */ *uap = v;
2924 struct cwdinfo *cwdi;
2925
2926 cwdi = p->p_cwdi;
2927 *retval = cwdi->cwdi_cmask;
2928 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
2929 return (0);
2930 }
2931
2932 /*
2933 * Void all references to file by ripping underlying filesystem
2934 * away from vnode.
2935 */
2936 /* ARGSUSED */
2937 int
2938 sys_revoke(p, v, retval)
2939 struct proc *p;
2940 void *v;
2941 register_t *retval;
2942 {
2943 register struct sys_revoke_args /* {
2944 syscallarg(const char *) path;
2945 } */ *uap = v;
2946 register struct vnode *vp;
2947 struct vattr vattr;
2948 int error;
2949 struct nameidata nd;
2950
2951 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2952 if ((error = namei(&nd)) != 0)
2953 return (error);
2954 vp = nd.ni_vp;
2955 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2956 goto out;
2957 if (p->p_ucred->cr_uid != vattr.va_uid &&
2958 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2959 goto out;
2960 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2961 VOP_REVOKE(vp, REVOKEALL);
2962 out:
2963 vrele(vp);
2964 return (error);
2965 }
2966
2967 /*
2968 * Convert a user file descriptor to a kernel file entry.
2969 */
2970 int
2971 getvnode(fdp, fd, fpp)
2972 struct filedesc *fdp;
2973 int fd;
2974 struct file **fpp;
2975 {
2976 struct vnode *vp;
2977 struct file *fp;
2978
2979 if ((u_int)fd >= fdp->fd_nfiles ||
2980 (fp = fdp->fd_ofiles[fd]) == NULL ||
2981 (fp->f_iflags & FIF_WANTCLOSE) != 0)
2982 return (EBADF);
2983
2984 FILE_USE(fp);
2985
2986 if (fp->f_type != DTYPE_VNODE) {
2987 FILE_UNUSE(fp, NULL);
2988 return (EINVAL);
2989 }
2990
2991 vp = (struct vnode *)fp->f_data;
2992 if (vp->v_type == VBAD) {
2993 FILE_UNUSE(fp, NULL);
2994 return (EBADF);
2995 }
2996
2997 *fpp = fp;
2998 return (0);
2999 }
3000