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