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