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