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