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