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