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