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