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