vfs_syscalls.c revision 1.348 1 /* $NetBSD: vfs_syscalls.c,v 1.348 2008/03/28 05:02:08 dholland 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 2008/03/28 05:02:08 dholland 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_mknod(struct lwp *l, const struct sys_mknod_args *uap, register_t *retval)
1816 {
1817 /* {
1818 syscallarg(const char *) path;
1819 syscallarg(int) mode;
1820 syscallarg(int) dev;
1821 } */
1822 struct proc *p = l->l_proc;
1823 struct vnode *vp;
1824 struct vattr vattr;
1825 int error, optype;
1826 struct nameidata nd;
1827 char *path;
1828 const char *cpath;
1829 enum uio_seg seg = UIO_USERSPACE;
1830
1831 if ((error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MKNOD,
1832 0, NULL, NULL, NULL)) != 0)
1833 return (error);
1834
1835 optype = VOP_MKNOD_DESCOFFSET;
1836
1837 VERIEXEC_PATH_GET(SCARG(uap, path), seg, cpath, path);
1838 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, seg, cpath);
1839
1840 if ((error = namei(&nd)) != 0)
1841 goto out;
1842 vp = nd.ni_vp;
1843 if (vp != NULL)
1844 error = EEXIST;
1845 else {
1846 VATTR_NULL(&vattr);
1847 /* We will read cwdi->cwdi_cmask unlocked. */
1848 vattr.va_mode =
1849 (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1850 vattr.va_rdev = SCARG(uap, dev);
1851
1852 switch (SCARG(uap, mode) & S_IFMT) {
1853 case S_IFMT: /* used by badsect to flag bad sectors */
1854 vattr.va_type = VBAD;
1855 break;
1856 case S_IFCHR:
1857 vattr.va_type = VCHR;
1858 break;
1859 case S_IFBLK:
1860 vattr.va_type = VBLK;
1861 break;
1862 case S_IFWHT:
1863 optype = VOP_WHITEOUT_DESCOFFSET;
1864 break;
1865 case S_IFREG:
1866 #if NVERIEXEC > 0
1867 error = veriexec_openchk(l, nd.ni_vp, nd.ni_dirp,
1868 O_CREAT);
1869 #endif /* NVERIEXEC > 0 */
1870 vattr.va_type = VREG;
1871 vattr.va_rdev = VNOVAL;
1872 optype = VOP_CREATE_DESCOFFSET;
1873 break;
1874 default:
1875 error = EINVAL;
1876 break;
1877 }
1878 }
1879 if (!error) {
1880 switch (optype) {
1881 case VOP_WHITEOUT_DESCOFFSET:
1882 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1883 if (error)
1884 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1885 vput(nd.ni_dvp);
1886 break;
1887
1888 case VOP_MKNOD_DESCOFFSET:
1889 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1890 &nd.ni_cnd, &vattr);
1891 if (error == 0)
1892 vput(nd.ni_vp);
1893 break;
1894
1895 case VOP_CREATE_DESCOFFSET:
1896 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp,
1897 &nd.ni_cnd, &vattr);
1898 if (error == 0)
1899 vput(nd.ni_vp);
1900 break;
1901 }
1902 } else {
1903 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1904 if (nd.ni_dvp == vp)
1905 vrele(nd.ni_dvp);
1906 else
1907 vput(nd.ni_dvp);
1908 if (vp)
1909 vrele(vp);
1910 }
1911 out:
1912 VERIEXEC_PATH_PUT(path);
1913 return (error);
1914 }
1915
1916 /*
1917 * Create a named pipe.
1918 */
1919 /* ARGSUSED */
1920 int
1921 sys_mkfifo(struct lwp *l, const struct sys_mkfifo_args *uap, register_t *retval)
1922 {
1923 /* {
1924 syscallarg(const char *) path;
1925 syscallarg(int) mode;
1926 } */
1927 struct proc *p = l->l_proc;
1928 struct vattr vattr;
1929 int error;
1930 struct nameidata nd;
1931
1932 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
1933 SCARG(uap, path));
1934 if ((error = namei(&nd)) != 0)
1935 return (error);
1936 if (nd.ni_vp != NULL) {
1937 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1938 if (nd.ni_dvp == nd.ni_vp)
1939 vrele(nd.ni_dvp);
1940 else
1941 vput(nd.ni_dvp);
1942 vrele(nd.ni_vp);
1943 return (EEXIST);
1944 }
1945 VATTR_NULL(&vattr);
1946 vattr.va_type = VFIFO;
1947 /* We will read cwdi->cwdi_cmask unlocked. */
1948 vattr.va_mode = (SCARG(uap, mode) & ALLPERMS) &~ p->p_cwdi->cwdi_cmask;
1949 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1950 if (error == 0)
1951 vput(nd.ni_vp);
1952 return (error);
1953 }
1954
1955 /*
1956 * Make a hard file link.
1957 */
1958 /* ARGSUSED */
1959 int
1960 sys_link(struct lwp *l, const struct sys_link_args *uap, register_t *retval)
1961 {
1962 /* {
1963 syscallarg(const char *) path;
1964 syscallarg(const char *) link;
1965 } */
1966 struct vnode *vp;
1967 struct nameidata nd;
1968 int error;
1969
1970 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
1971 SCARG(uap, path));
1972 if ((error = namei(&nd)) != 0)
1973 return (error);
1974 vp = nd.ni_vp;
1975 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
1976 SCARG(uap, link));
1977 if ((error = namei(&nd)) != 0)
1978 goto out;
1979 if (nd.ni_vp) {
1980 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
1981 if (nd.ni_dvp == nd.ni_vp)
1982 vrele(nd.ni_dvp);
1983 else
1984 vput(nd.ni_dvp);
1985 vrele(nd.ni_vp);
1986 error = EEXIST;
1987 goto out;
1988 }
1989 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1990 out:
1991 vrele(vp);
1992 return (error);
1993 }
1994
1995 /*
1996 * Make a symbolic link.
1997 */
1998 /* ARGSUSED */
1999 int
2000 sys_symlink(struct lwp *l, const struct sys_symlink_args *uap, register_t *retval)
2001 {
2002 /* {
2003 syscallarg(const char *) path;
2004 syscallarg(const char *) link;
2005 } */
2006 struct proc *p = l->l_proc;
2007 struct vattr vattr;
2008 char *path;
2009 int error;
2010 struct nameidata nd;
2011
2012 path = PNBUF_GET();
2013 error = copyinstr(SCARG(uap, path), path, MAXPATHLEN, NULL);
2014 if (error)
2015 goto out;
2016 NDINIT(&nd, CREATE, LOCKPARENT | TRYEMULROOT, UIO_USERSPACE,
2017 SCARG(uap, link));
2018 if ((error = namei(&nd)) != 0)
2019 goto out;
2020 if (nd.ni_vp) {
2021 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2022 if (nd.ni_dvp == nd.ni_vp)
2023 vrele(nd.ni_dvp);
2024 else
2025 vput(nd.ni_dvp);
2026 vrele(nd.ni_vp);
2027 error = EEXIST;
2028 goto out;
2029 }
2030 VATTR_NULL(&vattr);
2031 vattr.va_type = VLNK;
2032 /* We will read cwdi->cwdi_cmask unlocked. */
2033 vattr.va_mode = ACCESSPERMS &~ p->p_cwdi->cwdi_cmask;
2034 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, path);
2035 if (error == 0)
2036 vput(nd.ni_vp);
2037 out:
2038 PNBUF_PUT(path);
2039 return (error);
2040 }
2041
2042 /*
2043 * Delete a whiteout from the filesystem.
2044 */
2045 /* ARGSUSED */
2046 int
2047 sys_undelete(struct lwp *l, const struct sys_undelete_args *uap, register_t *retval)
2048 {
2049 /* {
2050 syscallarg(const char *) path;
2051 } */
2052 int error;
2053 struct nameidata nd;
2054
2055 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | TRYEMULROOT,
2056 UIO_USERSPACE, SCARG(uap, path));
2057 error = namei(&nd);
2058 if (error)
2059 return (error);
2060
2061 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
2062 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2063 if (nd.ni_dvp == nd.ni_vp)
2064 vrele(nd.ni_dvp);
2065 else
2066 vput(nd.ni_dvp);
2067 if (nd.ni_vp)
2068 vrele(nd.ni_vp);
2069 return (EEXIST);
2070 }
2071 if ((error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE)) != 0)
2072 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2073 vput(nd.ni_dvp);
2074 return (error);
2075 }
2076
2077 /*
2078 * Delete a name from the filesystem.
2079 */
2080 /* ARGSUSED */
2081 int
2082 sys_unlink(struct lwp *l, const struct sys_unlink_args *uap, register_t *retval)
2083 {
2084 /* {
2085 syscallarg(const char *) path;
2086 } */
2087
2088 return do_sys_unlink(SCARG(uap, path), UIO_USERSPACE);
2089 }
2090
2091 int
2092 do_sys_unlink(const char *arg, enum uio_seg seg)
2093 {
2094 struct vnode *vp;
2095 int error;
2096 struct nameidata nd;
2097 kauth_cred_t cred;
2098 char *path;
2099 const char *cpath;
2100
2101 VERIEXEC_PATH_GET(arg, seg, cpath, path);
2102 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, seg, cpath);
2103
2104 if ((error = namei(&nd)) != 0)
2105 goto out;
2106 vp = nd.ni_vp;
2107
2108 /*
2109 * The root of a mounted filesystem cannot be deleted.
2110 */
2111 if (vp->v_vflag & VV_ROOT) {
2112 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2113 if (nd.ni_dvp == vp)
2114 vrele(nd.ni_dvp);
2115 else
2116 vput(nd.ni_dvp);
2117 vput(vp);
2118 error = EBUSY;
2119 goto out;
2120 }
2121
2122 #if NVERIEXEC > 0
2123 /* Handle remove requests for veriexec entries. */
2124 if ((error = veriexec_removechk(curlwp, nd.ni_vp, nd.ni_dirp)) != 0) {
2125 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
2126 if (nd.ni_dvp == vp)
2127 vrele(nd.ni_dvp);
2128 else
2129 vput(nd.ni_dvp);
2130 vput(vp);
2131 goto out;
2132 }
2133 #endif /* NVERIEXEC > 0 */
2134
2135 cred = kauth_cred_get();
2136 #ifdef FILEASSOC
2137 (void)fileassoc_file_delete(vp);
2138 #endif /* FILEASSOC */
2139 error = VOP_REMOVE(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
2140 out:
2141 VERIEXEC_PATH_PUT(path);
2142 return (error);
2143 }
2144
2145 /*
2146 * Reposition read/write file offset.
2147 */
2148 int
2149 sys_lseek(struct lwp *l, const struct sys_lseek_args *uap, register_t *retval)
2150 {
2151 /* {
2152 syscallarg(int) fd;
2153 syscallarg(int) pad;
2154 syscallarg(off_t) offset;
2155 syscallarg(int) whence;
2156 } */
2157 kauth_cred_t cred = l->l_cred;
2158 file_t *fp;
2159 struct vnode *vp;
2160 struct vattr vattr;
2161 off_t newoff;
2162 int error, fd;
2163
2164 fd = SCARG(uap, fd);
2165
2166 if ((fp = fd_getfile(fd)) == NULL)
2167 return (EBADF);
2168
2169 vp = fp->f_data;
2170 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2171 error = ESPIPE;
2172 goto out;
2173 }
2174
2175 switch (SCARG(uap, whence)) {
2176 case SEEK_CUR:
2177 newoff = fp->f_offset + SCARG(uap, offset);
2178 break;
2179 case SEEK_END:
2180 error = VOP_GETATTR(vp, &vattr, cred);
2181 if (error) {
2182 goto out;
2183 }
2184 newoff = SCARG(uap, offset) + vattr.va_size;
2185 break;
2186 case SEEK_SET:
2187 newoff = SCARG(uap, offset);
2188 break;
2189 default:
2190 error = EINVAL;
2191 goto out;
2192 }
2193 if ((error = VOP_SEEK(vp, fp->f_offset, newoff, cred)) == 0) {
2194 *(off_t *)retval = fp->f_offset = newoff;
2195 }
2196 out:
2197 fd_putfile(fd);
2198 return (error);
2199 }
2200
2201 /*
2202 * Positional read system call.
2203 */
2204 int
2205 sys_pread(struct lwp *l, const struct sys_pread_args *uap, register_t *retval)
2206 {
2207 /* {
2208 syscallarg(int) fd;
2209 syscallarg(void *) buf;
2210 syscallarg(size_t) nbyte;
2211 syscallarg(off_t) offset;
2212 } */
2213 file_t *fp;
2214 struct vnode *vp;
2215 off_t offset;
2216 int error, fd = SCARG(uap, fd);
2217
2218 if ((fp = fd_getfile(fd)) == NULL)
2219 return (EBADF);
2220
2221 if ((fp->f_flag & FREAD) == 0) {
2222 fd_putfile(fd);
2223 return (EBADF);
2224 }
2225
2226 vp = fp->f_data;
2227 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2228 error = ESPIPE;
2229 goto out;
2230 }
2231
2232 offset = SCARG(uap, offset);
2233
2234 /*
2235 * XXX This works because no file systems actually
2236 * XXX take any action on the seek operation.
2237 */
2238 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2239 goto out;
2240
2241 /* dofileread() will unuse the descriptor for us */
2242 return (dofileread(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2243 &offset, 0, retval));
2244
2245 out:
2246 fd_putfile(fd);
2247 return (error);
2248 }
2249
2250 /*
2251 * Positional scatter read system call.
2252 */
2253 int
2254 sys_preadv(struct lwp *l, const struct sys_preadv_args *uap, register_t *retval)
2255 {
2256 /* {
2257 syscallarg(int) fd;
2258 syscallarg(const struct iovec *) iovp;
2259 syscallarg(int) iovcnt;
2260 syscallarg(off_t) offset;
2261 } */
2262 off_t offset = SCARG(uap, offset);
2263
2264 return do_filereadv(SCARG(uap, fd), SCARG(uap, iovp),
2265 SCARG(uap, iovcnt), &offset, 0, retval);
2266 }
2267
2268 /*
2269 * Positional write system call.
2270 */
2271 int
2272 sys_pwrite(struct lwp *l, const struct sys_pwrite_args *uap, register_t *retval)
2273 {
2274 /* {
2275 syscallarg(int) fd;
2276 syscallarg(const void *) buf;
2277 syscallarg(size_t) nbyte;
2278 syscallarg(off_t) offset;
2279 } */
2280 file_t *fp;
2281 struct vnode *vp;
2282 off_t offset;
2283 int error, fd = SCARG(uap, fd);
2284
2285 if ((fp = fd_getfile(fd)) == NULL)
2286 return (EBADF);
2287
2288 if ((fp->f_flag & FWRITE) == 0) {
2289 fd_putfile(fd);
2290 return (EBADF);
2291 }
2292
2293 vp = fp->f_data;
2294 if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
2295 error = ESPIPE;
2296 goto out;
2297 }
2298
2299 offset = SCARG(uap, offset);
2300
2301 /*
2302 * XXX This works because no file systems actually
2303 * XXX take any action on the seek operation.
2304 */
2305 if ((error = VOP_SEEK(vp, fp->f_offset, offset, fp->f_cred)) != 0)
2306 goto out;
2307
2308 /* dofilewrite() will unuse the descriptor for us */
2309 return (dofilewrite(fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
2310 &offset, 0, retval));
2311
2312 out:
2313 fd_putfile(fd);
2314 return (error);
2315 }
2316
2317 /*
2318 * Positional gather write system call.
2319 */
2320 int
2321 sys_pwritev(struct lwp *l, const struct sys_pwritev_args *uap, register_t *retval)
2322 {
2323 /* {
2324 syscallarg(int) fd;
2325 syscallarg(const struct iovec *) iovp;
2326 syscallarg(int) iovcnt;
2327 syscallarg(off_t) offset;
2328 } */
2329 off_t offset = SCARG(uap, offset);
2330
2331 return do_filewritev(SCARG(uap, fd), SCARG(uap, iovp),
2332 SCARG(uap, iovcnt), &offset, 0, retval);
2333 }
2334
2335 /*
2336 * Check access permissions.
2337 */
2338 int
2339 sys_access(struct lwp *l, const struct sys_access_args *uap, register_t *retval)
2340 {
2341 /* {
2342 syscallarg(const char *) path;
2343 syscallarg(int) flags;
2344 } */
2345 kauth_cred_t cred;
2346 struct vnode *vp;
2347 int error, flags;
2348 struct nameidata nd;
2349
2350 cred = kauth_cred_dup(l->l_cred);
2351 kauth_cred_seteuid(cred, kauth_cred_getuid(l->l_cred));
2352 kauth_cred_setegid(cred, kauth_cred_getgid(l->l_cred));
2353 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2354 SCARG(uap, path));
2355 /* Override default credentials */
2356 nd.ni_cnd.cn_cred = cred;
2357 if ((error = namei(&nd)) != 0)
2358 goto out;
2359 vp = nd.ni_vp;
2360
2361 /* Flags == 0 means only check for existence. */
2362 if (SCARG(uap, flags)) {
2363 flags = 0;
2364 if (SCARG(uap, flags) & R_OK)
2365 flags |= VREAD;
2366 if (SCARG(uap, flags) & W_OK)
2367 flags |= VWRITE;
2368 if (SCARG(uap, flags) & X_OK)
2369 flags |= VEXEC;
2370
2371 error = VOP_ACCESS(vp, flags, cred);
2372 if (!error && (flags & VWRITE))
2373 error = vn_writechk(vp);
2374 }
2375 vput(vp);
2376 out:
2377 kauth_cred_free(cred);
2378 return (error);
2379 }
2380
2381 /*
2382 * Common code for all sys_stat functions, including compat versions.
2383 */
2384 int
2385 do_sys_stat(const char *path, unsigned int nd_flags, struct stat *sb)
2386 {
2387 int error;
2388 struct nameidata nd;
2389
2390 NDINIT(&nd, LOOKUP, nd_flags | LOCKLEAF | TRYEMULROOT,
2391 UIO_USERSPACE, path);
2392 error = namei(&nd);
2393 if (error != 0)
2394 return error;
2395 error = vn_stat(nd.ni_vp, sb);
2396 vput(nd.ni_vp);
2397 return error;
2398 }
2399
2400 /*
2401 * Get file status; this version follows links.
2402 */
2403 /* ARGSUSED */
2404 int
2405 sys___stat30(struct lwp *l, const struct sys___stat30_args *uap, register_t *retval)
2406 {
2407 /* {
2408 syscallarg(const char *) path;
2409 syscallarg(struct stat *) ub;
2410 } */
2411 struct stat sb;
2412 int error;
2413
2414 error = do_sys_stat(SCARG(uap, path), FOLLOW, &sb);
2415 if (error)
2416 return error;
2417 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2418 }
2419
2420 /*
2421 * Get file status; this version does not follow links.
2422 */
2423 /* ARGSUSED */
2424 int
2425 sys___lstat30(struct lwp *l, const struct sys___lstat30_args *uap, register_t *retval)
2426 {
2427 /* {
2428 syscallarg(const char *) path;
2429 syscallarg(struct stat *) ub;
2430 } */
2431 struct stat sb;
2432 int error;
2433
2434 error = do_sys_stat(SCARG(uap, path), NOFOLLOW, &sb);
2435 if (error)
2436 return error;
2437 return copyout(&sb, SCARG(uap, ub), sizeof(sb));
2438 }
2439
2440 /*
2441 * Get configurable pathname variables.
2442 */
2443 /* ARGSUSED */
2444 int
2445 sys_pathconf(struct lwp *l, const struct sys_pathconf_args *uap, register_t *retval)
2446 {
2447 /* {
2448 syscallarg(const char *) path;
2449 syscallarg(int) name;
2450 } */
2451 int error;
2452 struct nameidata nd;
2453
2454 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2455 SCARG(uap, path));
2456 if ((error = namei(&nd)) != 0)
2457 return (error);
2458 error = VOP_PATHCONF(nd.ni_vp, SCARG(uap, name), retval);
2459 vput(nd.ni_vp);
2460 return (error);
2461 }
2462
2463 /*
2464 * Return target name of a symbolic link.
2465 */
2466 /* ARGSUSED */
2467 int
2468 sys_readlink(struct lwp *l, const struct sys_readlink_args *uap, register_t *retval)
2469 {
2470 /* {
2471 syscallarg(const char *) path;
2472 syscallarg(char *) buf;
2473 syscallarg(size_t) count;
2474 } */
2475 struct vnode *vp;
2476 struct iovec aiov;
2477 struct uio auio;
2478 int error;
2479 struct nameidata nd;
2480
2481 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
2482 SCARG(uap, path));
2483 if ((error = namei(&nd)) != 0)
2484 return (error);
2485 vp = nd.ni_vp;
2486 if (vp->v_type != VLNK)
2487 error = EINVAL;
2488 else if (!(vp->v_mount->mnt_flag & MNT_SYMPERM) ||
2489 (error = VOP_ACCESS(vp, VREAD, l->l_cred)) == 0) {
2490 aiov.iov_base = SCARG(uap, buf);
2491 aiov.iov_len = SCARG(uap, count);
2492 auio.uio_iov = &aiov;
2493 auio.uio_iovcnt = 1;
2494 auio.uio_offset = 0;
2495 auio.uio_rw = UIO_READ;
2496 KASSERT(l == curlwp);
2497 auio.uio_vmspace = l->l_proc->p_vmspace;
2498 auio.uio_resid = SCARG(uap, count);
2499 error = VOP_READLINK(vp, &auio, l->l_cred);
2500 }
2501 vput(vp);
2502 *retval = SCARG(uap, count) - auio.uio_resid;
2503 return (error);
2504 }
2505
2506 /*
2507 * Change flags of a file given a path name.
2508 */
2509 /* ARGSUSED */
2510 int
2511 sys_chflags(struct lwp *l, const struct sys_chflags_args *uap, register_t *retval)
2512 {
2513 /* {
2514 syscallarg(const char *) path;
2515 syscallarg(u_long) flags;
2516 } */
2517 struct vnode *vp;
2518 int error;
2519 struct nameidata nd;
2520
2521 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2522 SCARG(uap, path));
2523 if ((error = namei(&nd)) != 0)
2524 return (error);
2525 vp = nd.ni_vp;
2526 error = change_flags(vp, SCARG(uap, flags), l);
2527 vput(vp);
2528 return (error);
2529 }
2530
2531 /*
2532 * Change flags of a file given a file descriptor.
2533 */
2534 /* ARGSUSED */
2535 int
2536 sys_fchflags(struct lwp *l, const struct sys_fchflags_args *uap, register_t *retval)
2537 {
2538 /* {
2539 syscallarg(int) fd;
2540 syscallarg(u_long) flags;
2541 } */
2542 struct vnode *vp;
2543 file_t *fp;
2544 int error;
2545
2546 /* fd_getvnode() will use the descriptor for us */
2547 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2548 return (error);
2549 vp = fp->f_data;
2550 error = change_flags(vp, SCARG(uap, flags), l);
2551 VOP_UNLOCK(vp, 0);
2552 fd_putfile(SCARG(uap, fd));
2553 return (error);
2554 }
2555
2556 /*
2557 * Change flags of a file given a path name; this version does
2558 * not follow links.
2559 */
2560 int
2561 sys_lchflags(struct lwp *l, const struct sys_lchflags_args *uap, register_t *retval)
2562 {
2563 /* {
2564 syscallarg(const char *) path;
2565 syscallarg(u_long) flags;
2566 } */
2567 struct vnode *vp;
2568 int error;
2569 struct nameidata nd;
2570
2571 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2572 SCARG(uap, path));
2573 if ((error = namei(&nd)) != 0)
2574 return (error);
2575 vp = nd.ni_vp;
2576 error = change_flags(vp, SCARG(uap, flags), l);
2577 vput(vp);
2578 return (error);
2579 }
2580
2581 /*
2582 * Common routine to change flags of a file.
2583 */
2584 int
2585 change_flags(struct vnode *vp, u_long flags, struct lwp *l)
2586 {
2587 struct vattr vattr;
2588 int error;
2589
2590 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2591 /*
2592 * Non-superusers cannot change the flags on devices, even if they
2593 * own them.
2594 */
2595 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
2596 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2597 goto out;
2598 if (vattr.va_type == VCHR || vattr.va_type == VBLK) {
2599 error = EINVAL;
2600 goto out;
2601 }
2602 }
2603 VATTR_NULL(&vattr);
2604 vattr.va_flags = flags;
2605 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2606 out:
2607 return (error);
2608 }
2609
2610 /*
2611 * Change mode of a file given path name; this version follows links.
2612 */
2613 /* ARGSUSED */
2614 int
2615 sys_chmod(struct lwp *l, const struct sys_chmod_args *uap, register_t *retval)
2616 {
2617 /* {
2618 syscallarg(const char *) path;
2619 syscallarg(int) mode;
2620 } */
2621 int error;
2622 struct nameidata nd;
2623
2624 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2625 SCARG(uap, path));
2626 if ((error = namei(&nd)) != 0)
2627 return (error);
2628
2629 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2630
2631 vrele(nd.ni_vp);
2632 return (error);
2633 }
2634
2635 /*
2636 * Change mode of a file given a file descriptor.
2637 */
2638 /* ARGSUSED */
2639 int
2640 sys_fchmod(struct lwp *l, const struct sys_fchmod_args *uap, register_t *retval)
2641 {
2642 /* {
2643 syscallarg(int) fd;
2644 syscallarg(int) mode;
2645 } */
2646 file_t *fp;
2647 int error;
2648
2649 /* fd_getvnode() will use the descriptor for us */
2650 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2651 return (error);
2652 error = change_mode(fp->f_data, SCARG(uap, mode), l);
2653 fd_putfile(SCARG(uap, fd));
2654 return (error);
2655 }
2656
2657 /*
2658 * Change mode of a file given path name; this version does not follow links.
2659 */
2660 /* ARGSUSED */
2661 int
2662 sys_lchmod(struct lwp *l, const struct sys_lchmod_args *uap, register_t *retval)
2663 {
2664 /* {
2665 syscallarg(const char *) path;
2666 syscallarg(int) mode;
2667 } */
2668 int error;
2669 struct nameidata nd;
2670
2671 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2672 SCARG(uap, path));
2673 if ((error = namei(&nd)) != 0)
2674 return (error);
2675
2676 error = change_mode(nd.ni_vp, SCARG(uap, mode), l);
2677
2678 vrele(nd.ni_vp);
2679 return (error);
2680 }
2681
2682 /*
2683 * Common routine to set mode given a vnode.
2684 */
2685 static int
2686 change_mode(struct vnode *vp, int mode, struct lwp *l)
2687 {
2688 struct vattr vattr;
2689 int error;
2690
2691 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2692 VATTR_NULL(&vattr);
2693 vattr.va_mode = mode & ALLPERMS;
2694 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2695 VOP_UNLOCK(vp, 0);
2696 return (error);
2697 }
2698
2699 /*
2700 * Set ownership given a path name; this version follows links.
2701 */
2702 /* ARGSUSED */
2703 int
2704 sys_chown(struct lwp *l, const struct sys_chown_args *uap, register_t *retval)
2705 {
2706 /* {
2707 syscallarg(const char *) path;
2708 syscallarg(uid_t) uid;
2709 syscallarg(gid_t) gid;
2710 } */
2711 int error;
2712 struct nameidata nd;
2713
2714 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2715 SCARG(uap, path));
2716 if ((error = namei(&nd)) != 0)
2717 return (error);
2718
2719 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2720
2721 vrele(nd.ni_vp);
2722 return (error);
2723 }
2724
2725 /*
2726 * Set ownership given a path name; this version follows links.
2727 * Provides POSIX semantics.
2728 */
2729 /* ARGSUSED */
2730 int
2731 sys___posix_chown(struct lwp *l, const struct sys___posix_chown_args *uap, register_t *retval)
2732 {
2733 /* {
2734 syscallarg(const char *) path;
2735 syscallarg(uid_t) uid;
2736 syscallarg(gid_t) gid;
2737 } */
2738 int error;
2739 struct nameidata nd;
2740
2741 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
2742 SCARG(uap, path));
2743 if ((error = namei(&nd)) != 0)
2744 return (error);
2745
2746 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2747
2748 vrele(nd.ni_vp);
2749 return (error);
2750 }
2751
2752 /*
2753 * Set ownership given a file descriptor.
2754 */
2755 /* ARGSUSED */
2756 int
2757 sys_fchown(struct lwp *l, const struct sys_fchown_args *uap, register_t *retval)
2758 {
2759 /* {
2760 syscallarg(int) fd;
2761 syscallarg(uid_t) uid;
2762 syscallarg(gid_t) gid;
2763 } */
2764 int error;
2765 file_t *fp;
2766
2767 /* fd_getvnode() will use the descriptor for us */
2768 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2769 return (error);
2770 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
2771 l, 0);
2772 fd_putfile(SCARG(uap, fd));
2773 return (error);
2774 }
2775
2776 /*
2777 * Set ownership given a file descriptor, providing POSIX/XPG semantics.
2778 */
2779 /* ARGSUSED */
2780 int
2781 sys___posix_fchown(struct lwp *l, const struct sys___posix_fchown_args *uap, register_t *retval)
2782 {
2783 /* {
2784 syscallarg(int) fd;
2785 syscallarg(uid_t) uid;
2786 syscallarg(gid_t) gid;
2787 } */
2788 int error;
2789 file_t *fp;
2790
2791 /* fd_getvnode() will use the descriptor for us */
2792 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2793 return (error);
2794 error = change_owner(fp->f_data, SCARG(uap, uid), SCARG(uap, gid),
2795 l, 1);
2796 fd_putfile(SCARG(uap, fd));
2797 return (error);
2798 }
2799
2800 /*
2801 * Set ownership given a path name; this version does not follow links.
2802 */
2803 /* ARGSUSED */
2804 int
2805 sys_lchown(struct lwp *l, const struct sys_lchown_args *uap, register_t *retval)
2806 {
2807 /* {
2808 syscallarg(const char *) path;
2809 syscallarg(uid_t) uid;
2810 syscallarg(gid_t) gid;
2811 } */
2812 int error;
2813 struct nameidata nd;
2814
2815 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2816 SCARG(uap, path));
2817 if ((error = namei(&nd)) != 0)
2818 return (error);
2819
2820 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 0);
2821
2822 vrele(nd.ni_vp);
2823 return (error);
2824 }
2825
2826 /*
2827 * Set ownership given a path name; this version does not follow links.
2828 * Provides POSIX/XPG semantics.
2829 */
2830 /* ARGSUSED */
2831 int
2832 sys___posix_lchown(struct lwp *l, const struct sys___posix_lchown_args *uap, register_t *retval)
2833 {
2834 /* {
2835 syscallarg(const char *) path;
2836 syscallarg(uid_t) uid;
2837 syscallarg(gid_t) gid;
2838 } */
2839 int error;
2840 struct nameidata nd;
2841
2842 NDINIT(&nd, LOOKUP, NOFOLLOW | TRYEMULROOT, UIO_USERSPACE,
2843 SCARG(uap, path));
2844 if ((error = namei(&nd)) != 0)
2845 return (error);
2846
2847 error = change_owner(nd.ni_vp, SCARG(uap, uid), SCARG(uap, gid), l, 1);
2848
2849 vrele(nd.ni_vp);
2850 return (error);
2851 }
2852
2853 /*
2854 * Common routine to set ownership given a vnode.
2855 */
2856 static int
2857 change_owner(struct vnode *vp, uid_t uid, gid_t gid, struct lwp *l,
2858 int posix_semantics)
2859 {
2860 struct vattr vattr;
2861 mode_t newmode;
2862 int error;
2863
2864 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2865 if ((error = VOP_GETATTR(vp, &vattr, l->l_cred)) != 0)
2866 goto out;
2867
2868 #define CHANGED(x) ((int)(x) != -1)
2869 newmode = vattr.va_mode;
2870 if (posix_semantics) {
2871 /*
2872 * POSIX/XPG semantics: if the caller is not the super-user,
2873 * clear set-user-id and set-group-id bits. Both POSIX and
2874 * the XPG consider the behaviour for calls by the super-user
2875 * implementation-defined; we leave the set-user-id and set-
2876 * group-id settings intact in that case.
2877 */
2878 if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
2879 NULL) != 0)
2880 newmode &= ~(S_ISUID | S_ISGID);
2881 } else {
2882 /*
2883 * NetBSD semantics: when changing owner and/or group,
2884 * clear the respective bit(s).
2885 */
2886 if (CHANGED(uid))
2887 newmode &= ~S_ISUID;
2888 if (CHANGED(gid))
2889 newmode &= ~S_ISGID;
2890 }
2891 /* Update va_mode iff altered. */
2892 if (vattr.va_mode == newmode)
2893 newmode = VNOVAL;
2894
2895 VATTR_NULL(&vattr);
2896 vattr.va_uid = CHANGED(uid) ? uid : (uid_t)VNOVAL;
2897 vattr.va_gid = CHANGED(gid) ? gid : (gid_t)VNOVAL;
2898 vattr.va_mode = newmode;
2899 error = VOP_SETATTR(vp, &vattr, l->l_cred);
2900 #undef CHANGED
2901
2902 out:
2903 VOP_UNLOCK(vp, 0);
2904 return (error);
2905 }
2906
2907 /*
2908 * Set the access and modification times given a path name; this
2909 * version follows links.
2910 */
2911 /* ARGSUSED */
2912 int
2913 sys_utimes(struct lwp *l, const struct sys_utimes_args *uap, register_t *retval)
2914 {
2915 /* {
2916 syscallarg(const char *) path;
2917 syscallarg(const struct timeval *) tptr;
2918 } */
2919
2920 return do_sys_utimes(l, NULL, SCARG(uap, path), FOLLOW,
2921 SCARG(uap, tptr), UIO_USERSPACE);
2922 }
2923
2924 /*
2925 * Set the access and modification times given a file descriptor.
2926 */
2927 /* ARGSUSED */
2928 int
2929 sys_futimes(struct lwp *l, const struct sys_futimes_args *uap, register_t *retval)
2930 {
2931 /* {
2932 syscallarg(int) fd;
2933 syscallarg(const struct timeval *) tptr;
2934 } */
2935 int error;
2936 file_t *fp;
2937
2938 /* fd_getvnode() will use the descriptor for us */
2939 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
2940 return (error);
2941 error = do_sys_utimes(l, fp->f_data, NULL, 0, SCARG(uap, tptr),
2942 UIO_USERSPACE);
2943 fd_putfile(SCARG(uap, fd));
2944 return (error);
2945 }
2946
2947 /*
2948 * Set the access and modification times given a path name; this
2949 * version does not follow links.
2950 */
2951 int
2952 sys_lutimes(struct lwp *l, const struct sys_lutimes_args *uap, register_t *retval)
2953 {
2954 /* {
2955 syscallarg(const char *) path;
2956 syscallarg(const struct timeval *) tptr;
2957 } */
2958
2959 return do_sys_utimes(l, NULL, SCARG(uap, path), NOFOLLOW,
2960 SCARG(uap, tptr), UIO_USERSPACE);
2961 }
2962
2963 /*
2964 * Common routine to set access and modification times given a vnode.
2965 */
2966 int
2967 do_sys_utimes(struct lwp *l, struct vnode *vp, const char *path, int flag,
2968 const struct timeval *tptr, enum uio_seg seg)
2969 {
2970 struct vattr vattr;
2971 struct nameidata nd;
2972 int error;
2973
2974 VATTR_NULL(&vattr);
2975 if (tptr == NULL) {
2976 nanotime(&vattr.va_atime);
2977 vattr.va_mtime = vattr.va_atime;
2978 vattr.va_vaflags |= VA_UTIMES_NULL;
2979 } else {
2980 struct timeval tv[2];
2981
2982 if (seg != UIO_SYSSPACE) {
2983 error = copyin(tptr, &tv, sizeof (tv));
2984 if (error != 0)
2985 return error;
2986 tptr = tv;
2987 }
2988 TIMEVAL_TO_TIMESPEC(tptr, &vattr.va_atime);
2989 TIMEVAL_TO_TIMESPEC(tptr + 1, &vattr.va_mtime);
2990 }
2991
2992 if (vp == NULL) {
2993 NDINIT(&nd, LOOKUP, flag | TRYEMULROOT, UIO_USERSPACE, path);
2994 if ((error = namei(&nd)) != 0)
2995 return (error);
2996 vp = nd.ni_vp;
2997 } else
2998 nd.ni_vp = NULL;
2999
3000 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3001 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3002 VOP_UNLOCK(vp, 0);
3003
3004 if (nd.ni_vp != NULL)
3005 vrele(nd.ni_vp);
3006
3007 return (error);
3008 }
3009
3010 /*
3011 * Truncate a file given its path name.
3012 */
3013 /* ARGSUSED */
3014 int
3015 sys_truncate(struct lwp *l, const struct sys_truncate_args *uap, register_t *retval)
3016 {
3017 /* {
3018 syscallarg(const char *) path;
3019 syscallarg(int) pad;
3020 syscallarg(off_t) length;
3021 } */
3022 struct vnode *vp;
3023 struct vattr vattr;
3024 int error;
3025 struct nameidata nd;
3026
3027 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
3028 SCARG(uap, path));
3029 if ((error = namei(&nd)) != 0)
3030 return (error);
3031 vp = nd.ni_vp;
3032 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3033 if (vp->v_type == VDIR)
3034 error = EISDIR;
3035 else if ((error = vn_writechk(vp)) == 0 &&
3036 (error = VOP_ACCESS(vp, VWRITE, l->l_cred)) == 0) {
3037 VATTR_NULL(&vattr);
3038 vattr.va_size = SCARG(uap, length);
3039 error = VOP_SETATTR(vp, &vattr, l->l_cred);
3040 }
3041 vput(vp);
3042 return (error);
3043 }
3044
3045 /*
3046 * Truncate a file given a file descriptor.
3047 */
3048 /* ARGSUSED */
3049 int
3050 sys_ftruncate(struct lwp *l, const struct sys_ftruncate_args *uap, register_t *retval)
3051 {
3052 /* {
3053 syscallarg(int) fd;
3054 syscallarg(int) pad;
3055 syscallarg(off_t) length;
3056 } */
3057 struct vattr vattr;
3058 struct vnode *vp;
3059 file_t *fp;
3060 int error;
3061
3062 /* fd_getvnode() will use the descriptor for us */
3063 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3064 return (error);
3065 if ((fp->f_flag & FWRITE) == 0) {
3066 error = EINVAL;
3067 goto out;
3068 }
3069 vp = fp->f_data;
3070 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3071 if (vp->v_type == VDIR)
3072 error = EISDIR;
3073 else if ((error = vn_writechk(vp)) == 0) {
3074 VATTR_NULL(&vattr);
3075 vattr.va_size = SCARG(uap, length);
3076 error = VOP_SETATTR(vp, &vattr, fp->f_cred);
3077 }
3078 VOP_UNLOCK(vp, 0);
3079 out:
3080 fd_putfile(SCARG(uap, fd));
3081 return (error);
3082 }
3083
3084 /*
3085 * Sync an open file.
3086 */
3087 /* ARGSUSED */
3088 int
3089 sys_fsync(struct lwp *l, const struct sys_fsync_args *uap, register_t *retval)
3090 {
3091 /* {
3092 syscallarg(int) fd;
3093 } */
3094 struct vnode *vp;
3095 file_t *fp;
3096 int error;
3097
3098 /* fd_getvnode() will use the descriptor for us */
3099 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3100 return (error);
3101 vp = fp->f_data;
3102 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3103 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT, 0, 0);
3104 if (error == 0 && bioopsp != NULL &&
3105 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3106 (*bioopsp->io_fsync)(vp, 0);
3107 VOP_UNLOCK(vp, 0);
3108 fd_putfile(SCARG(uap, fd));
3109 return (error);
3110 }
3111
3112 /*
3113 * Sync a range of file data. API modeled after that found in AIX.
3114 *
3115 * FDATASYNC indicates that we need only save enough metadata to be able
3116 * to re-read the written data. Note we duplicate AIX's requirement that
3117 * the file be open for writing.
3118 */
3119 /* ARGSUSED */
3120 int
3121 sys_fsync_range(struct lwp *l, const struct sys_fsync_range_args *uap, register_t *retval)
3122 {
3123 /* {
3124 syscallarg(int) fd;
3125 syscallarg(int) flags;
3126 syscallarg(off_t) start;
3127 syscallarg(off_t) length;
3128 } */
3129 struct vnode *vp;
3130 file_t *fp;
3131 int flags, nflags;
3132 off_t s, e, len;
3133 int error;
3134
3135 /* fd_getvnode() will use the descriptor for us */
3136 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3137 return (error);
3138
3139 if ((fp->f_flag & FWRITE) == 0) {
3140 error = EBADF;
3141 goto out;
3142 }
3143
3144 flags = SCARG(uap, flags);
3145 if (((flags & (FDATASYNC | FFILESYNC)) == 0) ||
3146 ((~flags & (FDATASYNC | FFILESYNC)) == 0)) {
3147 error = EINVAL;
3148 goto out;
3149 }
3150 /* Now set up the flags for value(s) to pass to VOP_FSYNC() */
3151 if (flags & FDATASYNC)
3152 nflags = FSYNC_DATAONLY | FSYNC_WAIT;
3153 else
3154 nflags = FSYNC_WAIT;
3155 if (flags & FDISKSYNC)
3156 nflags |= FSYNC_CACHE;
3157
3158 len = SCARG(uap, length);
3159 /* If length == 0, we do the whole file, and s = l = 0 will do that */
3160 if (len) {
3161 s = SCARG(uap, start);
3162 e = s + len;
3163 if (e < s) {
3164 error = EINVAL;
3165 goto out;
3166 }
3167 } else {
3168 e = 0;
3169 s = 0;
3170 }
3171
3172 vp = fp->f_data;
3173 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3174 error = VOP_FSYNC(vp, fp->f_cred, nflags, s, e);
3175
3176 if (error == 0 && bioopsp != NULL &&
3177 vp->v_mount && (vp->v_mount->mnt_flag & MNT_SOFTDEP))
3178 (*bioopsp->io_fsync)(vp, nflags);
3179
3180 VOP_UNLOCK(vp, 0);
3181 out:
3182 fd_putfile(SCARG(uap, fd));
3183 return (error);
3184 }
3185
3186 /*
3187 * Sync the data of an open file.
3188 */
3189 /* ARGSUSED */
3190 int
3191 sys_fdatasync(struct lwp *l, const struct sys_fdatasync_args *uap, register_t *retval)
3192 {
3193 /* {
3194 syscallarg(int) fd;
3195 } */
3196 struct vnode *vp;
3197 file_t *fp;
3198 int error;
3199
3200 /* fd_getvnode() will use the descriptor for us */
3201 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3202 return (error);
3203 if ((fp->f_flag & FWRITE) == 0) {
3204 fd_putfile(SCARG(uap, fd));
3205 return (EBADF);
3206 }
3207 vp = fp->f_data;
3208 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3209 error = VOP_FSYNC(vp, fp->f_cred, FSYNC_WAIT|FSYNC_DATAONLY, 0, 0);
3210 VOP_UNLOCK(vp, 0);
3211 fd_putfile(SCARG(uap, fd));
3212 return (error);
3213 }
3214
3215 /*
3216 * Rename files, (standard) BSD semantics frontend.
3217 */
3218 /* ARGSUSED */
3219 int
3220 sys_rename(struct lwp *l, const struct sys_rename_args *uap, register_t *retval)
3221 {
3222 /* {
3223 syscallarg(const char *) from;
3224 syscallarg(const char *) to;
3225 } */
3226
3227 return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 0));
3228 }
3229
3230 /*
3231 * Rename files, POSIX semantics frontend.
3232 */
3233 /* ARGSUSED */
3234 int
3235 sys___posix_rename(struct lwp *l, const struct sys___posix_rename_args *uap, register_t *retval)
3236 {
3237 /* {
3238 syscallarg(const char *) from;
3239 syscallarg(const char *) to;
3240 } */
3241
3242 return (do_sys_rename(SCARG(uap, from), SCARG(uap, to), UIO_USERSPACE, 1));
3243 }
3244
3245 /*
3246 * Rename files. Source and destination must either both be directories,
3247 * or both not be directories. If target is a directory, it must be empty.
3248 * If `from' and `to' refer to the same object, the value of the `retain'
3249 * argument is used to determine whether `from' will be
3250 *
3251 * (retain == 0) deleted unless `from' and `to' refer to the same
3252 * object in the file system's name space (BSD).
3253 * (retain == 1) always retained (POSIX).
3254 */
3255 int
3256 do_sys_rename(const char *from, const char *to, enum uio_seg seg, int retain)
3257 {
3258 struct vnode *tvp, *fvp, *tdvp;
3259 struct nameidata fromnd, tond;
3260 struct mount *fs;
3261 struct lwp *l = curlwp;
3262 struct proc *p;
3263 uint32_t saveflag;
3264 int error;
3265
3266 NDINIT(&fromnd, DELETE, LOCKPARENT | SAVESTART | TRYEMULROOT,
3267 seg, from);
3268 if ((error = namei(&fromnd)) != 0)
3269 return (error);
3270 if (fromnd.ni_dvp != fromnd.ni_vp)
3271 VOP_UNLOCK(fromnd.ni_dvp, 0);
3272 fvp = fromnd.ni_vp;
3273
3274 fs = fvp->v_mount;
3275 error = VFS_RENAMELOCK_ENTER(fs);
3276 if (error) {
3277 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3278 vrele(fromnd.ni_dvp);
3279 vrele(fvp);
3280 goto out1;
3281 }
3282
3283 /*
3284 * close, partially, yet another race - ideally we should only
3285 * go as far as getting fromnd.ni_dvp before getting the per-fs
3286 * lock, and then continue to get fromnd.ni_vp, but we can't do
3287 * that with namei as it stands.
3288 *
3289 * This still won't prevent rmdir from nuking fromnd.ni_vp
3290 * under us. The real fix is to get the locks in the right
3291 * order and do the lookups in the right places, but that's a
3292 * major rototill.
3293 *
3294 * Preserve the SAVESTART in cn_flags, because who knows what
3295 * might happen if we don't.
3296 *
3297 * Note: this logic (as well as this whole function) is cloned
3298 * in nfs_serv.c. Proceed accordingly.
3299 */
3300 vrele(fvp);
3301 if ((fromnd.ni_cnd.cn_namelen == 1 &&
3302 fromnd.ni_cnd.cn_nameptr[0] == '.') ||
3303 (fromnd.ni_cnd.cn_namelen == 2 &&
3304 fromnd.ni_cnd.cn_nameptr[0] == '.' &&
3305 fromnd.ni_cnd.cn_nameptr[1] == '.')) {
3306 error = EINVAL;
3307 VFS_RENAMELOCK_EXIT(fs);
3308 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3309 vrele(fromnd.ni_dvp);
3310 goto out1;
3311 }
3312 saveflag = fromnd.ni_cnd.cn_flags & SAVESTART;
3313 fromnd.ni_cnd.cn_flags &= ~SAVESTART;
3314 vn_lock(fromnd.ni_dvp, LK_EXCLUSIVE | LK_RETRY);
3315 error = relookup(fromnd.ni_dvp, &fromnd.ni_vp, &fromnd.ni_cnd);
3316 fromnd.ni_cnd.cn_flags |= saveflag;
3317 if (error) {
3318 VOP_UNLOCK(fromnd.ni_dvp, 0);
3319 VFS_RENAMELOCK_EXIT(fs);
3320 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3321 vrele(fromnd.ni_dvp);
3322 goto out1;
3323 }
3324 VOP_UNLOCK(fromnd.ni_vp, 0);
3325 if (fromnd.ni_dvp != fromnd.ni_vp)
3326 VOP_UNLOCK(fromnd.ni_dvp, 0);
3327 fvp = fromnd.ni_vp;
3328
3329 NDINIT(&tond, RENAME,
3330 LOCKPARENT | LOCKLEAF | NOCACHE | SAVESTART | TRYEMULROOT
3331 | (fvp->v_type == VDIR ? CREATEDIR : 0),
3332 seg, to);
3333 if ((error = namei(&tond)) != 0) {
3334 VFS_RENAMELOCK_EXIT(fs);
3335 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3336 vrele(fromnd.ni_dvp);
3337 vrele(fvp);
3338 goto out1;
3339 }
3340 tdvp = tond.ni_dvp;
3341 tvp = tond.ni_vp;
3342
3343 if (tvp != NULL) {
3344 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3345 error = ENOTDIR;
3346 goto out;
3347 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3348 error = EISDIR;
3349 goto out;
3350 }
3351 }
3352
3353 if (fvp == tdvp)
3354 error = EINVAL;
3355
3356 /*
3357 * Source and destination refer to the same object.
3358 */
3359 if (fvp == tvp) {
3360 if (retain)
3361 error = -1;
3362 else if (fromnd.ni_dvp == tdvp &&
3363 fromnd.ni_cnd.cn_namelen == tond.ni_cnd.cn_namelen &&
3364 !memcmp(fromnd.ni_cnd.cn_nameptr,
3365 tond.ni_cnd.cn_nameptr,
3366 fromnd.ni_cnd.cn_namelen))
3367 error = -1;
3368 }
3369
3370 #if NVERIEXEC > 0
3371 if (!error) {
3372 char *f1, *f2;
3373
3374 f1 = malloc(fromnd.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3375 strlcpy(f1, fromnd.ni_cnd.cn_nameptr, fromnd.ni_cnd.cn_namelen);
3376
3377 f2 = malloc(tond.ni_cnd.cn_namelen + 1, M_TEMP, M_WAITOK);
3378 strlcpy(f2, tond.ni_cnd.cn_nameptr, tond.ni_cnd.cn_namelen);
3379
3380 error = veriexec_renamechk(l, fvp, f1, tvp, f2);
3381
3382 free(f1, M_TEMP);
3383 free(f2, M_TEMP);
3384 }
3385 #endif /* NVERIEXEC > 0 */
3386
3387 out:
3388 p = l->l_proc;
3389 if (!error) {
3390 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3391 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3392 VFS_RENAMELOCK_EXIT(fs);
3393 } else {
3394 VOP_ABORTOP(tond.ni_dvp, &tond.ni_cnd);
3395 if (tdvp == tvp)
3396 vrele(tdvp);
3397 else
3398 vput(tdvp);
3399 if (tvp)
3400 vput(tvp);
3401 VFS_RENAMELOCK_EXIT(fs);
3402 VOP_ABORTOP(fromnd.ni_dvp, &fromnd.ni_cnd);
3403 vrele(fromnd.ni_dvp);
3404 vrele(fvp);
3405 }
3406 vrele(tond.ni_startdir);
3407 PNBUF_PUT(tond.ni_cnd.cn_pnbuf);
3408 out1:
3409 if (fromnd.ni_startdir)
3410 vrele(fromnd.ni_startdir);
3411 PNBUF_PUT(fromnd.ni_cnd.cn_pnbuf);
3412 return (error == -1 ? 0 : error);
3413 }
3414
3415 /*
3416 * Make a directory file.
3417 */
3418 /* ARGSUSED */
3419 int
3420 sys_mkdir(struct lwp *l, const struct sys_mkdir_args *uap, register_t *retval)
3421 {
3422 /* {
3423 syscallarg(const char *) path;
3424 syscallarg(int) mode;
3425 } */
3426 struct proc *p = l->l_proc;
3427 struct vnode *vp;
3428 struct vattr vattr;
3429 int error;
3430 struct nameidata nd;
3431
3432 NDINIT(&nd, CREATE, LOCKPARENT | CREATEDIR | TRYEMULROOT, UIO_USERSPACE,
3433 SCARG(uap, path));
3434 if ((error = namei(&nd)) != 0)
3435 return (error);
3436 vp = nd.ni_vp;
3437 if (vp != NULL) {
3438 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3439 if (nd.ni_dvp == vp)
3440 vrele(nd.ni_dvp);
3441 else
3442 vput(nd.ni_dvp);
3443 vrele(vp);
3444 return (EEXIST);
3445 }
3446 VATTR_NULL(&vattr);
3447 vattr.va_type = VDIR;
3448 /* We will read cwdi->cwdi_cmask unlocked. */
3449 vattr.va_mode =
3450 (SCARG(uap, mode) & ACCESSPERMS) &~ p->p_cwdi->cwdi_cmask;
3451 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3452 if (!error)
3453 vput(nd.ni_vp);
3454 return (error);
3455 }
3456
3457 /*
3458 * Remove a directory file.
3459 */
3460 /* ARGSUSED */
3461 int
3462 sys_rmdir(struct lwp *l, const struct sys_rmdir_args *uap, register_t *retval)
3463 {
3464 /* {
3465 syscallarg(const char *) path;
3466 } */
3467 struct vnode *vp;
3468 int error;
3469 struct nameidata nd;
3470
3471 NDINIT(&nd, DELETE, LOCKPARENT | LOCKLEAF | TRYEMULROOT, UIO_USERSPACE,
3472 SCARG(uap, path));
3473 if ((error = namei(&nd)) != 0)
3474 return (error);
3475 vp = nd.ni_vp;
3476 if (vp->v_type != VDIR) {
3477 error = ENOTDIR;
3478 goto out;
3479 }
3480 /*
3481 * No rmdir "." please.
3482 */
3483 if (nd.ni_dvp == vp) {
3484 error = EINVAL;
3485 goto out;
3486 }
3487 /*
3488 * The root of a mounted filesystem cannot be deleted.
3489 */
3490 if (vp->v_vflag & VV_ROOT) {
3491 error = EBUSY;
3492 goto out;
3493 }
3494 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3495 return (error);
3496
3497 out:
3498 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd);
3499 if (nd.ni_dvp == vp)
3500 vrele(nd.ni_dvp);
3501 else
3502 vput(nd.ni_dvp);
3503 vput(vp);
3504 return (error);
3505 }
3506
3507 /*
3508 * Read a block of directory entries in a file system independent format.
3509 */
3510 int
3511 sys___getdents30(struct lwp *l, const struct sys___getdents30_args *uap, register_t *retval)
3512 {
3513 /* {
3514 syscallarg(int) fd;
3515 syscallarg(char *) buf;
3516 syscallarg(size_t) count;
3517 } */
3518 file_t *fp;
3519 int error, done;
3520
3521 /* fd_getvnode() will use the descriptor for us */
3522 if ((error = fd_getvnode(SCARG(uap, fd), &fp)) != 0)
3523 return (error);
3524 if ((fp->f_flag & FREAD) == 0) {
3525 error = EBADF;
3526 goto out;
3527 }
3528 error = vn_readdir(fp, SCARG(uap, buf), UIO_USERSPACE,
3529 SCARG(uap, count), &done, l, 0, 0);
3530 ktrgenio(SCARG(uap, fd), UIO_READ, SCARG(uap, buf), done, error);
3531 *retval = done;
3532 out:
3533 fd_putfile(SCARG(uap, fd));
3534 return (error);
3535 }
3536
3537 /*
3538 * Set the mode mask for creation of filesystem nodes.
3539 */
3540 int
3541 sys_umask(struct lwp *l, const struct sys_umask_args *uap, register_t *retval)
3542 {
3543 /* {
3544 syscallarg(mode_t) newmask;
3545 } */
3546 struct proc *p = l->l_proc;
3547 struct cwdinfo *cwdi;
3548
3549 /*
3550 * cwdi->cwdi_cmask will be read unlocked elsewhere. What's
3551 * important is that we serialize changes to the mask. The
3552 * rw_exit() will issue a write memory barrier on our behalf,
3553 * and force the changes out to other CPUs (as it must use an
3554 * atomic operation, draining the local CPU's store buffers).
3555 */
3556 cwdi = p->p_cwdi;
3557 rw_enter(&cwdi->cwdi_lock, RW_WRITER);
3558 *retval = cwdi->cwdi_cmask;
3559 cwdi->cwdi_cmask = SCARG(uap, newmask) & ALLPERMS;
3560 rw_exit(&cwdi->cwdi_lock);
3561
3562 return (0);
3563 }
3564
3565 int
3566 dorevoke(struct vnode *vp, kauth_cred_t cred)
3567 {
3568 struct vattr vattr;
3569 int error;
3570
3571 if ((error = VOP_GETATTR(vp, &vattr, cred)) != 0)
3572 return error;
3573 if (kauth_cred_geteuid(cred) != vattr.va_uid &&
3574 (error = kauth_authorize_generic(cred,
3575 KAUTH_GENERIC_ISSUSER, NULL)) == 0)
3576 VOP_REVOKE(vp, REVOKEALL);
3577 return (error);
3578 }
3579
3580 /*
3581 * Void all references to file by ripping underlying filesystem
3582 * away from vnode.
3583 */
3584 /* ARGSUSED */
3585 int
3586 sys_revoke(struct lwp *l, const struct sys_revoke_args *uap, register_t *retval)
3587 {
3588 /* {
3589 syscallarg(const char *) path;
3590 } */
3591 struct vnode *vp;
3592 int error;
3593 struct nameidata nd;
3594
3595 NDINIT(&nd, LOOKUP, FOLLOW | TRYEMULROOT, UIO_USERSPACE,
3596 SCARG(uap, path));
3597 if ((error = namei(&nd)) != 0)
3598 return (error);
3599 vp = nd.ni_vp;
3600 error = dorevoke(vp, l->l_cred);
3601 vrele(vp);
3602 return (error);
3603 }
3604
3605 /*
3606 * Convert a user file descriptor to a kernel file entry.
3607 */
3608 int
3609 getvnode(int fd, file_t **fpp)
3610 {
3611 struct vnode *vp;
3612 file_t *fp;
3613
3614 if ((fp = fd_getfile(fd)) == NULL)
3615 return (EBADF);
3616
3617 if (fp->f_type != DTYPE_VNODE) {
3618 fd_putfile(fd);
3619 return (EINVAL);
3620 }
3621
3622 vp = fp->f_data;
3623 if (vp->v_type == VBAD) {
3624 fd_putfile(fd);
3625 return (EBADF);
3626 }
3627
3628 *fpp = fp;
3629 return (0);
3630 }
3631