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