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