vfs_syscalls.c revision 1.133.4.4 1 /* $NetBSD: vfs_syscalls.c,v 1.133.4.4 1999/07/11 05:43:56 chs 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 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1537 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1538 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1539 out:
1540 return (error);
1541 }
1542
1543 /*
1544 * Reposition read/write file offset.
1545 */
1546 int
1547 sys_lseek(p, v, retval)
1548 struct proc *p;
1549 void *v;
1550 register_t *retval;
1551 {
1552 register struct sys_lseek_args /* {
1553 syscallarg(int) fd;
1554 syscallarg(int) pad;
1555 syscallarg(off_t) offset;
1556 syscallarg(int) whence;
1557 } */ *uap = v;
1558 struct ucred *cred = p->p_ucred;
1559 register struct filedesc *fdp = p->p_fd;
1560 register struct file *fp;
1561 struct vnode *vp;
1562 struct vattr vattr;
1563 register off_t newoff;
1564 int error;
1565
1566 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1567 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
1568 (fp->f_iflags & FIF_WANTCLOSE) != 0)
1569 return (EBADF);
1570
1571 FILE_USE(fp);
1572
1573 vp = (struct vnode *)fp->f_data;
1574 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1575 error = ESPIPE;
1576 goto out;
1577 }
1578
1579 switch (SCARG(uap, whence)) {
1580 case SEEK_CUR:
1581 newoff = fp->f_offset + SCARG(uap, offset);
1582 break;
1583 case SEEK_END:
1584 error = VOP_GETATTR(vp, &vattr, cred, p);
1585 if (error)
1586 goto out;
1587 newoff = SCARG(uap, offset) + vattr.va_size;
1588 break;
1589 case SEEK_SET:
1590 newoff = SCARG(uap, offset);
1591 break;
1592 default:
1593 error = EINVAL;
1594 goto out;
1595 }
1596 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1597 goto out;
1598
1599 *(off_t *)retval = fp->f_offset = newoff;
1600 out:
1601 FILE_UNUSE(fp, p);
1602 return (error);
1603 }
1604
1605 /*
1606 * Positional read system call.
1607 */
1608 int
1609 sys_pread(p, v, retval)
1610 struct proc *p;
1611 void *v;
1612 register_t *retval;
1613 {
1614 struct sys_pread_args /* {
1615 syscallarg(int) fd;
1616 syscallarg(void *) buf;
1617 syscallarg(size_t) nbyte;
1618 syscallarg(off_t) offset;
1619 } */ *uap = v;
1620 struct filedesc *fdp = p->p_fd;
1621 struct file *fp;
1622 struct vnode *vp;
1623 off_t offset;
1624 int error, fd = SCARG(uap, fd);
1625
1626 if ((u_int)fd >= fdp->fd_nfiles ||
1627 (fp = fdp->fd_ofiles[fd]) == NULL ||
1628 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1629 (fp->f_flag & FREAD) == 0)
1630 return (EBADF);
1631
1632 FILE_USE(fp);
1633
1634 vp = (struct vnode *)fp->f_data;
1635 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1636 error = ESPIPE;
1637 goto out;
1638 }
1639
1640 offset = SCARG(uap, offset);
1641
1642 /*
1643 * XXX This works because no file systems actually
1644 * XXX take any action on the seek operation.
1645 */
1646 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1647 goto out;
1648
1649 /* dofileread() will unuse the descriptor for us */
1650 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1651 &offset, 0, retval));
1652
1653 out:
1654 FILE_UNUSE(fp, p);
1655 return (error);
1656 }
1657
1658 /*
1659 * Positional scatter read system call.
1660 */
1661 int
1662 sys_preadv(p, v, retval)
1663 struct proc *p;
1664 void *v;
1665 register_t *retval;
1666 {
1667 struct sys_preadv_args /* {
1668 syscallarg(int) fd;
1669 syscallarg(const struct iovec *) iovp;
1670 syscallarg(int) iovcnt;
1671 syscallarg(off_t) offset;
1672 } */ *uap = v;
1673 struct filedesc *fdp = p->p_fd;
1674 struct file *fp;
1675 struct vnode *vp;
1676 off_t offset;
1677 int error, fd = SCARG(uap, fd);
1678
1679 if ((u_int)fd >= fdp->fd_nfiles ||
1680 (fp = fdp->fd_ofiles[fd]) == NULL ||
1681 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1682 (fp->f_flag & FREAD) == 0)
1683 return (EBADF);
1684
1685 FILE_USE(fp);
1686
1687 vp = (struct vnode *)fp->f_data;
1688 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1689 error = ESPIPE;
1690 goto out;
1691 }
1692
1693 offset = SCARG(uap, offset);
1694
1695 /*
1696 * XXX This works because no file systems actually
1697 * XXX take any action on the seek operation.
1698 */
1699 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1700 goto out;
1701
1702 /* dofilereadv() will unuse the descriptor for us */
1703 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1704 &offset, 0, retval));
1705
1706 out:
1707 FILE_UNUSE(fp, p);
1708 return (error);
1709 }
1710
1711 /*
1712 * Positional write system call.
1713 */
1714 int
1715 sys_pwrite(p, v, retval)
1716 struct proc *p;
1717 void *v;
1718 register_t *retval;
1719 {
1720 struct sys_pwrite_args /* {
1721 syscallarg(int) fd;
1722 syscallarg(const void *) buf;
1723 syscallarg(size_t) nbyte;
1724 syscallarg(off_t) offset;
1725 } */ *uap = v;
1726 struct filedesc *fdp = p->p_fd;
1727 struct file *fp;
1728 struct vnode *vp;
1729 off_t offset;
1730 int error, fd = SCARG(uap, fd);
1731
1732 if ((u_int)fd >= fdp->fd_nfiles ||
1733 (fp = fdp->fd_ofiles[fd]) == NULL ||
1734 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1735 (fp->f_flag & FWRITE) == 0)
1736 return (EBADF);
1737
1738 FILE_USE(fp);
1739
1740 vp = (struct vnode *)fp->f_data;
1741 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1742 error = ESPIPE;
1743 goto out;
1744 }
1745
1746 offset = SCARG(uap, offset);
1747
1748 /*
1749 * XXX This works because no file systems actually
1750 * XXX take any action on the seek operation.
1751 */
1752 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1753 goto out;
1754
1755 /* dofilewrite() will unuse the descriptor for us */
1756 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1757 &offset, 0, retval));
1758
1759 out:
1760 FILE_UNUSE(fp, p);
1761 return (error);
1762 }
1763
1764 /*
1765 * Positional gather write system call.
1766 */
1767 int
1768 sys_pwritev(p, v, retval)
1769 struct proc *p;
1770 void *v;
1771 register_t *retval;
1772 {
1773 struct sys_pwritev_args /* {
1774 syscallarg(int) fd;
1775 syscallarg(const struct iovec *) iovp;
1776 syscallarg(int) iovcnt;
1777 syscallarg(off_t) offset;
1778 } */ *uap = v;
1779 struct filedesc *fdp = p->p_fd;
1780 struct file *fp;
1781 struct vnode *vp;
1782 off_t offset;
1783 int error, fd = SCARG(uap, fd);
1784
1785 if ((u_int)fd >= fdp->fd_nfiles ||
1786 (fp = fdp->fd_ofiles[fd]) == NULL ||
1787 (fp->f_iflags & FIF_WANTCLOSE) != 0 ||
1788 (fp->f_flag & FWRITE) == 0)
1789 return (EBADF);
1790
1791 FILE_USE(fp);
1792
1793 vp = (struct vnode *)fp->f_data;
1794 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
1795 error = ESPIPE;
1796 goto out;
1797 }
1798
1799 offset = SCARG(uap, offset);
1800
1801 /*
1802 * XXX This works because no file systems actually
1803 * XXX take any action on the seek operation.
1804 */
1805 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1806 goto out;
1807
1808 /* dofilewritev() will unuse the descriptor for us */
1809 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1810 &offset, 0, retval));
1811
1812 out:
1813 FILE_UNUSE(fp, p);
1814 return (error);
1815 }
1816
1817 /*
1818 * Check access permissions.
1819 */
1820 int
1821 sys_access(p, v, retval)
1822 struct proc *p;
1823 void *v;
1824 register_t *retval;
1825 {
1826 register struct sys_access_args /* {
1827 syscallarg(const char *) path;
1828 syscallarg(int) flags;
1829 } */ *uap = v;
1830 register struct ucred *cred = p->p_ucred;
1831 register struct vnode *vp;
1832 int error, flags, t_gid, t_uid;
1833 struct nameidata nd;
1834
1835 t_uid = cred->cr_uid;
1836 t_gid = cred->cr_gid;
1837 cred->cr_uid = p->p_cred->p_ruid;
1838 cred->cr_gid = p->p_cred->p_rgid;
1839 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1840 SCARG(uap, path), p);
1841 if ((error = namei(&nd)) != 0)
1842 goto out1;
1843 vp = nd.ni_vp;
1844
1845 /* Flags == 0 means only check for existence. */
1846 if (SCARG(uap, flags)) {
1847 flags = 0;
1848 if (SCARG(uap, flags) & R_OK)
1849 flags |= VREAD;
1850 if (SCARG(uap, flags) & W_OK)
1851 flags |= VWRITE;
1852 if (SCARG(uap, flags) & X_OK)
1853 flags |= VEXEC;
1854
1855 error = VOP_ACCESS(vp, flags, cred, p);
1856 if (!error && (flags & VWRITE))
1857 error = vn_writechk(vp);
1858 }
1859 vput(vp);
1860 out1:
1861 cred->cr_uid = t_uid;
1862 cred->cr_gid = t_gid;
1863 return (error);
1864 }
1865
1866 /*
1867 * Get file status; this version follows links.
1868 */
1869 /* ARGSUSED */
1870 int
1871 sys___stat13(p, v, retval)
1872 struct proc *p;
1873 void *v;
1874 register_t *retval;
1875 {
1876 register struct sys___stat13_args /* {
1877 syscallarg(const char *) path;
1878 syscallarg(struct stat *) ub;
1879 } */ *uap = v;
1880 struct stat sb;
1881 int error;
1882 struct nameidata nd;
1883
1884 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1885 SCARG(uap, path), p);
1886 if ((error = namei(&nd)) != 0)
1887 return (error);
1888 error = vn_stat(nd.ni_vp, &sb, p);
1889 vput(nd.ni_vp);
1890 if (error)
1891 return (error);
1892 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1893 return (error);
1894 }
1895
1896 /*
1897 * Get file status; this version does not follow links.
1898 */
1899 /* ARGSUSED */
1900 int
1901 sys___lstat13(p, v, retval)
1902 struct proc *p;
1903 void *v;
1904 register_t *retval;
1905 {
1906 register struct sys___lstat13_args /* {
1907 syscallarg(const char *) path;
1908 syscallarg(struct stat *) ub;
1909 } */ *uap = v;
1910 struct stat sb;
1911 int error;
1912 struct nameidata nd;
1913
1914 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1915 SCARG(uap, path), p);
1916 if ((error = namei(&nd)) != 0)
1917 return (error);
1918 error = vn_stat(nd.ni_vp, &sb, p);
1919 vput(nd.ni_vp);
1920 if (error)
1921 return (error);
1922 error = copyout(&sb, SCARG(uap, ub), sizeof(sb));
1923 return (error);
1924 }
1925
1926 /*
1927 * Get configurable pathname variables.
1928 */
1929 /* ARGSUSED */
1930 int
1931 sys_pathconf(p, v, retval)
1932 struct proc *p;
1933 void *v;
1934 register_t *retval;
1935 {
1936 register struct sys_pathconf_args /* {
1937 syscallarg(const char *) path;
1938 syscallarg(int) name;
1939 } */ *uap = v;
1940 int error;
1941 struct nameidata nd;
1942
1943 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1944 SCARG(uap, path), p);
1945 if ((error = namei(&nd)) != 0)
1946 return (error);
1947 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1948 vput(nd.ni_vp);
1949 return (error);
1950 }
1951
1952 /*
1953 * Return target name of a symbolic link.
1954 */
1955 /* ARGSUSED */
1956 int
1957 sys_readlink(p, v, retval)
1958 struct proc *p;
1959 void *v;
1960 register_t *retval;
1961 {
1962 register struct sys_readlink_args /* {
1963 syscallarg(const char *) path;
1964 syscallarg(char *) buf;
1965 syscallarg(size_t) count;
1966 } */ *uap = v;
1967 register struct vnode *vp;
1968 struct iovec aiov;
1969 struct uio auio;
1970 int error;
1971 struct nameidata nd;
1972
1973 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1974 SCARG(uap, path), p);
1975 if ((error = namei(&nd)) != 0)
1976 return (error);
1977 vp = nd.ni_vp;
1978 if (vp->v_type != VLNK)
1979 error = EINVAL;
1980 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
1981 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
1982 aiov.iov_base = SCARG(uap, buf);
1983 aiov.iov_len = SCARG(uap, count);
1984 auio.uio_iov = &aiov;
1985 auio.uio_iovcnt = 1;
1986 auio.uio_offset = 0;
1987 auio.uio_rw = UIO_READ;
1988 auio.uio_segflg = UIO_USERSPACE;
1989 auio.uio_procp = p;
1990 auio.uio_resid = SCARG(uap, count);
1991 error = VOP_READLINK(vp, &auio, p->p_ucred);
1992 }
1993 vput(vp);
1994 *retval = SCARG(uap, count) - auio.uio_resid;
1995 return (error);
1996 }
1997
1998 /*
1999 * Change flags of a file given a path name.
2000 */
2001 /* ARGSUSED */
2002 int
2003 sys_chflags(p, v, retval)
2004 struct proc *p;
2005 void *v;
2006 register_t *retval;
2007 {
2008 register struct sys_chflags_args /* {
2009 syscallarg(const char *) path;
2010 syscallarg(u_long) flags;
2011 } */ *uap = v;
2012 register struct vnode *vp;
2013 struct vattr vattr;
2014 int error;
2015 struct nameidata nd;
2016
2017 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2018 if ((error = namei(&nd)) != 0)
2019 return (error);
2020 vp = nd.ni_vp;
2021 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2022 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2023 VATTR_NULL(&vattr);
2024 vattr.va_flags = SCARG(uap, flags);
2025 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2026 vput(vp);
2027 return (error);
2028 }
2029
2030 /*
2031 * Change flags of a file given a file descriptor.
2032 */
2033 /* ARGSUSED */
2034 int
2035 sys_fchflags(p, v, retval)
2036 struct proc *p;
2037 void *v;
2038 register_t *retval;
2039 {
2040 register struct sys_fchflags_args /* {
2041 syscallarg(int) fd;
2042 syscallarg(u_long) flags;
2043 } */ *uap = v;
2044 struct vattr vattr;
2045 struct vnode *vp;
2046 struct file *fp;
2047 int error;
2048
2049 /* getvnode() will use the descriptor for us */
2050 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2051 return (error);
2052 vp = (struct vnode *)fp->f_data;
2053 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2054 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2055 VATTR_NULL(&vattr);
2056 vattr.va_flags = SCARG(uap, flags);
2057 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2058 VOP_UNLOCK(vp, 0);
2059 FILE_UNUSE(fp, p);
2060 return (error);
2061 }
2062
2063 /*
2064 * Change mode of a file given path name; this version follows links.
2065 */
2066 /* ARGSUSED */
2067 int
2068 sys_chmod(p, v, retval)
2069 struct proc *p;
2070 void *v;
2071 register_t *retval;
2072 {
2073 register struct sys_chmod_args /* {
2074 syscallarg(const char *) path;
2075 syscallarg(int) mode;
2076 } */ *uap = v;
2077 int error;
2078 struct nameidata nd;
2079
2080 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2081 if ((error = namei(&nd)) != 0)
2082 return (error);
2083
2084 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2085
2086 vrele(nd.ni_vp);
2087 return (error);
2088 }
2089
2090 /*
2091 * Change mode of a file given a file descriptor.
2092 */
2093 /* ARGSUSED */
2094 int
2095 sys_fchmod(p, v, retval)
2096 struct proc *p;
2097 void *v;
2098 register_t *retval;
2099 {
2100 register struct sys_fchmod_args /* {
2101 syscallarg(int) fd;
2102 syscallarg(int) mode;
2103 } */ *uap = v;
2104 struct file *fp;
2105 int error;
2106
2107 /* getvnode() will use the descriptor for us */
2108 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2109 return (error);
2110
2111 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p);
2112 FILE_UNUSE(fp, p);
2113 return (error);
2114 }
2115
2116 /*
2117 * Change mode of a file given path name; this version does not follow links.
2118 */
2119 /* ARGSUSED */
2120 int
2121 sys_lchmod(p, v, retval)
2122 struct proc *p;
2123 void *v;
2124 register_t *retval;
2125 {
2126 register struct sys_lchmod_args /* {
2127 syscallarg(const char *) path;
2128 syscallarg(int) mode;
2129 } */ *uap = v;
2130 int error;
2131 struct nameidata nd;
2132
2133 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2134 if ((error = namei(&nd)) != 0)
2135 return (error);
2136
2137 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
2138
2139 vrele(nd.ni_vp);
2140 return (error);
2141 }
2142
2143 /*
2144 * Common routine to set mode given a vnode.
2145 */
2146 static int
2147 change_mode(vp, mode, p)
2148 struct vnode *vp;
2149 int mode;
2150 struct proc *p;
2151 {
2152 struct vattr vattr;
2153 int error;
2154
2155 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2156 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2157 VATTR_NULL(&vattr);
2158 vattr.va_mode = mode & ALLPERMS;
2159 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2160 VOP_UNLOCK(vp, 0);
2161 return (error);
2162 }
2163
2164 /*
2165 * Set ownership given a path name; this version follows links.
2166 */
2167 /* ARGSUSED */
2168 int
2169 sys_chown(p, v, retval)
2170 struct proc *p;
2171 void *v;
2172 register_t *retval;
2173 {
2174 register struct sys_chown_args /* {
2175 syscallarg(const char *) path;
2176 syscallarg(uid_t) uid;
2177 syscallarg(gid_t) gid;
2178 } */ *uap = v;
2179 int error;
2180 struct nameidata nd;
2181
2182 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2183 if ((error = namei(&nd)) != 0)
2184 return (error);
2185
2186 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2187
2188 vrele(nd.ni_vp);
2189 return (error);
2190 }
2191
2192 /*
2193 * Set ownership given a path name; this version follows links.
2194 * Provides POSIX semantics.
2195 */
2196 /* ARGSUSED */
2197 int
2198 sys___posix_chown(p, v, retval)
2199 struct proc *p;
2200 void *v;
2201 register_t *retval;
2202 {
2203 register struct sys_chown_args /* {
2204 syscallarg(const char *) path;
2205 syscallarg(uid_t) uid;
2206 syscallarg(gid_t) gid;
2207 } */ *uap = v;
2208 int error;
2209 struct nameidata nd;
2210
2211 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2212 if ((error = namei(&nd)) != 0)
2213 return (error);
2214
2215 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2216
2217 vrele(nd.ni_vp);
2218 return (error);
2219 }
2220
2221 /*
2222 * Set ownership given a file descriptor.
2223 */
2224 /* ARGSUSED */
2225 int
2226 sys_fchown(p, v, retval)
2227 struct proc *p;
2228 void *v;
2229 register_t *retval;
2230 {
2231 register struct sys_fchown_args /* {
2232 syscallarg(int) fd;
2233 syscallarg(uid_t) uid;
2234 syscallarg(gid_t) gid;
2235 } */ *uap = v;
2236 int error;
2237 struct file *fp;
2238
2239 /* getvnode() will use the descriptor for us */
2240 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2241 return (error);
2242
2243 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2244 SCARG(uap, gid), p, 0);
2245 FILE_UNUSE(fp, p);
2246 return (error);
2247 }
2248
2249 /*
2250 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2251 */
2252 /* ARGSUSED */
2253 int
2254 sys___posix_fchown(p, v, retval)
2255 struct proc *p;
2256 void *v;
2257 register_t *retval;
2258 {
2259 register struct sys_fchown_args /* {
2260 syscallarg(int) fd;
2261 syscallarg(uid_t) uid;
2262 syscallarg(gid_t) gid;
2263 } */ *uap = v;
2264 int error;
2265 struct file *fp;
2266
2267 /* getvnode() will use the descriptor for us */
2268 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2269 return (error);
2270
2271 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2272 SCARG(uap, gid), p, 1);
2273 FILE_UNUSE(fp, p);
2274 return (error);
2275 }
2276
2277 /*
2278 * Set ownership given a path name; this version does not follow links.
2279 */
2280 /* ARGSUSED */
2281 int
2282 sys_lchown(p, v, retval)
2283 struct proc *p;
2284 void *v;
2285 register_t *retval;
2286 {
2287 register struct sys_lchown_args /* {
2288 syscallarg(const char *) path;
2289 syscallarg(uid_t) uid;
2290 syscallarg(gid_t) gid;
2291 } */ *uap = v;
2292 int error;
2293 struct nameidata nd;
2294
2295 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2296 if ((error = namei(&nd)) != 0)
2297 return (error);
2298
2299 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
2300
2301 vrele(nd.ni_vp);
2302 return (error);
2303 }
2304
2305 /*
2306 * Set ownership given a path name; this version does not follow links.
2307 * Provides POSIX/XPG semantics.
2308 */
2309 /* ARGSUSED */
2310 int
2311 sys___posix_lchown(p, v, retval)
2312 struct proc *p;
2313 void *v;
2314 register_t *retval;
2315 {
2316 register struct sys_lchown_args /* {
2317 syscallarg(const char *) path;
2318 syscallarg(uid_t) uid;
2319 syscallarg(gid_t) gid;
2320 } */ *uap = v;
2321 int error;
2322 struct nameidata nd;
2323
2324 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2325 if ((error = namei(&nd)) != 0)
2326 return (error);
2327
2328 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
2329
2330 vrele(nd.ni_vp);
2331 return (error);
2332 }
2333
2334 /*
2335 * Common routine to set ownership given a vnode.
2336 */
2337 static int
2338 change_owner(vp, uid, gid, p, posix_semantics)
2339 register struct vnode *vp;
2340 uid_t uid;
2341 gid_t gid;
2342 struct proc *p;
2343 int posix_semantics;
2344 {
2345 struct vattr vattr;
2346 mode_t newmode;
2347 int error;
2348
2349 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2350 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2351 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2352 goto out;
2353
2354 #define CHANGED(x) ((x) != -1)
2355 newmode = vattr.va_mode;
2356 if (posix_semantics) {
2357 /*
2358 * POSIX/XPG semantics: if the caller is not the super-user,
2359 * clear set-user-id and set-group-id bits. Both POSIX and
2360 * the XPG consider the behaviour for calls by the super-user
2361 * implementation-defined; we leave the set-user-id and set-
2362 * group-id settings intact in that case.
2363 */
2364 if (suser(p->p_ucred, NULL) != 0)
2365 newmode &= ~(S_ISUID | S_ISGID);
2366 } else {
2367 /*
2368 * NetBSD semantics: when changing owner and/or group,
2369 * clear the respective bit(s).
2370 */
2371 if (CHANGED(uid))
2372 newmode &= ~S_ISUID;
2373 if (CHANGED(gid))
2374 newmode &= ~S_ISGID;
2375 }
2376 /* Update va_mode iff altered. */
2377 if (vattr.va_mode == newmode)
2378 newmode = VNOVAL;
2379
2380 VATTR_NULL(&vattr);
2381 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
2382 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
2383 vattr.va_mode = newmode;
2384 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2385 #undef CHANGED
2386
2387 out:
2388 VOP_UNLOCK(vp, 0);
2389 return (error);
2390 }
2391
2392 /*
2393 * Set the access and modification times given a path name; this
2394 * version follows links.
2395 */
2396 /* ARGSUSED */
2397 int
2398 sys_utimes(p, v, retval)
2399 struct proc *p;
2400 void *v;
2401 register_t *retval;
2402 {
2403 register struct sys_utimes_args /* {
2404 syscallarg(const char *) path;
2405 syscallarg(const struct timeval *) tptr;
2406 } */ *uap = v;
2407 int error;
2408 struct nameidata nd;
2409
2410 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2411 if ((error = namei(&nd)) != 0)
2412 return (error);
2413
2414 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2415
2416 vrele(nd.ni_vp);
2417 return (error);
2418 }
2419
2420 /*
2421 * Set the access and modification times given a file descriptor.
2422 */
2423 /* ARGSUSED */
2424 int
2425 sys_futimes(p, v, retval)
2426 struct proc *p;
2427 void *v;
2428 register_t *retval;
2429 {
2430 register struct sys_futimes_args /* {
2431 syscallarg(int) fd;
2432 syscallarg(const struct timeval *) tptr;
2433 } */ *uap = v;
2434 int error;
2435 struct file *fp;
2436
2437 /* getvnode() will use the descriptor for us */
2438 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2439 return (error);
2440
2441 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), p);
2442 FILE_UNUSE(fp, p);
2443 return (error);
2444 }
2445
2446 /*
2447 * Set the access and modification times given a path name; this
2448 * version does not follow links.
2449 */
2450 /* ARGSUSED */
2451 int
2452 sys_lutimes(p, v, retval)
2453 struct proc *p;
2454 void *v;
2455 register_t *retval;
2456 {
2457 register struct sys_lutimes_args /* {
2458 syscallarg(const char *) path;
2459 syscallarg(const struct timeval *) tptr;
2460 } */ *uap = v;
2461 int error;
2462 struct nameidata nd;
2463
2464 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2465 if ((error = namei(&nd)) != 0)
2466 return (error);
2467
2468 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2469
2470 vrele(nd.ni_vp);
2471 return (error);
2472 }
2473
2474 /*
2475 * Common routine to set access and modification times given a vnode.
2476 */
2477 static int
2478 change_utimes(vp, tptr, p)
2479 struct vnode *vp;
2480 const struct timeval *tptr;
2481 struct proc *p;
2482 {
2483 struct timeval tv[2];
2484 struct vattr vattr;
2485 int error;
2486
2487 VATTR_NULL(&vattr);
2488 if (tptr == NULL) {
2489 microtime(&tv[0]);
2490 tv[1] = tv[0];
2491 vattr.va_vaflags |= VA_UTIMES_NULL;
2492 } else {
2493 error = copyin(tptr, tv, sizeof(tv));
2494 if (error)
2495 return (error);
2496 }
2497 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2498 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2499 vattr.va_atime.tv_sec = tv[0].tv_sec;
2500 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2501 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2502 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2503 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2504 VOP_UNLOCK(vp, 0);
2505 return (error);
2506 }
2507
2508 /*
2509 * Truncate a file given its path name.
2510 */
2511 /* ARGSUSED */
2512 int
2513 sys_truncate(p, v, retval)
2514 struct proc *p;
2515 void *v;
2516 register_t *retval;
2517 {
2518 register struct sys_truncate_args /* {
2519 syscallarg(const char *) path;
2520 syscallarg(int) pad;
2521 syscallarg(off_t) length;
2522 } */ *uap = v;
2523 register struct vnode *vp;
2524 struct vattr vattr;
2525 int error;
2526 struct nameidata nd;
2527
2528 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2529 if ((error = namei(&nd)) != 0)
2530 return (error);
2531 vp = nd.ni_vp;
2532 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2533 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2534 if (vp->v_type == VDIR)
2535 error = EISDIR;
2536 else if ((error = vn_writechk(vp)) == 0 &&
2537 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2538 VATTR_NULL(&vattr);
2539 vattr.va_size = SCARG(uap, length);
2540 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2541 }
2542 vput(vp);
2543 return (error);
2544 }
2545
2546 /*
2547 * Truncate a file given a file descriptor.
2548 */
2549 /* ARGSUSED */
2550 int
2551 sys_ftruncate(p, v, retval)
2552 struct proc *p;
2553 void *v;
2554 register_t *retval;
2555 {
2556 register struct sys_ftruncate_args /* {
2557 syscallarg(int) fd;
2558 syscallarg(int) pad;
2559 syscallarg(off_t) length;
2560 } */ *uap = v;
2561 struct vattr vattr;
2562 struct vnode *vp;
2563 struct file *fp;
2564 int error;
2565
2566 /* getvnode() will use the descriptor for us */
2567 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2568 return (error);
2569 if ((fp->f_flag & FWRITE) == 0) {
2570 error = EINVAL;
2571 goto out;
2572 }
2573 vp = (struct vnode *)fp->f_data;
2574 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2575 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2576 if (vp->v_type == VDIR)
2577 error = EISDIR;
2578 else if ((error = vn_writechk(vp)) == 0) {
2579 VATTR_NULL(&vattr);
2580 vattr.va_size = SCARG(uap, length);
2581 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2582 }
2583 VOP_UNLOCK(vp, 0);
2584 out:
2585 FILE_UNUSE(fp, p);
2586 return (error);
2587 }
2588
2589 /*
2590 * Sync an open file.
2591 */
2592 /* ARGSUSED */
2593 int
2594 sys_fsync(p, v, retval)
2595 struct proc *p;
2596 void *v;
2597 register_t *retval;
2598 {
2599 struct sys_fsync_args /* {
2600 syscallarg(int) fd;
2601 } */ *uap = v;
2602 register struct vnode *vp;
2603 struct file *fp;
2604 int error;
2605
2606 /* getvnode() will use the descriptor for us */
2607 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2608 return (error);
2609 vp = (struct vnode *)fp->f_data;
2610 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2611 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
2612 VOP_UNLOCK(vp, 0);
2613 FILE_UNUSE(fp, p);
2614 return (error);
2615 }
2616
2617 /*
2618 * Sync the data of an open file.
2619 */
2620 /* ARGSUSED */
2621 int
2622 sys_fdatasync(p, v, retval)
2623 struct proc *p;
2624 void *v;
2625 register_t *retval;
2626 {
2627 struct sys_fdatasync_args /* {
2628 syscallarg(int) fd;
2629 } */ *uap = v;
2630 struct vnode *vp;
2631 struct file *fp;
2632 int error;
2633
2634 /* getvnode() will use the descriptor for us */
2635 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2636 return (error);
2637 vp = (struct vnode *)fp->f_data;
2638 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2639 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
2640 VOP_UNLOCK(vp, 0);
2641 FILE_UNUSE(fp, p);
2642 return (error);
2643 }
2644
2645 /*
2646 * Rename files, (standard) BSD semantics frontend.
2647 */
2648 /* ARGSUSED */
2649 int
2650 sys_rename(p, v, retval)
2651 struct proc *p;
2652 void *v;
2653 register_t *retval;
2654 {
2655 register struct sys_rename_args /* {
2656 syscallarg(const char *) from;
2657 syscallarg(const char *) to;
2658 } */ *uap = v;
2659
2660 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2661 }
2662
2663 /*
2664 * Rename files, POSIX semantics frontend.
2665 */
2666 /* ARGSUSED */
2667 int
2668 sys___posix_rename(p, v, retval)
2669 struct proc *p;
2670 void *v;
2671 register_t *retval;
2672 {
2673 register struct sys___posix_rename_args /* {
2674 syscallarg(const char *) from;
2675 syscallarg(const char *) to;
2676 } */ *uap = v;
2677
2678 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2679 }
2680
2681 /*
2682 * Rename files. Source and destination must either both be directories,
2683 * or both not be directories. If target is a directory, it must be empty.
2684 * If `from' and `to' refer to the same object, the value of the `retain'
2685 * argument is used to determine whether `from' will be
2686 *
2687 * (retain == 0) deleted unless `from' and `to' refer to the same
2688 * object in the file system's name space (BSD).
2689 * (retain == 1) always retained (POSIX).
2690 */
2691 static int
2692 rename_files(from, to, p, retain)
2693 const char *from, *to;
2694 struct proc *p;
2695 int retain;
2696 {
2697 register struct vnode *tvp, *fvp, *tdvp;
2698 struct nameidata fromnd, tond;
2699 int error;
2700
2701 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2702 from, p);
2703 if ((error = namei(&fromnd)) != 0)
2704 return (error);
2705 fvp = fromnd.ni_vp;
2706 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2707 UIO_USERSPACE, to, p);
2708 if ((error = namei(&tond)) != 0) {
2709 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2710 vrele(fromnd.ni_dvp);
2711 vrele(fvp);
2712 goto out1;
2713 }
2714 tdvp = tond.ni_dvp;
2715 tvp = tond.ni_vp;
2716
2717 if (tvp != NULL) {
2718 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2719 error = ENOTDIR;
2720 goto out;
2721 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2722 error = EISDIR;
2723 goto out;
2724 }
2725 }
2726
2727 if (fvp == tdvp)
2728 error = EINVAL;
2729
2730 /*
2731 * Source and destination refer to the same object.
2732 */
2733 if (fvp == tvp) {
2734 if (retain)
2735 error = -1;
2736 else if (fromnd.ni_dvp == tdvp &&
2737 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2738 !memcmp(fromnd.ni_cnd.cn_nameptr,
2739 tond.ni_cnd.cn_nameptr,
2740 fromnd.ni_cnd.cn_namelen))
2741 error = -1;
2742 }
2743
2744 out:
2745 if (!error) {
2746 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2747 if (fromnd.ni_dvp != tdvp)
2748 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2749 if (tvp) {
2750 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2751 }
2752 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2753 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2754 } else {
2755 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2756 if (tdvp == tvp)
2757 vrele(tdvp);
2758 else
2759 vput(tdvp);
2760 if (tvp)
2761 vput(tvp);
2762 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2763 vrele(fromnd.ni_dvp);
2764 vrele(fvp);
2765 }
2766 vrele(tond.ni_startdir);
2767 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2768 out1:
2769 if (fromnd.ni_startdir)
2770 vrele(fromnd.ni_startdir);
2771 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2772 return (error == -1 ? 0 : error);
2773 }
2774
2775 /*
2776 * Make a directory file.
2777 */
2778 /* ARGSUSED */
2779 int
2780 sys_mkdir(p, v, retval)
2781 struct proc *p;
2782 void *v;
2783 register_t *retval;
2784 {
2785 register struct sys_mkdir_args /* {
2786 syscallarg(const char *) path;
2787 syscallarg(int) mode;
2788 } */ *uap = v;
2789 register struct vnode *vp;
2790 struct vattr vattr;
2791 int error;
2792 struct nameidata nd;
2793
2794 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2795 if ((error = namei(&nd)) != 0)
2796 return (error);
2797 vp = nd.ni_vp;
2798 if (vp != NULL) {
2799 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2800 if (nd.ni_dvp == vp)
2801 vrele(nd.ni_dvp);
2802 else
2803 vput(nd.ni_dvp);
2804 vrele(vp);
2805 return (EEXIST);
2806 }
2807 VATTR_NULL(&vattr);
2808 vattr.va_type = VDIR;
2809 vattr.va_mode =
2810 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
2811 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2812 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2813 if (!error)
2814 vput(nd.ni_vp);
2815 return (error);
2816 }
2817
2818 /*
2819 * Remove a directory file.
2820 */
2821 /* ARGSUSED */
2822 int
2823 sys_rmdir(p, v, retval)
2824 struct proc *p;
2825 void *v;
2826 register_t *retval;
2827 {
2828 struct sys_rmdir_args /* {
2829 syscallarg(const char *) path;
2830 } */ *uap = v;
2831 register struct vnode *vp;
2832 int error;
2833 struct nameidata nd;
2834
2835 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2836 SCARG(uap, path), p);
2837 if ((error = namei(&nd)) != 0)
2838 return (error);
2839 vp = nd.ni_vp;
2840 if (vp->v_type != VDIR) {
2841 error = ENOTDIR;
2842 goto out;
2843 }
2844 /*
2845 * No rmdir "." please.
2846 */
2847 if (nd.ni_dvp == vp) {
2848 error = EINVAL;
2849 goto out;
2850 }
2851 /*
2852 * The root of a mounted filesystem cannot be deleted.
2853 */
2854 if (vp->v_flag & VROOT)
2855 error = EBUSY;
2856 out:
2857 if (!error) {
2858 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2859 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2860 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2861 } else {
2862 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2863 if (nd.ni_dvp == vp)
2864 vrele(nd.ni_dvp);
2865 else
2866 vput(nd.ni_dvp);
2867 vput(vp);
2868 }
2869 return (error);
2870 }
2871
2872 /*
2873 * Read a block of directory entries in a file system independent format.
2874 */
2875 int
2876 sys_getdents(p, v, retval)
2877 struct proc *p;
2878 void *v;
2879 register_t *retval;
2880 {
2881 register struct sys_getdents_args /* {
2882 syscallarg(int) fd;
2883 syscallarg(char *) buf;
2884 syscallarg(size_t) count;
2885 } */ *uap = v;
2886 struct file *fp;
2887 int error, done;
2888
2889 /* getvnode() will use the descriptor for us */
2890 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2891 return (error);
2892 if ((fp->f_flag & FREAD) == 0) {
2893 error = EBADF;
2894 goto out;
2895 }
2896 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2897 SCARG(uap, count), &done, p, 0, 0);
2898 *retval = done;
2899 out:
2900 FILE_UNUSE(fp, p);
2901 return (error);
2902 }
2903
2904 /*
2905 * Set the mode mask for creation of filesystem nodes.
2906 */
2907 int
2908 sys_umask(p, v, retval)
2909 struct proc *p;
2910 void *v;
2911 register_t *retval;
2912 {
2913 struct sys_umask_args /* {
2914 syscallarg(mode_t) newmask;
2915 } */ *uap = v;
2916 struct cwdinfo *cwdi;
2917
2918 cwdi = p->p_cwdi;
2919 *retval = cwdi->cwdi_cmask;
2920 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
2921 return (0);
2922 }
2923
2924 /*
2925 * Void all references to file by ripping underlying filesystem
2926 * away from vnode.
2927 */
2928 /* ARGSUSED */
2929 int
2930 sys_revoke(p, v, retval)
2931 struct proc *p;
2932 void *v;
2933 register_t *retval;
2934 {
2935 register struct sys_revoke_args /* {
2936 syscallarg(const char *) path;
2937 } */ *uap = v;
2938 register struct vnode *vp;
2939 struct vattr vattr;
2940 int error;
2941 struct nameidata nd;
2942
2943 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2944 if ((error = namei(&nd)) != 0)
2945 return (error);
2946 vp = nd.ni_vp;
2947 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2948 goto out;
2949 if (p->p_ucred->cr_uid != vattr.va_uid &&
2950 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2951 goto out;
2952 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2953 VOP_REVOKE(vp, REVOKEALL);
2954 out:
2955 vrele(vp);
2956 return (error);
2957 }
2958
2959 /*
2960 * Convert a user file descriptor to a kernel file entry.
2961 */
2962 int
2963 getvnode(fdp, fd, fpp)
2964 struct filedesc *fdp;
2965 int fd;
2966 struct file **fpp;
2967 {
2968 struct vnode *vp;
2969 struct file *fp;
2970
2971 if ((u_int)fd >= fdp->fd_nfiles ||
2972 (fp = fdp->fd_ofiles[fd]) == NULL ||
2973 (fp->f_iflags & FIF_WANTCLOSE) != 0)
2974 return (EBADF);
2975
2976 FILE_USE(fp);
2977
2978 if (fp->f_type != DTYPE_VNODE) {
2979 FILE_UNUSE(fp, NULL);
2980 return (EINVAL);
2981 }
2982
2983 vp = (struct vnode *)fp->f_data;
2984 if (vp->v_type == VBAD) {
2985 FILE_UNUSE(fp, NULL);
2986 return (EBADF);
2987 }
2988
2989 *fpp = fp;
2990 return (0);
2991 }
2992