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