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