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