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