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