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