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