vfs_syscalls.c revision 1.313 1 /* $NetBSD: vfs_syscalls.c,v 1.313 2007/05/15 19:47:45 elad 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.313 2007/05/15 19:47:45 elad 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 if (!path)
780 return ENOMEM;
781
782 bp = path + MAXPATHLEN;
783 *--bp = '\0';
784 error = getcwd_common(cwdi->cwdi_rdir, rootvnode, &bp, path,
785 MAXPATHLEN / 2, 0, l);
786 if (error) {
787 PNBUF_PUT(path);
788 return error;
789 }
790 len = strlen(bp);
791 /*
792 * for mount points that are below our root, we can see
793 * them, so we fix up the pathname and return them. The
794 * rest we cannot see, so we don't allow viewing the
795 * data.
796 */
797 if (strncmp(bp, sp->f_mntonname, len) == 0) {
798 strlcpy(sp->f_mntonname, &sp->f_mntonname[len],
799 sizeof(sp->f_mntonname));
800 if (sp->f_mntonname[0] == '\0')
801 (void)strlcpy(sp->f_mntonname, "/",
802 sizeof(sp->f_mntonname));
803 } else {
804 if (root)
805 (void)strlcpy(sp->f_mntonname, "/",
806 sizeof(sp->f_mntonname));
807 else
808 error = EPERM;
809 }
810 PNBUF_PUT(path);
811 }
812 sp->f_flag = mp->mnt_flag & MNT_VISFLAGMASK;
813 return error;
814 }
815
816 /*
817 * Get filesystem statistics by path.
818 */
819 int
820 do_sys_pstatvfs(struct lwp *l, const char *path, int flags, struct statvfs *sb)
821 {
822 struct mount *mp;
823 int error;
824 struct nameidata nd;
825
826 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, path, l);
827 if ((error = namei(&nd)) != 0)
828 return error;
829 mp = nd.ni_vp->v_mount;
830 error = dostatvfs(mp, sb, l, flags, 1);
831 vrele(nd.ni_vp);
832 return error;
833 }
834
835 /* ARGSUSED */
836 int
837 sys_statvfs1(struct lwp *l, void *v, register_t *retval)
838 {
839 struct sys_statvfs1_args /* {
840 syscallarg(const char *) path;
841 syscallarg(struct statvfs *) buf;
842 syscallarg(int) flags;
843 } */ *uap = v;
844 struct statvfs *sb;
845 int error;
846
847 sb = STATVFSBUF_GET();
848 error = do_sys_pstatvfs(l, SCARG(uap, path), SCARG(uap, flags), sb);
849 if (error == 0)
850 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
851 STATVFSBUF_PUT(sb);
852 return error;
853 }
854
855 /*
856 * Get filesystem statistics by fd.
857 */
858 int
859 do_sys_fstatvfs(struct lwp *l, int fd, int flags, struct statvfs *sb)
860 {
861 struct proc *p = l->l_proc;
862 struct file *fp;
863 struct mount *mp;
864 int error;
865
866 /* getvnode() will use the descriptor for us */
867 if ((error = getvnode(p->p_fd, fd, &fp)) != 0)
868 return (error);
869 mp = ((struct vnode *)fp->f_data)->v_mount;
870 error = dostatvfs(mp, sb, l, flags, 1);
871 FILE_UNUSE(fp, l);
872 return error;
873 }
874
875 /* ARGSUSED */
876 int
877 sys_fstatvfs1(struct lwp *l, void *v, register_t *retval)
878 {
879 struct sys_fstatvfs1_args /* {
880 syscallarg(int) fd;
881 syscallarg(struct statvfs *) buf;
882 syscallarg(int) flags;
883 } */ *uap = v;
884 struct statvfs *sb;
885 int error;
886
887 sb = STATVFSBUF_GET();
888 error = do_sys_fstatvfs(l, SCARG(uap, fd), SCARG(uap, flags), sb);
889 if (error != 0)
890 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
891 STATVFSBUF_PUT(sb);
892 return error;
893 }
894
895
896 /*
897 * Get statistics on all filesystems.
898 */
899 int
900 do_sys_getvfsstat(struct lwp *l, void *sfsp, size_t bufsize, int flags,
901 int (*copyfn)(const void *, void *, size_t), size_t entry_sz,
902 register_t *retval)
903 {
904 int root = 0;
905 struct proc *p = l->l_proc;
906 struct mount *mp, *nmp;
907 struct statvfs *sb;
908 size_t count, maxcount;
909 int error = 0;
910
911 sb = STATVFSBUF_GET();
912 maxcount = bufsize / entry_sz;
913 simple_lock(&mountlist_slock);
914 count = 0;
915 for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
916 mp = nmp) {
917 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
918 nmp = CIRCLEQ_NEXT(mp, mnt_list);
919 continue;
920 }
921 if (sfsp && count < maxcount) {
922 error = dostatvfs(mp, sb, l, flags, 0);
923 if (error) {
924 simple_lock(&mountlist_slock);
925 nmp = CIRCLEQ_NEXT(mp, mnt_list);
926 vfs_unbusy(mp);
927 continue;
928 }
929 error = copyfn(sb, sfsp, entry_sz);
930 if (error) {
931 vfs_unbusy(mp);
932 goto out;
933 }
934 sfsp = (char *)sfsp + entry_sz;
935 root |= strcmp(sb->f_mntonname, "/") == 0;
936 }
937 count++;
938 simple_lock(&mountlist_slock);
939 nmp = CIRCLEQ_NEXT(mp, mnt_list);
940 vfs_unbusy(mp);
941 }
942 simple_unlock(&mountlist_slock);
943 if (root == 0 && p->p_cwdi->cwdi_rdir) {
944 /*
945 * fake a root entry
946 */
947 error = dostatvfs(p->p_cwdi->cwdi_rdir->v_mount, sb, l, flags, 1);
948 if (error != 0)
949 goto out;
950 if (sfsp)
951 error = copyfn(sb, sfsp, entry_sz);
952 count++;
953 }
954 if (sfsp && count > maxcount)
955 *retval = maxcount;
956 else
957 *retval = count;
958 out:
959 STATVFSBUF_PUT(sb);
960 return error;
961 }
962
963 int
964 sys_getvfsstat(struct lwp *l, void *v, register_t *retval)
965 {
966 struct sys_getvfsstat_args /* {
967 syscallarg(struct statvfs *) buf;
968 syscallarg(size_t) bufsize;
969 syscallarg(int) flags;
970 } */ *uap = v;
971
972 return do_sys_getvfsstat(l, SCARG(uap, buf), SCARG(uap, bufsize),
973 SCARG(uap, flags), copyout, sizeof (struct statvfs), retval);
974 }
975
976 /*
977 * Change current working directory to a given file descriptor.
978 */
979 /* ARGSUSED */
980 int
981 sys_fchdir(struct lwp *l, void *v, register_t *retval)
982 {
983 struct sys_fchdir_args /* {
984 syscallarg(int) fd;
985 } */ *uap = v;
986 struct proc *p = l->l_proc;
987 struct filedesc *fdp = p->p_fd;
988 struct cwdinfo *cwdi = p->p_cwdi;
989 struct vnode *vp, *tdp;
990 struct mount *mp;
991 struct file *fp;
992 int error;
993
994 /* getvnode() will use the descriptor for us */
995 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
996 return (error);
997 vp = (struct vnode *)fp->f_data;
998
999 VREF(vp);
1000 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1001 if (vp->v_type != VDIR)
1002 error = ENOTDIR;
1003 else
1004 error = VOP_ACCESS(vp, VEXEC, l->l_cred, l);
1005 if (error) {
1006 vput(vp);
1007 goto out;
1008 }
1009 while ((mp = vp->v_mountedhere) != NULL) {
1010 if (vfs_busy(mp, 0, 0))
1011 continue;
1012
1013 vput(vp);
1014 error = VFS_ROOT(mp, &tdp);
1015 vfs_unbusy(mp);
1016 if (error)
1017 goto out;
1018 vp = tdp;
1019 }
1020 VOP_UNLOCK(vp, 0);
1021
1022 /*
1023 * Disallow changing to a directory not under the process's
1024 * current root directory (if there is one).
1025 */
1026 if (cwdi->cwdi_rdir && !vn_isunder(vp, NULL, l)) {
1027 vrele(vp);
1028 error = EPERM; /* operation not permitted */
1029 goto out;
1030 }
1031
1032 vrele(cwdi->cwdi_cdir);
1033 cwdi->cwdi_cdir = vp;
1034 out:
1035 FILE_UNUSE(fp, l);
1036 return (error);
1037 }
1038
1039 /*
1040 * Change this process's notion of the root directory to a given file
1041 * descriptor.
1042 */
1043 int
1044 sys_fchroot(struct lwp *l, void *v, register_t *retval)
1045 {
1046 struct sys_fchroot_args *uap = v;
1047 struct proc *p = l->l_proc;
1048 struct filedesc *fdp = p->p_fd;
1049 struct cwdinfo *cwdi = p->p_cwdi;
1050 struct vnode *vp;
1051 struct file *fp;
1052 int error;
1053
1054 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1055 KAUTH_REQ_SYSTEM_CHROOT_FCHROOT, NULL, NULL, NULL)) != 0)
1056 return error;
1057 /* getvnode() will use the descriptor for us */
1058 if ((error = getvnode(fdp, SCARG(uap, fd), &fp)) != 0)
1059 return error;
1060 vp = (struct vnode *) fp->f_data;
1061 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1062 if (vp->v_type != VDIR)
1063 error = ENOTDIR;
1064 else
1065 error = VOP_ACCESS(vp, VEXEC, l->l_cred, l);
1066 VOP_UNLOCK(vp, 0);
1067 if (error)
1068 goto out;
1069 VREF(vp);
1070
1071 /*
1072 * Prevent escaping from chroot by putting the root under
1073 * the working directory. Silently chdir to / if we aren't
1074 * already there.
1075 */
1076 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1077 /*
1078 * XXX would be more failsafe to change directory to a
1079 * deadfs node here instead
1080 */
1081 vrele(cwdi->cwdi_cdir);
1082 VREF(vp);
1083 cwdi->cwdi_cdir = vp;
1084 }
1085
1086 if (cwdi->cwdi_rdir != NULL)
1087 vrele(cwdi->cwdi_rdir);
1088 cwdi->cwdi_rdir = vp;
1089 out:
1090 FILE_UNUSE(fp, l);
1091 return (error);
1092 }
1093
1094 /*
1095 * Change current working directory (``.'').
1096 */
1097 /* ARGSUSED */
1098 int
1099 sys_chdir(struct lwp *l, void *v, register_t *retval)
1100 {
1101 struct sys_chdir_args /* {
1102 syscallarg(const char *) path;
1103 } */ *uap = v;
1104 struct proc *p = l->l_proc;
1105 struct cwdinfo *cwdi = p->p_cwdi;
1106 int error;
1107 struct nameidata nd;
1108
1109 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1110 SCARG(uap, path), l);
1111 if ((error = change_dir(&nd, l)) != 0)
1112 return (error);
1113 vrele(cwdi->cwdi_cdir);
1114 cwdi->cwdi_cdir = nd.ni_vp;
1115 return (0);
1116 }
1117
1118 /*
1119 * Change notion of root (``/'') directory.
1120 */
1121 /* ARGSUSED */
1122 int
1123 sys_chroot(struct lwp *l, void *v, register_t *retval)
1124 {
1125 struct sys_chroot_args /* {
1126 syscallarg(const char *) path;
1127 } */ *uap = v;
1128 struct proc *p = l->l_proc;
1129 struct cwdinfo *cwdi = p->p_cwdi;
1130 struct vnode *vp;
1131 int error;
1132 struct nameidata nd;
1133
1134 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_CHROOT,
1135 KAUTH_REQ_SYSTEM_CHROOT_CHROOT, NULL, NULL, NULL)) != 0)
1136 return (error);
1137 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1138 SCARG(uap, path), l);
1139 if ((error = change_dir(&nd, l)) != 0)
1140 return (error);
1141 if (cwdi->cwdi_rdir != NULL)
1142 vrele(cwdi->cwdi_rdir);
1143 vp = nd.ni_vp;
1144 cwdi->cwdi_rdir = vp;
1145
1146 /*
1147 * Prevent escaping from chroot by putting the root under
1148 * the working directory. Silently chdir to / if we aren't
1149 * already there.
1150 */
1151 if (!vn_isunder(cwdi->cwdi_cdir, vp, l)) {
1152 /*
1153 * XXX would be more failsafe to change directory to a
1154 * deadfs node here instead
1155 */
1156 vrele(cwdi->cwdi_cdir);
1157 VREF(vp);
1158 cwdi->cwdi_cdir = vp;
1159 }
1160
1161 return (0);
1162 }
1163
1164 /*
1165 * Common routine for chroot and chdir.
1166 */
1167 static int
1168 change_dir(struct nameidata *ndp, struct lwp *l)
1169 {
1170 struct vnode *vp;
1171 int error;
1172
1173 if ((error = namei(ndp)) != 0)
1174 return (error);
1175 vp = ndp->ni_vp;
1176 if (vp->v_type != VDIR)
1177 error = ENOTDIR;
1178 else
1179 error = VOP_ACCESS(vp, VEXEC, l->l_cred, l);
1180
1181 if (error)
1182 vput(vp);
1183 else
1184 VOP_UNLOCK(vp, 0);
1185 return (error);
1186 }
1187
1188 /*
1189 * Check permissions, allocate an open file structure,
1190 * and call the device open routine if any.
1191 */
1192 int
1193 sys_open(struct lwp *l, void *v, register_t *retval)
1194 {
1195 struct sys_open_args /* {
1196 syscallarg(const char *) path;
1197 syscallarg(int) flags;
1198 syscallarg(int) mode;
1199 } */ *uap = v;
1200 struct proc *p = l->l_proc;
1201 struct cwdinfo *cwdi = p->p_cwdi;
1202 struct filedesc *fdp = p->p_fd;
1203 struct file *fp;
1204 struct vnode *vp;
1205 int flags, cmode;
1206 int type, indx, error;
1207 struct flock lf;
1208 struct nameidata nd;
1209
1210 flags = FFLAGS(SCARG(uap, flags));
1211 if ((flags & (FREAD | FWRITE)) == 0)
1212 return (EINVAL);
1213 /* falloc() will use the file descriptor for us */
1214 if ((error = falloc(l, &fp, &indx)) != 0)
1215 return (error);
1216 cmode = ((SCARG(uap, mode) &~ cwdi->cwdi_cmask) & ALLPERMS) &~ S_ISTXT;
1217 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1218 l->l_dupfd = -indx - 1; /* XXX check for fdopen */
1219 if ((error = vn_open(&nd, flags, cmode)) != 0) {
1220 FILE_UNUSE(fp, l);
1221 fdp->fd_ofiles[indx] = NULL;
1222 ffree(fp);
1223 if ((error == EDUPFD || error == EMOVEFD) &&
1224 l->l_dupfd >= 0 && /* XXX from fdopen */
1225 (error =
1226 dupfdopen(l, indx, l->l_dupfd, flags, error)) == 0) {
1227 *retval = indx;
1228 return (0);
1229 }
1230 if (error == ERESTART)
1231 error = EINTR;
1232 fdremove(fdp, indx);
1233 return (error);
1234 }
1235 l->l_dupfd = 0;
1236 vp = nd.ni_vp;
1237 fp->f_flag = flags & FMASK;
1238 fp->f_type = DTYPE_VNODE;
1239 fp->f_ops = &vnops;
1240 fp->f_data = vp;
1241 if (flags & (O_EXLOCK | O_SHLOCK)) {
1242 lf.l_whence = SEEK_SET;
1243 lf.l_start = 0;
1244 lf.l_len = 0;
1245 if (flags & O_EXLOCK)
1246 lf.l_type = F_WRLCK;
1247 else
1248 lf.l_type = F_RDLCK;
1249 type = F_FLOCK;
1250 if ((flags & FNONBLOCK) == 0)
1251 type |= F_WAIT;
1252 VOP_UNLOCK(vp, 0);
1253 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1254 if (error) {
1255 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1256 FILE_UNUSE(fp, l);
1257 ffree(fp);
1258 fdremove(fdp, indx);
1259 return (error);
1260 }
1261 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1262 fp->f_flag |= FHASLOCK;
1263 }
1264 VOP_UNLOCK(vp, 0);
1265 *retval = indx;
1266 FILE_SET_MATURE(fp);
1267 FILE_UNUSE(fp, l);
1268 return (0);
1269 }
1270
1271 static void
1272 vfs__fhfree(fhandle_t *fhp)
1273 {
1274 size_t fhsize;
1275
1276 if (fhp == NULL) {
1277 return;
1278 }
1279 fhsize = FHANDLE_SIZE(fhp);
1280 kmem_free(fhp, fhsize);
1281 }
1282
1283 /*
1284 * vfs_composefh: compose a filehandle.
1285 */
1286
1287 int
1288 vfs_composefh(struct vnode *vp, fhandle_t *fhp, size_t *fh_size)
1289 {
1290 struct mount *mp;
1291 struct fid *fidp;
1292 int error;
1293 size_t needfhsize;
1294 size_t fidsize;
1295
1296 mp = vp->v_mount;
1297 fidp = NULL;
1298 if (*fh_size < FHANDLE_SIZE_MIN) {
1299 fidsize = 0;
1300 } else {
1301 fidsize = *fh_size - offsetof(fhandle_t, fh_fid);
1302 if (fhp != NULL) {
1303 memset(fhp, 0, *fh_size);
1304 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1305 fidp = &fhp->fh_fid;
1306 }
1307 }
1308 error = VFS_VPTOFH(vp, fidp, &fidsize);
1309 needfhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1310 if (error == 0 && *fh_size < needfhsize) {
1311 error = E2BIG;
1312 }
1313 *fh_size = needfhsize;
1314 return error;
1315 }
1316
1317 int
1318 vfs_composefh_alloc(struct vnode *vp, fhandle_t **fhpp)
1319 {
1320 struct mount *mp;
1321 fhandle_t *fhp;
1322 size_t fhsize;
1323 size_t fidsize;
1324 int error;
1325
1326 *fhpp = NULL;
1327 mp = vp->v_mount;
1328 fidsize = 0;
1329 error = VFS_VPTOFH(vp, NULL, &fidsize);
1330 KASSERT(error != 0);
1331 if (error != E2BIG) {
1332 goto out;
1333 }
1334 fhsize = FHANDLE_SIZE_FROM_FILEID_SIZE(fidsize);
1335 fhp = kmem_zalloc(fhsize, KM_SLEEP);
1336 if (fhp == NULL) {
1337 error = ENOMEM;
1338 goto out;
1339 }
1340 fhp->fh_fsid = mp->mnt_stat.f_fsidx;
1341 error = VFS_VPTOFH(vp, &fhp->fh_fid, &fidsize);
1342 if (error == 0) {
1343 KASSERT((FHANDLE_SIZE(fhp) == fhsize &&
1344 FHANDLE_FILEID(fhp)->fid_len == fidsize));
1345 *fhpp = fhp;
1346 } else {
1347 kmem_free(fhp, fhsize);
1348 }
1349 out:
1350 return error;
1351 }
1352
1353 void
1354 vfs_composefh_free(fhandle_t *fhp)
1355 {
1356
1357 vfs__fhfree(fhp);
1358 }
1359
1360 /*
1361 * vfs_fhtovp: lookup a vnode by a filehandle.
1362 */
1363
1364 int
1365 vfs_fhtovp(fhandle_t *fhp, struct vnode **vpp)
1366 {
1367 struct mount *mp;
1368 int error;
1369
1370 *vpp = NULL;
1371 mp = vfs_getvfs(FHANDLE_FSID(fhp));
1372 if (mp == NULL) {
1373 error = ESTALE;
1374 goto out;
1375 }
1376 if (mp->mnt_op->vfs_fhtovp == NULL) {
1377 error = EOPNOTSUPP;
1378 goto out;
1379 }
1380 error = VFS_FHTOVP(mp, FHANDLE_FILEID(fhp), vpp);
1381 out:
1382 return error;
1383 }
1384
1385 /*
1386 * vfs_copyinfh_alloc: allocate and copyin a filehandle, given
1387 * the needed size.
1388 */
1389
1390 int
1391 vfs_copyinfh_alloc(const void *ufhp, size_t fhsize, fhandle_t **fhpp)
1392 {
1393 fhandle_t *fhp;
1394 int error;
1395
1396 *fhpp = NULL;
1397 if (fhsize > FHANDLE_SIZE_MAX) {
1398 return EINVAL;
1399 }
1400 if (fhsize < FHANDLE_SIZE_MIN) {
1401 return EINVAL;
1402 }
1403 again:
1404 fhp = kmem_alloc(fhsize, KM_SLEEP);
1405 if (fhp == NULL) {
1406 return ENOMEM;
1407 }
1408 error = copyin(ufhp, fhp, fhsize);
1409 if (error == 0) {
1410 /* XXX this check shouldn't be here */
1411 if (FHANDLE_SIZE(fhp) == fhsize) {
1412 *fhpp = fhp;
1413 return 0;
1414 } else if (fhsize == NFSX_V2FH && FHANDLE_SIZE(fhp) < fhsize) {
1415 /*
1416 * a kludge for nfsv2 padded handles.
1417 */
1418 size_t sz;
1419
1420 sz = FHANDLE_SIZE(fhp);
1421 kmem_free(fhp, fhsize);
1422 fhsize = sz;
1423 goto again;
1424 } else {
1425 /*
1426 * userland told us wrong size.
1427 */
1428 error = EINVAL;
1429 }
1430 }
1431 kmem_free(fhp, fhsize);
1432 return error;
1433 }
1434
1435 void
1436 vfs_copyinfh_free(fhandle_t *fhp)
1437 {
1438
1439 vfs__fhfree(fhp);
1440 }
1441
1442 /*
1443 * Get file handle system call
1444 */
1445 int
1446 sys___getfh30(struct lwp *l, void *v, register_t *retval)
1447 {
1448 struct sys___getfh30_args /* {
1449 syscallarg(char *) fname;
1450 syscallarg(fhandle_t *) fhp;
1451 syscallarg(size_t *) fh_size;
1452 } */ *uap = v;
1453 struct vnode *vp;
1454 fhandle_t *fh;
1455 int error;
1456 struct nameidata nd;
1457 size_t sz;
1458 size_t usz;
1459
1460 /*
1461 * Must be super user
1462 */
1463 error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1464 0, NULL, NULL, NULL);
1465 if (error)
1466 return (error);
1467 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
1468 SCARG(uap, fname), l);
1469 error = namei(&nd);
1470 if (error)
1471 return (error);
1472 vp = nd.ni_vp;
1473 error = vfs_composefh_alloc(vp, &fh);
1474 vput(vp);
1475 if (error != 0) {
1476 goto out;
1477 }
1478 error = copyin(SCARG(uap, fh_size), &usz, sizeof(size_t));
1479 if (error != 0) {
1480 goto out;
1481 }
1482 sz = FHANDLE_SIZE(fh);
1483 error = copyout(&sz, SCARG(uap, fh_size), sizeof(size_t));
1484 if (error != 0) {
1485 goto out;
1486 }
1487 if (usz >= sz) {
1488 error = copyout(fh, SCARG(uap, fhp), sz);
1489 } else {
1490 error = E2BIG;
1491 }
1492 out:
1493 vfs_composefh_free(fh);
1494 return (error);
1495 }
1496
1497 /*
1498 * Open a file given a file handle.
1499 *
1500 * Check permissions, allocate an open file structure,
1501 * and call the device open routine if any.
1502 */
1503
1504 int
1505 dofhopen(struct lwp *l, const void *ufhp, size_t fhsize, int oflags,
1506 register_t *retval)
1507 {
1508 struct filedesc *fdp = l->l_proc->p_fd;
1509 struct file *fp;
1510 struct vnode *vp = NULL;
1511 kauth_cred_t cred = l->l_cred;
1512 struct file *nfp;
1513 int type, indx, error=0;
1514 struct flock lf;
1515 struct vattr va;
1516 fhandle_t *fh;
1517 int flags;
1518
1519 /*
1520 * Must be super user
1521 */
1522 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1523 0, NULL, NULL, NULL)))
1524 return (error);
1525
1526 flags = FFLAGS(oflags);
1527 if ((flags & (FREAD | FWRITE)) == 0)
1528 return (EINVAL);
1529 if ((flags & O_CREAT))
1530 return (EINVAL);
1531 /* falloc() will use the file descriptor for us */
1532 if ((error = falloc(l, &nfp, &indx)) != 0)
1533 return (error);
1534 fp = nfp;
1535 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1536 if (error != 0) {
1537 goto bad;
1538 }
1539 error = vfs_fhtovp(fh, &vp);
1540 if (error != 0) {
1541 goto bad;
1542 }
1543
1544 /* Now do an effective vn_open */
1545
1546 if (vp->v_type == VSOCK) {
1547 error = EOPNOTSUPP;
1548 goto bad;
1549 }
1550 if (flags & FREAD) {
1551 if ((error = VOP_ACCESS(vp, VREAD, cred, l)) != 0)
1552 goto bad;
1553 }
1554 if (flags & (FWRITE | O_TRUNC)) {
1555 if (vp->v_type == VDIR) {
1556 error = EISDIR;
1557 goto bad;
1558 }
1559 if ((error = vn_writechk(vp)) != 0 ||
1560 (error = VOP_ACCESS(vp, VWRITE, cred, l)) != 0)
1561 goto bad;
1562 }
1563 if (flags & O_TRUNC) {
1564 VOP_UNLOCK(vp, 0); /* XXX */
1565 VOP_LEASE(vp, l, cred, LEASE_WRITE);
1566 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
1567 VATTR_NULL(&va);
1568 va.va_size = 0;
1569 error = VOP_SETATTR(vp, &va, cred, l);
1570 if (error)
1571 goto bad;
1572 }
1573 if ((error = VOP_OPEN(vp, flags, cred, l)) != 0)
1574 goto bad;
1575 if (vp->v_type == VREG &&
1576 uvn_attach(vp, flags & FWRITE ? VM_PROT_WRITE : 0) == NULL) {
1577 error = EIO;
1578 goto bad;
1579 }
1580 if (flags & FWRITE)
1581 vp->v_writecount++;
1582
1583 /* done with modified vn_open, now finish what sys_open does. */
1584
1585 fp->f_flag = flags & FMASK;
1586 fp->f_type = DTYPE_VNODE;
1587 fp->f_ops = &vnops;
1588 fp->f_data = vp;
1589 if (flags & (O_EXLOCK | O_SHLOCK)) {
1590 lf.l_whence = SEEK_SET;
1591 lf.l_start = 0;
1592 lf.l_len = 0;
1593 if (flags & O_EXLOCK)
1594 lf.l_type = F_WRLCK;
1595 else
1596 lf.l_type = F_RDLCK;
1597 type = F_FLOCK;
1598 if ((flags & FNONBLOCK) == 0)
1599 type |= F_WAIT;
1600 VOP_UNLOCK(vp, 0);
1601 error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, type);
1602 if (error) {
1603 (void) vn_close(vp, fp->f_flag, fp->f_cred, l);
1604 FILE_UNUSE(fp, l);
1605 ffree(fp);
1606 fdremove(fdp, indx);
1607 return (error);
1608 }
1609 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1610 fp->f_flag |= FHASLOCK;
1611 }
1612 VOP_UNLOCK(vp, 0);
1613 *retval = indx;
1614 FILE_SET_MATURE(fp);
1615 FILE_UNUSE(fp, l);
1616 vfs_copyinfh_free(fh);
1617 return (0);
1618
1619 bad:
1620 FILE_UNUSE(fp, l);
1621 ffree(fp);
1622 fdremove(fdp, indx);
1623 if (vp != NULL)
1624 vput(vp);
1625 vfs_copyinfh_free(fh);
1626 return (error);
1627 }
1628
1629 int
1630 sys___fhopen40(struct lwp *l, void *v, register_t *retval)
1631 {
1632 struct sys___fhopen40_args /* {
1633 syscallarg(const void *) fhp;
1634 syscallarg(size_t) fh_size;
1635 syscallarg(int) flags;
1636 } */ *uap = v;
1637
1638 return dofhopen(l, SCARG(uap, fhp), SCARG(uap, fh_size),
1639 SCARG(uap, flags), retval);
1640 }
1641
1642 int
1643 do_fhstat(struct lwp *l, const void *ufhp, size_t fhsize, struct stat *sb)
1644 {
1645 int error;
1646 fhandle_t *fh;
1647 struct vnode *vp;
1648
1649 /*
1650 * Must be super user
1651 */
1652 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1653 0, NULL, NULL, NULL)))
1654 return (error);
1655
1656 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1657 if (error != 0)
1658 return error;
1659
1660 error = vfs_fhtovp(fh, &vp);
1661 vfs_copyinfh_free(fh);
1662 if (error != 0)
1663 return error;
1664
1665 error = vn_stat(vp, sb, l);
1666 vput(vp);
1667 return error;
1668 }
1669
1670
1671 /* ARGSUSED */
1672 int
1673 sys___fhstat40(struct lwp *l, void *v, register_t *retval)
1674 {
1675 struct sys___fhstat40_args /* {
1676 syscallarg(const void *) fhp;
1677 syscallarg(size_t) fh_size;
1678 syscallarg(struct stat *) sb;
1679 } */ *uap = v;
1680 struct stat sb;
1681 int error;
1682
1683 error = do_fhstat(l, SCARG(uap, fhp), SCARG(uap, fh_size), &sb);
1684 if (error)
1685 return error;
1686 return copyout(&sb, SCARG(uap, sb), sizeof(sb));
1687 }
1688
1689 int
1690 do_fhstatvfs(struct lwp *l, const void *ufhp, size_t fhsize, struct statvfs *sb,
1691 int flags)
1692 {
1693 fhandle_t *fh;
1694 struct mount *mp;
1695 struct vnode *vp;
1696 int error;
1697
1698 /*
1699 * Must be super user
1700 */
1701 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_FILEHANDLE,
1702 0, NULL, NULL, NULL)))
1703 return error;
1704
1705 error = vfs_copyinfh_alloc(ufhp, fhsize, &fh);
1706 if (error != 0)
1707 return error;
1708
1709 error = vfs_fhtovp(fh, &vp);
1710 vfs_copyinfh_free(fh);
1711 if (error != 0)
1712 return error;
1713
1714 mp = vp->v_mount;
1715 error = dostatvfs(mp, sb, l, flags, 1);
1716 vput(vp);
1717 return error;
1718 }
1719
1720 /* ARGSUSED */
1721 int
1722 sys___fhstatvfs140(struct lwp *l, void *v, register_t *retval)
1723 {
1724 struct sys___fhstatvfs140_args /* {
1725 syscallarg(const void *) fhp;
1726 syscallarg(size_t) fh_size;
1727 syscallarg(struct statvfs *) buf;
1728 syscallarg(int) flags;
1729 } */ *uap = v;
1730 struct statvfs *sb = STATVFSBUF_GET();
1731 int error;
1732
1733 error = do_fhstatvfs(l, SCARG(uap, fhp), SCARG(uap, fh_size), sb,
1734 SCARG(uap, flags));
1735 if (error == 0)
1736 error = copyout(sb, SCARG(uap, buf), sizeof(*sb));
1737 STATVFSBUF_PUT(sb);
1738 return error;
1739 }
1740
1741 /*
1742 * Create a special file.
1743 */
1744 /* ARGSUSED */
1745 int
1746 sys_mknod(struct lwp *l, void *v, register_t *retval)
1747 {
1748 struct sys_mknod_args /* {
1749 syscallarg(const char *) path;
1750 syscallarg(int) mode;
1751 syscallarg(int) dev;
1752 } */ *uap = v;
1753 struct proc *p = l->l_proc;
1754 struct vnode *vp;
1755 struct vattr vattr;
1756 int error, optype;
1757 struct nameidata nd;
1758
1759 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
1760 0, NULL, NULL, NULL)) != 0)
1761 return (error);
1762
1763 optype = VOP_MKNOD_DESCOFFSET;
1764 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1765 if ((error = namei(&nd)) != 0)
1766 return (error);
1767 vp = nd.ni_vp;
1768 if (vp != NULL)
1769 error = EEXIST;
1770 else {
1771 VATTR_NULL(&vattr);
1772 vattr.va_mode =
1773 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1774 vattr.va_rdev = SCARG(uap, dev);
1775
1776 switch (SCARG(uap, mode) & S_IFMT) {
1777 case S_IFMT: /* used by badsect to flag bad sectors */
1778 vattr.va_type = VBAD;
1779 break;
1780 case S_IFCHR:
1781 vattr.va_type = VCHR;
1782 break;
1783 case S_IFBLK:
1784 vattr.va_type = VBLK;
1785 break;
1786 case S_IFWHT:
1787 optype = VOP_WHITEOUT_DESCOFFSET;
1788 break;
1789 case S_IFREG:
1790 vattr.va_type = VREG;
1791 vattr.va_rdev = VNOVAL;
1792 optype = VOP_CREATE_DESCOFFSET;
1793 break;
1794 default:
1795 error = EINVAL;
1796 break;
1797 }
1798 }
1799 if (!error) {
1800 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1801 switch (optype) {
1802 case VOP_WHITEOUT_DESCOFFSET:
1803 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1804 if (error)
1805 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1806 vput(nd.ni_dvp);
1807 break;
1808
1809 case VOP_MKNOD_DESCOFFSET:
1810 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1811 &nd.ni_cnd, &vattr);
1812 if (error == 0)
1813 vput(nd.ni_vp);
1814 break;
1815
1816 case VOP_CREATE_DESCOFFSET:
1817 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
1818 &nd.ni_cnd, &vattr);
1819 if (error == 0)
1820 vput(nd.ni_vp);
1821 break;
1822 }
1823 } else {
1824 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1825 if (nd.ni_dvp == vp)
1826 vrele(nd.ni_dvp);
1827 else
1828 vput(nd.ni_dvp);
1829 if (vp)
1830 vrele(vp);
1831 }
1832 return (error);
1833 }
1834
1835 /*
1836 * Create a named pipe.
1837 */
1838 /* ARGSUSED */
1839 int
1840 sys_mkfifo(struct lwp *l, void *v, register_t *retval)
1841 {
1842 struct sys_mkfifo_args /* {
1843 syscallarg(const char *) path;
1844 syscallarg(int) mode;
1845 } */ *uap = v;
1846 struct proc *p = l->l_proc;
1847 struct vattr vattr;
1848 int error;
1849 struct nameidata nd;
1850
1851 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1852 if ((error = namei(&nd)) != 0)
1853 return (error);
1854 if (nd.ni_vp != NULL) {
1855 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1856 if (nd.ni_dvp == nd.ni_vp)
1857 vrele(nd.ni_dvp);
1858 else
1859 vput(nd.ni_dvp);
1860 vrele(nd.ni_vp);
1861 return (EEXIST);
1862 }
1863 VATTR_NULL(&vattr);
1864 vattr.va_type = VFIFO;
1865 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1866 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1867 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1868 if (error == 0)
1869 vput(nd.ni_vp);
1870 return (error);
1871 }
1872
1873 /*
1874 * Make a hard file link.
1875 */
1876 /* ARGSUSED */
1877 int
1878 sys_link(struct lwp *l, void *v, register_t *retval)
1879 {
1880 struct sys_link_args /* {
1881 syscallarg(const char *) path;
1882 syscallarg(const char *) link;
1883 } */ *uap = v;
1884 struct vnode *vp;
1885 struct nameidata nd;
1886 int error;
1887
1888 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
1889 if ((error = namei(&nd)) != 0)
1890 return (error);
1891 vp = nd.ni_vp;
1892 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, link), l);
1893 if ((error = namei(&nd)) != 0)
1894 goto out;
1895 if (nd.ni_vp) {
1896 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1897 if (nd.ni_dvp == nd.ni_vp)
1898 vrele(nd.ni_dvp);
1899 else
1900 vput(nd.ni_dvp);
1901 vrele(nd.ni_vp);
1902 error = EEXIST;
1903 goto out;
1904 }
1905 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1906 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
1907 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1908 out:
1909 vrele(vp);
1910 return (error);
1911 }
1912
1913 /*
1914 * Make a symbolic link.
1915 */
1916 /* ARGSUSED */
1917 int
1918 sys_symlink(struct lwp *l, void *v, register_t *retval)
1919 {
1920 struct sys_symlink_args /* {
1921 syscallarg(const char *) path;
1922 syscallarg(const char *) link;
1923 } */ *uap = v;
1924 struct proc *p = l->l_proc;
1925 struct vattr vattr;
1926 char *path;
1927 int error;
1928 struct nameidata nd;
1929
1930 path = PNBUF_GET();
1931 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
1932 if (error)
1933 goto out;
1934 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, link), l);
1935 if ((error = namei(&nd)) != 0)
1936 goto out;
1937 if (nd.ni_vp) {
1938 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1939 if (nd.ni_dvp == nd.ni_vp)
1940 vrele(nd.ni_dvp);
1941 else
1942 vput(nd.ni_dvp);
1943 vrele(nd.ni_vp);
1944 error = EEXIST;
1945 goto out;
1946 }
1947 VATTR_NULL(&vattr);
1948 vattr.va_type = VLNK;
1949 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
1950 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1951 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
1952 if (error == 0)
1953 vput(nd.ni_vp);
1954 out:
1955 PNBUF_PUT(path);
1956 return (error);
1957 }
1958
1959 /*
1960 * Delete a whiteout from the filesystem.
1961 */
1962 /* ARGSUSED */
1963 int
1964 sys_undelete(struct lwp *l, void *v, register_t *retval)
1965 {
1966 struct sys_undelete_args /* {
1967 syscallarg(const char *) path;
1968 } */ *uap = v;
1969 int error;
1970 struct nameidata nd;
1971
1972 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT, UIO_USERSPACE,
1973 SCARG(uap, path), l);
1974 error = namei(&nd);
1975 if (error)
1976 return (error);
1977
1978 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1979 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1980 if (nd.ni_dvp == nd.ni_vp)
1981 vrele(nd.ni_dvp);
1982 else
1983 vput(nd.ni_dvp);
1984 if (nd.ni_vp)
1985 vrele(nd.ni_vp);
1986 return (EEXIST);
1987 }
1988 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
1989 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
1990 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1991 vput(nd.ni_dvp);
1992 return (error);
1993 }
1994
1995 /*
1996 * Delete a name from the filesystem.
1997 */
1998 /* ARGSUSED */
1999 int
2000 sys_unlink(struct lwp *l, void *v, register_t *retval)
2001 {
2002 struct sys_unlink_args /* {
2003 syscallarg(const char *) path;
2004 } */ *uap = v;
2005 struct vnode *vp;
2006 int error;
2007 struct nameidata nd;
2008 #if NVERIEXEC > 0
2009 pathname_t pathbuf = NULL;
2010 #endif /* NVERIEXEC > 0 */
2011
2012 #if NVERIEXEC > 0
2013 error = pathname_get(SCARG(uap, path), UIO_USERSPACE, &pathbuf);
2014 if (error)
2015 return (error);
2016
2017 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE,
2018 pathname_path(pathbuf), l);
2019 #else
2020 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2021 SCARG(uap, path), l);
2022 #endif /* NVERIEXEC > 0 */
2023
2024 if ((error = namei(&nd)) != 0)
2025 goto out;
2026 vp = nd.ni_vp;
2027
2028 /*
2029 * The root of a mounted filesystem cannot be deleted.
2030 */
2031 if (vp->v_flag & VROOT) {
2032 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2033 if (nd.ni_dvp == vp)
2034 vrele(nd.ni_dvp);
2035 else
2036 vput(nd.ni_dvp);
2037 vput(vp);
2038 error = EBUSY;
2039 goto out;
2040 }
2041
2042 #if NVERIEXEC > 0
2043 /* Handle remove requests for veriexec entries. */
2044 if (!error)
2045 error = veriexec_removechk(vp, pathname_path(pathbuf), l);
2046
2047 if (error) {
2048 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2049 if (nd.ni_dvp == vp)
2050 vrele(nd.ni_dvp);
2051 else
2052 vput(nd.ni_dvp);
2053 vput(vp);
2054 goto out;
2055 }
2056 #endif /* NVERIEXEC > 0 */
2057
2058 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
2059 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2060 #ifdef FILEASSOC
2061 (void)fileassoc_file_delete(vp);
2062 #endif /* FILEASSOC */
2063 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2064 out:
2065 #if NVERIEXEC > 0
2066 pathname_put(pathbuf);
2067 #endif /* NVERIEXEC > 0 */
2068 return (error);
2069 }
2070
2071 /*
2072 * Reposition read/write file offset.
2073 */
2074 int
2075 sys_lseek(struct lwp *l, void *v, register_t *retval)
2076 {
2077 struct sys_lseek_args /* {
2078 syscallarg(int) fd;
2079 syscallarg(int) pad;
2080 syscallarg(off_t) offset;
2081 syscallarg(int) whence;
2082 } */ *uap = v;
2083 struct proc *p = l->l_proc;
2084 kauth_cred_t cred = l->l_cred;
2085 struct filedesc *fdp = p->p_fd;
2086 struct file *fp;
2087 struct vnode *vp;
2088 struct vattr vattr;
2089 off_t newoff;
2090 int error;
2091
2092 if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
2093 return (EBADF);
2094
2095 FILE_USE(fp);
2096
2097 vp = (struct vnode *)fp->f_data;
2098 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2099 error = ESPIPE;
2100 goto out;
2101 }
2102
2103 switch (SCARG(uap, whence)) {
2104 case SEEK_CUR:
2105 newoff = fp->f_offset + SCARG(uap, offset);
2106 break;
2107 case SEEK_END:
2108 error = VOP_GETATTR(vp, &vattr, cred, l);
2109 if (error)
2110 goto out;
2111 newoff = SCARG(uap, offset) + vattr.va_size;
2112 break;
2113 case SEEK_SET:
2114 newoff = SCARG(uap, offset);
2115 break;
2116 default:
2117 error = EINVAL;
2118 goto out;
2119 }
2120 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) != 0)
2121 goto out;
2122
2123 *(off_t *)retval = fp->f_offset = newoff;
2124 out:
2125 FILE_UNUSE(fp, l);
2126 return (error);
2127 }
2128
2129 /*
2130 * Positional read system call.
2131 */
2132 int
2133 sys_pread(struct lwp *l, void *v, register_t *retval)
2134 {
2135 struct sys_pread_args /* {
2136 syscallarg(int) fd;
2137 syscallarg(void *) buf;
2138 syscallarg(size_t) nbyte;
2139 syscallarg(off_t) offset;
2140 } */ *uap = v;
2141 struct proc *p = l->l_proc;
2142 struct filedesc *fdp = p->p_fd;
2143 struct file *fp;
2144 struct vnode *vp;
2145 off_t offset;
2146 int error, fd = SCARG(uap, fd);
2147
2148 if ((fp = fd_getfile(fdp, fd)) == NULL)
2149 return (EBADF);
2150
2151 if ((fp->f_flag & FREAD) == 0) {
2152 simple_unlock(&fp->f_slock);
2153 return (EBADF);
2154 }
2155
2156 FILE_USE(fp);
2157
2158 vp = (struct vnode *)fp->f_data;
2159 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2160 error = ESPIPE;
2161 goto out;
2162 }
2163
2164 offset = SCARG(uap, offset);
2165
2166 /*
2167 * XXX This works because no file systems actually
2168 * XXX take any action on the seek operation.
2169 */
2170 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2171 goto out;
2172
2173 /* dofileread() will unuse the descriptor for us */
2174 return (dofileread(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2175 &offset, 0, retval));
2176
2177 out:
2178 FILE_UNUSE(fp, l);
2179 return (error);
2180 }
2181
2182 /*
2183 * Positional scatter read system call.
2184 */
2185 int
2186 sys_preadv(struct lwp *l, void *v, register_t *retval)
2187 {
2188 struct sys_preadv_args /* {
2189 syscallarg(int) fd;
2190 syscallarg(const struct iovec *) iovp;
2191 syscallarg(int) iovcnt;
2192 syscallarg(off_t) offset;
2193 } */ *uap = v;
2194 struct proc *p = l->l_proc;
2195 struct filedesc *fdp = p->p_fd;
2196 struct file *fp;
2197 struct vnode *vp;
2198 off_t offset;
2199 int error, fd = SCARG(uap, fd);
2200
2201 if ((fp = fd_getfile(fdp, fd)) == NULL)
2202 return (EBADF);
2203
2204 if ((fp->f_flag & FREAD) == 0) {
2205 simple_unlock(&fp->f_slock);
2206 return (EBADF);
2207 }
2208
2209 FILE_USE(fp);
2210
2211 vp = (struct vnode *)fp->f_data;
2212 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2213 error = ESPIPE;
2214 goto out;
2215 }
2216
2217 offset = SCARG(uap, offset);
2218
2219 /*
2220 * XXX This works because no file systems actually
2221 * XXX take any action on the seek operation.
2222 */
2223 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2224 goto out;
2225
2226 /* dofilereadv() will unuse the descriptor for us */
2227 return (dofilereadv(l, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
2228 &offset, 0, retval));
2229
2230 out:
2231 FILE_UNUSE(fp, l);
2232 return (error);
2233 }
2234
2235 /*
2236 * Positional write system call.
2237 */
2238 int
2239 sys_pwrite(struct lwp *l, void *v, register_t *retval)
2240 {
2241 struct sys_pwrite_args /* {
2242 syscallarg(int) fd;
2243 syscallarg(const void *) buf;
2244 syscallarg(size_t) nbyte;
2245 syscallarg(off_t) offset;
2246 } */ *uap = v;
2247 struct proc *p = l->l_proc;
2248 struct filedesc *fdp = p->p_fd;
2249 struct file *fp;
2250 struct vnode *vp;
2251 off_t offset;
2252 int error, fd = SCARG(uap, fd);
2253
2254 if ((fp = fd_getfile(fdp, fd)) == NULL)
2255 return (EBADF);
2256
2257 if ((fp->f_flag & FWRITE) == 0) {
2258 simple_unlock(&fp->f_slock);
2259 return (EBADF);
2260 }
2261
2262 FILE_USE(fp);
2263
2264 vp = (struct vnode *)fp->f_data;
2265 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2266 error = ESPIPE;
2267 goto out;
2268 }
2269
2270 offset = SCARG(uap, offset);
2271
2272 /*
2273 * XXX This works because no file systems actually
2274 * XXX take any action on the seek operation.
2275 */
2276 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2277 goto out;
2278
2279 /* dofilewrite() will unuse the descriptor for us */
2280 return (dofilewrite(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2281 &offset, 0, retval));
2282
2283 out:
2284 FILE_UNUSE(fp, l);
2285 return (error);
2286 }
2287
2288 /*
2289 * Positional gather write system call.
2290 */
2291 int
2292 sys_pwritev(struct lwp *l, void *v, register_t *retval)
2293 {
2294 struct sys_pwritev_args /* {
2295 syscallarg(int) fd;
2296 syscallarg(const struct iovec *) iovp;
2297 syscallarg(int) iovcnt;
2298 syscallarg(off_t) offset;
2299 } */ *uap = v;
2300 struct proc *p = l->l_proc;
2301 struct filedesc *fdp = p->p_fd;
2302 struct file *fp;
2303 struct vnode *vp;
2304 off_t offset;
2305 int error, fd = SCARG(uap, fd);
2306
2307 if ((fp = fd_getfile(fdp, fd)) == NULL)
2308 return (EBADF);
2309
2310 if ((fp->f_flag & FWRITE) == 0) {
2311 simple_unlock(&fp->f_slock);
2312 return (EBADF);
2313 }
2314
2315 FILE_USE(fp);
2316
2317 vp = (struct vnode *)fp->f_data;
2318 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2319 error = ESPIPE;
2320 goto out;
2321 }
2322
2323 offset = SCARG(uap, offset);
2324
2325 /*
2326 * XXX This works because no file systems actually
2327 * XXX take any action on the seek operation.
2328 */
2329 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2330 goto out;
2331
2332 /* dofilewritev() will unuse the descriptor for us */
2333 return (dofilewritev(l, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
2334 &offset, 0, retval));
2335
2336 out:
2337 FILE_UNUSE(fp, l);
2338 return (error);
2339 }
2340
2341 /*
2342 * Check access permissions.
2343 */
2344 int
2345 sys_access(struct lwp *l, void *v, register_t *retval)
2346 {
2347 struct sys_access_args /* {
2348 syscallarg(const char *) path;
2349 syscallarg(int) flags;
2350 } */ *uap = v;
2351 kauth_cred_t cred;
2352 struct vnode *vp;
2353 int error, flags;
2354 struct nameidata nd;
2355
2356 cred = kauth_cred_dup(l->l_cred);
2357 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2358 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2359 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2360 SCARG(uap, path), l);
2361 /* Override default credentials */
2362 nd.ni_cnd.cn_cred = cred;
2363 if ((error = namei(&nd)) != 0)
2364 goto out;
2365 vp = nd.ni_vp;
2366
2367 /* Flags == 0 means only check for existence. */
2368 if (SCARG(uap, flags)) {
2369 flags = 0;
2370 if (SCARG(uap, flags) & R_OK)
2371 flags |= VREAD;
2372 if (SCARG(uap, flags) & W_OK)
2373 flags |= VWRITE;
2374 if (SCARG(uap, flags) & X_OK)
2375 flags |= VEXEC;
2376
2377 error = VOP_ACCESS(vp, flags, cred, l);
2378 if (!error && (flags & VWRITE))
2379 error = vn_writechk(vp);
2380 }
2381 vput(vp);
2382 out:
2383 kauth_cred_free(cred);
2384 return (error);
2385 }
2386
2387 /*
2388 * Common code for all sys_stat functions, including compat versions.
2389 */
2390 int
2391 do_sys_stat(struct lwp *l, const char *path, unsigned int nd_flags,
2392 struct stat *sb)
2393 {
2394 int error;
2395 struct nameidata nd;
2396
2397 NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT , UIO_USERSPACE, path, l);
2398 error = namei(&nd);
2399 if (error != 0)
2400 return error;
2401 error = vn_stat(nd.ni_vp, sb, l);
2402 vput(nd.ni_vp);
2403 return error;
2404 }
2405
2406 /*
2407 * Get file status; this version follows links.
2408 */
2409 /* ARGSUSED */
2410 int
2411 sys___stat30(struct lwp *l, void *v, register_t *retval)
2412 {
2413 struct sys___stat30_args /* {
2414 syscallarg(const char *) path;
2415 syscallarg(struct stat *) ub;
2416 } */ *uap = v;
2417 struct stat sb;
2418 int error;
2419
2420 error = do_sys_stat(l, SCARG(uap, path), FOLLOW, &sb);
2421 if (error)
2422 return error;
2423 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2424 }
2425
2426 /*
2427 * Get file status; this version does not follow links.
2428 */
2429 /* ARGSUSED */
2430 int
2431 sys___lstat30(struct lwp *l, void *v, register_t *retval)
2432 {
2433 struct sys___lstat30_args /* {
2434 syscallarg(const char *) path;
2435 syscallarg(struct stat *) ub;
2436 } */ *uap = v;
2437 struct stat sb;
2438 int error;
2439
2440 error = do_sys_stat(l, SCARG(uap, path), NOFOLLOW, &sb);
2441 if (error)
2442 return error;
2443 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2444 }
2445
2446 /*
2447 * Get configurable pathname variables.
2448 */
2449 /* ARGSUSED */
2450 int
2451 sys_pathconf(struct lwp *l, void *v, register_t *retval)
2452 {
2453 struct sys_pathconf_args /* {
2454 syscallarg(const char *) path;
2455 syscallarg(int) name;
2456 } */ *uap = v;
2457 int error;
2458 struct nameidata nd;
2459
2460 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2461 SCARG(uap, path), l);
2462 if ((error = namei(&nd)) != 0)
2463 return (error);
2464 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2465 vput(nd.ni_vp);
2466 return (error);
2467 }
2468
2469 /*
2470 * Return target name of a symbolic link.
2471 */
2472 /* ARGSUSED */
2473 int
2474 sys_readlink(struct lwp *l, void *v, register_t *retval)
2475 {
2476 struct sys_readlink_args /* {
2477 syscallarg(const char *) path;
2478 syscallarg(char *) buf;
2479 syscallarg(size_t) count;
2480 } */ *uap = v;
2481 struct vnode *vp;
2482 struct iovec aiov;
2483 struct uio auio;
2484 int error;
2485 struct nameidata nd;
2486
2487 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2488 SCARG(uap, path), l);
2489 if ((error = namei(&nd)) != 0)
2490 return (error);
2491 vp = nd.ni_vp;
2492 if (vp->v_type != VLNK)
2493 error = EINVAL;
2494 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2495 (error = VOP_ACCESS(vp, VREAD, l->l_cred, l)) == 0) {
2496 aiov.iov_base = SCARG(uap, buf);
2497 aiov.iov_len = SCARG(uap, count);
2498 auio.uio_iov = &aiov;
2499 auio.uio_iovcnt = 1;
2500 auio.uio_offset = 0;
2501 auio.uio_rw = UIO_READ;
2502 KASSERT(l == curlwp);
2503 auio.uio_vmspace = l->l_proc->p_vmspace;
2504 auio.uio_resid = SCARG(uap, count);
2505 error = VOP_READLINK(vp, &auio, l->l_cred);
2506 }
2507 vput(vp);
2508 *retval = SCARG(uap, count) - auio.uio_resid;
2509 return (error);
2510 }
2511
2512 /*
2513 * Change flags of a file given a path name.
2514 */
2515 /* ARGSUSED */
2516 int
2517 sys_chflags(struct lwp *l, void *v, register_t *retval)
2518 {
2519 struct sys_chflags_args /* {
2520 syscallarg(const char *) path;
2521 syscallarg(u_long) flags;
2522 } */ *uap = v;
2523 struct vnode *vp;
2524 int error;
2525 struct nameidata nd;
2526
2527 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2528 if ((error = namei(&nd)) != 0)
2529 return (error);
2530 vp = nd.ni_vp;
2531 error = change_flags(vp, SCARG(uap, flags), l);
2532 vput(vp);
2533 return (error);
2534 }
2535
2536 /*
2537 * Change flags of a file given a file descriptor.
2538 */
2539 /* ARGSUSED */
2540 int
2541 sys_fchflags(struct lwp *l, void *v, register_t *retval)
2542 {
2543 struct sys_fchflags_args /* {
2544 syscallarg(int) fd;
2545 syscallarg(u_long) flags;
2546 } */ *uap = v;
2547 struct proc *p = l->l_proc;
2548 struct vnode *vp;
2549 struct file *fp;
2550 int error;
2551
2552 /* getvnode() will use the descriptor for us */
2553 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2554 return (error);
2555 vp = (struct vnode *)fp->f_data;
2556 error = change_flags(vp, SCARG(uap, flags), l);
2557 VOP_UNLOCK(vp, 0);
2558 FILE_UNUSE(fp, l);
2559 return (error);
2560 }
2561
2562 /*
2563 * Change flags of a file given a path name; this version does
2564 * not follow links.
2565 */
2566 int
2567 sys_lchflags(struct lwp *l, void *v, register_t *retval)
2568 {
2569 struct sys_lchflags_args /* {
2570 syscallarg(const char *) path;
2571 syscallarg(u_long) flags;
2572 } */ *uap = v;
2573 struct vnode *vp;
2574 int error;
2575 struct nameidata nd;
2576
2577 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2578 if ((error = namei(&nd)) != 0)
2579 return (error);
2580 vp = nd.ni_vp;
2581 error = change_flags(vp, SCARG(uap, flags), l);
2582 vput(vp);
2583 return (error);
2584 }
2585
2586 /*
2587 * Common routine to change flags of a file.
2588 */
2589 int
2590 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2591 {
2592 struct vattr vattr;
2593 int error;
2594
2595 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2597 /*
2598 * Non-superusers cannot change the flags on devices, even if they
2599 * own them.
2600 */
2601 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
2602 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
2603 goto out;
2604 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2605 error = EINVAL;
2606 goto out;
2607 }
2608 }
2609 VATTR_NULL(&vattr);
2610 vattr.va_flags = flags;
2611 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2612 out:
2613 return (error);
2614 }
2615
2616 /*
2617 * Change mode of a file given path name; this version follows links.
2618 */
2619 /* ARGSUSED */
2620 int
2621 sys_chmod(struct lwp *l, void *v, register_t *retval)
2622 {
2623 struct sys_chmod_args /* {
2624 syscallarg(const char *) path;
2625 syscallarg(int) mode;
2626 } */ *uap = v;
2627 int error;
2628 struct nameidata nd;
2629
2630 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2631 if ((error = namei(&nd)) != 0)
2632 return (error);
2633
2634 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2635
2636 vrele(nd.ni_vp);
2637 return (error);
2638 }
2639
2640 /*
2641 * Change mode of a file given a file descriptor.
2642 */
2643 /* ARGSUSED */
2644 int
2645 sys_fchmod(struct lwp *l, void *v, register_t *retval)
2646 {
2647 struct sys_fchmod_args /* {
2648 syscallarg(int) fd;
2649 syscallarg(int) mode;
2650 } */ *uap = v;
2651 struct proc *p = l->l_proc;
2652 struct file *fp;
2653 int error;
2654
2655 /* getvnode() will use the descriptor for us */
2656 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2657 return (error);
2658
2659 error = change_mode((struct vnode *)fp->f_data, SCARG(uap, mode), l);
2660 FILE_UNUSE(fp, l);
2661 return (error);
2662 }
2663
2664 /*
2665 * Change mode of a file given path name; this version does not follow links.
2666 */
2667 /* ARGSUSED */
2668 int
2669 sys_lchmod(struct lwp *l, void *v, register_t *retval)
2670 {
2671 struct sys_lchmod_args /* {
2672 syscallarg(const char *) path;
2673 syscallarg(int) mode;
2674 } */ *uap = v;
2675 int error;
2676 struct nameidata nd;
2677
2678 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2679 if ((error = namei(&nd)) != 0)
2680 return (error);
2681
2682 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2683
2684 vrele(nd.ni_vp);
2685 return (error);
2686 }
2687
2688 /*
2689 * Common routine to set mode given a vnode.
2690 */
2691 static int
2692 change_mode(struct vnode *vp, int mode, struct lwp *l)
2693 {
2694 struct vattr vattr;
2695 int error;
2696
2697 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2698 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2699 VATTR_NULL(&vattr);
2700 vattr.va_mode = mode & ALLPERMS;
2701 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2702 VOP_UNLOCK(vp, 0);
2703 return (error);
2704 }
2705
2706 /*
2707 * Set ownership given a path name; this version follows links.
2708 */
2709 /* ARGSUSED */
2710 int
2711 sys_chown(struct lwp *l, void *v, register_t *retval)
2712 {
2713 struct sys_chown_args /* {
2714 syscallarg(const char *) path;
2715 syscallarg(uid_t) uid;
2716 syscallarg(gid_t) gid;
2717 } */ *uap = v;
2718 int error;
2719 struct nameidata nd;
2720
2721 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2722 if ((error = namei(&nd)) != 0)
2723 return (error);
2724
2725 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2726
2727 vrele(nd.ni_vp);
2728 return (error);
2729 }
2730
2731 /*
2732 * Set ownership given a path name; this version follows links.
2733 * Provides POSIX semantics.
2734 */
2735 /* ARGSUSED */
2736 int
2737 sys___posix_chown(struct lwp *l, void *v, register_t *retval)
2738 {
2739 struct sys_chown_args /* {
2740 syscallarg(const char *) path;
2741 syscallarg(uid_t) uid;
2742 syscallarg(gid_t) gid;
2743 } */ *uap = v;
2744 int error;
2745 struct nameidata nd;
2746
2747 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2748 if ((error = namei(&nd)) != 0)
2749 return (error);
2750
2751 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2752
2753 vrele(nd.ni_vp);
2754 return (error);
2755 }
2756
2757 /*
2758 * Set ownership given a file descriptor.
2759 */
2760 /* ARGSUSED */
2761 int
2762 sys_fchown(struct lwp *l, void *v, register_t *retval)
2763 {
2764 struct sys_fchown_args /* {
2765 syscallarg(int) fd;
2766 syscallarg(uid_t) uid;
2767 syscallarg(gid_t) gid;
2768 } */ *uap = v;
2769 struct proc *p = l->l_proc;
2770 int error;
2771 struct file *fp;
2772
2773 /* getvnode() will use the descriptor for us */
2774 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2775 return (error);
2776
2777 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2778 SCARG(uap, gid), l, 0);
2779 FILE_UNUSE(fp, l);
2780 return (error);
2781 }
2782
2783 /*
2784 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2785 */
2786 /* ARGSUSED */
2787 int
2788 sys___posix_fchown(struct lwp *l, void *v, register_t *retval)
2789 {
2790 struct sys_fchown_args /* {
2791 syscallarg(int) fd;
2792 syscallarg(uid_t) uid;
2793 syscallarg(gid_t) gid;
2794 } */ *uap = v;
2795 struct proc *p = l->l_proc;
2796 int error;
2797 struct file *fp;
2798
2799 /* getvnode() will use the descriptor for us */
2800 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
2801 return (error);
2802
2803 error = change_owner((struct vnode *)fp->f_data, SCARG(uap, uid),
2804 SCARG(uap, gid), l, 1);
2805 FILE_UNUSE(fp, l);
2806 return (error);
2807 }
2808
2809 /*
2810 * Set ownership given a path name; this version does not follow links.
2811 */
2812 /* ARGSUSED */
2813 int
2814 sys_lchown(struct lwp *l, void *v, register_t *retval)
2815 {
2816 struct sys_lchown_args /* {
2817 syscallarg(const char *) path;
2818 syscallarg(uid_t) uid;
2819 syscallarg(gid_t) gid;
2820 } */ *uap = v;
2821 int error;
2822 struct nameidata nd;
2823
2824 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2825 if ((error = namei(&nd)) != 0)
2826 return (error);
2827
2828 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2829
2830 vrele(nd.ni_vp);
2831 return (error);
2832 }
2833
2834 /*
2835 * Set ownership given a path name; this version does not follow links.
2836 * Provides POSIX/XPG semantics.
2837 */
2838 /* ARGSUSED */
2839 int
2840 sys___posix_lchown(struct lwp *l, void *v, register_t *retval)
2841 {
2842 struct sys_lchown_args /* {
2843 syscallarg(const char *) path;
2844 syscallarg(uid_t) uid;
2845 syscallarg(gid_t) gid;
2846 } */ *uap = v;
2847 int error;
2848 struct nameidata nd;
2849
2850 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
2851 if ((error = namei(&nd)) != 0)
2852 return (error);
2853
2854 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2855
2856 vrele(nd.ni_vp);
2857 return (error);
2858 }
2859
2860 /*
2861 * Common routine to set ownership given a vnode.
2862 */
2863 static int
2864 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2865 int posix_semantics)
2866 {
2867 struct vattr vattr;
2868 mode_t newmode;
2869 int error;
2870
2871 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
2872 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2873 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
2874 goto out;
2875
2876 #define CHANGED(x) ((int)(x) != -1)
2877 newmode = vattr.va_mode;
2878 if (posix_semantics) {
2879 /*
2880 * POSIX/XPG semantics: if the caller is not the super-user,
2881 * clear set-user-id and set-group-id bits. Both POSIX and
2882 * the XPG consider the behaviour for calls by the super-user
2883 * implementation-defined; we leave the set-user-id and set-
2884 * group-id settings intact in that case.
2885 */
2886 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2887 NULL) != 0)
2888 newmode &= ~(S_ISUID | S_ISGID);
2889 } else {
2890 /*
2891 * NetBSD semantics: when changing owner and/or group,
2892 * clear the respective bit(s).
2893 */
2894 if (CHANGED(uid))
2895 newmode &= ~S_ISUID;
2896 if (CHANGED(gid))
2897 newmode &= ~S_ISGID;
2898 }
2899 /* Update va_mode iff altered. */
2900 if (vattr.va_mode == newmode)
2901 newmode = VNOVAL;
2902
2903 VATTR_NULL(&vattr);
2904 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2905 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2906 vattr.va_mode = newmode;
2907 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
2908 #undef CHANGED
2909
2910 out:
2911 VOP_UNLOCK(vp, 0);
2912 return (error);
2913 }
2914
2915 /*
2916 * Set the access and modification times given a path name; this
2917 * version follows links.
2918 */
2919 /* ARGSUSED */
2920 int
2921 sys_utimes(struct lwp *l, void *v, register_t *retval)
2922 {
2923 struct sys_utimes_args /* {
2924 syscallarg(const char *) path;
2925 syscallarg(const struct timeval *) tptr;
2926 } */ *uap = v;
2927
2928 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
2929 SCARG(uap, tptr), UIO_USERSPACE);
2930 }
2931
2932 /*
2933 * Set the access and modification times given a file descriptor.
2934 */
2935 /* ARGSUSED */
2936 int
2937 sys_futimes(struct lwp *l, void *v, register_t *retval)
2938 {
2939 struct sys_futimes_args /* {
2940 syscallarg(int) fd;
2941 syscallarg(const struct timeval *) tptr;
2942 } */ *uap = v;
2943 int error;
2944 struct file *fp;
2945
2946 /* getvnode() will use the descriptor for us */
2947 if ((error = getvnode(l->l_proc->p_fd, SCARG(uap, fd), &fp)) != 0)
2948 return (error);
2949
2950 error = do_sys_utimes(l, fp->f_data, NULL, 0,
2951 SCARG(uap, tptr), UIO_USERSPACE);
2952
2953 FILE_UNUSE(fp, l);
2954 return (error);
2955 }
2956
2957 /*
2958 * Set the access and modification times given a path name; this
2959 * version does not follow links.
2960 */
2961 int
2962 sys_lutimes(struct lwp *l, void *v, register_t *retval)
2963 {
2964 struct sys_lutimes_args /* {
2965 syscallarg(const char *) path;
2966 syscallarg(const struct timeval *) tptr;
2967 } */ *uap = v;
2968
2969 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
2970 SCARG(uap, tptr), UIO_USERSPACE);
2971 }
2972
2973 /*
2974 * Common routine to set access and modification times given a vnode.
2975 */
2976 int
2977 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
2978 const struct timeval *tptr, enum uio_seg seg)
2979 {
2980 struct vattr vattr;
2981 struct nameidata nd;
2982 int error;
2983
2984 VATTR_NULL(&vattr);
2985 if (tptr == NULL) {
2986 nanotime(&vattr.va_atime);
2987 vattr.va_mtime = vattr.va_atime;
2988 vattr.va_vaflags |= VA_UTIMES_NULL;
2989 } else {
2990 struct timeval tv[2];
2991
2992 if (seg != UIO_SYSSPACE) {
2993 error = copyin(tptr, &tv, sizeof (tv));
2994 if (error != 0)
2995 return error;
2996 tptr = tv;
2997 }
2998 TIMEVAL_TO_TIMESPEC(tptr, &vattr.va_atime);
2999 TIMEVAL_TO_TIMESPEC(tptr + 1, &vattr.va_mtime);
3000 }
3001
3002 if (vp == NULL) {
3003 NDINIT(&nd, LOOKUP, flag | TRYEMULROOT, UIO_USERSPACE, path, l);
3004 if ((error = namei(&nd)) != 0)
3005 return (error);
3006 vp = nd.ni_vp;
3007 } else
3008 nd.ni_vp = NULL;
3009
3010 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3011 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3012 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
3013 VOP_UNLOCK(vp, 0);
3014
3015 if (nd.ni_vp != NULL)
3016 vrele(nd.ni_vp);
3017
3018 return (error);
3019 }
3020
3021 /*
3022 * Truncate a file given its path name.
3023 */
3024 /* ARGSUSED */
3025 int
3026 sys_truncate(struct lwp *l, void *v, register_t *retval)
3027 {
3028 struct sys_truncate_args /* {
3029 syscallarg(const char *) path;
3030 syscallarg(int) pad;
3031 syscallarg(off_t) length;
3032 } */ *uap = v;
3033 struct vnode *vp;
3034 struct vattr vattr;
3035 int error;
3036 struct nameidata nd;
3037
3038 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
3039 if ((error = namei(&nd)) != 0)
3040 return (error);
3041 vp = nd.ni_vp;
3042 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3043 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3044 if (vp->v_type == VDIR)
3045 error = EISDIR;
3046 else if ((error = vn_writechk(vp)) == 0 &&
3047 (error = VOP_ACCESS(vp, VWRITE, l->l_cred, l)) == 0) {
3048 VATTR_NULL(&vattr);
3049 vattr.va_size = SCARG(uap, length);
3050 error = VOP_SETATTR(vp, &vattr, l->l_cred, l);
3051 }
3052 vput(vp);
3053 return (error);
3054 }
3055
3056 /*
3057 * Truncate a file given a file descriptor.
3058 */
3059 /* ARGSUSED */
3060 int
3061 sys_ftruncate(struct lwp *l, void *v, register_t *retval)
3062 {
3063 struct sys_ftruncate_args /* {
3064 syscallarg(int) fd;
3065 syscallarg(int) pad;
3066 syscallarg(off_t) length;
3067 } */ *uap = v;
3068 struct proc *p = l->l_proc;
3069 struct vattr vattr;
3070 struct vnode *vp;
3071 struct file *fp;
3072 int error;
3073
3074 /* getvnode() will use the descriptor for us */
3075 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3076 return (error);
3077 if ((fp->f_flag & FWRITE) == 0) {
3078 error = EINVAL;
3079 goto out;
3080 }
3081 vp = (struct vnode *)fp->f_data;
3082 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3083 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3084 if (vp->v_type == VDIR)
3085 error = EISDIR;
3086 else if ((error = vn_writechk(vp)) == 0) {
3087 VATTR_NULL(&vattr);
3088 vattr.va_size = SCARG(uap, length);
3089 error = VOP_SETATTR(vp, &vattr, fp->f_cred, l);
3090 }
3091 VOP_UNLOCK(vp, 0);
3092 out:
3093 FILE_UNUSE(fp, l);
3094 return (error);
3095 }
3096
3097 /*
3098 * Sync an open file.
3099 */
3100 /* ARGSUSED */
3101 int
3102 sys_fsync(struct lwp *l, void *v, register_t *retval)
3103 {
3104 struct sys_fsync_args /* {
3105 syscallarg(int) fd;
3106 } */ *uap = v;
3107 struct proc *p = l->l_proc;
3108 struct vnode *vp;
3109 struct file *fp;
3110 int error;
3111
3112 /* getvnode() will use the descriptor for us */
3113 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3114 return (error);
3115 vp = (struct vnode *)fp->f_data;
3116 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3117 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0, l);
3118 if (error == 0 && bioops.io_fsync != NULL &&
3119 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3120 (*bioops.io_fsync)(vp, 0);
3121 VOP_UNLOCK(vp, 0);
3122 FILE_UNUSE(fp, l);
3123 return (error);
3124 }
3125
3126 /*
3127 * Sync a range of file data. API modeled after that found in AIX.
3128 *
3129 * FDATASYNC indicates that we need only save enough metadata to be able
3130 * to re-read the written data. Note we duplicate AIX's requirement that
3131 * the file be open for writing.
3132 */
3133 /* ARGSUSED */
3134 int
3135 sys_fsync_range(struct lwp *l, void *v, register_t *retval)
3136 {
3137 struct sys_fsync_range_args /* {
3138 syscallarg(int) fd;
3139 syscallarg(int) flags;
3140 syscallarg(off_t) start;
3141 syscallarg(off_t) length;
3142 } */ *uap = v;
3143 struct proc *p = l->l_proc;
3144 struct vnode *vp;
3145 struct file *fp;
3146 int flags, nflags;
3147 off_t s, e, len;
3148 int error;
3149
3150 /* getvnode() will use the descriptor for us */
3151 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3152 return (error);
3153
3154 if ((fp->f_flag & FWRITE) == 0) {
3155 error = EBADF;
3156 goto out;
3157 }
3158
3159 flags = SCARG(uap, flags);
3160 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3161 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3162 error = EINVAL;
3163 goto out;
3164 }
3165 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3166 if (flags & FDATASYNC)
3167 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3168 else
3169 nflags = FSYNC_WAIT;
3170 if (flags & FDISKSYNC)
3171 nflags |= FSYNC_CACHE;
3172
3173 len = SCARG(uap, length);
3174 /* If length == 0, we do the whole file, and s = l = 0 will do that */
3175 if (len) {
3176 s = SCARG(uap, start);
3177 e = s + len;
3178 if (e < s) {
3179 error = EINVAL;
3180 goto out;
3181 }
3182 } else {
3183 e = 0;
3184 s = 0;
3185 }
3186
3187 vp = (struct vnode *)fp->f_data;
3188 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3189 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e, l);
3190
3191 if (error == 0 && bioops.io_fsync != NULL &&
3192 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3193 (*bioops.io_fsync)(vp, nflags);
3194
3195 VOP_UNLOCK(vp, 0);
3196 out:
3197 FILE_UNUSE(fp, l);
3198 return (error);
3199 }
3200
3201 /*
3202 * Sync the data of an open file.
3203 */
3204 /* ARGSUSED */
3205 int
3206 sys_fdatasync(struct lwp *l, void *v, register_t *retval)
3207 {
3208 struct sys_fdatasync_args /* {
3209 syscallarg(int) fd;
3210 } */ *uap = v;
3211 struct proc *p = l->l_proc;
3212 struct vnode *vp;
3213 struct file *fp;
3214 int error;
3215
3216 /* getvnode() will use the descriptor for us */
3217 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3218 return (error);
3219 if ((fp->f_flag & FWRITE) == 0) {
3220 FILE_UNUSE(fp, l);
3221 return (EBADF);
3222 }
3223 vp = (struct vnode *)fp->f_data;
3224 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3225 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0, l);
3226 VOP_UNLOCK(vp, 0);
3227 FILE_UNUSE(fp, l);
3228 return (error);
3229 }
3230
3231 /*
3232 * Rename files, (standard) BSD semantics frontend.
3233 */
3234 /* ARGSUSED */
3235 int
3236 sys_rename(struct lwp *l, void *v, register_t *retval)
3237 {
3238 struct sys_rename_args /* {
3239 syscallarg(const char *) from;
3240 syscallarg(const char *) to;
3241 } */ *uap = v;
3242
3243 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 0));
3244 }
3245
3246 /*
3247 * Rename files, POSIX semantics frontend.
3248 */
3249 /* ARGSUSED */
3250 int
3251 sys___posix_rename(struct lwp *l, void *v, register_t *retval)
3252 {
3253 struct sys___posix_rename_args /* {
3254 syscallarg(const char *) from;
3255 syscallarg(const char *) to;
3256 } */ *uap = v;
3257
3258 return (rename_files(SCARG(uap, from), SCARG(uap, to), l, 1));
3259 }
3260
3261 /*
3262 * Rename files. Source and destination must either both be directories,
3263 * or both not be directories. If target is a directory, it must be empty.
3264 * If `from' and `to' refer to the same object, the value of the `retain'
3265 * argument is used to determine whether `from' will be
3266 *
3267 * (retain == 0) deleted unless `from' and `to' refer to the same
3268 * object in the file system's name space (BSD).
3269 * (retain == 1) always retained (POSIX).
3270 */
3271 static int
3272 rename_files(const char *from, const char *to, struct lwp *l, int retain)
3273 {
3274 struct vnode *tvp, *fvp, *tdvp;
3275 struct nameidata fromnd, tond;
3276 struct proc *p;
3277 int error;
3278
3279 NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT, UIO_USERSPACE,
3280 from, l);
3281 if ((error = namei(&fromnd)) != 0)
3282 return (error);
3283 if (fromnd.ni_dvp != fromnd.ni_vp)
3284 VOP_UNLOCK(fromnd.ni_dvp, 0);
3285 fvp = fromnd.ni_vp;
3286 NDINIT(&tond, RENAME, LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT |
3287 (fvp->v_type == VDIR ? CREATEDIR : 0), UIO_USERSPACE, to, l);
3288 if ((error = namei(&tond)) != 0) {
3289 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3290 vrele(fromnd.ni_dvp);
3291 vrele(fvp);
3292 goto out1;
3293 }
3294 tdvp = tond.ni_dvp;
3295 tvp = tond.ni_vp;
3296
3297 if (tvp != NULL) {
3298 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3299 error = ENOTDIR;
3300 goto out;
3301 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3302 error = EISDIR;
3303 goto out;
3304 }
3305 }
3306
3307 if (fvp == tdvp)
3308 error = EINVAL;
3309
3310 /*
3311 * Source and destination refer to the same object.
3312 */
3313 if (fvp == tvp) {
3314 if (retain)
3315 error = -1;
3316 else if (fromnd.ni_dvp == tdvp &&
3317 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3318 !memcmp(fromnd.ni_cnd.cn_nameptr,
3319 tond.ni_cnd.cn_nameptr,
3320 fromnd.ni_cnd.cn_namelen))
3321 error = -1;
3322 }
3323
3324 #if NVERIEXEC > 0
3325 if (!error) {
3326 char *f1, *f2;
3327
3328 f1 = malloc(fromnd.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3329 strlcpy(f1, fromnd.ni_cnd.cn_nameptr, fromnd.ni_cnd.cn_namelen);
3330
3331 f2 = malloc(tond.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3332 strlcpy(f1, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen);
3333
3334 error = veriexec_renamechk(fvp, f1, tvp, f2, l);
3335
3336 free(f1, M_TEMP);
3337 free(f2, M_TEMP);
3338 }
3339 #endif /* NVERIEXEC > 0 */
3340
3341 out:
3342 p = l->l_proc;
3343 if (!error) {
3344 VOP_LEASE(tdvp, l, l->l_cred, LEASE_WRITE);
3345 if (fromnd.ni_dvp != tdvp)
3346 VOP_LEASE(fromnd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3347 if (tvp) {
3348 VOP_LEASE(tvp, l, l->l_cred, LEASE_WRITE);
3349 }
3350 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3351 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3352 } else {
3353 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3354 if (tdvp == tvp)
3355 vrele(tdvp);
3356 else
3357 vput(tdvp);
3358 if (tvp)
3359 vput(tvp);
3360 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3361 vrele(fromnd.ni_dvp);
3362 vrele(fvp);
3363 }
3364 vrele(tond.ni_startdir);
3365 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3366 out1:
3367 if (fromnd.ni_startdir)
3368 vrele(fromnd.ni_startdir);
3369 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3370 return (error == -1 ? 0 : error);
3371 }
3372
3373 /*
3374 * Make a directory file.
3375 */
3376 /* ARGSUSED */
3377 int
3378 sys_mkdir(struct lwp *l, void *v, register_t *retval)
3379 {
3380 struct sys_mkdir_args /* {
3381 syscallarg(const char *) path;
3382 syscallarg(int) mode;
3383 } */ *uap = v;
3384 struct proc *p = l->l_proc;
3385 struct vnode *vp;
3386 struct vattr vattr;
3387 int error;
3388 struct nameidata nd;
3389
3390 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, UIO_USERSPACE,
3391 SCARG(uap, path), l);
3392 if ((error = namei(&nd)) != 0)
3393 return (error);
3394 vp = nd.ni_vp;
3395 if (vp != NULL) {
3396 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3397 if (nd.ni_dvp == vp)
3398 vrele(nd.ni_dvp);
3399 else
3400 vput(nd.ni_dvp);
3401 vrele(vp);
3402 return (EEXIST);
3403 }
3404 VATTR_NULL(&vattr);
3405 vattr.va_type = VDIR;
3406 vattr.va_mode =
3407 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3408 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3409 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3410 if (!error)
3411 vput(nd.ni_vp);
3412 return (error);
3413 }
3414
3415 /*
3416 * Remove a directory file.
3417 */
3418 /* ARGSUSED */
3419 int
3420 sys_rmdir(struct lwp *l, void *v, register_t *retval)
3421 {
3422 struct sys_rmdir_args /* {
3423 syscallarg(const char *) path;
3424 } */ *uap = v;
3425 struct vnode *vp;
3426 int error;
3427 struct nameidata nd;
3428
3429 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
3430 SCARG(uap, path), l);
3431 if ((error = namei(&nd)) != 0)
3432 return (error);
3433 vp = nd.ni_vp;
3434 if (vp->v_type != VDIR) {
3435 error = ENOTDIR;
3436 goto out;
3437 }
3438 /*
3439 * No rmdir "." please.
3440 */
3441 if (nd.ni_dvp == vp) {
3442 error = EINVAL;
3443 goto out;
3444 }
3445 /*
3446 * The root of a mounted filesystem cannot be deleted.
3447 */
3448 if (vp->v_flag & VROOT) {
3449 error = EBUSY;
3450 goto out;
3451 }
3452 VOP_LEASE(nd.ni_dvp, l, l->l_cred, LEASE_WRITE);
3453 VOP_LEASE(vp, l, l->l_cred, LEASE_WRITE);
3454 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3455 return (error);
3456
3457 out:
3458 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3459 if (nd.ni_dvp == vp)
3460 vrele(nd.ni_dvp);
3461 else
3462 vput(nd.ni_dvp);
3463 vput(vp);
3464 return (error);
3465 }
3466
3467 /*
3468 * Read a block of directory entries in a file system independent format.
3469 */
3470 int
3471 sys___getdents30(struct lwp *l, void *v, register_t *retval)
3472 {
3473 struct sys___getdents30_args /* {
3474 syscallarg(int) fd;
3475 syscallarg(char *) buf;
3476 syscallarg(size_t) count;
3477 } */ *uap = v;
3478 struct proc *p = l->l_proc;
3479 struct file *fp;
3480 int error, done;
3481
3482 /* getvnode() will use the descriptor for us */
3483 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
3484 return (error);
3485 if ((fp->f_flag & FREAD) == 0) {
3486 error = EBADF;
3487 goto out;
3488 }
3489 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3490 SCARG(uap, count), &done, l, 0, 0);
3491 #ifdef KTRACE
3492 if (!error && KTRPOINT(p, KTR_GENIO)) {
3493 struct iovec iov;
3494 iov.iov_base = SCARG(uap, buf);
3495 iov.iov_len = done;
3496 ktrgenio(l, SCARG(uap, fd), UIO_READ, &iov, done, 0);
3497 }
3498 #endif
3499 *retval = done;
3500 out:
3501 FILE_UNUSE(fp, l);
3502 return (error);
3503 }
3504
3505 /*
3506 * Set the mode mask for creation of filesystem nodes.
3507 */
3508 int
3509 sys_umask(struct lwp *l, void *v, register_t *retval)
3510 {
3511 struct sys_umask_args /* {
3512 syscallarg(mode_t) newmask;
3513 } */ *uap = v;
3514 struct proc *p = l->l_proc;
3515 struct cwdinfo *cwdi;
3516
3517 cwdi = p->p_cwdi;
3518 *retval = cwdi->cwdi_cmask;
3519 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3520 return (0);
3521 }
3522
3523 /*
3524 * Void all references to file by ripping underlying filesystem
3525 * away from vnode.
3526 */
3527 /* ARGSUSED */
3528 int
3529 sys_revoke(struct lwp *l, void *v, register_t *retval)
3530 {
3531 struct sys_revoke_args /* {
3532 syscallarg(const char *) path;
3533 } */ *uap = v;
3534 struct vnode *vp;
3535 struct vattr vattr;
3536 int error;
3537 struct nameidata nd;
3538
3539 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE, SCARG(uap, path), l);
3540 if ((error = namei(&nd)) != 0)
3541 return (error);
3542 vp = nd.ni_vp;
3543 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred, l)) != 0)
3544 goto out;
3545 if (kauth_cred_geteuid(l->l_cred) != vattr.va_uid &&
3546 (error = kauth_authorize_generic(l->l_cred,
3547 KAUTH_GENERIC_ISSUSER, NULL)) != 0)
3548 goto out;
3549 if (vp->v_usecount > 1 || (vp->v_flag & (VALIASED | VLAYER)))
3550 VOP_REVOKE(vp, REVOKEALL);
3551 out:
3552 vrele(vp);
3553 return (error);
3554 }
3555
3556 /*
3557 * Convert a user file descriptor to a kernel file entry.
3558 */
3559 int
3560 getvnode(struct filedesc *fdp, int fd, struct file **fpp)
3561 {
3562 struct vnode *vp;
3563 struct file *fp;
3564
3565 if ((fp = fd_getfile(fdp, fd)) == NULL)
3566 return (EBADF);
3567
3568 FILE_USE(fp);
3569
3570 if (fp->f_type != DTYPE_VNODE) {
3571 FILE_UNUSE(fp, NULL);
3572 return (EINVAL);
3573 }
3574
3575 vp = (struct vnode *)fp->f_data;
3576 if (vp->v_type == VBAD) {
3577 FILE_UNUSE(fp, NULL);
3578 return (EBADF);
3579 }
3580
3581 *fpp = fp;
3582 return (0);
3583 }
3584