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