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