vfs_syscalls.c revision 1.238.4.3 1 /* $NetBSD: vfs_syscalls.c,v 1.238.4.3 2006/04/19 05:14:00 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.3 2006/04/19 05:14:00 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 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 kauth_cred_t cred = p->p_cred;
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 = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
1267 &p->p_acflag)))
1268 return (error);
1269
1270 flags = FFLAGS(SCARG(uap, flags));
1271 if ((flags & (FREAD | FWRITE)) == 0)
1272 return (EINVAL);
1273 if ((flags & O_CREAT))
1274 return (EINVAL);
1275 /* falloc() will use the file descriptor for us */
1276 if ((error = falloc(p, &nfp, &indx)) != 0)
1277 return (error);
1278 fp = nfp;
1279 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1280 goto bad;
1281
1282 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL) {
1283 error = ESTALE;
1284 goto bad;
1285 }
1286
1287 if (mp->mnt_op->vfs_fhtovp == NULL) {
1288 error = EOPNOTSUPP;
1289 goto bad;
1290 }
1291
1292 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)) != 0) {
1293 vp = NULL; /* most likely unnecessary sanity for bad: */
1294 goto bad;
1295 }
1296
1297 /* Now do an effective vn_open */
1298
1299 if (vp->v_type == VSOCK) {
1300 error = EOPNOTSUPP;
1301 goto bad;
1302 }
1303 if (flags & FREAD) {
1304 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
1305 goto bad;
1306 }
1307 if (flags & (FWRITE | O_TRUNC)) {
1308 if (vp->v_type == VDIR) {
1309 error = EISDIR;
1310 goto bad;
1311 }
1312 if ((error = vn_writechk(vp)) != 0 ||
1313 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
1314 goto bad;
1315 }
1316 if (flags & O_TRUNC) {
1317 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
1318 goto bad;
1319 VOP_UNLOCK(vp, 0); /* XXX */
1320 VOP_LEASE(vp, l, cred, LEASE_WRITE);
1321 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1322 VATTR_NULL(&va);
1323 va.va_size = 0;
1324 error = VOP_SETATTR(vp, &va, cred, l);
1325 vn_finished_write(mp, 0);
1326 if (error)
1327 goto bad;
1328 }
1329 if ((error = VOP_OPEN(vp, flags, cred, l)) != 0)
1330 goto bad;
1331 if (vp->v_type == VREG &&
1332 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
1333 error = EIO;
1334 goto bad;
1335 }
1336 if (flags & FWRITE)
1337 vp->v_writecount++;
1338
1339 /* done with modified vn_open, now finish what sys_open does. */
1340
1341 fp->f_flag = flags & FMASK;
1342 fp->f_type = DTYPE_VNODE;
1343 fp->f_ops = &vnops;
1344 fp->f_data = vp;
1345 if (flags & (O_EXLOCK | O_SHLOCK)) {
1346 lf.l_whence = SEEK_SET;
1347 lf.l_start = 0;
1348 lf.l_len = 0;
1349 if (flags & O_EXLOCK)
1350 lf.l_type = F_WRLCK;
1351 else
1352 lf.l_type = F_RDLCK;
1353 type = F_FLOCK;
1354 if ((flags & FNONBLOCK) == 0)
1355 type |= F_WAIT;
1356 VOP_UNLOCK(vp, 0);
1357 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1358 if (error) {
1359 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1360 FILE_UNUSE(fp, l);
1361 ffree(fp);
1362 fdremove(fdp, indx);
1363 return (error);
1364 }
1365 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1366 fp->f_flag |= FHASLOCK;
1367 }
1368 VOP_UNLOCK(vp, 0);
1369 *retval = indx;
1370 FILE_SET_MATURE(fp);
1371 FILE_UNUSE(fp, l);
1372 return (0);
1373
1374 bad:
1375 FILE_UNUSE(fp, l);
1376 ffree(fp);
1377 fdremove(fdp, indx);
1378 if (vp != NULL)
1379 vput(vp);
1380 return (error);
1381 }
1382
1383 /* ARGSUSED */
1384 int
1385 sys_fhstat(struct lwp *l, void *v, register_t *retval)
1386 {
1387 struct sys_fhstat_args /* {
1388 syscallarg(const fhandle_t *) fhp;
1389 syscallarg(struct stat *) sb;
1390 } */ *uap = v;
1391 struct proc *p = l->l_proc;
1392 struct stat sb;
1393 int error;
1394 fhandle_t fh;
1395 struct mount *mp;
1396 struct vnode *vp;
1397
1398 /*
1399 * Must be super user
1400 */
1401 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
1402 &p->p_acflag)))
1403 return (error);
1404
1405 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1406 return (error);
1407
1408 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1409 return (ESTALE);
1410 if (mp->mnt_op->vfs_fhtovp == NULL)
1411 return EOPNOTSUPP;
1412 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1413 return (error);
1414 error = vn_stat(vp, &sb, l);
1415 vput(vp);
1416 if (error)
1417 return (error);
1418 error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
1419 return (error);
1420 }
1421
1422 /* ARGSUSED */
1423 int
1424 sys_fhstatvfs1(struct lwp *l, void *v, register_t *retval)
1425 {
1426 struct sys_fhstatvfs1_args /* {
1427 syscallarg(const fhandle_t *) fhp;
1428 syscallarg(struct statvfs *) buf;
1429 syscallarg(int) flags;
1430 } */ *uap = v;
1431 struct proc *p = l->l_proc;
1432 struct statvfs sbuf;
1433 fhandle_t fh;
1434 struct mount *mp;
1435 struct vnode *vp;
1436 int error;
1437
1438 /*
1439 * Must be super user
1440 */
1441 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
1442 &p->p_acflag)) != 0)
1443 return error;
1444
1445 if ((error = copyin(SCARG(uap, fhp), &fh, sizeof(fhandle_t))) != 0)
1446 return error;
1447
1448 if ((mp = vfs_getvfs(&fh.fh_fsid)) == NULL)
1449 return ESTALE;
1450 if (mp->mnt_op->vfs_fhtovp == NULL)
1451 return EOPNOTSUPP;
1452 if ((error = VFS_FHTOVP(mp, &fh.fh_fid, &vp)))
1453 return error;
1454
1455 mp = vp->v_mount;
1456 if ((error = dostatvfs(mp, &sbuf, l, SCARG(uap, flags), 1)) != 0) {
1457 vput(vp);
1458 return error;
1459 }
1460 vput(vp);
1461 return copyout(&sbuf, SCARG(uap, buf), sizeof(sbuf));
1462 }
1463
1464 /*
1465 * Create a special file.
1466 */
1467 /* ARGSUSED */
1468 int
1469 sys_mknod(struct lwp *l, void *v, register_t *retval)
1470 {
1471 struct sys_mknod_args /* {
1472 syscallarg(const char *) path;
1473 syscallarg(int) mode;
1474 syscallarg(int) dev;
1475 } */ *uap = v;
1476 struct proc *p = l->l_proc;
1477 struct vnode *vp;
1478 struct mount *mp;
1479 struct vattr vattr;
1480 int error;
1481 int whiteout = 0;
1482 struct nameidata nd;
1483
1484 if ((error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
1485 &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_cred, 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_cred, 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_cred, LEASE_WRITE);
1651 VOP_LEASE(vp, l, p->p_cred, 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_cred, 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_cred, 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_cred, LEASE_WRITE);
1833 VOP_LEASE(vp, l, p->p_cred, 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 kauth_cred_t cred = p->p_cred;
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 kauth_cred_t cred;
2122 struct vnode *vp;
2123 int error, flags;
2124 struct nameidata nd;
2125
2126 cred = kauth_cred_dup(p->p_cred);
2127 kauth_cred_seteuid(cred, kauth_cred_getuid(p->p_cred));
2128 kauth_cred_setegid(cred, kauth_cred_getgid(p->p_cred));
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 kauth_cred_free(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_cred, 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_cred);
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_cred, 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 (kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
2372 &p->p_acflag) != 0) {
2373 if ((error = VOP_GETATTR(vp, &vattr, p->p_cred, l)) != 0)
2374 goto out;
2375 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2376 error = EINVAL;
2377 goto out;
2378 }
2379 }
2380 VATTR_NULL(&vattr);
2381 vattr.va_flags = flags;
2382 error = VOP_SETATTR(vp, &vattr, p->p_cred, l);
2383 out:
2384 vn_finished_write(mp, 0);
2385 return (error);
2386 }
2387
2388 /*
2389 * Change mode of a file given path name; this version follows links.
2390 */
2391 /* ARGSUSED */
2392 int
2393 sys_chmod(struct lwp *l, void *v, register_t *retval)
2394 {
2395 struct sys_chmod_args /* {
2396 syscallarg(const char *) path;
2397 syscallarg(int) mode;
2398 } */ *uap = v;
2399 int error;
2400 struct nameidata nd;
2401
2402 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2403 if ((error = namei(&nd)) != 0)
2404 return (error);
2405
2406 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2407
2408 vrele(nd.ni_vp);
2409 return (error);
2410 }
2411
2412 /*
2413 * Change mode of a file given a file descriptor.
2414 */
2415 /* ARGSUSED */
2416 int
2417 sys_fchmod(struct lwp *l, void *v, register_t *retval)
2418 {
2419 struct sys_fchmod_args /* {
2420 syscallarg(int) fd;
2421 syscallarg(int) mode;
2422 } */ *uap = v;
2423 struct proc *p = l->l_proc;
2424 struct file *fp;
2425 int error;
2426
2427 /* getvnode() will use the descriptor for us */
2428 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2429 return (error);
2430
2431 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), l);
2432 FILE_UNUSE(fp, l);
2433 return (error);
2434 }
2435
2436 /*
2437 * Change mode of a file given path name; this version does not follow links.
2438 */
2439 /* ARGSUSED */
2440 int
2441 sys_lchmod(struct lwp *l, void *v, register_t *retval)
2442 {
2443 struct sys_lchmod_args /* {
2444 syscallarg(const char *) path;
2445 syscallarg(int) mode;
2446 } */ *uap = v;
2447 int error;
2448 struct nameidata nd;
2449
2450 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2451 if ((error = namei(&nd)) != 0)
2452 return (error);
2453
2454 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2455
2456 vrele(nd.ni_vp);
2457 return (error);
2458 }
2459
2460 /*
2461 * Common routine to set mode given a vnode.
2462 */
2463 static int
2464 change_mode(struct vnode *vp, int mode, struct lwp *l)
2465 {
2466 struct proc *p = l->l_proc;
2467 struct mount *mp;
2468 struct vattr vattr;
2469 int error;
2470
2471 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2472 return (error);
2473 VOP_LEASE(vp, l, p->p_cred, LEASE_WRITE);
2474 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2475 VATTR_NULL(&vattr);
2476 vattr.va_mode = mode & ALLPERMS;
2477 error = VOP_SETATTR(vp, &vattr, p->p_cred, l);
2478 VOP_UNLOCK(vp, 0);
2479 vn_finished_write(mp, 0);
2480 return (error);
2481 }
2482
2483 /*
2484 * Set ownership given a path name; this version follows links.
2485 */
2486 /* ARGSUSED */
2487 int
2488 sys_chown(struct lwp *l, void *v, register_t *retval)
2489 {
2490 struct sys_chown_args /* {
2491 syscallarg(const char *) path;
2492 syscallarg(uid_t) uid;
2493 syscallarg(gid_t) gid;
2494 } */ *uap = v;
2495 int error;
2496 struct nameidata nd;
2497
2498 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2499 if ((error = namei(&nd)) != 0)
2500 return (error);
2501
2502 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2503
2504 vrele(nd.ni_vp);
2505 return (error);
2506 }
2507
2508 /*
2509 * Set ownership given a path name; this version follows links.
2510 * Provides POSIX semantics.
2511 */
2512 /* ARGSUSED */
2513 int
2514 sys___posix_chown(struct lwp *l, void *v, register_t *retval)
2515 {
2516 struct sys_chown_args /* {
2517 syscallarg(const char *) path;
2518 syscallarg(uid_t) uid;
2519 syscallarg(gid_t) gid;
2520 } */ *uap = v;
2521 int error;
2522 struct nameidata nd;
2523
2524 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2525 if ((error = namei(&nd)) != 0)
2526 return (error);
2527
2528 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2529
2530 vrele(nd.ni_vp);
2531 return (error);
2532 }
2533
2534 /*
2535 * Set ownership given a file descriptor.
2536 */
2537 /* ARGSUSED */
2538 int
2539 sys_fchown(struct lwp *l, void *v, register_t *retval)
2540 {
2541 struct sys_fchown_args /* {
2542 syscallarg(int) fd;
2543 syscallarg(uid_t) uid;
2544 syscallarg(gid_t) gid;
2545 } */ *uap = v;
2546 struct proc *p = l->l_proc;
2547 int error;
2548 struct file *fp;
2549
2550 /* getvnode() will use the descriptor for us */
2551 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2552 return (error);
2553
2554 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2555 SCARG(uap, gid), l, 0);
2556 FILE_UNUSE(fp, l);
2557 return (error);
2558 }
2559
2560 /*
2561 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2562 */
2563 /* ARGSUSED */
2564 int
2565 sys___posix_fchown(struct lwp *l, void *v, register_t *retval)
2566 {
2567 struct sys_fchown_args /* {
2568 syscallarg(int) fd;
2569 syscallarg(uid_t) uid;
2570 syscallarg(gid_t) gid;
2571 } */ *uap = v;
2572 struct proc *p = l->l_proc;
2573 int error;
2574 struct file *fp;
2575
2576 /* getvnode() will use the descriptor for us */
2577 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2578 return (error);
2579
2580 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2581 SCARG(uap, gid), l, 1);
2582 FILE_UNUSE(fp, l);
2583 return (error);
2584 }
2585
2586 /*
2587 * Set ownership given a path name; this version does not follow links.
2588 */
2589 /* ARGSUSED */
2590 int
2591 sys_lchown(struct lwp *l, void *v, register_t *retval)
2592 {
2593 struct sys_lchown_args /* {
2594 syscallarg(const char *) path;
2595 syscallarg(uid_t) uid;
2596 syscallarg(gid_t) gid;
2597 } */ *uap = v;
2598 int error;
2599 struct nameidata nd;
2600
2601 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2602 if ((error = namei(&nd)) != 0)
2603 return (error);
2604
2605 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2606
2607 vrele(nd.ni_vp);
2608 return (error);
2609 }
2610
2611 /*
2612 * Set ownership given a path name; this version does not follow links.
2613 * Provides POSIX/XPG semantics.
2614 */
2615 /* ARGSUSED */
2616 int
2617 sys___posix_lchown(struct lwp *l, void *v, register_t *retval)
2618 {
2619 struct sys_lchown_args /* {
2620 syscallarg(const char *) path;
2621 syscallarg(uid_t) uid;
2622 syscallarg(gid_t) gid;
2623 } */ *uap = v;
2624 int error;
2625 struct nameidata nd;
2626
2627 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2628 if ((error = namei(&nd)) != 0)
2629 return (error);
2630
2631 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2632
2633 vrele(nd.ni_vp);
2634 return (error);
2635 }
2636
2637 /*
2638 * Common routine to set ownership given a vnode.
2639 */
2640 static int
2641 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2642 int posix_semantics)
2643 {
2644 struct proc *p = l->l_proc;
2645 struct mount *mp;
2646 struct vattr vattr;
2647 mode_t newmode;
2648 int error;
2649
2650 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2651 return (error);
2652 VOP_LEASE(vp, l, p->p_cred, LEASE_WRITE);
2653 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2654 if ((error = VOP_GETATTR(vp, &vattr, p->p_cred, l)) != 0)
2655 goto out;
2656
2657 #define CHANGED(x) ((int)(x) != -1)
2658 newmode = vattr.va_mode;
2659 if (posix_semantics) {
2660 /*
2661 * POSIX/XPG semantics: if the caller is not the super-user,
2662 * clear set-user-id and set-group-id bits. Both POSIX and
2663 * the XPG consider the behaviour for calls by the super-user
2664 * implementation-defined; we leave the set-user-id and set-
2665 * group-id settings intact in that case.
2666 */
2667 if (kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
2668 NULL) != 0)
2669 newmode &= ~(S_ISUID | S_ISGID);
2670 } else {
2671 /*
2672 * NetBSD semantics: when changing owner and/or group,
2673 * clear the respective bit(s).
2674 */
2675 if (CHANGED(uid))
2676 newmode &= ~S_ISUID;
2677 if (CHANGED(gid))
2678 newmode &= ~S_ISGID;
2679 }
2680 /* Update va_mode iff altered. */
2681 if (vattr.va_mode == newmode)
2682 newmode = VNOVAL;
2683
2684 VATTR_NULL(&vattr);
2685 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2686 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2687 vattr.va_mode = newmode;
2688 error = VOP_SETATTR(vp, &vattr, p->p_cred, l);
2689 #undef CHANGED
2690
2691 out:
2692 VOP_UNLOCK(vp, 0);
2693 vn_finished_write(mp, 0);
2694 return (error);
2695 }
2696
2697 /*
2698 * Set the access and modification times given a path name; this
2699 * version follows links.
2700 */
2701 /* ARGSUSED */
2702 int
2703 sys_utimes(struct lwp *l, void *v, register_t *retval)
2704 {
2705 struct sys_utimes_args /* {
2706 syscallarg(const char *) path;
2707 syscallarg(const struct timeval *) tptr;
2708 } */ *uap = v;
2709 int error;
2710 struct nameidata nd;
2711
2712 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2713 if ((error = namei(&nd)) != 0)
2714 return (error);
2715
2716 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), l);
2717
2718 vrele(nd.ni_vp);
2719 return (error);
2720 }
2721
2722 /*
2723 * Set the access and modification times given a file descriptor.
2724 */
2725 /* ARGSUSED */
2726 int
2727 sys_futimes(struct lwp *l, void *v, register_t *retval)
2728 {
2729 struct sys_futimes_args /* {
2730 syscallarg(int) fd;
2731 syscallarg(const struct timeval *) tptr;
2732 } */ *uap = v;
2733 struct proc *p = l->l_proc;
2734 int error;
2735 struct file *fp;
2736
2737 /* getvnode() will use the descriptor for us */
2738 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2739 return (error);
2740
2741 error = change_utimes((struct vnode *)fp->f_data, SCARG(uap, tptr), l);
2742 FILE_UNUSE(fp, l);
2743 return (error);
2744 }
2745
2746 /*
2747 * Set the access and modification times given a path name; this
2748 * version does not follow links.
2749 */
2750 /* ARGSUSED */
2751 int
2752 sys_lutimes(struct lwp *l, void *v, register_t *retval)
2753 {
2754 struct sys_lutimes_args /* {
2755 syscallarg(const char *) path;
2756 syscallarg(const struct timeval *) tptr;
2757 } */ *uap = v;
2758 int error;
2759 struct nameidata nd;
2760
2761 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2762 if ((error = namei(&nd)) != 0)
2763 return (error);
2764
2765 error = change_utimes(nd.ni_vp, SCARG(uap, tptr), l);
2766
2767 vrele(nd.ni_vp);
2768 return (error);
2769 }
2770
2771 /*
2772 * Common routine to set access and modification times given a vnode.
2773 */
2774 static int
2775 change_utimes(struct vnode *vp, const struct timeval *tptr, struct lwp *l)
2776 {
2777 struct proc *p = l->l_proc;
2778 struct mount *mp;
2779 struct vattr vattr;
2780 int error;
2781
2782 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2783 return (error);
2784 VATTR_NULL(&vattr);
2785 if (tptr == NULL) {
2786 nanotime(&vattr.va_atime);
2787 vattr.va_mtime = vattr.va_atime;
2788 vattr.va_vaflags |= VA_UTIMES_NULL;
2789 } else {
2790 struct timeval tv[2];
2791
2792 error = copyin(tptr, tv, sizeof(tv));
2793 if (error)
2794 goto out;
2795 TIMEVAL_TO_TIMESPEC(&tv[0], &vattr.va_atime);
2796 TIMEVAL_TO_TIMESPEC(&tv[1], &vattr.va_mtime);
2797 }
2798 VOP_LEASE(vp, l, p->p_cred, LEASE_WRITE);
2799 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2800 error = VOP_SETATTR(vp, &vattr, p->p_cred, l);
2801 VOP_UNLOCK(vp, 0);
2802 out:
2803 vn_finished_write(mp, 0);
2804 return (error);
2805 }
2806
2807 /*
2808 * Truncate a file given its path name.
2809 */
2810 /* ARGSUSED */
2811 int
2812 sys_truncate(struct lwp *l, void *v, register_t *retval)
2813 {
2814 struct sys_truncate_args /* {
2815 syscallarg(const char *) path;
2816 syscallarg(int) pad;
2817 syscallarg(off_t) length;
2818 } */ *uap = v;
2819 struct proc *p = l->l_proc;
2820 struct vnode *vp;
2821 struct mount *mp;
2822 struct vattr vattr;
2823 int error;
2824 struct nameidata nd;
2825
2826 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
2827 if ((error = namei(&nd)) != 0)
2828 return (error);
2829 vp = nd.ni_vp;
2830 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
2831 vrele(vp);
2832 return (error);
2833 }
2834 VOP_LEASE(vp, l, p->p_cred, LEASE_WRITE);
2835 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2836 if (vp->v_type == VDIR)
2837 error = EISDIR;
2838 else if ((error = vn_writechk(vp)) == 0 &&
2839 (error = VOP_ACCESS(vp, VWRITE, p->p_cred, l)) == 0) {
2840 VATTR_NULL(&vattr);
2841 vattr.va_size = SCARG(uap, length);
2842 error = VOP_SETATTR(vp, &vattr, p->p_cred, l);
2843 }
2844 vput(vp);
2845 vn_finished_write(mp, 0);
2846 return (error);
2847 }
2848
2849 /*
2850 * Truncate a file given a file descriptor.
2851 */
2852 /* ARGSUSED */
2853 int
2854 sys_ftruncate(struct lwp *l, void *v, register_t *retval)
2855 {
2856 struct sys_ftruncate_args /* {
2857 syscallarg(int) fd;
2858 syscallarg(int) pad;
2859 syscallarg(off_t) length;
2860 } */ *uap = v;
2861 struct proc *p = l->l_proc;
2862 struct mount *mp;
2863 struct vattr vattr;
2864 struct vnode *vp;
2865 struct file *fp;
2866 int error;
2867
2868 /* getvnode() will use the descriptor for us */
2869 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2870 return (error);
2871 if ((fp->f_flag & FWRITE) == 0) {
2872 error = EINVAL;
2873 goto out;
2874 }
2875 vp = (struct vnode *)fp->f_data;
2876 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
2877 FILE_UNUSE(fp, l);
2878 return (error);
2879 }
2880 VOP_LEASE(vp, l, p->p_cred, LEASE_WRITE);
2881 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2882 if (vp->v_type == VDIR)
2883 error = EISDIR;
2884 else if ((error = vn_writechk(vp)) == 0) {
2885 VATTR_NULL(&vattr);
2886 vattr.va_size = SCARG(uap, length);
2887 error = VOP_SETATTR(vp, &vattr, fp->f_cred, l);
2888 }
2889 VOP_UNLOCK(vp, 0);
2890 vn_finished_write(mp, 0);
2891 out:
2892 FILE_UNUSE(fp, l);
2893 return (error);
2894 }
2895
2896 /*
2897 * Sync an open file.
2898 */
2899 /* ARGSUSED */
2900 int
2901 sys_fsync(struct lwp *l, void *v, register_t *retval)
2902 {
2903 struct sys_fsync_args /* {
2904 syscallarg(int) fd;
2905 } */ *uap = v;
2906 struct proc *p = l->l_proc;
2907 struct vnode *vp;
2908 struct mount *mp;
2909 struct file *fp;
2910 int error;
2911
2912 /* getvnode() will use the descriptor for us */
2913 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2914 return (error);
2915 vp = (struct vnode *)fp->f_data;
2916 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
2917 FILE_UNUSE(fp, l);
2918 return (error);
2919 }
2920 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2921 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, l);
2922 if (error == 0 && bioops.io_fsync != NULL &&
2923 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
2924 (*bioops.io_fsync)(vp, 0);
2925 VOP_UNLOCK(vp, 0);
2926 vn_finished_write(mp, 0);
2927 FILE_UNUSE(fp, l);
2928 return (error);
2929 }
2930
2931 /*
2932 * Sync a range of file data. API modeled after that found in AIX.
2933 *
2934 * FDATASYNC indicates that we need only save enough metadata to be able
2935 * to re-read the written data. Note we duplicate AIX's requirement that
2936 * the file be open for writing.
2937 */
2938 /* ARGSUSED */
2939 int
2940 sys_fsync_range(struct lwp *l, void *v, register_t *retval)
2941 {
2942 struct sys_fsync_range_args /* {
2943 syscallarg(int) fd;
2944 syscallarg(int) flags;
2945 syscallarg(off_t) start;
2946 syscallarg(off_t) length;
2947 } */ *uap = v;
2948 struct proc *p = l->l_proc;
2949 struct vnode *vp;
2950 struct file *fp;
2951 int flags, nflags;
2952 off_t s, e, len;
2953 int error;
2954
2955 /* getvnode() will use the descriptor for us */
2956 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2957 return (error);
2958
2959 if ((fp->f_flag & FWRITE) == 0) {
2960 FILE_UNUSE(fp, l);
2961 return (EBADF);
2962 }
2963
2964 flags = SCARG(uap, flags);
2965 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
2966 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
2967 return (EINVAL);
2968 }
2969 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
2970 if (flags & FDATASYNC)
2971 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
2972 else
2973 nflags = FSYNC_WAIT;
2974 if (flags & FDISKSYNC)
2975 nflags |= FSYNC_CACHE;
2976
2977 len = SCARG(uap, length);
2978 /* If length == 0, we do the whole file, and s = l = 0 will do that */
2979 if (len) {
2980 s = SCARG(uap, start);
2981 e = s + len;
2982 if (e < s) {
2983 FILE_UNUSE(fp, l);
2984 return (EINVAL);
2985 }
2986 } else {
2987 e = 0;
2988 s = 0;
2989 }
2990
2991 vp = (struct vnode *)fp->f_data;
2992 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2993 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, l);
2994
2995 if (error == 0 && bioops.io_fsync != NULL &&
2996 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
2997 (*bioops.io_fsync)(vp, nflags);
2998
2999 VOP_UNLOCK(vp, 0);
3000 FILE_UNUSE(fp, l);
3001 return (error);
3002 }
3003
3004 /*
3005 * Sync the data of an open file.
3006 */
3007 /* ARGSUSED */
3008 int
3009 sys_fdatasync(struct lwp *l, void *v, register_t *retval)
3010 {
3011 struct sys_fdatasync_args /* {
3012 syscallarg(int) fd;
3013 } */ *uap = v;
3014 struct proc *p = l->l_proc;
3015 struct vnode *vp;
3016 struct file *fp;
3017 int error;
3018
3019 /* getvnode() will use the descriptor for us */
3020 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3021 return (error);
3022 if ((fp->f_flag & FWRITE) == 0) {
3023 FILE_UNUSE(fp, l);
3024 return (EBADF);
3025 }
3026 vp = (struct vnode *)fp->f_data;
3027 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3028 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, l);
3029 VOP_UNLOCK(vp, 0);
3030 FILE_UNUSE(fp, l);
3031 return (error);
3032 }
3033
3034 /*
3035 * Rename files, (standard) BSD semantics frontend.
3036 */
3037 /* ARGSUSED */
3038 int
3039 sys_rename(struct lwp *l, void *v, register_t *retval)
3040 {
3041 struct sys_rename_args /* {
3042 syscallarg(const char *) from;
3043 syscallarg(const char *) to;
3044 } */ *uap = v;
3045
3046 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 0));
3047 }
3048
3049 /*
3050 * Rename files, POSIX semantics frontend.
3051 */
3052 /* ARGSUSED */
3053 int
3054 sys___posix_rename(struct lwp *l, void *v, register_t *retval)
3055 {
3056 struct sys___posix_rename_args /* {
3057 syscallarg(const char *) from;
3058 syscallarg(const char *) to;
3059 } */ *uap = v;
3060
3061 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 1));
3062 }
3063
3064 /*
3065 * Rename files. Source and destination must either both be directories,
3066 * or both not be directories. If target is a directory, it must be empty.
3067 * If `from' and `to' refer to the same object, the value of the `retain'
3068 * argument is used to determine whether `from' will be
3069 *
3070 * (retain == 0) deleted unless `from' and `to' refer to the same
3071 * object in the file system's name space (BSD).
3072 * (retain == 1) always retained (POSIX).
3073 */
3074 static int
3075 rename_files(const char *from, const char *to, struct lwp *l, int retain)
3076 {
3077 struct mount *mp = NULL;
3078 struct vnode *tvp, *fvp, *tdvp;
3079 struct nameidata fromnd, tond;
3080 struct proc *p;
3081 int error;
3082
3083 NDINIT(&fromnd, DELETE, WANTPARENT | SAVESTART, UIO_USERSPACE,
3084 from, l);
3085 if ((error = namei(&fromnd)) != 0)
3086 return (error);
3087 fvp = fromnd.ni_vp;
3088 error = vn_start_write(fvp, &mp, V_WAIT | V_PCATCH);
3089 if (error != 0) {
3090 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3091 vrele(fromnd.ni_dvp);
3092 vrele(fvp);
3093 if (fromnd.ni_startdir)
3094 vrele(fromnd.ni_startdir);
3095 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3096 return (error);
3097 }
3098 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART |
3099 (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, l);
3100 if ((error = namei(&tond)) != 0) {
3101 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3102 vrele(fromnd.ni_dvp);
3103 vrele(fvp);
3104 goto out1;
3105 }
3106 tdvp = tond.ni_dvp;
3107 tvp = tond.ni_vp;
3108
3109 if (tvp != NULL) {
3110 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3111 error = ENOTDIR;
3112 goto out;
3113 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3114 error = EISDIR;
3115 goto out;
3116 }
3117 }
3118
3119 if (fvp == tdvp)
3120 error = EINVAL;
3121
3122 /*
3123 * Source and destination refer to the same object.
3124 */
3125 if (fvp == tvp) {
3126 if (retain)
3127 error = -1;
3128 else if (fromnd.ni_dvp == tdvp &&
3129 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3130 !memcmp(fromnd.ni_cnd.cn_nameptr,
3131 tond.ni_cnd.cn_nameptr,
3132 fromnd.ni_cnd.cn_namelen))
3133 error = -1;
3134 }
3135
3136 #ifdef VERIFIED_EXEC
3137 if (!error)
3138 error = veriexec_renamechk(fvp, fromnd.ni_dirp, tond.ni_dirp, l);
3139 #endif /* VERIFIED_EXEC */
3140
3141 out:
3142 p = l->l_proc;
3143 if (!error) {
3144 VOP_LEASE(tdvp, l, p->p_cred, LEASE_WRITE);
3145 if (fromnd.ni_dvp != tdvp)
3146 VOP_LEASE(fromnd.ni_dvp, l, p->p_cred, LEASE_WRITE);
3147 if (tvp) {
3148 VOP_LEASE(tvp, l, p->p_cred, LEASE_WRITE);
3149 }
3150 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3151 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3152 } else {
3153 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3154 if (tdvp == tvp)
3155 vrele(tdvp);
3156 else
3157 vput(tdvp);
3158 if (tvp)
3159 vput(tvp);
3160 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3161 vrele(fromnd.ni_dvp);
3162 vrele(fvp);
3163 }
3164 vrele(tond.ni_startdir);
3165 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3166 out1:
3167 vn_finished_write(mp, 0);
3168 if (fromnd.ni_startdir)
3169 vrele(fromnd.ni_startdir);
3170 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3171 return (error == -1 ? 0 : error);
3172 }
3173
3174 /*
3175 * Make a directory file.
3176 */
3177 /* ARGSUSED */
3178 int
3179 sys_mkdir(struct lwp *l, void *v, register_t *retval)
3180 {
3181 struct sys_mkdir_args /* {
3182 syscallarg(const char *) path;
3183 syscallarg(int) mode;
3184 } */ *uap = v;
3185 struct proc *p = l->l_proc;
3186 struct mount *mp;
3187 struct vnode *vp;
3188 struct vattr vattr;
3189 int error;
3190 struct nameidata nd;
3191
3192 restart:
3193 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR, UIO_USERSPACE,
3194 SCARG(uap, path), l);
3195 if ((error = namei(&nd)) != 0)
3196 return (error);
3197 vp = nd.ni_vp;
3198 if (vp != NULL) {
3199 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3200 if (nd.ni_dvp == vp)
3201 vrele(nd.ni_dvp);
3202 else
3203 vput(nd.ni_dvp);
3204 vrele(vp);
3205 return (EEXIST);
3206 }
3207 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3208 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3209 if (nd.ni_dvp == vp)
3210 vrele(nd.ni_dvp);
3211 else
3212 vput(nd.ni_dvp);
3213 if ((error = vn_start_write(NULL, &mp,
3214 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
3215 return (error);
3216 goto restart;
3217 }
3218 VATTR_NULL(&vattr);
3219 vattr.va_type = VDIR;
3220 vattr.va_mode =
3221 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3222 VOP_LEASE(nd.ni_dvp, l, p->p_cred, LEASE_WRITE);
3223 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3224 if (!error)
3225 vput(nd.ni_vp);
3226 vn_finished_write(mp, 0);
3227 return (error);
3228 }
3229
3230 /*
3231 * Remove a directory file.
3232 */
3233 /* ARGSUSED */
3234 int
3235 sys_rmdir(struct lwp *l, void *v, register_t *retval)
3236 {
3237 struct sys_rmdir_args /* {
3238 syscallarg(const char *) path;
3239 } */ *uap = v;
3240 struct proc *p = l->l_proc;
3241 struct mount *mp;
3242 struct vnode *vp;
3243 int error;
3244 struct nameidata nd;
3245
3246 restart:
3247 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE,
3248 SCARG(uap, path), l);
3249 if ((error = namei(&nd)) != 0)
3250 return (error);
3251 vp = nd.ni_vp;
3252 if (vp->v_type != VDIR) {
3253 error = ENOTDIR;
3254 goto out;
3255 }
3256 /*
3257 * No rmdir "." please.
3258 */
3259 if (nd.ni_dvp == vp) {
3260 error = EINVAL;
3261 goto out;
3262 }
3263 /*
3264 * The root of a mounted filesystem cannot be deleted.
3265 */
3266 if (vp->v_flag & VROOT) {
3267 error = EBUSY;
3268 goto out;
3269 }
3270 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3271 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3272 if (nd.ni_dvp == vp)
3273 vrele(nd.ni_dvp);
3274 else
3275 vput(nd.ni_dvp);
3276 vput(vp);
3277 if ((error = vn_start_write(NULL, &mp,
3278 V_WAIT | V_SLEEPONLY | V_PCATCH)) != 0)
3279 return (error);
3280 goto restart;
3281 }
3282 VOP_LEASE(nd.ni_dvp, l, p->p_cred, LEASE_WRITE);
3283 VOP_LEASE(vp, l, p->p_cred, LEASE_WRITE);
3284 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3285 vn_finished_write(mp, 0);
3286 return (error);
3287
3288 out:
3289 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3290 if (nd.ni_dvp == vp)
3291 vrele(nd.ni_dvp);
3292 else
3293 vput(nd.ni_dvp);
3294 vput(vp);
3295 return (error);
3296 }
3297
3298 /*
3299 * Read a block of directory entries in a file system independent format.
3300 */
3301 int
3302 sys___getdents30(struct lwp *l, void *v, register_t *retval)
3303 {
3304 struct sys___getdents30_args /* {
3305 syscallarg(int) fd;
3306 syscallarg(char *) buf;
3307 syscallarg(size_t) count;
3308 } */ *uap = v;
3309 struct proc *p = l->l_proc;
3310 struct file *fp;
3311 int error, done;
3312
3313 /* getvnode() will use the descriptor for us */
3314 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3315 return (error);
3316 if ((fp->f_flag & FREAD) == 0) {
3317 error = EBADF;
3318 goto out;
3319 }
3320 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3321 SCARG(uap, count), &done, l, 0, 0);
3322 #ifdef KTRACE
3323 if (!error && KTRPOINT(p, KTR_GENIO)) {
3324 struct iovec iov;
3325 iov.iov_base = SCARG(uap, buf);
3326 iov.iov_len = done;
3327 ktrgenio(l, SCARG(uap, fd), UIO_READ, &iov, done, 0);
3328 }
3329 #endif
3330 *retval = done;
3331 out:
3332 FILE_UNUSE(fp, l);
3333 return (error);
3334 }
3335
3336 /*
3337 * Set the mode mask for creation of filesystem nodes.
3338 */
3339 int
3340 sys_umask(struct lwp *l, void *v, register_t *retval)
3341 {
3342 struct sys_umask_args /* {
3343 syscallarg(mode_t) newmask;
3344 } */ *uap = v;
3345 struct proc *p = l->l_proc;
3346 struct cwdinfo *cwdi;
3347
3348 cwdi = p->p_cwdi;
3349 *retval = cwdi->cwdi_cmask;
3350 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3351 return (0);
3352 }
3353
3354 /*
3355 * Void all references to file by ripping underlying filesystem
3356 * away from vnode.
3357 */
3358 /* ARGSUSED */
3359 int
3360 sys_revoke(struct lwp *l, void *v, register_t *retval)
3361 {
3362 struct sys_revoke_args /* {
3363 syscallarg(const char *) path;
3364 } */ *uap = v;
3365 struct proc *p = l->l_proc;
3366 struct mount *mp;
3367 struct vnode *vp;
3368 struct vattr vattr;
3369 int error;
3370 struct nameidata nd;
3371
3372 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), l);
3373 if ((error = namei(&nd)) != 0)
3374 return (error);
3375 vp = nd.ni_vp;
3376 if ((error = VOP_GETATTR(vp, &vattr, p->p_cred, l)) != 0)
3377 goto out;
3378 if (kauth_cred_geteuid(p->p_cred) != vattr.va_uid &&
3379 (error = kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER,
3380 &p->p_acflag)) != 0)
3381 goto out;
3382 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3383 goto out;
3384 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3385 VOP_REVOKE(vp, REVOKEALL);
3386 vn_finished_write(mp, 0);
3387 out:
3388 vrele(vp);
3389 return (error);
3390 }
3391
3392 /*
3393 * Convert a user file descriptor to a kernel file entry.
3394 */
3395 int
3396 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3397 {
3398 struct vnode *vp;
3399 struct file *fp;
3400
3401 if ((fp = fd_getfile(fdp, fd)) == NULL)
3402 return (EBADF);
3403
3404 FILE_USE(fp);
3405
3406 if (fp->f_type != DTYPE_VNODE) {
3407 FILE_UNUSE(fp, NULL);
3408 return (EINVAL);
3409 }
3410
3411 vp = (struct vnode *)fp->f_data;
3412 if (vp->v_type == VBAD) {
3413 FILE_UNUSE(fp, NULL);
3414 return (EBADF);
3415 }
3416
3417 *fpp = fp;
3418 return (0);
3419 }
3420