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