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