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