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