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