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