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