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