vfs_syscalls.c revision 1.120 1 /* $NetBSD: vfs_syscalls.c,v 1.120 1998/06/30 19:36:24 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)vfs_syscalls.c 8.42 (Berkeley) 7/31/95
41 */
42
43 #include "opt_uvm.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/namei.h>
48 #include <sys/filedesc.h>
49 #include <sys/kernel.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/proc.h>
55 #include <sys/uio.h>
56 #include <sys/malloc.h>
57 #include <sys/dirent.h>
58
59 #include <sys/syscallargs.h>
60
61 #include <vm/vm.h>
62 #include <sys/sysctl.h>
63
64 #if defined(UVM)
65 #include <uvm/uvm_extern.h>
66 #endif
67
68 static int change_dir __P((struct nameidata *, struct proc *));
69 static int change_mode __P((struct vnode *, int, struct proc *p));
70 static int change_owner __P((struct vnode *, uid_t, gid_t, struct proc *,
71 int));
72 static int change_utimes __P((struct vnode *vp, const struct timeval *,
73 struct proc *p));
74 static int rename_files __P((const char *, const char *, struct proc *, int));
75
76 void checkdirs __P((struct vnode *));
77 int dounmount __P((struct mount *, int, struct proc *));
78
79 /*
80 * Virtual File System System Calls
81 */
82
83 /*
84 * Mount a file system.
85 */
86
87 #if defined(COMPAT_09) || defined(COMPAT_43) || defined(COMPAT_44)
88 /*
89 * This table is used to maintain compatibility with 4.3BSD
90 * and NetBSD 0.9 mount syscalls. Note, the order is important!
91 */
92 const char *mountcompatnames[] = {
93 NULL, /* 0 = MOUNT_NONE */
94 MOUNT_FFS, /* 1 */
95 MOUNT_NFS, /* 2 */
96 MOUNT_MFS, /* 3 */
97 MOUNT_MSDOS, /* 4 */
98 MOUNT_LFS, /* 5 */
99 NULL, /* 6 = MOUNT_LOFS */
100 MOUNT_FDESC, /* 7 */
101 MOUNT_PORTAL, /* 8 */
102 MOUNT_NULL, /* 9 */
103 MOUNT_UMAP, /* 10 */
104 MOUNT_KERNFS, /* 11 */
105 MOUNT_PROCFS, /* 12 */
106 MOUNT_AFS, /* 13 */
107 MOUNT_CD9660, /* 14 = MOUNT_ISOFS */
108 MOUNT_UNION, /* 15 */
109 MOUNT_ADOSFS, /* 16 */
110 MOUNT_EXT2FS, /* 17 */
111 };
112 const int nmountcompatnames = sizeof(mountcompatnames) /
113 sizeof(mountcompatnames[0]);
114 #endif /* COMPAT_09 || COMPAT_43 */
115
116 /* ARGSUSED */
117 int
118 sys_mount(p, v, retval)
119 struct proc *p;
120 void *v;
121 register_t *retval;
122 {
123 register struct sys_mount_args /* {
124 syscallarg(const char *) type;
125 syscallarg(const char *) path;
126 syscallarg(int) flags;
127 syscallarg(void *) data;
128 } */ *uap = v;
129 struct vnode *vp;
130 struct mount *mp;
131 int error, flag = 0;
132 char fstypename[MFSNAMELEN];
133 struct vattr va;
134 struct nameidata nd;
135 struct vfsops *vfs;
136
137 /*
138 * Get vnode to be covered
139 */
140 NDINIT(&nd, LOOKUP, FOLLOW , UIO_USERSPACE,
141 SCARG(uap, path), p);
142 if ((error = namei(&nd)) != 0)
143 return (error);
144 vp = nd.ni_vp;
145 /*
146 * A lookup in VFS_MOUNT might result in an attempt to
147 * lock this vnode again, so make the lock resursive.
148 */
149 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY | LK_CANRECURSE);
150 if (SCARG(uap, flags) & MNT_UPDATE) {
151 if ((vp->v_flag & VROOT) == 0) {
152 vput(vp);
153 return (EINVAL);
154 }
155 mp = vp->v_mount;
156 flag = mp->mnt_flag;
157 vfs = mp->mnt_op;
158 /*
159 * We only allow the filesystem to be reloaded if it
160 * is currently mounted read-only.
161 */
162 if ((SCARG(uap, flags) & MNT_RELOAD) &&
163 ((mp->mnt_flag & MNT_RDONLY) == 0)) {
164 vput(vp);
165 return (EOPNOTSUPP); /* Needs translation */
166 }
167 mp->mnt_flag |=
168 SCARG(uap, flags) & (MNT_RELOAD | MNT_FORCE | MNT_UPDATE);
169 /*
170 * Only root, or the user that did the original mount is
171 * permitted to update it.
172 */
173 if (mp->mnt_stat.f_owner != p->p_ucred->cr_uid &&
174 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
175 vput(vp);
176 return (error);
177 }
178 /*
179 * Do not allow NFS export by non-root users. Silently
180 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
181 */
182 if (p->p_ucred->cr_uid != 0) {
183 if (SCARG(uap, flags) & MNT_EXPORTED) {
184 vput(vp);
185 return (EPERM);
186 }
187 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
188 }
189 if (vfs_busy(mp, LK_NOWAIT, 0)) {
190 vput(vp);
191 return (EPERM);
192 }
193 VOP_UNLOCK(vp, 0);
194 goto update;
195 }
196 /*
197 * If the user is not root, ensure that they own the directory
198 * onto which we are attempting to mount.
199 */
200 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0 ||
201 (va.va_uid != p->p_ucred->cr_uid &&
202 (error = suser(p->p_ucred, &p->p_acflag)) != 0)) {
203 vput(vp);
204 return (error);
205 }
206 /*
207 * Do not allow NFS export by non-root users. Silently
208 * enforce MNT_NOSUID and MNT_NODEV for non-root users.
209 */
210 if (p->p_ucred->cr_uid != 0) {
211 if (SCARG(uap, flags) & MNT_EXPORTED) {
212 vput(vp);
213 return (EPERM);
214 }
215 SCARG(uap, flags) |= MNT_NOSUID | MNT_NODEV;
216 }
217 if ((error = vinvalbuf(vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
218 return (error);
219 if (vp->v_type != VDIR) {
220 vput(vp);
221 return (ENOTDIR);
222 }
223 error = copyinstr(SCARG(uap, type), fstypename, MFSNAMELEN, NULL);
224 if (error) {
225 #if defined(COMPAT_09) || defined(COMPAT_43)
226 /*
227 * Historically filesystem types were identified by number.
228 * If we get an integer for the filesystem type instead of a
229 * string, we check to see if it matches one of the historic
230 * filesystem types.
231 */
232 u_long fsindex = (u_long)SCARG(uap, type);
233 if (fsindex >= nmountcompatnames ||
234 mountcompatnames[fsindex] == NULL) {
235 vput(vp);
236 return (ENODEV);
237 }
238 strncpy(fstypename, mountcompatnames[fsindex], MFSNAMELEN);
239 #else
240 vput(vp);
241 return (error);
242 #endif
243 }
244 #ifdef COMPAT_10
245 /* Accept `ufs' as an alias for `ffs'. */
246 if (!strncmp(fstypename, "ufs", MFSNAMELEN))
247 strncpy(fstypename, "ffs", MFSNAMELEN);
248 #endif
249 if ((vfs = vfs_getopsbyname(fstypename)) == NULL) {
250 vput(vp);
251 return (ENODEV);
252 }
253 if (vp->v_mountedhere != NULL) {
254 vput(vp);
255 return (EBUSY);
256 }
257
258 /*
259 * Allocate and initialize the file system.
260 */
261 mp = (struct mount *)malloc((u_long)sizeof(struct mount),
262 M_MOUNT, M_WAITOK);
263 bzero((char *)mp, (u_long)sizeof(struct mount));
264 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0);
265 (void)vfs_busy(mp, LK_NOWAIT, 0);
266 mp->mnt_op = vfs;
267 vfs->vfs_refcount++;
268 vp->v_mountedhere = mp;
269 mp->mnt_vnodecovered = vp;
270 mp->mnt_stat.f_owner = p->p_ucred->cr_uid;
271 update:
272 /*
273 * Set the mount level flags.
274 */
275 if (SCARG(uap, flags) & MNT_RDONLY)
276 mp->mnt_flag |= MNT_RDONLY;
277 else if (mp->mnt_flag & MNT_RDONLY)
278 mp->mnt_flag |= MNT_WANTRDWR;
279 mp->mnt_flag &=~ (MNT_NOSUID | MNT_NOEXEC | MNT_NODEV |
280 MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC | MNT_NOCOREDUMP |
281 MNT_NOATIME | MNT_SYMPERM);
282 mp->mnt_flag |= SCARG(uap, flags) & (MNT_NOSUID | MNT_NOEXEC |
283 MNT_NODEV | MNT_SYNCHRONOUS | MNT_UNION | MNT_ASYNC |
284 MNT_NOCOREDUMP | MNT_NOATIME | MNT_SYMPERM);
285 /*
286 * Mount the filesystem.
287 */
288 error = VFS_MOUNT(mp, SCARG(uap, path), SCARG(uap, data), &nd, p);
289 if (mp->mnt_flag & MNT_UPDATE) {
290 vrele(vp);
291 if (mp->mnt_flag & MNT_WANTRDWR)
292 mp->mnt_flag &= ~MNT_RDONLY;
293 mp->mnt_flag &=~
294 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_WANTRDWR);
295 if (error)
296 mp->mnt_flag = flag;
297 vfs_unbusy(mp);
298 return (error);
299 }
300 /*
301 * Put the new filesystem on the mount list after root.
302 */
303 cache_purge(vp);
304 if (!error) {
305 simple_lock(&mountlist_slock);
306 CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
307 simple_unlock(&mountlist_slock);
308 checkdirs(vp);
309 VOP_UNLOCK(vp, 0);
310 vfs_unbusy(mp);
311 (void) VFS_STATFS(mp, &mp->mnt_stat, p);
312 if ((error = VFS_START(mp, 0, p)))
313 vrele(vp);
314 } else {
315 mp->mnt_vnodecovered->v_mountedhere = (struct mount *)0;
316 vfs->vfs_refcount--;
317 vfs_unbusy(mp);
318 free((caddr_t)mp, M_MOUNT);
319 vput(vp);
320 }
321 return (error);
322 }
323
324 /*
325 * Scan all active processes to see if any of them have a current
326 * or root directory onto which the new filesystem has just been
327 * mounted. If so, replace them with the new mount point.
328 */
329 void
330 checkdirs(olddp)
331 struct vnode *olddp;
332 {
333 struct filedesc *fdp;
334 struct vnode *newdp;
335 struct proc *p;
336
337 if (olddp->v_usecount == 1)
338 return;
339 if (VFS_ROOT(olddp->v_mountedhere, &newdp))
340 panic("mount: lost mount");
341 for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
342 fdp = p->p_fd;
343 if (fdp->fd_cdir == olddp) {
344 vrele(fdp->fd_cdir);
345 VREF(newdp);
346 fdp->fd_cdir = newdp;
347 }
348 if (fdp->fd_rdir == olddp) {
349 vrele(fdp->fd_rdir);
350 VREF(newdp);
351 fdp->fd_rdir = newdp;
352 }
353 }
354 if (rootvnode == olddp) {
355 vrele(rootvnode);
356 VREF(newdp);
357 rootvnode = newdp;
358 }
359 vput(newdp);
360 }
361
362 /*
363 * Unmount a file system.
364 *
365 * Note: unmount takes a path to the vnode mounted on as argument,
366 * not special file (as before).
367 */
368 /* ARGSUSED */
369 int
370 sys_unmount(p, v, retval)
371 struct proc *p;
372 void *v;
373 register_t *retval;
374 {
375 register struct sys_unmount_args /* {
376 syscallarg(const char *) path;
377 syscallarg(int) flags;
378 } */ *uap = v;
379 register struct vnode *vp;
380 struct mount *mp;
381 int error;
382 struct nameidata nd;
383
384 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
385 SCARG(uap, path), p);
386 if ((error = namei(&nd)) != 0)
387 return (error);
388 vp = nd.ni_vp;
389 mp = vp->v_mount;
390
391 /*
392 * Only root, or the user that did the original mount is
393 * permitted to unmount this filesystem.
394 */
395 if ((mp->mnt_stat.f_owner != p->p_ucred->cr_uid) &&
396 (error = suser(p->p_ucred, &p->p_acflag)) != 0) {
397 vput(vp);
398 return (error);
399 }
400
401 /*
402 * Don't allow unmounting the root file system.
403 */
404 if (mp->mnt_flag & MNT_ROOTFS) {
405 vput(vp);
406 return (EINVAL);
407 }
408
409 /*
410 * Must be the root of the filesystem
411 */
412 if ((vp->v_flag & VROOT) == 0) {
413 vput(vp);
414 return (EINVAL);
415 }
416 vput(vp);
417
418 if (vfs_busy(mp, 0, 0))
419 return (EBUSY);
420
421 return (dounmount(mp, SCARG(uap, flags), p));
422 }
423
424 /*
425 * Do the actual file system unmount. File system is assumed to have been
426 * marked busy by the caller.
427 */
428 int
429 dounmount(mp, flags, p)
430 register struct mount *mp;
431 int flags;
432 struct proc *p;
433 {
434 struct vnode *coveredvp;
435 int error;
436
437 simple_lock(&mountlist_slock);
438 mp->mnt_flag |= MNT_UNMOUNT;
439 vfs_unbusy(mp);
440 lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK, &mountlist_slock);
441 if (mp->mnt_flag & MNT_EXPUBLIC)
442 vfs_setpublicfs(NULL, NULL, NULL);
443 mp->mnt_flag &=~ MNT_ASYNC;
444 #if !defined(UVM)
445 vnode_pager_umount(mp); /* release cached vnodes */
446 #endif
447 cache_purgevfs(mp); /* remove cache entries for this file sys */
448 if (((mp->mnt_flag & MNT_RDONLY) ||
449 (error = VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p)) == 0) ||
450 (flags & MNT_FORCE))
451 error = VFS_UNMOUNT(mp, flags, p);
452 simple_lock(&mountlist_slock);
453 if (error) {
454 mp->mnt_flag &= ~MNT_UNMOUNT;
455 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK | LK_REENABLE,
456 &mountlist_slock);
457 if (mp->mnt_flag & MNT_MWAIT)
458 wakeup((caddr_t)mp);
459 return (error);
460 }
461 CIRCLEQ_REMOVE(&mountlist, mp, mnt_list);
462 if ((coveredvp = mp->mnt_vnodecovered) != NULLVP) {
463 coveredvp->v_mountedhere = NULL;
464 vrele(coveredvp);
465 }
466 mp->mnt_op->vfs_refcount--;
467 if (mp->mnt_vnodelist.lh_first != NULL)
468 panic("unmount: dangling vnode");
469 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_slock);
470 if (mp->mnt_flag & MNT_MWAIT)
471 wakeup((caddr_t)mp);
472 free((caddr_t)mp, M_MOUNT);
473 return (0);
474 }
475
476 /*
477 * Sync each mounted filesystem.
478 */
479 #ifdef DEBUG
480 int syncprt = 0;
481 struct ctldebug debug0 = { "syncprt", &syncprt };
482 #endif
483
484 /* ARGSUSED */
485 int
486 sys_sync(p, v, retval)
487 struct proc *p;
488 void *v;
489 register_t *retval;
490 {
491 register struct mount *mp, *nmp;
492 int asyncflag;
493
494 simple_lock(&mountlist_slock);
495 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
496 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
497 nmp = mp->mnt_list.cqe_prev;
498 continue;
499 }
500 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
501 asyncflag = mp->mnt_flag & MNT_ASYNC;
502 mp->mnt_flag &= ~MNT_ASYNC;
503 #if defined(UVM)
504 uvm_vnp_sync(mp);
505 #else
506 vnode_pager_sync(mp);
507 #endif
508 VFS_SYNC(mp, MNT_NOWAIT, p->p_ucred, p);
509 if (asyncflag)
510 mp->mnt_flag |= MNT_ASYNC;
511 }
512 simple_lock(&mountlist_slock);
513 nmp = mp->mnt_list.cqe_prev;
514 vfs_unbusy(mp);
515
516 }
517 simple_unlock(&mountlist_slock);
518 #ifdef DEBUG
519 if (syncprt)
520 vfs_bufstats();
521 #endif /* DEBUG */
522 return (0);
523 }
524
525 /*
526 * Change filesystem quotas.
527 */
528 /* ARGSUSED */
529 int
530 sys_quotactl(p, v, retval)
531 struct proc *p;
532 void *v;
533 register_t *retval;
534 {
535 register struct sys_quotactl_args /* {
536 syscallarg(const char *) path;
537 syscallarg(int) cmd;
538 syscallarg(int) uid;
539 syscallarg(caddr_t) arg;
540 } */ *uap = v;
541 register struct mount *mp;
542 int error;
543 struct nameidata nd;
544
545 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
546 if ((error = namei(&nd)) != 0)
547 return (error);
548 mp = nd.ni_vp->v_mount;
549 vrele(nd.ni_vp);
550 return (VFS_QUOTACTL(mp, SCARG(uap, cmd), SCARG(uap, uid),
551 SCARG(uap, arg), p));
552 }
553
554 /*
555 * Get filesystem statistics.
556 */
557 /* ARGSUSED */
558 int
559 sys_statfs(p, v, retval)
560 struct proc *p;
561 void *v;
562 register_t *retval;
563 {
564 register struct sys_statfs_args /* {
565 syscallarg(const char *) path;
566 syscallarg(struct statfs *) buf;
567 } */ *uap = v;
568 register struct mount *mp;
569 register struct statfs *sp;
570 int error;
571 struct nameidata nd;
572
573 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
574 if ((error = namei(&nd)) != 0)
575 return (error);
576 mp = nd.ni_vp->v_mount;
577 sp = &mp->mnt_stat;
578 vrele(nd.ni_vp);
579 if ((error = VFS_STATFS(mp, sp, p)) != 0)
580 return (error);
581 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
582 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
583 }
584
585 /*
586 * Get filesystem statistics.
587 */
588 /* ARGSUSED */
589 int
590 sys_fstatfs(p, v, retval)
591 struct proc *p;
592 void *v;
593 register_t *retval;
594 {
595 register struct sys_fstatfs_args /* {
596 syscallarg(int) fd;
597 syscallarg(struct statfs *) buf;
598 } */ *uap = v;
599 struct file *fp;
600 struct mount *mp;
601 register struct statfs *sp;
602 int error;
603
604 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
605 return (error);
606 mp = ((struct vnode *)fp->f_data)->v_mount;
607 sp = &mp->mnt_stat;
608 if ((error = VFS_STATFS(mp, sp, p)) != 0)
609 return (error);
610 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
611 return (copyout(sp, SCARG(uap, buf), sizeof(*sp)));
612 }
613
614 /*
615 * Get statistics on all filesystems.
616 */
617 int
618 sys_getfsstat(p, v, retval)
619 struct proc *p;
620 void *v;
621 register_t *retval;
622 {
623 register struct sys_getfsstat_args /* {
624 syscallarg(struct statfs *) buf;
625 syscallarg(long) bufsize;
626 syscallarg(int) flags;
627 } */ *uap = v;
628 register struct mount *mp, *nmp;
629 register struct statfs *sp;
630 caddr_t sfsp;
631 long count, maxcount, error;
632
633 maxcount = SCARG(uap, bufsize) / sizeof(struct statfs);
634 sfsp = (caddr_t)SCARG(uap, buf);
635 simple_lock(&mountlist_slock);
636 count = 0;
637 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
638 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
639 nmp = mp->mnt_list.cqe_next;
640 continue;
641 }
642 if (sfsp && count < maxcount) {
643 sp = &mp->mnt_stat;
644 /*
645 * If MNT_NOWAIT is specified, do not refresh the
646 * fsstat cache. MNT_WAIT overrides MNT_NOWAIT.
647 */
648 if (((SCARG(uap, flags) & MNT_NOWAIT) == 0 ||
649 (SCARG(uap, flags) & MNT_WAIT)) &&
650 (error = VFS_STATFS(mp, sp, p)) != 0) {
651 simple_lock(&mountlist_slock);
652 nmp = mp->mnt_list.cqe_next;
653 vfs_unbusy(mp);
654 continue;
655 }
656 sp->f_flags = mp->mnt_flag & MNT_VISFLAGMASK;
657 error = copyout(sp, sfsp, sizeof(*sp));
658 if (error)
659 return (error);
660 sfsp += sizeof(*sp);
661 }
662 count++;
663 simple_lock(&mountlist_slock);
664 nmp = mp->mnt_list.cqe_next;
665 vfs_unbusy(mp);
666 }
667 simple_unlock(&mountlist_slock);
668 if (sfsp && count > maxcount)
669 *retval = maxcount;
670 else
671 *retval = count;
672 return (0);
673 }
674
675 /*
676 * Change current working directory to a given file descriptor.
677 */
678 /* ARGSUSED */
679 int
680 sys_fchdir(p, v, retval)
681 struct proc *p;
682 void *v;
683 register_t *retval;
684 {
685 struct sys_fchdir_args /* {
686 syscallarg(int) fd;
687 } */ *uap = v;
688 register struct filedesc *fdp = p->p_fd;
689 struct vnode *vp, *tdp;
690 struct mount *mp;
691 struct file *fp;
692 int error;
693
694 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
695 return (error);
696 vp = (struct vnode *)fp->f_data;
697 VREF(vp);
698 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
699 if (vp->v_type != VDIR)
700 error = ENOTDIR;
701 else
702 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
703 while (!error && (mp = vp->v_mountedhere) != NULL) {
704 if (vfs_busy(mp, 0, 0))
705 continue;
706 error = VFS_ROOT(mp, &tdp);
707 vfs_unbusy(mp);
708 if (error)
709 break;
710 vput(vp);
711 vp = tdp;
712 }
713 if (error) {
714 vput(vp);
715 return (error);
716 }
717 VOP_UNLOCK(vp, 0);
718 vrele(fdp->fd_cdir);
719 fdp->fd_cdir = vp;
720 return (0);
721 }
722
723 /*
724 * Change current working directory (``.'').
725 */
726 /* ARGSUSED */
727 int
728 sys_chdir(p, v, retval)
729 struct proc *p;
730 void *v;
731 register_t *retval;
732 {
733 struct sys_chdir_args /* {
734 syscallarg(const char *) path;
735 } */ *uap = v;
736 register struct filedesc *fdp = p->p_fd;
737 int error;
738 struct nameidata nd;
739
740 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
741 SCARG(uap, path), p);
742 if ((error = change_dir(&nd, p)) != 0)
743 return (error);
744 vrele(fdp->fd_cdir);
745 fdp->fd_cdir = nd.ni_vp;
746 return (0);
747 }
748
749 /*
750 * Change notion of root (``/'') directory.
751 */
752 /* ARGSUSED */
753 int
754 sys_chroot(p, v, retval)
755 struct proc *p;
756 void *v;
757 register_t *retval;
758 {
759 struct sys_chroot_args /* {
760 syscallarg(const char *) path;
761 } */ *uap = v;
762 register struct filedesc *fdp = p->p_fd;
763 int error;
764 struct nameidata nd;
765
766 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
767 return (error);
768 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
769 SCARG(uap, path), p);
770 if ((error = change_dir(&nd, p)) != 0)
771 return (error);
772 if (fdp->fd_rdir != NULL)
773 vrele(fdp->fd_rdir);
774 fdp->fd_rdir = nd.ni_vp;
775 return (0);
776 }
777
778 /*
779 * Common routine for chroot and chdir.
780 */
781 static int
782 change_dir(ndp, p)
783 register struct nameidata *ndp;
784 struct proc *p;
785 {
786 struct vnode *vp;
787 int error;
788
789 if ((error = namei(ndp)) != 0)
790 return (error);
791 vp = ndp->ni_vp;
792 if (vp->v_type != VDIR)
793 error = ENOTDIR;
794 else
795 error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
796
797 if (error)
798 vput(vp);
799 else
800 VOP_UNLOCK(vp, 0);
801 return (error);
802 }
803
804 /*
805 * Check permissions, allocate an open file structure,
806 * and call the device open routine if any.
807 */
808 int
809 sys_open(p, v, retval)
810 struct proc *p;
811 void *v;
812 register_t *retval;
813 {
814 register struct sys_open_args /* {
815 syscallarg(const char *) path;
816 syscallarg(int) flags;
817 syscallarg(int) mode;
818 } */ *uap = v;
819 register struct filedesc *fdp = p->p_fd;
820 register struct file *fp;
821 register struct vnode *vp;
822 int flags, cmode;
823 struct file *nfp;
824 int type, indx, error;
825 struct flock lf;
826 struct nameidata nd;
827 extern struct fileops vnops;
828
829 flags = FFLAGS(SCARG(uap, flags));
830 if ((flags & (FREAD | FWRITE)) == 0)
831 return (EINVAL);
832 if ((error = falloc(p, &nfp, &indx)) != 0)
833 return (error);
834 fp = nfp;
835 cmode = ((SCARG(uap, mode) &~ fdp->fd_cmask) & ALLPERMS) &~ S_ISTXT;
836 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
837 p->p_dupfd = -indx - 1; /* XXX check for fdopen */
838 if ((error = vn_open(&nd, flags, cmode)) != 0) {
839 ffree(fp);
840 if ((error == ENODEV || error == ENXIO) &&
841 p->p_dupfd >= 0 && /* XXX from fdopen */
842 (error =
843 dupfdopen(fdp, indx, p->p_dupfd, flags, error)) == 0) {
844 *retval = indx;
845 return (0);
846 }
847 if (error == ERESTART)
848 error = EINTR;
849 fdp->fd_ofiles[indx] = NULL;
850 return (error);
851 }
852 p->p_dupfd = 0;
853 vp = nd.ni_vp;
854 fp->f_flag = flags & FMASK;
855 fp->f_type = DTYPE_VNODE;
856 fp->f_ops = &vnops;
857 fp->f_data = (caddr_t)vp;
858 if (flags & (O_EXLOCK | O_SHLOCK)) {
859 lf.l_whence = SEEK_SET;
860 lf.l_start = 0;
861 lf.l_len = 0;
862 if (flags & O_EXLOCK)
863 lf.l_type = F_WRLCK;
864 else
865 lf.l_type = F_RDLCK;
866 type = F_FLOCK;
867 if ((flags & FNONBLOCK) == 0)
868 type |= F_WAIT;
869 VOP_UNLOCK(vp, 0);
870 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, type);
871 if (error) {
872 (void) vn_close(vp, fp->f_flag, fp->f_cred, p);
873 ffree(fp);
874 fdp->fd_ofiles[indx] = NULL;
875 return (error);
876 }
877 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
878 fp->f_flag |= FHASLOCK;
879 }
880 VOP_UNLOCK(vp, 0);
881 *retval = indx;
882 return (0);
883 }
884
885 /*
886 * Create a special file.
887 */
888 /* ARGSUSED */
889 int
890 sys_mknod(p, v, retval)
891 struct proc *p;
892 void *v;
893 register_t *retval;
894 {
895 register struct sys_mknod_args /* {
896 syscallarg(const char *) path;
897 syscallarg(int) mode;
898 syscallarg(int) dev;
899 } */ *uap = v;
900 register struct vnode *vp;
901 struct vattr vattr;
902 int error;
903 int whiteout = 0;
904 struct nameidata nd;
905
906 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
907 return (error);
908 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
909 if ((error = namei(&nd)) != 0)
910 return (error);
911 vp = nd.ni_vp;
912 if (vp != NULL)
913 error = EEXIST;
914 else {
915 VATTR_NULL(&vattr);
916 vattr.va_mode =
917 (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
918 vattr.va_rdev = SCARG(uap, dev);
919 whiteout = 0;
920
921 switch (SCARG(uap, mode) & S_IFMT) {
922 case S_IFMT: /* used by badsect to flag bad sectors */
923 vattr.va_type = VBAD;
924 break;
925 case S_IFCHR:
926 vattr.va_type = VCHR;
927 break;
928 case S_IFBLK:
929 vattr.va_type = VBLK;
930 break;
931 case S_IFWHT:
932 whiteout = 1;
933 break;
934 default:
935 error = EINVAL;
936 break;
937 }
938 }
939 if (!error) {
940 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
941 if (whiteout) {
942 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
943 if (error)
944 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
945 vput(nd.ni_dvp);
946 } else {
947 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
948 &nd.ni_cnd, &vattr);
949 }
950 } else {
951 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
952 if (nd.ni_dvp == vp)
953 vrele(nd.ni_dvp);
954 else
955 vput(nd.ni_dvp);
956 if (vp)
957 vrele(vp);
958 }
959 return (error);
960 }
961
962 /*
963 * Create a named pipe.
964 */
965 /* ARGSUSED */
966 int
967 sys_mkfifo(p, v, retval)
968 struct proc *p;
969 void *v;
970 register_t *retval;
971 {
972 register struct sys_mkfifo_args /* {
973 syscallarg(const char *) path;
974 syscallarg(int) mode;
975 } */ *uap = v;
976 struct vattr vattr;
977 int error;
978 struct nameidata nd;
979
980 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
981 if ((error = namei(&nd)) != 0)
982 return (error);
983 if (nd.ni_vp != NULL) {
984 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
985 if (nd.ni_dvp == nd.ni_vp)
986 vrele(nd.ni_dvp);
987 else
988 vput(nd.ni_dvp);
989 vrele(nd.ni_vp);
990 return (EEXIST);
991 }
992 VATTR_NULL(&vattr);
993 vattr.va_type = VFIFO;
994 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_fd->fd_cmask;
995 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
996 return (VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr));
997 }
998
999 /*
1000 * Make a hard file link.
1001 */
1002 /* ARGSUSED */
1003 int
1004 sys_link(p, v, retval)
1005 struct proc *p;
1006 void *v;
1007 register_t *retval;
1008 {
1009 register struct sys_link_args /* {
1010 syscallarg(const char *) path;
1011 syscallarg(const char *) link;
1012 } */ *uap = v;
1013 register struct vnode *vp;
1014 struct nameidata nd;
1015 int error;
1016
1017 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1018 if ((error = namei(&nd)) != 0)
1019 return (error);
1020 vp = nd.ni_vp;
1021 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1022 if ((error = namei(&nd)) != 0)
1023 goto out;
1024 if (nd.ni_vp) {
1025 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1026 if (nd.ni_dvp == nd.ni_vp)
1027 vrele(nd.ni_dvp);
1028 else
1029 vput(nd.ni_dvp);
1030 vrele(nd.ni_vp);
1031 error = EEXIST;
1032 goto out;
1033 }
1034 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1035 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1036 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1037 out:
1038 vrele(vp);
1039 return (error);
1040 }
1041
1042 /*
1043 * Make a symbolic link.
1044 */
1045 /* ARGSUSED */
1046 int
1047 sys_symlink(p, v, retval)
1048 struct proc *p;
1049 void *v;
1050 register_t *retval;
1051 {
1052 register struct sys_symlink_args /* {
1053 syscallarg(const char *) path;
1054 syscallarg(const char *) link;
1055 } */ *uap = v;
1056 struct vattr vattr;
1057 char *path;
1058 int error;
1059 struct nameidata nd;
1060
1061 MALLOC(path, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1062 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1063 if (error)
1064 goto out;
1065 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, link), p);
1066 if ((error = namei(&nd)) != 0)
1067 goto out;
1068 if (nd.ni_vp) {
1069 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1070 if (nd.ni_dvp == nd.ni_vp)
1071 vrele(nd.ni_dvp);
1072 else
1073 vput(nd.ni_dvp);
1074 vrele(nd.ni_vp);
1075 error = EEXIST;
1076 goto out;
1077 }
1078 VATTR_NULL(&vattr);
1079 vattr.va_mode = ACCESSPERMS &~ p->p_fd->fd_cmask;
1080 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1081 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1082 out:
1083 FREE(path, M_NAMEI);
1084 return (error);
1085 }
1086
1087 /*
1088 * Delete a whiteout from the filesystem.
1089 */
1090 /* ARGSUSED */
1091 int
1092 sys_undelete(p, v, retval)
1093 struct proc *p;
1094 void *v;
1095 register_t *retval;
1096 {
1097 register struct sys_undelete_args /* {
1098 syscallarg(const char *) path;
1099 } */ *uap = v;
1100 int error;
1101 struct nameidata nd;
1102
1103 NDINIT(&nd, DELETE, LOCKPARENT|DOWHITEOUT, UIO_USERSPACE,
1104 SCARG(uap, path), p);
1105 error = namei(&nd);
1106 if (error)
1107 return (error);
1108
1109 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1110 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1111 if (nd.ni_dvp == nd.ni_vp)
1112 vrele(nd.ni_dvp);
1113 else
1114 vput(nd.ni_dvp);
1115 if (nd.ni_vp)
1116 vrele(nd.ni_vp);
1117 return (EEXIST);
1118 }
1119
1120 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1121 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1122 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1123 vput(nd.ni_dvp);
1124 return (error);
1125 }
1126
1127 /*
1128 * Delete a name from the filesystem.
1129 */
1130 /* ARGSUSED */
1131 int
1132 sys_unlink(p, v, retval)
1133 struct proc *p;
1134 void *v;
1135 register_t *retval;
1136 {
1137 struct sys_unlink_args /* {
1138 syscallarg(const char *) path;
1139 } */ *uap = v;
1140 register struct vnode *vp;
1141 int error;
1142 struct nameidata nd;
1143
1144 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
1145 SCARG(uap, path), p);
1146 if ((error = namei(&nd)) != 0)
1147 return (error);
1148 vp = nd.ni_vp;
1149
1150 /*
1151 * The root of a mounted filesystem cannot be deleted.
1152 */
1153 if (vp->v_flag & VROOT) {
1154 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1155 if (nd.ni_dvp == vp)
1156 vrele(nd.ni_dvp);
1157 else
1158 vput(nd.ni_dvp);
1159 vput(vp);
1160 error = EBUSY;
1161 goto out;
1162 }
1163
1164 #if defined(UVM)
1165 (void)uvm_vnp_uncache(vp);
1166 #else
1167 (void)vnode_pager_uncache(vp);
1168 #endif
1169
1170 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
1171 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1172 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
1173 out:
1174 return (error);
1175 }
1176
1177 /*
1178 * Reposition read/write file offset.
1179 */
1180 int
1181 sys_lseek(p, v, retval)
1182 struct proc *p;
1183 void *v;
1184 register_t *retval;
1185 {
1186 register struct sys_lseek_args /* {
1187 syscallarg(int) fd;
1188 syscallarg(int) pad;
1189 syscallarg(off_t) offset;
1190 syscallarg(int) whence;
1191 } */ *uap = v;
1192 struct ucred *cred = p->p_ucred;
1193 register struct filedesc *fdp = p->p_fd;
1194 register struct file *fp;
1195 struct vnode *vp;
1196 struct vattr vattr;
1197 register off_t newoff;
1198 int error;
1199
1200 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
1201 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
1202 return (EBADF);
1203
1204 vp = (struct vnode *)fp->f_data;
1205 if (fp->f_type != DTYPE_VNODE
1206 || vp->v_type == VFIFO)
1207 return (ESPIPE);
1208
1209 switch (SCARG(uap, whence)) {
1210 case SEEK_CUR:
1211 newoff = fp->f_offset + SCARG(uap, offset);
1212 break;
1213 case SEEK_END:
1214 error = VOP_GETATTR(vp, &vattr, cred, p);
1215 if (error)
1216 return (error);
1217 newoff = SCARG(uap, offset) + vattr.va_size;
1218 break;
1219 case SEEK_SET:
1220 newoff = SCARG(uap, offset);
1221 break;
1222 default:
1223 return (EINVAL);
1224 }
1225 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
1226 return (error);
1227
1228 *(off_t *)retval = fp->f_offset = newoff;
1229 return (0);
1230 }
1231
1232 /*
1233 * Positional read system call.
1234 */
1235 int
1236 sys_pread(p, v, retval)
1237 struct proc *p;
1238 void *v;
1239 register_t *retval;
1240 {
1241 struct sys_pread_args /* {
1242 syscallarg(int) fd;
1243 syscallarg(void *) buf;
1244 syscallarg(size_t) nbyte;
1245 syscallarg(off_t) offset;
1246 } */ *uap = v;
1247 struct filedesc *fdp = p->p_fd;
1248 struct file *fp;
1249 struct vnode *vp;
1250 off_t offset;
1251 int error, fd = SCARG(uap, fd);
1252
1253 if ((u_int)fd >= fdp->fd_nfiles ||
1254 (fp = fdp->fd_ofiles[fd]) == NULL ||
1255 (fp->f_flag & FREAD) == 0)
1256 return (EBADF);
1257
1258 vp = (struct vnode *)fp->f_data;
1259 if (fp->f_type != DTYPE_VNODE
1260 || vp->v_type == VFIFO)
1261 return (ESPIPE);
1262
1263 offset = SCARG(uap, offset);
1264
1265 /*
1266 * XXX This works because no file systems actually
1267 * XXX take any action on the seek operation.
1268 */
1269 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1270 return (error);
1271
1272 return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1273 &offset, 0, retval));
1274 }
1275
1276 /*
1277 * Positional scatter read system call.
1278 */
1279 int
1280 sys_preadv(p, v, retval)
1281 struct proc *p;
1282 void *v;
1283 register_t *retval;
1284 {
1285 struct sys_preadv_args /* {
1286 syscallarg(int) fd;
1287 syscallarg(const struct iovec *) iovp;
1288 syscallarg(int) iovcnt;
1289 syscallarg(off_t) offset;
1290 } */ *uap = v;
1291 struct filedesc *fdp = p->p_fd;
1292 struct file *fp;
1293 struct vnode *vp;
1294 off_t offset;
1295 int error, fd = SCARG(uap, fd);
1296
1297 if ((u_int)fd >= fdp->fd_nfiles ||
1298 (fp = fdp->fd_ofiles[fd]) == NULL ||
1299 (fp->f_flag & FREAD) == 0)
1300 return (EBADF);
1301
1302 vp = (struct vnode *)fp->f_data;
1303 if (fp->f_type != DTYPE_VNODE
1304 || vp->v_type == VFIFO)
1305 return (ESPIPE);
1306
1307 offset = SCARG(uap, offset);
1308
1309 /*
1310 * XXX This works because no file systems actually
1311 * XXX take any action on the seek operation.
1312 */
1313 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1314 return (error);
1315
1316 return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1317 &offset, 0, retval));
1318 }
1319
1320 /*
1321 * Positional write system call.
1322 */
1323 int
1324 sys_pwrite(p, v, retval)
1325 struct proc *p;
1326 void *v;
1327 register_t *retval;
1328 {
1329 struct sys_pwrite_args /* {
1330 syscallarg(int) fd;
1331 syscallarg(const void *) buf;
1332 syscallarg(size_t) nbyte;
1333 syscallarg(off_t) offset;
1334 } */ *uap = v;
1335 struct filedesc *fdp = p->p_fd;
1336 struct file *fp;
1337 struct vnode *vp;
1338 off_t offset;
1339 int error, fd = SCARG(uap, fd);
1340
1341 if ((u_int)fd >= fdp->fd_nfiles ||
1342 (fp = fdp->fd_ofiles[fd]) == NULL ||
1343 (fp->f_flag & FWRITE) == 0)
1344 return (EBADF);
1345
1346 vp = (struct vnode *)fp->f_data;
1347 if (fp->f_type != DTYPE_VNODE
1348 || vp->v_type == VFIFO)
1349 return (ESPIPE);
1350
1351 offset = SCARG(uap, offset);
1352
1353 /*
1354 * XXX This works because no file systems actually
1355 * XXX take any action on the seek operation.
1356 */
1357 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1358 return (error);
1359
1360 return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
1361 &offset, 0, retval));
1362 }
1363
1364 /*
1365 * Positional gather write system call.
1366 */
1367 int
1368 sys_pwritev(p, v, retval)
1369 struct proc *p;
1370 void *v;
1371 register_t *retval;
1372 {
1373 struct sys_pwritev_args /* {
1374 syscallarg(int) fd;
1375 syscallarg(const struct iovec *) iovp;
1376 syscallarg(int) iovcnt;
1377 syscallarg(off_t) offset;
1378 } */ *uap = v;
1379 struct filedesc *fdp = p->p_fd;
1380 struct file *fp;
1381 struct vnode *vp;
1382 off_t offset;
1383 int error, fd = SCARG(uap, fd);
1384
1385 if ((u_int)fd >= fdp->fd_nfiles ||
1386 (fp = fdp->fd_ofiles[fd]) == NULL ||
1387 (fp->f_flag & FWRITE) == 0)
1388 return (EBADF);
1389
1390 vp = (struct vnode *)fp->f_data;
1391 if (fp->f_type != DTYPE_VNODE
1392 || vp->v_type == VFIFO)
1393 return (ESPIPE);
1394
1395 offset = SCARG(uap, offset);
1396
1397 /*
1398 * XXX This works because no file systems actually
1399 * XXX take any action on the seek operation.
1400 */
1401 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
1402 return (error);
1403
1404 return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
1405 &offset, 0, retval));
1406 }
1407
1408 /*
1409 * Check access permissions.
1410 */
1411 int
1412 sys_access(p, v, retval)
1413 struct proc *p;
1414 void *v;
1415 register_t *retval;
1416 {
1417 register struct sys_access_args /* {
1418 syscallarg(const char *) path;
1419 syscallarg(int) flags;
1420 } */ *uap = v;
1421 register struct ucred *cred = p->p_ucred;
1422 register struct vnode *vp;
1423 int error, flags, t_gid, t_uid;
1424 struct nameidata nd;
1425
1426 t_uid = cred->cr_uid;
1427 t_gid = cred->cr_gid;
1428 cred->cr_uid = p->p_cred->p_ruid;
1429 cred->cr_gid = p->p_cred->p_rgid;
1430 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1431 SCARG(uap, path), p);
1432 if ((error = namei(&nd)) != 0)
1433 goto out1;
1434 vp = nd.ni_vp;
1435
1436 /* Flags == 0 means only check for existence. */
1437 if (SCARG(uap, flags)) {
1438 flags = 0;
1439 if (SCARG(uap, flags) & R_OK)
1440 flags |= VREAD;
1441 if (SCARG(uap, flags) & W_OK)
1442 flags |= VWRITE;
1443 if (SCARG(uap, flags) & X_OK)
1444 flags |= VEXEC;
1445 if ((flags & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
1446 error = VOP_ACCESS(vp, flags, cred, p);
1447 }
1448 vput(vp);
1449 out1:
1450 cred->cr_uid = t_uid;
1451 cred->cr_gid = t_gid;
1452 return (error);
1453 }
1454
1455 /*
1456 * Get file status; this version follows links.
1457 */
1458 /* ARGSUSED */
1459 int
1460 sys___stat13(p, v, retval)
1461 struct proc *p;
1462 void *v;
1463 register_t *retval;
1464 {
1465 register struct sys___stat13_args /* {
1466 syscallarg(const char *) path;
1467 syscallarg(struct stat *) ub;
1468 } */ *uap = v;
1469 struct stat sb;
1470 int error;
1471 struct nameidata nd;
1472
1473 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1474 SCARG(uap, path), p);
1475 if ((error = namei(&nd)) != 0)
1476 return (error);
1477 error = vn_stat(nd.ni_vp, &sb, p);
1478 vput(nd.ni_vp);
1479 if (error)
1480 return (error);
1481 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1482 return (error);
1483 }
1484
1485 /*
1486 * Get file status; this version does not follow links.
1487 */
1488 /* ARGSUSED */
1489 int
1490 sys___lstat13(p, v, retval)
1491 struct proc *p;
1492 void *v;
1493 register_t *retval;
1494 {
1495 register struct sys___lstat13_args /* {
1496 syscallarg(const char *) path;
1497 syscallarg(struct stat *) ub;
1498 } */ *uap = v;
1499 struct stat sb;
1500 int error;
1501 struct nameidata nd;
1502
1503 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1504 SCARG(uap, path), p);
1505 if ((error = namei(&nd)) != 0)
1506 return (error);
1507 error = vn_stat(nd.ni_vp, &sb, p);
1508 vput(nd.ni_vp);
1509 if (error)
1510 return (error);
1511 error = copyout(&sb, SCARG(uap, ub), sizeof (sb));
1512 return (error);
1513 }
1514
1515 /*
1516 * Get configurable pathname variables.
1517 */
1518 /* ARGSUSED */
1519 int
1520 sys_pathconf(p, v, retval)
1521 struct proc *p;
1522 void *v;
1523 register_t *retval;
1524 {
1525 register struct sys_pathconf_args /* {
1526 syscallarg(const char *) path;
1527 syscallarg(int) name;
1528 } */ *uap = v;
1529 int error;
1530 struct nameidata nd;
1531
1532 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
1533 SCARG(uap, path), p);
1534 if ((error = namei(&nd)) != 0)
1535 return (error);
1536 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
1537 vput(nd.ni_vp);
1538 return (error);
1539 }
1540
1541 /*
1542 * Return target name of a symbolic link.
1543 */
1544 /* ARGSUSED */
1545 int
1546 sys_readlink(p, v, retval)
1547 struct proc *p;
1548 void *v;
1549 register_t *retval;
1550 {
1551 register struct sys_readlink_args /* {
1552 syscallarg(const char *) path;
1553 syscallarg(char *) buf;
1554 syscallarg(size_t) count;
1555 } */ *uap = v;
1556 register struct vnode *vp;
1557 struct iovec aiov;
1558 struct uio auio;
1559 int error;
1560 struct nameidata nd;
1561
1562 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF, UIO_USERSPACE,
1563 SCARG(uap, path), p);
1564 if ((error = namei(&nd)) != 0)
1565 return (error);
1566 vp = nd.ni_vp;
1567 if (vp->v_type != VLNK)
1568 error = EINVAL;
1569 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
1570 (error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) == 0) {
1571 aiov.iov_base = SCARG(uap, buf);
1572 aiov.iov_len = SCARG(uap, count);
1573 auio.uio_iov = &aiov;
1574 auio.uio_iovcnt = 1;
1575 auio.uio_offset = 0;
1576 auio.uio_rw = UIO_READ;
1577 auio.uio_segflg = UIO_USERSPACE;
1578 auio.uio_procp = p;
1579 auio.uio_resid = SCARG(uap, count);
1580 error = VOP_READLINK(vp, &auio, p->p_ucred);
1581 }
1582 vput(vp);
1583 *retval = SCARG(uap, count) - auio.uio_resid;
1584 return (error);
1585 }
1586
1587 /*
1588 * Change flags of a file given a path name.
1589 */
1590 /* ARGSUSED */
1591 int
1592 sys_chflags(p, v, retval)
1593 struct proc *p;
1594 void *v;
1595 register_t *retval;
1596 {
1597 register struct sys_chflags_args /* {
1598 syscallarg(const char *) path;
1599 syscallarg(u_long) flags;
1600 } */ *uap = v;
1601 register struct vnode *vp;
1602 struct vattr vattr;
1603 int error;
1604 struct nameidata nd;
1605
1606 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1607 if ((error = namei(&nd)) != 0)
1608 return (error);
1609 vp = nd.ni_vp;
1610 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1611 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1612 VATTR_NULL(&vattr);
1613 vattr.va_flags = SCARG(uap, flags);
1614 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1615 vput(vp);
1616 return (error);
1617 }
1618
1619 /*
1620 * Change flags of a file given a file descriptor.
1621 */
1622 /* ARGSUSED */
1623 int
1624 sys_fchflags(p, v, retval)
1625 struct proc *p;
1626 void *v;
1627 register_t *retval;
1628 {
1629 register struct sys_fchflags_args /* {
1630 syscallarg(int) fd;
1631 syscallarg(u_long) flags;
1632 } */ *uap = v;
1633 struct vattr vattr;
1634 struct vnode *vp;
1635 struct file *fp;
1636 int error;
1637
1638 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1639 return (error);
1640 vp = (struct vnode *)fp->f_data;
1641 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1642 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1643 VATTR_NULL(&vattr);
1644 vattr.va_flags = SCARG(uap, flags);
1645 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1646 VOP_UNLOCK(vp, 0);
1647 return (error);
1648 }
1649
1650 /*
1651 * Change mode of a file given path name; this version follows links.
1652 */
1653 /* ARGSUSED */
1654 int
1655 sys_chmod(p, v, retval)
1656 struct proc *p;
1657 void *v;
1658 register_t *retval;
1659 {
1660 register struct sys_chmod_args /* {
1661 syscallarg(const char *) path;
1662 syscallarg(int) mode;
1663 } */ *uap = v;
1664 int error;
1665 struct nameidata nd;
1666
1667 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1668 if ((error = namei(&nd)) != 0)
1669 return (error);
1670
1671 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
1672
1673 vrele(nd.ni_vp);
1674 return (error);
1675 }
1676
1677 /*
1678 * Change mode of a file given a file descriptor.
1679 */
1680 /* ARGSUSED */
1681 int
1682 sys_fchmod(p, v, retval)
1683 struct proc *p;
1684 void *v;
1685 register_t *retval;
1686 {
1687 register struct sys_fchmod_args /* {
1688 syscallarg(int) fd;
1689 syscallarg(int) mode;
1690 } */ *uap = v;
1691 struct file *fp;
1692 int error;
1693
1694 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1695 return (error);
1696
1697 return (change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), p));
1698 }
1699
1700 /*
1701 * Change mode of a file given path name; this version does not follow links.
1702 */
1703 /* ARGSUSED */
1704 int
1705 sys_lchmod(p, v, retval)
1706 struct proc *p;
1707 void *v;
1708 register_t *retval;
1709 {
1710 register struct sys_lchmod_args /* {
1711 syscallarg(const char *) path;
1712 syscallarg(int) mode;
1713 } */ *uap = v;
1714 int error;
1715 struct nameidata nd;
1716
1717 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1718 if ((error = namei(&nd)) != 0)
1719 return (error);
1720
1721 error = change_mode(nd.ni_vp, SCARG(uap, mode), p);
1722
1723 vrele(nd.ni_vp);
1724 return (error);
1725 }
1726
1727 /*
1728 * Common routine to set mode given a vnode.
1729 */
1730 static int
1731 change_mode(vp, mode, p)
1732 struct vnode *vp;
1733 int mode;
1734 struct proc *p;
1735 {
1736 struct vattr vattr;
1737 int error;
1738
1739 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1740 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1741 VATTR_NULL(&vattr);
1742 vattr.va_mode = mode & ALLPERMS;
1743 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1744 VOP_UNLOCK(vp, 0);
1745 return (error);
1746 }
1747
1748 /*
1749 * Set ownership given a path name; this version follows links.
1750 */
1751 /* ARGSUSED */
1752 int
1753 sys_chown(p, v, retval)
1754 struct proc *p;
1755 void *v;
1756 register_t *retval;
1757 {
1758 register struct sys_chown_args /* {
1759 syscallarg(const char *) path;
1760 syscallarg(uid_t) uid;
1761 syscallarg(gid_t) gid;
1762 } */ *uap = v;
1763 int error;
1764 struct nameidata nd;
1765
1766 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1767 if ((error = namei(&nd)) != 0)
1768 return (error);
1769
1770 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
1771
1772 vrele(nd.ni_vp);
1773 return (error);
1774 }
1775
1776 /*
1777 * Set ownership given a path name; this version follows links.
1778 * Provides POSIX semantics.
1779 */
1780 /* ARGSUSED */
1781 int
1782 sys___posix_chown(p, v, retval)
1783 struct proc *p;
1784 void *v;
1785 register_t *retval;
1786 {
1787 register struct sys_chown_args /* {
1788 syscallarg(const char *) path;
1789 syscallarg(uid_t) uid;
1790 syscallarg(gid_t) gid;
1791 } */ *uap = v;
1792 int error;
1793 struct nameidata nd;
1794
1795 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1796 if ((error = namei(&nd)) != 0)
1797 return (error);
1798
1799 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
1800
1801 vrele(nd.ni_vp);
1802 return (error);
1803 }
1804
1805 /*
1806 * Set ownership given a file descriptor.
1807 */
1808 /* ARGSUSED */
1809 int
1810 sys_fchown(p, v, retval)
1811 struct proc *p;
1812 void *v;
1813 register_t *retval;
1814 {
1815 register struct sys_fchown_args /* {
1816 syscallarg(int) fd;
1817 syscallarg(uid_t) uid;
1818 syscallarg(gid_t) gid;
1819 } */ *uap = v;
1820 int error;
1821 struct file *fp;
1822
1823 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1824 return (error);
1825
1826 return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
1827 SCARG(uap, gid), p, 0));
1828 }
1829
1830 /*
1831 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
1832 */
1833 /* ARGSUSED */
1834 int
1835 sys___posix_fchown(p, v, retval)
1836 struct proc *p;
1837 void *v;
1838 register_t *retval;
1839 {
1840 register struct sys_fchown_args /* {
1841 syscallarg(int) fd;
1842 syscallarg(uid_t) uid;
1843 syscallarg(gid_t) gid;
1844 } */ *uap = v;
1845 int error;
1846 struct file *fp;
1847
1848 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
1849 return (error);
1850
1851 return (change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
1852 SCARG(uap, gid), p, 1));
1853 }
1854
1855 /*
1856 * Set ownership given a path name; this version does not follow links.
1857 */
1858 /* ARGSUSED */
1859 int
1860 sys_lchown(p, v, retval)
1861 struct proc *p;
1862 void *v;
1863 register_t *retval;
1864 {
1865 register struct sys_lchown_args /* {
1866 syscallarg(const char *) path;
1867 syscallarg(uid_t) uid;
1868 syscallarg(gid_t) gid;
1869 } */ *uap = v;
1870 int error;
1871 struct nameidata nd;
1872
1873 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1874 if ((error = namei(&nd)) != 0)
1875 return (error);
1876
1877 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 0);
1878
1879 vrele(nd.ni_vp);
1880 return (error);
1881 }
1882
1883 /*
1884 * Set ownership given a path name; this version does not follow links.
1885 * Provides POSIX/XPG semantics.
1886 */
1887 /* ARGSUSED */
1888 int
1889 sys___posix_lchown(p, v, retval)
1890 struct proc *p;
1891 void *v;
1892 register_t *retval;
1893 {
1894 register struct sys_lchown_args /* {
1895 syscallarg(const char *) path;
1896 syscallarg(uid_t) uid;
1897 syscallarg(gid_t) gid;
1898 } */ *uap = v;
1899 int error;
1900 struct nameidata nd;
1901
1902 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1903 if ((error = namei(&nd)) != 0)
1904 return (error);
1905
1906 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), p, 1);
1907
1908 vrele(nd.ni_vp);
1909 return (error);
1910 }
1911
1912 /*
1913 * Common routine to set ownership given a vnode.
1914 */
1915 static int
1916 change_owner(vp, uid, gid, p, posix_semantics)
1917 register struct vnode *vp;
1918 uid_t uid;
1919 gid_t gid;
1920 struct proc *p;
1921 int posix_semantics;
1922 {
1923 struct vattr vattr;
1924 mode_t newmode;
1925 int error;
1926
1927 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
1928 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1929 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
1930 goto out;
1931
1932 #define CHANGED(x) ((x) != -1)
1933 newmode = vattr.va_mode;
1934 if (posix_semantics) {
1935 /*
1936 * POSIX/XPG semantics: if the caller is not the super-user,
1937 * clear set-user-id and set-group-id bits. Both POSIX and
1938 * the XPG consider the behaviour for calls by the super-user
1939 * implementation-defined; we leave the set-user-id and set-
1940 * group-id settings intact in that case.
1941 */
1942 if (suser(p->p_ucred, NULL) != 0)
1943 newmode &= ~(S_ISUID | S_ISGID);
1944 } else {
1945 /*
1946 * NetBSD semantics: when changing owner and/or group,
1947 * clear the respective bit(s).
1948 */
1949 if (CHANGED(uid))
1950 newmode &= ~S_ISUID;
1951 if (CHANGED(gid))
1952 newmode &= ~S_ISGID;
1953 }
1954 /* Update va_mode iff altered. */
1955 if (vattr.va_mode == newmode)
1956 newmode = VNOVAL;
1957
1958 VATTR_NULL(&vattr);
1959 vattr.va_uid = CHANGED(uid) ? uid : VNOVAL;
1960 vattr.va_gid = CHANGED(gid) ? gid : VNOVAL;
1961 vattr.va_mode = newmode;
1962 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
1963 #undef CHANGED
1964
1965 out:
1966 VOP_UNLOCK(vp, 0);
1967 return (error);
1968 }
1969
1970 /*
1971 * Set the access and modification times given a path name; this
1972 * version follows links.
1973 */
1974 /* ARGSUSED */
1975 int
1976 sys_utimes(p, v, retval)
1977 struct proc *p;
1978 void *v;
1979 register_t *retval;
1980 {
1981 register struct sys_utimes_args /* {
1982 syscallarg(const char *) path;
1983 syscallarg(const struct timeval *) tptr;
1984 } */ *uap = v;
1985 int error;
1986 struct nameidata nd;
1987
1988 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
1989 if ((error = namei(&nd)) != 0)
1990 return (error);
1991
1992 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
1993
1994 vrele(nd.ni_vp);
1995 return (error);
1996 }
1997
1998 /*
1999 * Set the access and modification times given a file descriptor.
2000 */
2001 /* ARGSUSED */
2002 int
2003 sys_futimes(p, v, retval)
2004 struct proc *p;
2005 void *v;
2006 register_t *retval;
2007 {
2008 register struct sys_futimes_args /* {
2009 syscallarg(int) fd;
2010 syscallarg(const struct timeval *) tptr;
2011 } */ *uap = v;
2012 int error;
2013 struct file *fp;
2014
2015 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2016 return (error);
2017
2018 return (change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr),
2019 p));
2020 }
2021
2022 /*
2023 * Set the access and modification times given a path name; this
2024 * version does not follow links.
2025 */
2026 /* ARGSUSED */
2027 int
2028 sys_lutimes(p, v, retval)
2029 struct proc *p;
2030 void *v;
2031 register_t *retval;
2032 {
2033 register struct sys_lutimes_args /* {
2034 syscallarg(const char *) path;
2035 syscallarg(const struct timeval *) tptr;
2036 } */ *uap = v;
2037 int error;
2038 struct nameidata nd;
2039
2040 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2041 if ((error = namei(&nd)) != 0)
2042 return (error);
2043
2044 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), p);
2045
2046 vrele(nd.ni_vp);
2047 return (error);
2048 }
2049
2050 /*
2051 * Common routine to set access and modification times given a vnode.
2052 */
2053 static int
2054 change_utimes(vp, tptr, p)
2055 struct vnode *vp;
2056 const struct timeval *tptr;
2057 struct proc *p;
2058 {
2059 struct timeval tv[2];
2060 struct vattr vattr;
2061 int error;
2062
2063 VATTR_NULL(&vattr);
2064 if (tptr == NULL) {
2065 microtime(&tv[0]);
2066 tv[1] = tv[0];
2067 vattr.va_vaflags |= VA_UTIMES_NULL;
2068 } else {
2069 error = copyin(tptr, tv, sizeof (tv));
2070 if (error)
2071 return (error);
2072 }
2073 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2074 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2075 vattr.va_atime.tv_sec = tv[0].tv_sec;
2076 vattr.va_atime.tv_nsec = tv[0].tv_usec * 1000;
2077 vattr.va_mtime.tv_sec = tv[1].tv_sec;
2078 vattr.va_mtime.tv_nsec = tv[1].tv_usec * 1000;
2079 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2080 VOP_UNLOCK(vp, 0);
2081 return (error);
2082 }
2083
2084 /*
2085 * Truncate a file given its path name.
2086 */
2087 /* ARGSUSED */
2088 int
2089 sys_truncate(p, v, retval)
2090 struct proc *p;
2091 void *v;
2092 register_t *retval;
2093 {
2094 register struct sys_truncate_args /* {
2095 syscallarg(const char *) path;
2096 syscallarg(int) pad;
2097 syscallarg(off_t) length;
2098 } */ *uap = v;
2099 register struct vnode *vp;
2100 struct vattr vattr;
2101 int error;
2102 struct nameidata nd;
2103
2104 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2105 if ((error = namei(&nd)) != 0)
2106 return (error);
2107 vp = nd.ni_vp;
2108 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2109 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2110 if (vp->v_type == VDIR)
2111 error = EISDIR;
2112 else if ((error = vn_writechk(vp)) == 0 &&
2113 (error = VOP_ACCESS(vp, VWRITE, p->p_ucred, p)) == 0) {
2114 VATTR_NULL(&vattr);
2115 vattr.va_size = SCARG(uap, length);
2116 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
2117 }
2118 vput(vp);
2119 return (error);
2120 }
2121
2122 /*
2123 * Truncate a file given a file descriptor.
2124 */
2125 /* ARGSUSED */
2126 int
2127 sys_ftruncate(p, v, retval)
2128 struct proc *p;
2129 void *v;
2130 register_t *retval;
2131 {
2132 register struct sys_ftruncate_args /* {
2133 syscallarg(int) fd;
2134 syscallarg(int) pad;
2135 syscallarg(off_t) length;
2136 } */ *uap = v;
2137 struct vattr vattr;
2138 struct vnode *vp;
2139 struct file *fp;
2140 int error;
2141
2142 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2143 return (error);
2144 if ((fp->f_flag & FWRITE) == 0)
2145 return (EINVAL);
2146 vp = (struct vnode *)fp->f_data;
2147 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2148 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2149 if (vp->v_type == VDIR)
2150 error = EISDIR;
2151 else if ((error = vn_writechk(vp)) == 0) {
2152 VATTR_NULL(&vattr);
2153 vattr.va_size = SCARG(uap, length);
2154 error = VOP_SETATTR(vp, &vattr, fp->f_cred, p);
2155 }
2156 VOP_UNLOCK(vp, 0);
2157 return (error);
2158 }
2159
2160 /*
2161 * Sync an open file.
2162 */
2163 /* ARGSUSED */
2164 int
2165 sys_fsync(p, v, retval)
2166 struct proc *p;
2167 void *v;
2168 register_t *retval;
2169 {
2170 struct sys_fsync_args /* {
2171 syscallarg(int) fd;
2172 } */ *uap = v;
2173 register struct vnode *vp;
2174 struct file *fp;
2175 int error;
2176
2177 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2178 return (error);
2179 vp = (struct vnode *)fp->f_data;
2180 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2181 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, p);
2182 VOP_UNLOCK(vp, 0);
2183 return (error);
2184 }
2185
2186 /*
2187 * Sync the data of an open file.
2188 */
2189 /* ARGSUSED */
2190 int
2191 sys_fdatasync(p, v, retval)
2192 struct proc *p;
2193 void *v;
2194 register_t *retval;
2195 {
2196 struct sys_fdatasync_args /* {
2197 syscallarg(int) fd;
2198 } */ *uap = v;
2199 struct vnode *vp;
2200 struct file *fp;
2201 int error;
2202
2203 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2204 return (error);
2205 vp = (struct vnode *)fp->f_data;
2206 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2207 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, p);
2208 VOP_UNLOCK(vp, 0);
2209 return (error);
2210 }
2211
2212 /*
2213 * Rename files, (standard) BSD semantics frontend.
2214 */
2215 /* ARGSUSED */
2216 int
2217 sys_rename(p, v, retval)
2218 struct proc *p;
2219 void *v;
2220 register_t *retval;
2221 {
2222 register struct sys_rename_args /* {
2223 syscallarg(const char *) from;
2224 syscallarg(const char *) to;
2225 } */ *uap = v;
2226
2227 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 0));
2228 }
2229
2230 /*
2231 * Rename files, POSIX semantics frontend.
2232 */
2233 /* ARGSUSED */
2234 int
2235 sys___posix_rename(p, v, retval)
2236 struct proc *p;
2237 void *v;
2238 register_t *retval;
2239 {
2240 register struct sys___posix_rename_args /* {
2241 syscallarg(const char *) from;
2242 syscallarg(const char *) to;
2243 } */ *uap = v;
2244
2245 return (rename_files(SCARG(uap, from), SCARG(uap, to), p, 1));
2246 }
2247
2248 /*
2249 * Rename files. Source and destination must either both be directories,
2250 * or both not be directories. If target is a directory, it must be empty.
2251 * If `from' and `to' refer to the same object, the value of the `retain'
2252 * argument is used to determine whether `from' will be
2253 *
2254 * (retain == 0) deleted unless `from' and `to' refer to the same
2255 * object in the file system's name space (BSD).
2256 * (retain == 1) always retained (POSIX).
2257 */
2258 static int
2259 rename_files(from, to, p, retain)
2260 const char *from, *to;
2261 struct proc *p;
2262 int retain;
2263 {
2264 register struct vnode *tvp, *fvp, *tdvp;
2265 struct nameidata fromnd, tond;
2266 int error;
2267
2268 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
2269 from, p);
2270 if ((error = namei(&fromnd)) != 0)
2271 return (error);
2272 fvp = fromnd.ni_vp;
2273 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART,
2274 UIO_USERSPACE, to, p);
2275 if ((error = namei(&tond)) != 0) {
2276 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2277 vrele(fromnd.ni_dvp);
2278 vrele(fvp);
2279 goto out1;
2280 }
2281 tdvp = tond.ni_dvp;
2282 tvp = tond.ni_vp;
2283
2284 if (tvp != NULL) {
2285 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
2286 error = ENOTDIR;
2287 goto out;
2288 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
2289 error = EISDIR;
2290 goto out;
2291 }
2292 }
2293
2294 if (fvp == tdvp)
2295 error = EINVAL;
2296
2297 /*
2298 * Source and destination refer to the same object.
2299 */
2300 if (fvp == tvp) {
2301 if (retain)
2302 error = -1;
2303 else if (fromnd.ni_dvp == tdvp &&
2304 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
2305 !bcmp(fromnd.ni_cnd.cn_nameptr,
2306 tond.ni_cnd.cn_nameptr,
2307 fromnd.ni_cnd.cn_namelen))
2308 error = -1;
2309 }
2310
2311 out:
2312 if (!error) {
2313 VOP_LEASE(tdvp, p, p->p_ucred, LEASE_WRITE);
2314 if (fromnd.ni_dvp != tdvp)
2315 VOP_LEASE(fromnd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2316 if (tvp) {
2317 #if defined(UVM)
2318 (void)uvm_vnp_uncache(tvp);
2319 #else
2320 (void)vnode_pager_uncache(tvp);
2321 #endif
2322 VOP_LEASE(tvp, p, p->p_ucred, LEASE_WRITE);
2323 }
2324 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
2325 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
2326 } else {
2327 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
2328 if (tdvp == tvp)
2329 vrele(tdvp);
2330 else
2331 vput(tdvp);
2332 if (tvp)
2333 vput(tvp);
2334 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
2335 vrele(fromnd.ni_dvp);
2336 vrele(fvp);
2337 }
2338 vrele(tond.ni_startdir);
2339 FREE(tond.ni_cnd.cn_pnbuf, M_NAMEI);
2340 out1:
2341 if (fromnd.ni_startdir)
2342 vrele(fromnd.ni_startdir);
2343 FREE(fromnd.ni_cnd.cn_pnbuf, M_NAMEI);
2344 return (error == -1 ? 0 : error);
2345 }
2346
2347 /*
2348 * Make a directory file.
2349 */
2350 /* ARGSUSED */
2351 int
2352 sys_mkdir(p, v, retval)
2353 struct proc *p;
2354 void *v;
2355 register_t *retval;
2356 {
2357 register struct sys_mkdir_args /* {
2358 syscallarg(const char *) path;
2359 syscallarg(int) mode;
2360 } */ *uap = v;
2361 register struct vnode *vp;
2362 struct vattr vattr;
2363 int error;
2364 struct nameidata nd;
2365
2366 NDINIT(&nd, CREATE, LOCKPARENT, UIO_USERSPACE, SCARG(uap, path), p);
2367 if ((error = namei(&nd)) != 0)
2368 return (error);
2369 vp = nd.ni_vp;
2370 if (vp != NULL) {
2371 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2372 if (nd.ni_dvp == vp)
2373 vrele(nd.ni_dvp);
2374 else
2375 vput(nd.ni_dvp);
2376 vrele(vp);
2377 return (EEXIST);
2378 }
2379 VATTR_NULL(&vattr);
2380 vattr.va_type = VDIR;
2381 vattr.va_mode = (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_fd->fd_cmask;
2382 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2383 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
2384 if (!error)
2385 vput(nd.ni_vp);
2386 return (error);
2387 }
2388
2389 /*
2390 * Remove a directory file.
2391 */
2392 /* ARGSUSED */
2393 int
2394 sys_rmdir(p, v, retval)
2395 struct proc *p;
2396 void *v;
2397 register_t *retval;
2398 {
2399 struct sys_rmdir_args /* {
2400 syscallarg(const char *) path;
2401 } */ *uap = v;
2402 register struct vnode *vp;
2403 int error;
2404 struct nameidata nd;
2405
2406 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
2407 SCARG(uap, path), p);
2408 if ((error = namei(&nd)) != 0)
2409 return (error);
2410 vp = nd.ni_vp;
2411 if (vp->v_type != VDIR) {
2412 error = ENOTDIR;
2413 goto out;
2414 }
2415 /*
2416 * No rmdir "." please.
2417 */
2418 if (nd.ni_dvp == vp) {
2419 error = EINVAL;
2420 goto out;
2421 }
2422 /*
2423 * The root of a mounted filesystem cannot be deleted.
2424 */
2425 if (vp->v_flag & VROOT)
2426 error = EBUSY;
2427 out:
2428 if (!error) {
2429 VOP_LEASE(nd.ni_dvp, p, p->p_ucred, LEASE_WRITE);
2430 VOP_LEASE(vp, p, p->p_ucred, LEASE_WRITE);
2431 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2432 } else {
2433 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2434 if (nd.ni_dvp == vp)
2435 vrele(nd.ni_dvp);
2436 else
2437 vput(nd.ni_dvp);
2438 vput(vp);
2439 }
2440 return (error);
2441 }
2442
2443 /*
2444 * Read a block of directory entries in a file system independent format.
2445 */
2446 int
2447 sys_getdents(p, v, retval)
2448 struct proc *p;
2449 void *v;
2450 register_t *retval;
2451 {
2452 register struct sys_getdents_args /* {
2453 syscallarg(int) fd;
2454 syscallarg(char *) buf;
2455 syscallarg(size_t) count;
2456 } */ *uap = v;
2457 struct file *fp;
2458 int error, done;
2459
2460 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2461 return (error);
2462 if ((fp->f_flag & FREAD) == 0)
2463 return (EBADF);
2464 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
2465 SCARG(uap, count), &done, p, 0, 0);
2466 *retval = done;
2467 return (error);
2468 }
2469
2470 /*
2471 * Set the mode mask for creation of filesystem nodes.
2472 */
2473 int
2474 sys_umask(p, v, retval)
2475 struct proc *p;
2476 void *v;
2477 register_t *retval;
2478 {
2479 struct sys_umask_args /* {
2480 syscallarg(mode_t) newmask;
2481 } */ *uap = v;
2482 register struct filedesc *fdp;
2483
2484 fdp = p->p_fd;
2485 *retval = fdp->fd_cmask;
2486 fdp->fd_cmask = SCARG(uap, newmask) & ALLPERMS;
2487 return (0);
2488 }
2489
2490 /*
2491 * Void all references to file by ripping underlying filesystem
2492 * away from vnode.
2493 */
2494 /* ARGSUSED */
2495 int
2496 sys_revoke(p, v, retval)
2497 struct proc *p;
2498 void *v;
2499 register_t *retval;
2500 {
2501 register struct sys_revoke_args /* {
2502 syscallarg(const char *) path;
2503 } */ *uap = v;
2504 register struct vnode *vp;
2505 struct vattr vattr;
2506 int error;
2507 struct nameidata nd;
2508
2509 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
2510 if ((error = namei(&nd)) != 0)
2511 return (error);
2512 vp = nd.ni_vp;
2513 if ((error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) != 0)
2514 goto out;
2515 if (p->p_ucred->cr_uid != vattr.va_uid &&
2516 (error = suser(p->p_ucred, &p->p_acflag)) != 0)
2517 goto out;
2518 if (vp->v_usecount > 1 || (vp->v_flag & VALIASED))
2519 VOP_REVOKE(vp, REVOKEALL);
2520 out:
2521 vrele(vp);
2522 return (error);
2523 }
2524
2525 /*
2526 * Convert a user file descriptor to a kernel file entry.
2527 */
2528 int
2529 getvnode(fdp, fd, fpp)
2530 struct filedesc *fdp;
2531 int fd;
2532 struct file **fpp;
2533 {
2534 struct vnode *vp;
2535 struct file *fp;
2536
2537 if ((u_int)fd >= fdp->fd_nfiles ||
2538 (fp = fdp->fd_ofiles[fd]) == NULL)
2539 return (EBADF);
2540 if (fp->f_type != DTYPE_VNODE)
2541 return (EINVAL);
2542 vp = (struct vnode *)fp->f_data;
2543 if (vp->v_type == VBAD)
2544 return (EBADF);
2545 *fpp = fp;
2546 return (0);
2547 }
2548