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