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