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