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