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