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