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