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