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