xref: /netbsd-src/sys/kern/vfs_vnops.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: vfs_vnops.c,v 1.197 2017/11/30 20:25:55 christos 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.197 2017/11/30 20:25:55 christos 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 
109 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
110 
111 #include <sys/verified_exec.h>
112 
113 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
114 	    kauth_cred_t cred, int flags);
115 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
116 	    kauth_cred_t cred, int flags);
117 static int vn_closefile(file_t *fp);
118 static int vn_poll(file_t *fp, int events);
119 static int vn_fcntl(file_t *fp, u_int com, void *data);
120 static int vn_statfile(file_t *fp, struct stat *sb);
121 static int vn_ioctl(file_t *fp, u_long com, void *data);
122 static int vn_mmap(struct file *, off_t *, size_t, int, int *, int *,
123 		   struct uvm_object **, int *);
124 
125 const 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  */
143 int
144 vn_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 
269 bad:
270 	if (error)
271 		vput(vp);
272 out:
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  */
281 int
282 vn_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 
294 int
295 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
296 {
297 	int permbits = 0;
298 	int error;
299 
300 	if ((fflags & O_DIRECTORY) != 0 && vp->v_type != VDIR)
301 		return ENOTDIR;
302 
303 	if ((fflags & O_REGULAR) != 0 && vp->v_type != VREG)
304 		return EFTYPE;
305 
306 	if ((fflags & FREAD) != 0) {
307 		permbits = VREAD;
308 	}
309 	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
310 		permbits |= VWRITE;
311 		if (vp->v_type == VDIR) {
312 			error = EISDIR;
313 			goto bad;
314 		}
315 		error = vn_writechk(vp);
316 		if (error != 0)
317 			goto bad;
318 	}
319 	error = VOP_ACCESS(vp, permbits, cred);
320 bad:
321 	return error;
322 }
323 
324 /*
325  * Mark a vnode as having executable mappings.
326  */
327 void
328 vn_markexec(struct vnode *vp)
329 {
330 
331 	if ((vp->v_iflag & VI_EXECMAP) != 0) {
332 		/* Safe unlocked, as long as caller holds a reference. */
333 		return;
334 	}
335 
336 	mutex_enter(vp->v_interlock);
337 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
338 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
339 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
340 		vp->v_iflag |= VI_EXECMAP;
341 	}
342 	mutex_exit(vp->v_interlock);
343 }
344 
345 /*
346  * Mark a vnode as being the text of a process.
347  * Fail if the vnode is currently writable.
348  */
349 int
350 vn_marktext(struct vnode *vp)
351 {
352 
353 	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
354 		/* Safe unlocked, as long as caller holds a reference. */
355 		return (0);
356 	}
357 
358 	mutex_enter(vp->v_interlock);
359 	if (vp->v_writecount != 0) {
360 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
361 		mutex_exit(vp->v_interlock);
362 		return (ETXTBSY);
363 	}
364 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
365 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
366 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
367 	}
368 	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
369 	mutex_exit(vp->v_interlock);
370 	return (0);
371 }
372 
373 /*
374  * Vnode close call
375  *
376  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
377  */
378 int
379 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
380 {
381 	int error;
382 
383 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
384 	if (flags & FWRITE) {
385 		mutex_enter(vp->v_interlock);
386 		KASSERT(vp->v_writecount > 0);
387 		vp->v_writecount--;
388 		mutex_exit(vp->v_interlock);
389 	}
390 	error = VOP_CLOSE(vp, flags, cred);
391 	vput(vp);
392 	return (error);
393 }
394 
395 static int
396 enforce_rlimit_fsize(struct vnode *vp, struct uio *uio, int ioflag)
397 {
398 	struct lwp *l = curlwp;
399 	off_t testoff;
400 
401 	if (uio->uio_rw != UIO_WRITE || vp->v_type != VREG)
402 		return 0;
403 
404 	KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
405 	if (ioflag & IO_APPEND)
406 		testoff = vp->v_size;
407 	else
408 		testoff = uio->uio_offset;
409 
410 	if (testoff + uio->uio_resid >
411 	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
412 		mutex_enter(proc_lock);
413 		psignal(l->l_proc, SIGXFSZ);
414 		mutex_exit(proc_lock);
415 		return EFBIG;
416 	}
417 
418 	return 0;
419 }
420 
421 /*
422  * Package up an I/O request on a vnode into a uio and do it.
423  */
424 int
425 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
426     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
427     struct lwp *l)
428 {
429 	struct uio auio;
430 	struct iovec aiov;
431 	int error;
432 
433 	if ((ioflg & IO_NODELOCKED) == 0) {
434 		if (rw == UIO_READ) {
435 			vn_lock(vp, LK_SHARED | LK_RETRY);
436 		} else /* UIO_WRITE */ {
437 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
438 		}
439 	}
440 	auio.uio_iov = &aiov;
441 	auio.uio_iovcnt = 1;
442 	aiov.iov_base = base;
443 	aiov.iov_len = len;
444 	auio.uio_resid = len;
445 	auio.uio_offset = offset;
446 	auio.uio_rw = rw;
447 	if (segflg == UIO_SYSSPACE) {
448 		UIO_SETUP_SYSSPACE(&auio);
449 	} else {
450 		auio.uio_vmspace = l->l_proc->p_vmspace;
451 	}
452 
453 	if ((error = enforce_rlimit_fsize(vp, &auio, ioflg)) != 0)
454 		goto out;
455 
456 	if (rw == UIO_READ) {
457 		error = VOP_READ(vp, &auio, ioflg, cred);
458 	} else {
459 		error = VOP_WRITE(vp, &auio, ioflg, cred);
460 	}
461 
462 	if (aresid)
463 		*aresid = auio.uio_resid;
464 	else
465 		if (auio.uio_resid && error == 0)
466 			error = EIO;
467 
468  out:
469 	if ((ioflg & IO_NODELOCKED) == 0) {
470 		VOP_UNLOCK(vp);
471 	}
472 	return (error);
473 }
474 
475 int
476 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
477     struct lwp *l, off_t **cookies, int *ncookies)
478 {
479 	struct vnode *vp = fp->f_vnode;
480 	struct iovec aiov;
481 	struct uio auio;
482 	int error, eofflag;
483 
484 	/* Limit the size on any kernel buffers used by VOP_READDIR */
485 	count = min(MAXBSIZE, count);
486 
487 unionread:
488 	if (vp->v_type != VDIR)
489 		return (EINVAL);
490 	aiov.iov_base = bf;
491 	aiov.iov_len = count;
492 	auio.uio_iov = &aiov;
493 	auio.uio_iovcnt = 1;
494 	auio.uio_rw = UIO_READ;
495 	if (segflg == UIO_SYSSPACE) {
496 		UIO_SETUP_SYSSPACE(&auio);
497 	} else {
498 		KASSERT(l == curlwp);
499 		auio.uio_vmspace = l->l_proc->p_vmspace;
500 	}
501 	auio.uio_resid = count;
502 	vn_lock(vp, LK_SHARED | LK_RETRY);
503 	auio.uio_offset = fp->f_offset;
504 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
505 		    ncookies);
506 	mutex_enter(&fp->f_lock);
507 	fp->f_offset = auio.uio_offset;
508 	mutex_exit(&fp->f_lock);
509 	VOP_UNLOCK(vp);
510 	if (error)
511 		return (error);
512 
513 	if (count == auio.uio_resid && vn_union_readdir_hook) {
514 		struct vnode *ovp = vp;
515 
516 		error = (*vn_union_readdir_hook)(&vp, fp, l);
517 		if (error)
518 			return (error);
519 		if (vp != ovp)
520 			goto unionread;
521 	}
522 
523 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
524 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
525 		struct vnode *tvp = vp;
526 		vp = vp->v_mount->mnt_vnodecovered;
527 		vref(vp);
528 		mutex_enter(&fp->f_lock);
529 		fp->f_vnode = vp;
530 		fp->f_offset = 0;
531 		mutex_exit(&fp->f_lock);
532 		vrele(tvp);
533 		goto unionread;
534 	}
535 	*done = count - auio.uio_resid;
536 	return error;
537 }
538 
539 /*
540  * File table vnode read routine.
541  */
542 static int
543 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
544     int flags)
545 {
546 	struct vnode *vp = fp->f_vnode;
547 	int error, ioflag, fflag;
548 	size_t count;
549 
550 	ioflag = IO_ADV_ENCODE(fp->f_advice);
551 	fflag = fp->f_flag;
552 	if (fflag & FNONBLOCK)
553 		ioflag |= IO_NDELAY;
554 	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
555 		ioflag |= IO_SYNC;
556 	if (fflag & FALTIO)
557 		ioflag |= IO_ALTSEMANTICS;
558 	if (fflag & FDIRECT)
559 		ioflag |= IO_DIRECT;
560 	vn_lock(vp, LK_SHARED | LK_RETRY);
561 	uio->uio_offset = *offset;
562 	count = uio->uio_resid;
563 	error = VOP_READ(vp, uio, ioflag, cred);
564 	if (flags & FOF_UPDATE_OFFSET)
565 		*offset += count - uio->uio_resid;
566 	VOP_UNLOCK(vp);
567 	return (error);
568 }
569 
570 /*
571  * File table vnode write routine.
572  */
573 static int
574 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
575     int flags)
576 {
577 	struct vnode *vp = fp->f_vnode;
578 	int error, ioflag, fflag;
579 	size_t count;
580 
581 	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
582 	fflag = fp->f_flag;
583 	if (vp->v_type == VREG && (fflag & O_APPEND))
584 		ioflag |= IO_APPEND;
585 	if (fflag & FNONBLOCK)
586 		ioflag |= IO_NDELAY;
587 	if (fflag & FFSYNC ||
588 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
589 		ioflag |= IO_SYNC;
590 	else if (fflag & FDSYNC)
591 		ioflag |= IO_DSYNC;
592 	if (fflag & FALTIO)
593 		ioflag |= IO_ALTSEMANTICS;
594 	if (fflag & FDIRECT)
595 		ioflag |= IO_DIRECT;
596 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
597 	uio->uio_offset = *offset;
598 	count = uio->uio_resid;
599 
600 	if ((error = enforce_rlimit_fsize(vp, uio, ioflag)) != 0)
601 		goto out;
602 
603 	error = VOP_WRITE(vp, uio, ioflag, cred);
604 
605 	if (flags & FOF_UPDATE_OFFSET) {
606 		if (ioflag & IO_APPEND) {
607 			/*
608 			 * SUSv3 describes behaviour for count = 0 as following:
609 			 * "Before any action ... is taken, and if nbyte is zero
610 			 * and the file is a regular file, the write() function
611 			 * ... in the absence of errors ... shall return zero
612 			 * and have no other results."
613 			 */
614 			if (count)
615 				*offset = uio->uio_offset;
616 		} else
617 			*offset += count - uio->uio_resid;
618 	}
619 
620  out:
621 	VOP_UNLOCK(vp);
622 	return (error);
623 }
624 
625 /*
626  * File table vnode stat routine.
627  */
628 static int
629 vn_statfile(file_t *fp, struct stat *sb)
630 {
631 	struct vnode *vp = fp->f_vnode;
632 	int error;
633 
634 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
635 	error = vn_stat(vp, sb);
636 	VOP_UNLOCK(vp);
637 	return error;
638 }
639 
640 int
641 vn_stat(struct vnode *vp, struct stat *sb)
642 {
643 	struct vattr va;
644 	int error;
645 	mode_t mode;
646 
647 	memset(&va, 0, sizeof(va));
648 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
649 	if (error)
650 		return (error);
651 	/*
652 	 * Copy from vattr table
653 	 */
654 	memset(sb, 0, sizeof(*sb));
655 	sb->st_dev = va.va_fsid;
656 	sb->st_ino = va.va_fileid;
657 	mode = va.va_mode;
658 	switch (vp->v_type) {
659 	case VREG:
660 		mode |= S_IFREG;
661 		break;
662 	case VDIR:
663 		mode |= S_IFDIR;
664 		break;
665 	case VBLK:
666 		mode |= S_IFBLK;
667 		break;
668 	case VCHR:
669 		mode |= S_IFCHR;
670 		break;
671 	case VLNK:
672 		mode |= S_IFLNK;
673 		break;
674 	case VSOCK:
675 		mode |= S_IFSOCK;
676 		break;
677 	case VFIFO:
678 		mode |= S_IFIFO;
679 		break;
680 	default:
681 		return (EBADF);
682 	}
683 	sb->st_mode = mode;
684 	sb->st_nlink = va.va_nlink;
685 	sb->st_uid = va.va_uid;
686 	sb->st_gid = va.va_gid;
687 	sb->st_rdev = va.va_rdev;
688 	sb->st_size = va.va_size;
689 	sb->st_atimespec = va.va_atime;
690 	sb->st_mtimespec = va.va_mtime;
691 	sb->st_ctimespec = va.va_ctime;
692 	sb->st_birthtimespec = va.va_birthtime;
693 	sb->st_blksize = va.va_blocksize;
694 	sb->st_flags = va.va_flags;
695 	sb->st_gen = 0;
696 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
697 	return (0);
698 }
699 
700 /*
701  * File table vnode fcntl routine.
702  */
703 static int
704 vn_fcntl(file_t *fp, u_int com, void *data)
705 {
706 	struct vnode *vp = fp->f_vnode;
707 	int error;
708 
709 	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
710 	return (error);
711 }
712 
713 /*
714  * File table vnode ioctl routine.
715  */
716 static int
717 vn_ioctl(file_t *fp, u_long com, void *data)
718 {
719 	struct vnode *vp = fp->f_vnode, *ovp;
720 	struct vattr vattr;
721 	int error;
722 
723 	switch (vp->v_type) {
724 
725 	case VREG:
726 	case VDIR:
727 		if (com == FIONREAD) {
728 			vn_lock(vp, LK_SHARED | LK_RETRY);
729 			error = VOP_GETATTR(vp, &vattr, kauth_cred_get());
730 			VOP_UNLOCK(vp);
731 			if (error)
732 				return (error);
733 			*(int *)data = vattr.va_size - fp->f_offset;
734 			return (0);
735 		}
736 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
737 			/*
738 			 * Files don't have send queues, so there never
739 			 * are any bytes in them, nor is there any
740 			 * open space in them.
741 			 */
742 			*(int *)data = 0;
743 			return (0);
744 		}
745 		if (com == FIOGETBMAP) {
746 			daddr_t *block;
747 
748 			if (*(daddr_t *)data < 0)
749 				return (EINVAL);
750 			block = (daddr_t *)data;
751 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
752 		}
753 		if (com == OFIOGETBMAP) {
754 			daddr_t ibn, obn;
755 
756 			if (*(int32_t *)data < 0)
757 				return (EINVAL);
758 			ibn = (daddr_t)*(int32_t *)data;
759 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
760 			*(int32_t *)data = (int32_t)obn;
761 			return error;
762 		}
763 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
764 			return (0);			/* XXX */
765 		/* fall into ... */
766 	case VFIFO:
767 	case VCHR:
768 	case VBLK:
769 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
770 		    kauth_cred_get());
771 		if (error == 0 && com == TIOCSCTTY) {
772 			vref(vp);
773 			mutex_enter(proc_lock);
774 			ovp = curproc->p_session->s_ttyvp;
775 			curproc->p_session->s_ttyvp = vp;
776 			mutex_exit(proc_lock);
777 			if (ovp != NULL)
778 				vrele(ovp);
779 		}
780 		return (error);
781 
782 	default:
783 		return (EPASSTHROUGH);
784 	}
785 }
786 
787 /*
788  * File table vnode poll routine.
789  */
790 static int
791 vn_poll(file_t *fp, int events)
792 {
793 
794 	return (VOP_POLL(fp->f_vnode, events));
795 }
796 
797 /*
798  * File table vnode kqfilter routine.
799  */
800 int
801 vn_kqfilter(file_t *fp, struct knote *kn)
802 {
803 
804 	return (VOP_KQFILTER(fp->f_vnode, kn));
805 }
806 
807 static int
808 vn_mmap(struct file *fp, off_t *offp, size_t size, int prot, int *flagsp,
809 	int *advicep, struct uvm_object **uobjp, int *maxprotp)
810 {
811 	struct uvm_object *uobj;
812 	struct vnode *vp;
813 	struct vattr va;
814 	struct lwp *l;
815 	vm_prot_t maxprot;
816 	off_t off;
817 	int error, flags;
818 	bool needwritemap;
819 
820 	l = curlwp;
821 
822 	off = *offp;
823 	flags = *flagsp;
824 	maxprot = VM_PROT_EXECUTE;
825 
826 	vp = fp->f_vnode;
827 	if (vp->v_type != VREG && vp->v_type != VCHR &&
828 	    vp->v_type != VBLK) {
829 		/* only REG/CHR/BLK support mmap */
830 		return ENODEV;
831 	}
832 	if (vp->v_type != VCHR && off < 0) {
833 		return EINVAL;
834 	}
835 	if (vp->v_type != VCHR && (off_t)(off + size) < off) {
836 		/* no offset wrapping */
837 		return EOVERFLOW;
838 	}
839 
840 	/* special case: catch SunOS style /dev/zero */
841 	if (vp->v_type == VCHR &&
842 	    (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
843 		*uobjp = NULL;
844 		*maxprotp = VM_PROT_ALL;
845 		return 0;
846 	}
847 
848 	/*
849 	 * Old programs may not select a specific sharing type, so
850 	 * default to an appropriate one.
851 	 *
852 	 * XXX: how does MAP_ANON fit in the picture?
853 	 */
854 	if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
855 #if defined(DEBUG)
856 		struct proc *p = l->l_proc;
857 		printf("WARNING: defaulted mmap() share type to "
858 		       "%s (pid %d command %s)\n", vp->v_type == VCHR ?
859 		       "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
860 		       p->p_comm);
861 #endif
862 		if (vp->v_type == VCHR)
863 			flags |= MAP_SHARED;	/* for a device */
864 		else
865 			flags |= MAP_PRIVATE;	/* for a file */
866 	}
867 
868 	/*
869 	 * MAP_PRIVATE device mappings don't make sense (and aren't
870 	 * supported anyway).  However, some programs rely on this,
871 	 * so just change it to MAP_SHARED.
872 	 */
873 	if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
874 		flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
875 	}
876 
877 	/*
878 	 * now check protection
879 	 */
880 
881 	/* check read access */
882 	if (fp->f_flag & FREAD)
883 		maxprot |= VM_PROT_READ;
884 	else if (prot & PROT_READ) {
885 		return EACCES;
886 	}
887 
888 	/* check write access, shared case first */
889 	if (flags & MAP_SHARED) {
890 		/*
891 		 * if the file is writable, only add PROT_WRITE to
892 		 * maxprot if the file is not immutable, append-only.
893 		 * otherwise, if we have asked for PROT_WRITE, return
894 		 * EPERM.
895 		 */
896 		if (fp->f_flag & FWRITE) {
897 			vn_lock(vp, LK_SHARED | LK_RETRY);
898 			error = VOP_GETATTR(vp, &va, l->l_cred);
899 			VOP_UNLOCK(vp);
900 			if (error) {
901 				return error;
902 			}
903 			if ((va.va_flags &
904 			     (SF_SNAPSHOT|IMMUTABLE|APPEND)) == 0)
905 				maxprot |= VM_PROT_WRITE;
906 			else if (prot & PROT_WRITE) {
907 				return EPERM;
908 			}
909 		} else if (prot & PROT_WRITE) {
910 			return EACCES;
911 		}
912 	} else {
913 		/* MAP_PRIVATE mappings can always write to */
914 		maxprot |= VM_PROT_WRITE;
915 	}
916 
917 	/*
918 	 * Don't allow mmap for EXEC if the file system
919 	 * is mounted NOEXEC.
920 	 */
921 	if ((prot & PROT_EXEC) != 0 &&
922 	    (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0) {
923 		return EACCES;
924 	}
925 
926 	if (vp->v_type != VCHR) {
927 		error = VOP_MMAP(vp, prot, curlwp->l_cred);
928 		if (error) {
929 			return error;
930 		}
931 		vref(vp);
932 		uobj = &vp->v_uobj;
933 
934 		/*
935 		 * If the vnode is being mapped with PROT_EXEC,
936 		 * then mark it as text.
937 		 */
938 		if (prot & PROT_EXEC) {
939 			vn_markexec(vp);
940 		}
941 	} else {
942 		int i = maxprot;
943 
944 		/*
945 		 * XXX Some devices don't like to be mapped with
946 		 * XXX PROT_EXEC or PROT_WRITE, but we don't really
947 		 * XXX have a better way of handling this, right now
948 		 */
949 		do {
950 			uobj = udv_attach(vp->v_rdev,
951 					  (flags & MAP_SHARED) ? i :
952 					  (i & ~VM_PROT_WRITE), off, size);
953 			i--;
954 		} while ((uobj == NULL) && (i > 0));
955 		if (uobj == NULL) {
956 			return EINVAL;
957 		}
958 		*advicep = UVM_ADV_RANDOM;
959 	}
960 
961 	/*
962 	 * Set vnode flags to indicate the new kinds of mapping.
963 	 * We take the vnode lock in exclusive mode here to serialize
964 	 * with direct I/O.
965 	 *
966 	 * Safe to check for these flag values without a lock, as
967 	 * long as a reference to the vnode is held.
968 	 */
969 	needwritemap = (vp->v_iflag & VI_WRMAP) == 0 &&
970 		(flags & MAP_SHARED) != 0 &&
971 		(maxprot & VM_PROT_WRITE) != 0;
972 	if ((vp->v_vflag & VV_MAPPED) == 0 || needwritemap) {
973 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
974 		vp->v_vflag |= VV_MAPPED;
975 		if (needwritemap) {
976 			mutex_enter(vp->v_interlock);
977 			vp->v_iflag |= VI_WRMAP;
978 			mutex_exit(vp->v_interlock);
979 		}
980 		VOP_UNLOCK(vp);
981 	}
982 
983 #if NVERIEXEC > 0
984 
985 	/*
986 	 * Check if the file can be executed indirectly.
987 	 *
988 	 * XXX: This gives false warnings about "Incorrect access type"
989 	 * XXX: if the mapping is not executable. Harmless, but will be
990 	 * XXX: fixed as part of other changes.
991 	 */
992 	if (veriexec_verify(l, vp, "(mmap)", VERIEXEC_INDIRECT,
993 			    NULL)) {
994 
995 		/*
996 		 * Don't allow executable mappings if we can't
997 		 * indirectly execute the file.
998 		 */
999 		if (prot & VM_PROT_EXECUTE) {
1000 			return EPERM;
1001 		}
1002 
1003 		/*
1004 		 * Strip the executable bit from 'maxprot' to make sure
1005 		 * it can't be made executable later.
1006 		 */
1007 		maxprot &= ~VM_PROT_EXECUTE;
1008 	}
1009 #endif /* NVERIEXEC > 0 */
1010 
1011 	*uobjp = uobj;
1012 	*maxprotp = maxprot;
1013 	*flagsp = flags;
1014 
1015 	return 0;
1016 }
1017 
1018 
1019 
1020 /*
1021  * Check that the vnode is still valid, and if so
1022  * acquire requested lock.
1023  */
1024 int
1025 vn_lock(struct vnode *vp, int flags)
1026 {
1027 	int error;
1028 
1029 #if 0
1030 	KASSERT(vp->v_usecount > 0 || (vp->v_iflag & VI_ONWORKLST) != 0);
1031 #endif
1032 	KASSERT((flags & ~(LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY)) == 0);
1033 	KASSERT(!mutex_owned(vp->v_interlock));
1034 
1035 #ifdef DIAGNOSTIC
1036 	if (wapbl_vphaswapbl(vp))
1037 		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
1038 #endif
1039 
1040 	error = VOP_LOCK(vp, flags);
1041 	if ((flags & LK_RETRY) != 0 && error == ENOENT)
1042 		error = VOP_LOCK(vp, flags);
1043 
1044 	KASSERT((flags & LK_RETRY) == 0 || (flags & LK_NOWAIT) != 0 ||
1045 	    error == 0);
1046 
1047 	return error;
1048 }
1049 
1050 /*
1051  * File table vnode close routine.
1052  */
1053 static int
1054 vn_closefile(file_t *fp)
1055 {
1056 
1057 	return vn_close(fp->f_vnode, fp->f_flag, fp->f_cred);
1058 }
1059 
1060 /*
1061  * Simplified in-kernel wrapper calls for extended attribute access.
1062  * Both calls pass in a NULL credential, authorizing a "kernel" access.
1063  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1064  */
1065 int
1066 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1067     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
1068 {
1069 	struct uio auio;
1070 	struct iovec aiov;
1071 	int error;
1072 
1073 	aiov.iov_len = *buflen;
1074 	aiov.iov_base = bf;
1075 
1076 	auio.uio_iov = &aiov;
1077 	auio.uio_iovcnt = 1;
1078 	auio.uio_rw = UIO_READ;
1079 	auio.uio_offset = 0;
1080 	auio.uio_resid = *buflen;
1081 	UIO_SETUP_SYSSPACE(&auio);
1082 
1083 	if ((ioflg & IO_NODELOCKED) == 0)
1084 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1085 
1086 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
1087 
1088 	if ((ioflg & IO_NODELOCKED) == 0)
1089 		VOP_UNLOCK(vp);
1090 
1091 	if (error == 0)
1092 		*buflen = *buflen - auio.uio_resid;
1093 
1094 	return (error);
1095 }
1096 
1097 /*
1098  * XXX Failure mode if partially written?
1099  */
1100 int
1101 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1102     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
1103 {
1104 	struct uio auio;
1105 	struct iovec aiov;
1106 	int error;
1107 
1108 	aiov.iov_len = buflen;
1109 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
1110 
1111 	auio.uio_iov = &aiov;
1112 	auio.uio_iovcnt = 1;
1113 	auio.uio_rw = UIO_WRITE;
1114 	auio.uio_offset = 0;
1115 	auio.uio_resid = buflen;
1116 	UIO_SETUP_SYSSPACE(&auio);
1117 
1118 	if ((ioflg & IO_NODELOCKED) == 0) {
1119 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1120 	}
1121 
1122 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
1123 
1124 	if ((ioflg & IO_NODELOCKED) == 0) {
1125 		VOP_UNLOCK(vp);
1126 	}
1127 
1128 	return (error);
1129 }
1130 
1131 int
1132 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1133     const char *attrname, struct lwp *l)
1134 {
1135 	int error;
1136 
1137 	if ((ioflg & IO_NODELOCKED) == 0) {
1138 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1139 	}
1140 
1141 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
1142 	if (error == EOPNOTSUPP)
1143 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
1144 
1145 	if ((ioflg & IO_NODELOCKED) == 0) {
1146 		VOP_UNLOCK(vp);
1147 	}
1148 
1149 	return (error);
1150 }
1151 
1152 void
1153 vn_ra_allocctx(struct vnode *vp)
1154 {
1155 	struct uvm_ractx *ra = NULL;
1156 
1157 	KASSERT(mutex_owned(vp->v_interlock));
1158 
1159 	if (vp->v_type != VREG) {
1160 		return;
1161 	}
1162 	if (vp->v_ractx != NULL) {
1163 		return;
1164 	}
1165 	if (vp->v_ractx == NULL) {
1166 		mutex_exit(vp->v_interlock);
1167 		ra = uvm_ra_allocctx();
1168 		mutex_enter(vp->v_interlock);
1169 		if (ra != NULL && vp->v_ractx == NULL) {
1170 			vp->v_ractx = ra;
1171 			ra = NULL;
1172 		}
1173 	}
1174 	if (ra != NULL) {
1175 		uvm_ra_freectx(ra);
1176 	}
1177 }
1178 
1179 int
1180 vn_fifo_bypass(void *v)
1181 {
1182 	struct vop_generic_args *ap = v;
1183 
1184 	return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v);
1185 }
1186