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