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