vfs_vnops.c revision 1.204
1/*	$NetBSD: vfs_vnops.c,v 1.204 2019/12/16 22:47:54 ad Exp $	*/
2
3/*-
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1982, 1986, 1989, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 * (c) UNIX System Laboratories, Inc.
36 * All or some portions of this file are derived from material licensed
37 * to the University of California by American Telephone and Telegraph
38 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
39 * the permission of UNIX System Laboratories, Inc.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 *    notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 *    notice, this list of conditions and the following disclaimer in the
48 *    documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 *    may be used to endorse or promote products derived from this software
51 *    without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 *	@(#)vfs_vnops.c	8.14 (Berkeley) 6/15/95
66 */
67
68#include <sys/cdefs.h>
69__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.204 2019/12/16 22:47:54 ad Exp $");
70
71#include "veriexec.h"
72
73#include <sys/param.h>
74#include <sys/systm.h>
75#include <sys/kernel.h>
76#include <sys/file.h>
77#include <sys/stat.h>
78#include <sys/buf.h>
79#include <sys/proc.h>
80#include <sys/mount.h>
81#include <sys/namei.h>
82#include <sys/vnode.h>
83#include <sys/ioctl.h>
84#include <sys/tty.h>
85#include <sys/poll.h>
86#include <sys/kauth.h>
87#include <sys/syslog.h>
88#include <sys/fstrans.h>
89#include <sys/atomic.h>
90#include <sys/filedesc.h>
91#include <sys/wapbl.h>
92#include <sys/mman.h>
93
94#include <miscfs/specfs/specdev.h>
95#include <miscfs/fifofs/fifo.h>
96
97#include <uvm/uvm_extern.h>
98#include <uvm/uvm_readahead.h>
99#include <uvm/uvm_device.h>
100
101#ifdef UNION
102#include <fs/union/union.h>
103#endif
104
105#ifndef COMPAT_ZERODEV
106#define COMPAT_ZERODEV(dev)	(0)
107#endif
108
109int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
110
111#include <sys/verified_exec.h>
112
113static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
114	    kauth_cred_t cred, int flags);
115static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
116	    kauth_cred_t cred, int flags);
117static int vn_closefile(file_t *fp);
118static int vn_poll(file_t *fp, int events);
119static int vn_fcntl(file_t *fp, u_int com, void *data);
120static int vn_statfile(file_t *fp, struct stat *sb);
121static int vn_ioctl(file_t *fp, u_long com, void *data);
122static int vn_mmap(struct file *, off_t *, size_t, int, int *, int *,
123		   struct uvm_object **, int *);
124
125const struct fileops vnops = {
126	.fo_name = "vn",
127	.fo_read = vn_read,
128	.fo_write = vn_write,
129	.fo_ioctl = vn_ioctl,
130	.fo_fcntl = vn_fcntl,
131	.fo_poll = vn_poll,
132	.fo_stat = vn_statfile,
133	.fo_close = vn_closefile,
134	.fo_kqfilter = vn_kqfilter,
135	.fo_restart = fnullop_restart,
136	.fo_mmap = vn_mmap,
137};
138
139/*
140 * Common code for vnode open operations.
141 * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
142 */
143int
144vn_open(struct nameidata *ndp, int fmode, int cmode)
145{
146	struct vnode *vp;
147	struct lwp *l = curlwp;
148	kauth_cred_t cred = l->l_cred;
149	struct vattr va;
150	int error;
151	const char *pathstring;
152
153	if ((fmode & (O_CREAT | O_DIRECTORY)) == (O_CREAT | O_DIRECTORY))
154		return EINVAL;
155
156	ndp->ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
157
158	if (fmode & O_CREAT) {
159		ndp->ni_cnd.cn_nameiop = CREATE;
160		ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
161		if ((fmode & O_EXCL) == 0 &&
162		    ((fmode & O_NOFOLLOW) == 0))
163			ndp->ni_cnd.cn_flags |= FOLLOW;
164	} else {
165		ndp->ni_cnd.cn_nameiop = LOOKUP;
166		ndp->ni_cnd.cn_flags |= LOCKLEAF;
167		if ((fmode & O_NOFOLLOW) == 0)
168			ndp->ni_cnd.cn_flags |= FOLLOW;
169	}
170
171	pathstring = pathbuf_stringcopy_get(ndp->ni_pathbuf);
172	if (pathstring == NULL) {
173		return ENOMEM;
174	}
175
176	error = namei(ndp);
177	if (error)
178		goto out;
179
180	vp = ndp->ni_vp;
181
182#if NVERIEXEC > 0
183	error = veriexec_openchk(l, ndp->ni_vp, pathstring, fmode);
184	if (error) {
185		/* We have to release the locks ourselves */
186		if (fmode & O_CREAT) {
187			if (vp == NULL) {
188				vput(ndp->ni_dvp);
189			} else {
190				VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
191				if (ndp->ni_dvp == ndp->ni_vp)
192					vrele(ndp->ni_dvp);
193				else
194					vput(ndp->ni_dvp);
195				ndp->ni_dvp = NULL;
196				vput(vp);
197			}
198		} else {
199			vput(vp);
200		}
201		goto out;
202	}
203#endif /* NVERIEXEC > 0 */
204
205	if (fmode & O_CREAT) {
206		if (ndp->ni_vp == NULL) {
207			vattr_null(&va);
208			va.va_type = VREG;
209			va.va_mode = cmode;
210			if (fmode & O_EXCL)
211				 va.va_vaflags |= VA_EXCLUSIVE;
212			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
213					   &ndp->ni_cnd, &va);
214			if (error) {
215				vput(ndp->ni_dvp);
216				goto out;
217			}
218			fmode &= ~O_TRUNC;
219			vp = ndp->ni_vp;
220			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
221			vput(ndp->ni_dvp);
222		} else {
223			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
224			if (ndp->ni_dvp == ndp->ni_vp)
225				vrele(ndp->ni_dvp);
226			else
227				vput(ndp->ni_dvp);
228			ndp->ni_dvp = NULL;
229			vp = ndp->ni_vp;
230			if (fmode & O_EXCL) {
231				error = EEXIST;
232				goto bad;
233			}
234			fmode &= ~O_CREAT;
235		}
236	} else {
237		vp = ndp->ni_vp;
238	}
239	if (vp->v_type == VSOCK) {
240		error = EOPNOTSUPP;
241		goto bad;
242	}
243	if (ndp->ni_vp->v_type == VLNK) {
244		error = EFTYPE;
245		goto bad;
246	}
247
248	if ((fmode & O_CREAT) == 0) {
249		error = vn_openchk(vp, cred, fmode);
250		if (error != 0)
251			goto bad;
252	}
253
254	if (fmode & O_TRUNC) {
255		vattr_null(&va);
256		va.va_size = 0;
257		error = VOP_SETATTR(vp, &va, cred);
258		if (error != 0)
259			goto bad;
260	}
261	if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
262		goto bad;
263	if (fmode & FWRITE) {
264		mutex_enter(vp->v_interlock);
265		vp->v_writecount++;
266		mutex_exit(vp->v_interlock);
267	}
268
269bad:
270	if (error)
271		vput(vp);
272out:
273	pathbuf_stringcopy_put(ndp->ni_pathbuf, pathstring);
274	return (error);
275}
276
277/*
278 * Check for write permissions on the specified vnode.
279 * Prototype text segments cannot be written.
280 */
281int
282vn_writechk(struct vnode *vp)
283{
284
285	/*
286	 * If the vnode is in use as a process's text,
287	 * we can't allow writing.
288	 */
289	if (vp->v_iflag & VI_TEXT)
290		return (ETXTBSY);
291	return (0);
292}
293
294int
295vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
296{
297	int permbits = 0;
298	int error;
299
300	if (vp->v_type == VNON || vp->v_type == VBAD)
301		return ENXIO;
302
303	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
304		return ENOTDIR;
305
306	if ((fflags & O_REGULAR) != 0 && vp->v_type != VREG)
307		return EFTYPE;
308
309	if ((fflags & FREAD) != 0) {
310		permbits = VREAD;
311	}
312	if ((fflags & FEXEC) != 0) {
313		permbits |= VEXEC;
314	}
315	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
316		permbits |= VWRITE;
317		if (vp->v_type == VDIR) {
318			error = EISDIR;
319			goto bad;
320		}
321		error = vn_writechk(vp);
322		if (error != 0)
323			goto bad;
324	}
325	error = VOP_ACCESS(vp, permbits, cred);
326bad:
327	return error;
328}
329
330/*
331 * Mark a vnode as having executable mappings.
332 */
333void
334vn_markexec(struct vnode *vp)
335{
336
337	if ((vp->v_iflag & VI_EXECMAP) != 0) {
338		/* Safe unlocked, as long as caller holds a reference. */
339		return;
340	}
341
342	mutex_enter(vp->v_interlock);
343	if ((vp->v_iflag & VI_EXECMAP) == 0) {
344		cpu_count(CPU_COUNT_FILEPAGES, -vp->v_uobj.uo_npages);
345		cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
346		vp->v_iflag |= VI_EXECMAP;
347	}
348	mutex_exit(vp->v_interlock);
349}
350
351/*
352 * Mark a vnode as being the text of a process.
353 * Fail if the vnode is currently writable.
354 */
355int
356vn_marktext(struct vnode *vp)
357{
358
359	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
360		/* Safe unlocked, as long as caller holds a reference. */
361		return (0);
362	}
363
364	mutex_enter(vp->v_interlock);
365	if (vp->v_writecount != 0) {
366		KASSERT((vp->v_iflag & VI_TEXT) == 0);
367		mutex_exit(vp->v_interlock);
368		return (ETXTBSY);
369	}
370	if ((vp->v_iflag & VI_EXECMAP) == 0) {
371		cpu_count(CPU_COUNT_FILEPAGES, -vp->v_uobj.uo_npages);
372		cpu_count(CPU_COUNT_EXECPAGES, vp->v_uobj.uo_npages);
373	}
374	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
375	mutex_exit(vp->v_interlock);
376	return (0);
377}
378
379/*
380 * Vnode close call
381 *
382 * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
383 */
384int
385vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
386{
387	int error;
388
389	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
390	if (flags & FWRITE) {
391		mutex_enter(vp->v_interlock);
392		KASSERT(vp->v_writecount > 0);
393		vp->v_writecount--;
394		mutex_exit(vp->v_interlock);
395	}
396	error = VOP_CLOSE(vp, flags, cred);
397	vput(vp);
398	return (error);
399}
400
401static int
402enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
403{
404	struct lwp *l = curlwp;
405	off_t testoff;
406
407	if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
408		return 0;
409
410	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
411	if (ioflag & IO_APPEND)
412		testoff = vp->v_size;
413	else
414		testoff = uio->uio_offset;
415
416	if (testoff + uio->uio_resid >
417	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
418		mutex_enter(proc_lock);
419		psignal(l->l_proc, SIGXFSZ);
420		mutex_exit(proc_lock);
421		return EFBIG;
422	}
423
424	return 0;
425}
426
427/*
428 * Package up an I/O request on a vnode into a uio and do it.
429 */
430int
431vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
432    enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
433    struct lwp *l)
434{
435	struct uio auio;
436	struct iovec aiov;
437	int error;
438
439	if ((ioflg & IO_NODELOCKED) == 0) {
440		if (rw == UIO_READ) {
441			vn_lock(vp, LK_SHARED | LK_RETRY);
442		} else /* UIO_WRITE */ {
443			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
444		}
445	}
446	auio.uio_iov = &aiov;
447	auio.uio_iovcnt = 1;
448	aiov.iov_base = base;
449	aiov.iov_len = len;
450	auio.uio_resid = len;
451	auio.uio_offset = offset;
452	auio.uio_rw = rw;
453	if (segflg == UIO_SYSSPACE) {
454		UIO_SETUP_SYSSPACE(&auio);
455	} else {
456		auio.uio_vmspace = l->l_proc->p_vmspace;
457	}
458
459	if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
460		goto out;
461
462	if (rw == UIO_READ) {
463		error = VOP_READ(vp, &auio, ioflg, cred);
464	} else {
465		error = VOP_WRITE(vp, &auio, ioflg, cred);
466	}
467
468	if (aresid)
469		*aresid = auio.uio_resid;
470	else
471		if (auio.uio_resid && error == 0)
472			error = EIO;
473
474 out:
475	if ((ioflg & IO_NODELOCKED) == 0) {
476		VOP_UNLOCK(vp);
477	}
478	return (error);
479}
480
481int
482vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
483    struct lwp *l, off_t **cookies, int *ncookies)
484{
485	struct vnode *vp = fp->f_vnode;
486	struct iovec aiov;
487	struct uio auio;
488	int error, eofflag;
489
490	/* Limit the size on any kernel buffers used by VOP_READDIR */
491	count = uimin(MAXBSIZE, count);
492
493unionread:
494	if (vp->v_type != VDIR)
495		return (EINVAL);
496	aiov.iov_base = bf;
497	aiov.iov_len = count;
498	auio.uio_iov = &aiov;
499	auio.uio_iovcnt = 1;
500	auio.uio_rw = UIO_READ;
501	if (segflg == UIO_SYSSPACE) {
502		UIO_SETUP_SYSSPACE(&auio);
503	} else {
504		KASSERT(l == curlwp);
505		auio.uio_vmspace = l->l_proc->p_vmspace;
506	}
507	auio.uio_resid = count;
508	vn_lock(vp, LK_SHARED | LK_RETRY);
509	auio.uio_offset = fp->f_offset;
510	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
511		    ncookies);
512	mutex_enter(&fp->f_lock);
513	fp->f_offset = auio.uio_offset;
514	mutex_exit(&fp->f_lock);
515	VOP_UNLOCK(vp);
516	if (error)
517		return (error);
518
519	if (count == auio.uio_resid && vn_union_readdir_hook) {
520		struct vnode *ovp = vp;
521
522		error = (*vn_union_readdir_hook)(&vp, fp, l);
523		if (error)
524			return (error);
525		if (vp != ovp)
526			goto unionread;
527	}
528
529	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
530	    (vp->v_mount->mnt_flag & MNT_UNION)) {
531		struct vnode *tvp = vp;
532		vp = vp->v_mount->mnt_vnodecovered;
533		vref(vp);
534		mutex_enter(&fp->f_lock);
535		fp->f_vnode = vp;
536		fp->f_offset = 0;
537		mutex_exit(&fp->f_lock);
538		vrele(tvp);
539		goto unionread;
540	}
541	*done = count - auio.uio_resid;
542	return error;
543}
544
545/*
546 * File table vnode read routine.
547 */
548static int
549vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
550    int flags)
551{
552	struct vnode *vp = fp->f_vnode;
553	int error, ioflag, fflag;
554	size_t count;
555
556	ioflag = IO_ADV_ENCODE(fp->f_advice);
557	fflag = fp->f_flag;
558	if (fflag & FNONBLOCK)
559		ioflag |= IO_NDELAY;
560	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
561		ioflag |= IO_SYNC;
562	if (fflag & FALTIO)
563		ioflag |= IO_ALTSEMANTICS;
564	if (fflag & FDIRECT)
565		ioflag |= IO_DIRECT;
566	vn_lock(vp, LK_SHARED | LK_RETRY);
567	uio->uio_offset = *offset;
568	count = uio->uio_resid;
569	error = VOP_READ(vp, uio, ioflag, cred);
570	if (flags & FOF_UPDATE_OFFSET)
571		*offset += count - uio->uio_resid;
572	VOP_UNLOCK(vp);
573	return (error);
574}
575
576/*
577 * File table vnode write routine.
578 */
579static int
580vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
581    int flags)
582{
583	struct vnode *vp = fp->f_vnode;
584	int error, ioflag, fflag;
585	size_t count;
586
587	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
588	fflag = fp->f_flag;
589	if (vp->v_type == VREG && (fflag & O_APPEND))
590		ioflag |= IO_APPEND;
591	if (fflag & FNONBLOCK)
592		ioflag |= IO_NDELAY;
593	if (fflag & FFSYNC ||
594	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
595		ioflag |= IO_SYNC;
596	else if (fflag & FDSYNC)
597		ioflag |= IO_DSYNC;
598	if (fflag & FALTIO)
599		ioflag |= IO_ALTSEMANTICS;
600	if (fflag & FDIRECT)
601		ioflag |= IO_DIRECT;
602	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
603	uio->uio_offset = *offset;
604	count = uio->uio_resid;
605
606	if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
607		goto out;
608
609	error = VOP_WRITE(vp, uio, ioflag, cred);
610
611	if (flags & FOF_UPDATE_OFFSET) {
612		if (ioflag & IO_APPEND) {
613			/*
614			 * SUSv3 describes behaviour for count = 0 as following:
615			 * "Before any action ... is taken, and if nbyte is zero
616			 * and the file is a regular file, the write() function
617			 * ... in the absence of errors ... shall return zero
618			 * and have no other results."
619			 */
620			if (count)
621				*offset = uio->uio_offset;
622		} else
623			*offset += count - uio->uio_resid;
624	}
625
626 out:
627	VOP_UNLOCK(vp);
628	return (error);
629}
630
631/*
632 * File table vnode stat routine.
633 */
634static int
635vn_statfile(file_t *fp, struct stat *sb)
636{
637	struct vnode *vp = fp->f_vnode;
638	int error;
639
640	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
641	error = vn_stat(vp, sb);
642	VOP_UNLOCK(vp);
643	return error;
644}
645
646int
647vn_stat(struct vnode *vp, struct stat *sb)
648{
649	struct vattr va;
650	int error;
651	mode_t mode;
652
653	memset(&va, 0, sizeof(va));
654	error = VOP_GETATTR(vp, &va, kauth_cred_get());
655	if (error)
656		return (error);
657	/*
658	 * Copy from vattr table
659	 */
660	memset(sb, 0, sizeof(*sb));
661	sb->st_dev = va.va_fsid;
662	sb->st_ino = va.va_fileid;
663	mode = va.va_mode;
664	switch (vp->v_type) {
665	case VREG:
666		mode |= S_IFREG;
667		break;
668	case VDIR:
669		mode |= S_IFDIR;
670		break;
671	case VBLK:
672		mode |= S_IFBLK;
673		break;
674	case VCHR:
675		mode |= S_IFCHR;
676		break;
677	case VLNK:
678		mode |= S_IFLNK;
679		break;
680	case VSOCK:
681		mode |= S_IFSOCK;
682		break;
683	case VFIFO:
684		mode |= S_IFIFO;
685		break;
686	default:
687		return (EBADF);
688	}
689	sb->st_mode = mode;
690	sb->st_nlink = va.va_nlink;
691	sb->st_uid = va.va_uid;
692	sb->st_gid = va.va_gid;
693	sb->st_rdev = va.va_rdev;
694	sb->st_size = va.va_size;
695	sb->st_atimespec = va.va_atime;
696	sb->st_mtimespec = va.va_mtime;
697	sb->st_ctimespec = va.va_ctime;
698	sb->st_birthtimespec = va.va_birthtime;
699	sb->st_blksize = va.va_blocksize;
700	sb->st_flags = va.va_flags;
701	sb->st_gen = 0;
702	sb->st_blocks = va.va_bytes / S_BLKSIZE;
703	return (0);
704}
705
706/*
707 * File table vnode fcntl routine.
708 */
709static int
710vn_fcntl(file_t *fp, u_int com, void *data)
711{
712	struct vnode *vp = fp->f_vnode;
713	int error;
714
715	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
716	return (error);
717}
718
719/*
720 * File table vnode ioctl routine.
721 */
722static int
723vn_ioctl(file_t *fp, u_long com, void *data)
724{
725	struct vnode *vp = fp->f_vnode, *ovp;
726	struct vattr vattr;
727	int error;
728
729	switch (vp->v_type) {
730
731	case VREG:
732	case VDIR:
733		if (com == FIONREAD) {
734			vn_lock(vp, LK_SHARED | LK_RETRY);
735			error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
736			VOP_UNLOCK(vp);
737			if (error)
738				return (error);
739			*(int *)data = vattr.va_size - fp->f_offset;
740			return (0);
741		}
742		if ((com == FIONWRITE) || (com == FIONSPACE)) {
743			/*
744			 * Files don't have send queues, so there never
745			 * are any bytes in them, nor is there any
746			 * open space in them.
747			 */
748			*(int *)data = 0;
749			return (0);
750		}
751		if (com == FIOGETBMAP) {
752			daddr_t *block;
753
754			if (*(daddr_t *)data < 0)
755				return (EINVAL);
756			block = (daddr_t *)data;
757			return (VOP_BMAP(vp, *block, NULL, block, NULL));
758		}
759		if (com == OFIOGETBMAP) {
760			daddr_t ibn, obn;
761
762			if (*(int32_t *)data < 0)
763				return (EINVAL);
764			ibn = (daddr_t)*(int32_t *)data;
765			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
766			*(int32_t *)data = (int32_t)obn;
767			return error;
768		}
769		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
770			return (0);			/* XXX */
771		/* FALLTHROUGH */
772	case VFIFO:
773	case VCHR:
774	case VBLK:
775		error = VOP_IOCTL(vp, com, data, fp->f_flag,
776		    kauth_cred_get());
777		if (error == 0 && com == TIOCSCTTY) {
778			vref(vp);
779			mutex_enter(proc_lock);
780			ovp = curproc->p_session->s_ttyvp;
781			curproc->p_session->s_ttyvp = vp;
782			mutex_exit(proc_lock);
783			if (ovp != NULL)
784				vrele(ovp);
785		}
786		return (error);
787
788	default:
789		return (EPASSTHROUGH);
790	}
791}
792
793/*
794 * File table vnode poll routine.
795 */
796static int
797vn_poll(file_t *fp, int events)
798{
799
800	return (VOP_POLL(fp->f_vnode, events));
801}
802
803/*
804 * File table vnode kqfilter routine.
805 */
806int
807vn_kqfilter(file_t *fp, struct knote *kn)
808{
809
810	return (VOP_KQFILTER(fp->f_vnode, kn));
811}
812
813static int
814vn_mmap(struct file *fp, off_t *offp, size_t size, int prot, int *flagsp,
815	int *advicep, struct uvm_object **uobjp, int *maxprotp)
816{
817	struct uvm_object *uobj;
818	struct vnode *vp;
819	struct vattr va;
820	struct lwp *l;
821	vm_prot_t maxprot;
822	off_t off;
823	int error, flags;
824	bool needwritemap;
825
826	l = curlwp;
827
828	off = *offp;
829	flags = *flagsp;
830	maxprot = VM_PROT_EXECUTE;
831
832	vp = fp->f_vnode;
833	if (vp->v_type != VREG && vp->v_type != VCHR &&
834	    vp->v_type != VBLK) {
835		/* only REG/CHR/BLK support mmap */
836		return ENODEV;
837	}
838	if (vp->v_type != VCHR && off < 0) {
839		return EINVAL;
840	}
841	if (vp->v_type != VCHR && (off_t)(off + size) < off) {
842		/* no offset wrapping */
843		return EOVERFLOW;
844	}
845
846	/* special case: catch SunOS style /dev/zero */
847	if (vp->v_type == VCHR &&
848	    (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
849		*uobjp = NULL;
850		*maxprotp = VM_PROT_ALL;
851		return 0;
852	}
853
854	/*
855	 * Old programs may not select a specific sharing type, so
856	 * default to an appropriate one.
857	 *
858	 * XXX: how does MAP_ANON fit in the picture?
859	 */
860	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
861#if defined(DEBUG)
862		struct proc *p = l->l_proc;
863		printf("WARNING: defaulted mmap() share type to "
864		       "%s (pid %d command %s)\n", vp->v_type == VCHR ?
865		       "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
866		       p->p_comm);
867#endif
868		if (vp->v_type == VCHR)
869			flags |= MAP_SHARED;	/* for a device */
870		else
871			flags |= MAP_PRIVATE;	/* for a file */
872	}
873
874	/*
875	 * MAP_PRIVATE device mappings don't make sense (and aren't
876	 * supported anyway).  However, some programs rely on this,
877	 * so just change it to MAP_SHARED.
878	 */
879	if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
880		flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
881	}
882
883	/*
884	 * now check protection
885	 */
886
887	/* check read access */
888	if (fp->f_flag & FREAD)
889		maxprot |= VM_PROT_READ;
890	else if (prot & PROT_READ) {
891		return EACCES;
892	}
893
894	/* check write access, shared case first */
895	if (flags & MAP_SHARED) {
896		/*
897		 * if the file is writable, only add PROT_WRITE to
898		 * maxprot if the file is not immutable, append-only.
899		 * otherwise, if we have asked for PROT_WRITE, return
900		 * EPERM.
901		 */
902		if (fp->f_flag & FWRITE) {
903			vn_lock(vp, LK_SHARED | LK_RETRY);
904			error = VOP_GETATTR(vp, &va, l->l_cred);
905			VOP_UNLOCK(vp);
906			if (error) {
907				return error;
908			}
909			if ((va.va_flags &
910			     (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
911				maxprot |= VM_PROT_WRITE;
912			else if (prot & PROT_WRITE) {
913				return EPERM;
914			}
915		} else if (prot & PROT_WRITE) {
916			return EACCES;
917		}
918	} else {
919		/* MAP_PRIVATE mappings can always write to */
920		maxprot |= VM_PROT_WRITE;
921	}
922
923	/*
924	 * Don't allow mmap for EXEC if the file system
925	 * is mounted NOEXEC.
926	 */
927	if ((prot & PROT_EXEC) != 0 &&
928	    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
929		return EACCES;
930	}
931
932	if (vp->v_type != VCHR) {
933		error = VOP_MMAP(vp, prot, curlwp->l_cred);
934		if (error) {
935			return error;
936		}
937		vref(vp);
938		uobj = &vp->v_uobj;
939
940		/*
941		 * If the vnode is being mapped with PROT_EXEC,
942		 * then mark it as text.
943		 */
944		if (prot & PROT_EXEC) {
945			vn_markexec(vp);
946		}
947	} else {
948		int i = maxprot;
949
950		/*
951		 * XXX Some devices don't like to be mapped with
952		 * XXX PROT_EXEC or PROT_WRITE, but we don't really
953		 * XXX have a better way of handling this, right now
954		 */
955		do {
956			uobj = udv_attach(vp->v_rdev,
957					  (flags & MAP_SHARED) ? i :
958					  (i & ~VM_PROT_WRITE), off, size);
959			i--;
960		} while ((uobj == NULL) && (i > 0));
961		if (uobj == NULL) {
962			return EINVAL;
963		}
964		*advicep = UVM_ADV_RANDOM;
965	}
966
967	/*
968	 * Set vnode flags to indicate the new kinds of mapping.
969	 * We take the vnode lock in exclusive mode here to serialize
970	 * with direct I/O.
971	 *
972	 * Safe to check for these flag values without a lock, as
973	 * long as a reference to the vnode is held.
974	 */
975	needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
976		(flags & MAP_SHARED) != 0 &&
977		(maxprot & VM_PROT_WRITE) != 0;
978	if ((vp->v_vflag & VV_MAPPED) == 0 || needwritemap) {
979		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
980		vp->v_vflag |= VV_MAPPED;
981		if (needwritemap) {
982			mutex_enter(vp->v_interlock);
983			vp->v_iflag |= VI_WRMAP;
984			mutex_exit(vp->v_interlock);
985		}
986		VOP_UNLOCK(vp);
987	}
988
989#if NVERIEXEC > 0
990
991	/*
992	 * Check if the file can be executed indirectly.
993	 *
994	 * XXX: This gives false warnings about "Incorrect access type"
995	 * XXX: if the mapping is not executable. Harmless, but will be
996	 * XXX: fixed as part of other changes.
997	 */
998	if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT,
999			    NULL)) {
1000
1001		/*
1002		 * Don't allow executable mappings if we can't
1003		 * indirectly execute the file.
1004		 */
1005		if (prot & VM_PROT_EXECUTE) {
1006			return EPERM;
1007		}
1008
1009		/*
1010		 * Strip the executable bit from 'maxprot' to make sure
1011		 * it can't be made executable later.
1012		 */
1013		maxprot &= ~VM_PROT_EXECUTE;
1014	}
1015#endif /* NVERIEXEC > 0 */
1016
1017	*uobjp = uobj;
1018	*maxprotp = maxprot;
1019	*flagsp = flags;
1020
1021	return 0;
1022}
1023
1024
1025
1026/*
1027 * Check that the vnode is still valid, and if so
1028 * acquire requested lock.
1029 */
1030int
1031vn_lock(struct vnode *vp, int flags)
1032{
1033	int error;
1034
1035#if 0
1036	KASSERT(vp->v_usecount > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
1037#endif
1038	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY|
1039	    LK_UPGRADE|LK_DOWNGRADE)) == 0);
1040	KASSERT((flags & LK_NOWAIT) != 0 || !mutex_owned(vp->v_interlock));
1041
1042#ifdef DIAGNOSTIC
1043	if (wapbl_vphaswapbl(vp))
1044		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
1045#endif
1046
1047	error = VOP_LOCK(vp, flags);
1048	if ((flags & LK_RETRY) != 0 && error == ENOENT)
1049		error = VOP_LOCK(vp, flags);
1050
1051	KASSERT((flags & LK_RETRY) == 0 || (flags & LK_NOWAIT) != 0 ||
1052	    error == 0);
1053
1054	return error;
1055}
1056
1057/*
1058 * File table vnode close routine.
1059 */
1060static int
1061vn_closefile(file_t *fp)
1062{
1063
1064	return vn_close(fp->f_vnode, fp->f_flag, fp->f_cred);
1065}
1066
1067/*
1068 * Simplified in-kernel wrapper calls for extended attribute access.
1069 * Both calls pass in a NULL credential, authorizing a "kernel" access.
1070 * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1071 */
1072int
1073vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1074    const char *attrname, size_t *buflen, void *bf, struct lwp *l)
1075{
1076	struct uio auio;
1077	struct iovec aiov;
1078	int error;
1079
1080	aiov.iov_len = *buflen;
1081	aiov.iov_base = bf;
1082
1083	auio.uio_iov = &aiov;
1084	auio.uio_iovcnt = 1;
1085	auio.uio_rw = UIO_READ;
1086	auio.uio_offset = 0;
1087	auio.uio_resid = *buflen;
1088	UIO_SETUP_SYSSPACE(&auio);
1089
1090	if ((ioflg & IO_NODELOCKED) == 0)
1091		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1092
1093	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
1094
1095	if ((ioflg & IO_NODELOCKED) == 0)
1096		VOP_UNLOCK(vp);
1097
1098	if (error == 0)
1099		*buflen = *buflen - auio.uio_resid;
1100
1101	return (error);
1102}
1103
1104/*
1105 * XXX Failure mode if partially written?
1106 */
1107int
1108vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1109    const char *attrname, size_t buflen, const void *bf, struct lwp *l)
1110{
1111	struct uio auio;
1112	struct iovec aiov;
1113	int error;
1114
1115	aiov.iov_len = buflen;
1116	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
1117
1118	auio.uio_iov = &aiov;
1119	auio.uio_iovcnt = 1;
1120	auio.uio_rw = UIO_WRITE;
1121	auio.uio_offset = 0;
1122	auio.uio_resid = buflen;
1123	UIO_SETUP_SYSSPACE(&auio);
1124
1125	if ((ioflg & IO_NODELOCKED) == 0) {
1126		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1127	}
1128
1129	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
1130
1131	if ((ioflg & IO_NODELOCKED) == 0) {
1132		VOP_UNLOCK(vp);
1133	}
1134
1135	return (error);
1136}
1137
1138int
1139vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1140    const char *attrname, struct lwp *l)
1141{
1142	int error;
1143
1144	if ((ioflg & IO_NODELOCKED) == 0) {
1145		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1146	}
1147
1148	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
1149	if (error == EOPNOTSUPP)
1150		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
1151
1152	if ((ioflg & IO_NODELOCKED) == 0) {
1153		VOP_UNLOCK(vp);
1154	}
1155
1156	return (error);
1157}
1158
1159void
1160vn_ra_allocctx(struct vnode *vp)
1161{
1162	struct uvm_ractx *ra = NULL;
1163
1164	KASSERT(mutex_owned(vp->v_interlock));
1165
1166	if (vp->v_type != VREG) {
1167		return;
1168	}
1169	if (vp->v_ractx != NULL) {
1170		return;
1171	}
1172	if (vp->v_ractx == NULL) {
1173		mutex_exit(vp->v_interlock);
1174		ra = uvm_ra_allocctx();
1175		mutex_enter(vp->v_interlock);
1176		if (ra != NULL && vp->v_ractx == NULL) {
1177			vp->v_ractx = ra;
1178			ra = NULL;
1179		}
1180	}
1181	if (ra != NULL) {
1182		uvm_ra_freectx(ra);
1183	}
1184}
1185
1186int
1187vn_fifo_bypass(void *v)
1188{
1189	struct vop_generic_args *ap = v;
1190
1191	return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
1192}
1193
1194/*
1195 * Open block device by device number
1196 */
1197int
1198vn_bdev_open(dev_t dev, struct vnode **vpp, struct lwp *l)
1199{
1200	int     error;
1201
1202	if ((error = bdevvp(dev, vpp)) != 0)
1203		return error;
1204
1205	if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
1206		vrele(*vpp);
1207		return error;
1208	}
1209	mutex_enter((*vpp)->v_interlock);
1210	(*vpp)->v_writecount++;
1211	mutex_exit((*vpp)->v_interlock);
1212
1213	return 0;
1214}
1215
1216/*
1217 * Lookup the provided name in the filesystem.  If the file exists,
1218 * is a valid block device, and isn't being used by anyone else,
1219 * set *vpp to the file's vnode.
1220 */
1221int
1222vn_bdev_openpath(struct pathbuf *pb, struct vnode **vpp, struct lwp *l)
1223{
1224	struct nameidata nd;
1225	struct vnode *vp;
1226	dev_t dev;
1227	enum vtype vt;
1228	int     error;
1229
1230	NDINIT(&nd, LOOKUP, FOLLOW, pb);
1231	if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0)
1232		return error;
1233
1234	vp = nd.ni_vp;
1235	dev = vp->v_rdev;
1236	vt = vp->v_type;
1237
1238	VOP_UNLOCK(vp);
1239	(void) vn_close(vp, FREAD | FWRITE, l->l_cred);
1240
1241	if (vt != VBLK)
1242		return ENOTBLK;
1243
1244	return vn_bdev_open(dev, vpp, l);
1245}
1246