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