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