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