xref: /netbsd-src/sys/kern/vfs_vnops.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: vfs_vnops.c,v 1.164 2009/04/04 10:12:51 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.164 2009/04/04 10:12:51 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 
93 #include <miscfs/specfs/specdev.h>
94 
95 #include <uvm/uvm_extern.h>
96 #include <uvm/uvm_readahead.h>
97 
98 #ifdef UNION
99 #include <fs/union/union.h>
100 #endif
101 
102 int (*vn_union_readdir_hook) (struct vnode **, struct file *, struct lwp *);
103 
104 #include <sys/verified_exec.h>
105 
106 static int vn_read(file_t *fp, off_t *offset, struct uio *uio,
107 	    kauth_cred_t cred, int flags);
108 static int vn_write(file_t *fp, off_t *offset, struct uio *uio,
109 	    kauth_cred_t cred, int flags);
110 static int vn_closefile(file_t *fp);
111 static int vn_poll(file_t *fp, int events);
112 static int vn_fcntl(file_t *fp, u_int com, void *data);
113 static int vn_statfile(file_t *fp, struct stat *sb);
114 static int vn_ioctl(file_t *fp, u_long com, void *data);
115 
116 const struct fileops vnops = {
117 	.fo_read = vn_read,
118 	.fo_write = vn_write,
119 	.fo_ioctl = vn_ioctl,
120 	.fo_fcntl = vn_fcntl,
121 	.fo_poll = vn_poll,
122 	.fo_stat = vn_statfile,
123 	.fo_close = vn_closefile,
124 	.fo_kqfilter = vn_kqfilter,
125 	.fo_drain = fnullop_drain,
126 };
127 
128 /*
129  * Common code for vnode open operations.
130  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
131  */
132 int
133 vn_open(struct nameidata *ndp, int fmode, int cmode)
134 {
135 	struct vnode *vp;
136 	struct lwp *l = curlwp;
137 	kauth_cred_t cred = l->l_cred;
138 	struct vattr va;
139 	int error;
140 	char *path;
141 
142 	ndp->ni_cnd.cn_flags &= TRYEMULROOT | NOCHROOT;
143 
144 	if (fmode & O_CREAT) {
145 		ndp->ni_cnd.cn_nameiop = CREATE;
146 		ndp->ni_cnd.cn_flags |= LOCKPARENT | LOCKLEAF;
147 		if ((fmode & O_EXCL) == 0 &&
148 		    ((fmode & O_NOFOLLOW) == 0))
149 			ndp->ni_cnd.cn_flags |= FOLLOW;
150 	} else {
151 		ndp->ni_cnd.cn_nameiop = LOOKUP;
152 		ndp->ni_cnd.cn_flags |= LOCKLEAF;
153 		if ((fmode & O_NOFOLLOW) == 0)
154 			ndp->ni_cnd.cn_flags |= FOLLOW;
155 	}
156 
157 	VERIEXEC_PATH_GET(ndp->ni_dirp, ndp->ni_segflg, ndp->ni_dirp, path);
158 
159 	error = namei(ndp);
160 	if (error)
161 		goto out;
162 
163 	vp = ndp->ni_vp;
164 
165 #if NVERIEXEC > 0
166 	error = veriexec_openchk(l, ndp->ni_vp, ndp->ni_dirp, fmode);
167 	if (error)
168 		goto bad;
169 #endif /* NVERIEXEC > 0 */
170 
171 	if (fmode & O_CREAT) {
172 		if (ndp->ni_vp == NULL) {
173 			VATTR_NULL(&va);
174 			va.va_type = VREG;
175 			va.va_mode = cmode;
176 			if (fmode & O_EXCL)
177 				 va.va_vaflags |= VA_EXCLUSIVE;
178 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
179 					   &ndp->ni_cnd, &va);
180 			if (error)
181 				goto out;
182 			fmode &= ~O_TRUNC;
183 			vp = ndp->ni_vp;
184 		} else {
185 			VOP_ABORTOP(ndp->ni_dvp, &ndp->ni_cnd);
186 			if (ndp->ni_dvp == ndp->ni_vp)
187 				vrele(ndp->ni_dvp);
188 			else
189 				vput(ndp->ni_dvp);
190 			ndp->ni_dvp = NULL;
191 			vp = ndp->ni_vp;
192 			if (fmode & O_EXCL) {
193 				error = EEXIST;
194 				goto bad;
195 			}
196 			fmode &= ~O_CREAT;
197 		}
198 	} else {
199 		vp = ndp->ni_vp;
200 	}
201 	if (vp->v_type == VSOCK) {
202 		error = EOPNOTSUPP;
203 		goto bad;
204 	}
205 	if (ndp->ni_vp->v_type == VLNK) {
206 		error = EFTYPE;
207 		goto bad;
208 	}
209 
210 	if ((fmode & O_CREAT) == 0) {
211 		error = vn_openchk(vp, cred, fmode);
212 		if (error != 0)
213 			goto bad;
214 	}
215 
216 	if (fmode & O_TRUNC) {
217 		VOP_UNLOCK(vp, 0);			/* XXX */
218 
219 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
220 		VATTR_NULL(&va);
221 		va.va_size = 0;
222 		error = VOP_SETATTR(vp, &va, cred);
223 		if (error != 0)
224 			goto bad;
225 	}
226 	if ((error = VOP_OPEN(vp, fmode, cred)) != 0)
227 		goto bad;
228 	if (fmode & FWRITE) {
229 		mutex_enter(&vp->v_interlock);
230 		vp->v_writecount++;
231 		mutex_exit(&vp->v_interlock);
232 	}
233 
234 bad:
235 	if (error)
236 		vput(vp);
237 out:
238 	VERIEXEC_PATH_PUT(path);
239 	return (error);
240 }
241 
242 /*
243  * Check for write permissions on the specified vnode.
244  * Prototype text segments cannot be written.
245  */
246 int
247 vn_writechk(struct vnode *vp)
248 {
249 
250 	/*
251 	 * If the vnode is in use as a process's text,
252 	 * we can't allow writing.
253 	 */
254 	if (vp->v_iflag & VI_TEXT)
255 		return (ETXTBSY);
256 	return (0);
257 }
258 
259 int
260 vn_openchk(struct vnode *vp, kauth_cred_t cred, int fflags)
261 {
262 	int permbits = 0;
263 	int error;
264 
265 	if ((fflags & FREAD) != 0) {
266 		permbits = VREAD;
267 	}
268 	if ((fflags & (FWRITE | O_TRUNC)) != 0) {
269 		permbits |= VWRITE;
270 		if (vp->v_type == VDIR) {
271 			error = EISDIR;
272 			goto bad;
273 		}
274 		error = vn_writechk(vp);
275 		if (error != 0)
276 			goto bad;
277 	}
278 	error = VOP_ACCESS(vp, permbits, cred);
279 bad:
280 	return error;
281 }
282 
283 /*
284  * Mark a vnode as having executable mappings.
285  */
286 void
287 vn_markexec(struct vnode *vp)
288 {
289 
290 	if ((vp->v_iflag & VI_EXECMAP) != 0) {
291 		/* Safe unlocked, as long as caller holds a reference. */
292 		return;
293 	}
294 
295 	mutex_enter(&vp->v_interlock);
296 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
297 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
298 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
299 		vp->v_iflag |= VI_EXECMAP;
300 	}
301 	mutex_exit(&vp->v_interlock);
302 }
303 
304 /*
305  * Mark a vnode as being the text of a process.
306  * Fail if the vnode is currently writable.
307  */
308 int
309 vn_marktext(struct vnode *vp)
310 {
311 
312 	if ((vp->v_iflag & (VI_TEXT|VI_EXECMAP)) == (VI_TEXT|VI_EXECMAP)) {
313 		/* Safe unlocked, as long as caller holds a reference. */
314 		return (0);
315 	}
316 
317 	mutex_enter(&vp->v_interlock);
318 	if (vp->v_writecount != 0) {
319 		KASSERT((vp->v_iflag & VI_TEXT) == 0);
320 		mutex_exit(&vp->v_interlock);
321 		return (ETXTBSY);
322 	}
323 	if ((vp->v_iflag & VI_EXECMAP) == 0) {
324 		atomic_add_int(&uvmexp.filepages, -vp->v_uobj.uo_npages);
325 		atomic_add_int(&uvmexp.execpages, vp->v_uobj.uo_npages);
326 	}
327 	vp->v_iflag |= (VI_TEXT | VI_EXECMAP);
328 	mutex_exit(&vp->v_interlock);
329 	return (0);
330 }
331 
332 /*
333  * Vnode close call
334  *
335  * Note: takes an unlocked vnode, while VOP_CLOSE takes a locked node.
336  */
337 int
338 vn_close(struct vnode *vp, int flags, kauth_cred_t cred)
339 {
340 	int error;
341 
342 	if (flags & FWRITE) {
343 		mutex_enter(&vp->v_interlock);
344 		vp->v_writecount--;
345 		mutex_exit(&vp->v_interlock);
346 	}
347 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
348 	error = VOP_CLOSE(vp, flags, cred);
349 	vput(vp);
350 	return (error);
351 }
352 
353 /*
354  * Package up an I/O request on a vnode into a uio and do it.
355  */
356 int
357 vn_rdwr(enum uio_rw rw, struct vnode *vp, void *base, int len, off_t offset,
358     enum uio_seg segflg, int ioflg, kauth_cred_t cred, size_t *aresid,
359     struct lwp *l)
360 {
361 	struct uio auio;
362 	struct iovec aiov;
363 	int error;
364 
365 	if ((ioflg & IO_NODELOCKED) == 0) {
366 		if (rw == UIO_READ) {
367 			vn_lock(vp, LK_SHARED | LK_RETRY);
368 		} else /* UIO_WRITE */ {
369 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
370 		}
371 	}
372 	auio.uio_iov = &aiov;
373 	auio.uio_iovcnt = 1;
374 	aiov.iov_base = base;
375 	aiov.iov_len = len;
376 	auio.uio_resid = len;
377 	auio.uio_offset = offset;
378 	auio.uio_rw = rw;
379 	if (segflg == UIO_SYSSPACE) {
380 		UIO_SETUP_SYSSPACE(&auio);
381 	} else {
382 		auio.uio_vmspace = l->l_proc->p_vmspace;
383 	}
384 	if (rw == UIO_READ) {
385 		error = VOP_READ(vp, &auio, ioflg, cred);
386 	} else {
387 		error = VOP_WRITE(vp, &auio, ioflg, cred);
388 	}
389 	if (aresid)
390 		*aresid = auio.uio_resid;
391 	else
392 		if (auio.uio_resid && error == 0)
393 			error = EIO;
394 	if ((ioflg & IO_NODELOCKED) == 0) {
395 		VOP_UNLOCK(vp, 0);
396 	}
397 	return (error);
398 }
399 
400 int
401 vn_readdir(file_t *fp, char *bf, int segflg, u_int count, int *done,
402     struct lwp *l, off_t **cookies, int *ncookies)
403 {
404 	struct vnode *vp = (struct vnode *)fp->f_data;
405 	struct iovec aiov;
406 	struct uio auio;
407 	int error, eofflag;
408 
409 	/* Limit the size on any kernel buffers used by VOP_READDIR */
410 	count = min(MAXBSIZE, count);
411 
412 unionread:
413 	if (vp->v_type != VDIR)
414 		return (EINVAL);
415 	aiov.iov_base = bf;
416 	aiov.iov_len = count;
417 	auio.uio_iov = &aiov;
418 	auio.uio_iovcnt = 1;
419 	auio.uio_rw = UIO_READ;
420 	if (segflg == UIO_SYSSPACE) {
421 		UIO_SETUP_SYSSPACE(&auio);
422 	} else {
423 		KASSERT(l == curlwp);
424 		auio.uio_vmspace = l->l_proc->p_vmspace;
425 	}
426 	auio.uio_resid = count;
427 	vn_lock(vp, LK_SHARED | LK_RETRY);
428 	auio.uio_offset = fp->f_offset;
429 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookies,
430 		    ncookies);
431 	FILE_LOCK(fp);
432 	fp->f_offset = auio.uio_offset;
433 	FILE_UNLOCK(fp);
434 	VOP_UNLOCK(vp, 0);
435 	if (error)
436 		return (error);
437 
438 	if (count == auio.uio_resid && vn_union_readdir_hook) {
439 		struct vnode *ovp = vp;
440 
441 		error = (*vn_union_readdir_hook)(&vp, fp, l);
442 		if (error)
443 			return (error);
444 		if (vp != ovp)
445 			goto unionread;
446 	}
447 
448 	if (count == auio.uio_resid && (vp->v_vflag & VV_ROOT) &&
449 	    (vp->v_mount->mnt_flag & MNT_UNION)) {
450 		struct vnode *tvp = vp;
451 		vp = vp->v_mount->mnt_vnodecovered;
452 		VREF(vp);
453 		FILE_LOCK(fp);
454 		fp->f_data = vp;
455 		fp->f_offset = 0;
456 		FILE_UNLOCK(fp);
457 		vrele(tvp);
458 		goto unionread;
459 	}
460 	*done = count - auio.uio_resid;
461 	return error;
462 }
463 
464 /*
465  * File table vnode read routine.
466  */
467 static int
468 vn_read(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
469     int flags)
470 {
471 	struct vnode *vp = (struct vnode *)fp->f_data;
472 	int count, error, ioflag, fflag;
473 
474 	ioflag = IO_ADV_ENCODE(fp->f_advice);
475 	fflag = fp->f_flag;
476 	if (fflag & FNONBLOCK)
477 		ioflag |= IO_NDELAY;
478 	if ((fflag & (FFSYNC | FRSYNC)) == (FFSYNC | FRSYNC))
479 		ioflag |= IO_SYNC;
480 	if (fflag & FALTIO)
481 		ioflag |= IO_ALTSEMANTICS;
482 	if (fflag & FDIRECT)
483 		ioflag |= IO_DIRECT;
484 	vn_lock(vp, LK_SHARED | LK_RETRY);
485 	uio->uio_offset = *offset;
486 	count = uio->uio_resid;
487 	error = VOP_READ(vp, uio, ioflag, cred);
488 	if (flags & FOF_UPDATE_OFFSET)
489 		*offset += count - uio->uio_resid;
490 	VOP_UNLOCK(vp, 0);
491 	return (error);
492 }
493 
494 /*
495  * File table vnode write routine.
496  */
497 static int
498 vn_write(file_t *fp, off_t *offset, struct uio *uio, kauth_cred_t cred,
499     int flags)
500 {
501 	struct vnode *vp = (struct vnode *)fp->f_data;
502 	int count, error, ioflag, fflag;
503 
504 	ioflag = IO_ADV_ENCODE(fp->f_advice) | IO_UNIT;
505 	fflag = fp->f_flag;
506 	if (vp->v_type == VREG && (fflag & O_APPEND))
507 		ioflag |= IO_APPEND;
508 	if (fflag & FNONBLOCK)
509 		ioflag |= IO_NDELAY;
510 	if (fflag & FFSYNC ||
511 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
512 		ioflag |= IO_SYNC;
513 	else if (fflag & FDSYNC)
514 		ioflag |= IO_DSYNC;
515 	if (fflag & FALTIO)
516 		ioflag |= IO_ALTSEMANTICS;
517 	if (fflag & FDIRECT)
518 		ioflag |= IO_DIRECT;
519 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
520 	uio->uio_offset = *offset;
521 	count = uio->uio_resid;
522 	error = VOP_WRITE(vp, uio, ioflag, cred);
523 	if (flags & FOF_UPDATE_OFFSET) {
524 		if (ioflag & IO_APPEND) {
525 			/*
526 			 * SUSv3 describes behaviour for count = 0 as following:
527 			 * "Before any action ... is taken, and if nbyte is zero
528 			 * and the file is a regular file, the write() function
529 			 * ... in the absence of errors ... shall return zero
530 			 * and have no other results."
531 			 */
532 			if (count)
533 				*offset = uio->uio_offset;
534 		} else
535 			*offset += count - uio->uio_resid;
536 	}
537 	VOP_UNLOCK(vp, 0);
538 	return (error);
539 }
540 
541 /*
542  * File table vnode stat routine.
543  */
544 static int
545 vn_statfile(file_t *fp, struct stat *sb)
546 {
547 	struct vnode *vp = (struct vnode *)fp->f_data;
548 
549 	return vn_stat(vp, sb);
550 }
551 
552 int
553 vn_stat(struct vnode *vp, struct stat *sb)
554 {
555 	struct vattr va;
556 	int error;
557 	mode_t mode;
558 
559 	error = VOP_GETATTR(vp, &va, kauth_cred_get());
560 	if (error)
561 		return (error);
562 	/*
563 	 * Copy from vattr table
564 	 */
565 	sb->st_dev = va.va_fsid;
566 	sb->st_ino = va.va_fileid;
567 	mode = va.va_mode;
568 	switch (vp->v_type) {
569 	case VREG:
570 		mode |= S_IFREG;
571 		break;
572 	case VDIR:
573 		mode |= S_IFDIR;
574 		break;
575 	case VBLK:
576 		mode |= S_IFBLK;
577 		break;
578 	case VCHR:
579 		mode |= S_IFCHR;
580 		break;
581 	case VLNK:
582 		mode |= S_IFLNK;
583 		break;
584 	case VSOCK:
585 		mode |= S_IFSOCK;
586 		break;
587 	case VFIFO:
588 		mode |= S_IFIFO;
589 		break;
590 	default:
591 		return (EBADF);
592 	};
593 	sb->st_mode = mode;
594 	sb->st_nlink = va.va_nlink;
595 	sb->st_uid = va.va_uid;
596 	sb->st_gid = va.va_gid;
597 	sb->st_rdev = va.va_rdev;
598 	sb->st_size = va.va_size;
599 	sb->st_atimespec = va.va_atime;
600 	sb->st_mtimespec = va.va_mtime;
601 	sb->st_ctimespec = va.va_ctime;
602 	sb->st_birthtimespec = va.va_birthtime;
603 	sb->st_blksize = va.va_blocksize;
604 	sb->st_flags = va.va_flags;
605 	sb->st_gen = 0;
606 	sb->st_blocks = va.va_bytes / S_BLKSIZE;
607 	return (0);
608 }
609 
610 /*
611  * File table vnode fcntl routine.
612  */
613 static int
614 vn_fcntl(file_t *fp, u_int com, void *data)
615 {
616 	struct vnode *vp = fp->f_data;
617 	int error;
618 
619 	error = VOP_FCNTL(vp, com, data, fp->f_flag, kauth_cred_get());
620 	return (error);
621 }
622 
623 /*
624  * File table vnode ioctl routine.
625  */
626 static int
627 vn_ioctl(file_t *fp, u_long com, void *data)
628 {
629 	struct vnode *vp = fp->f_data, *ovp;
630 	struct vattr vattr;
631 	int error;
632 
633 	switch (vp->v_type) {
634 
635 	case VREG:
636 	case VDIR:
637 		if (com == FIONREAD) {
638 			error = VOP_GETATTR(vp, &vattr,
639 			    kauth_cred_get());
640 			if (error)
641 				return (error);
642 			*(int *)data = vattr.va_size - fp->f_offset;
643 			return (0);
644 		}
645 		if ((com == FIONWRITE) || (com == FIONSPACE)) {
646 			/*
647 			 * Files don't have send queues, so there never
648 			 * are any bytes in them, nor is there any
649 			 * open space in them.
650 			 */
651 			*(int *)data = 0;
652 			return (0);
653 		}
654 		if (com == FIOGETBMAP) {
655 			daddr_t *block;
656 
657 			if (*(daddr_t *)data < 0)
658 				return (EINVAL);
659 			block = (daddr_t *)data;
660 			return (VOP_BMAP(vp, *block, NULL, block, NULL));
661 		}
662 		if (com == OFIOGETBMAP) {
663 			daddr_t ibn, obn;
664 
665 			if (*(int32_t *)data < 0)
666 				return (EINVAL);
667 			ibn = (daddr_t)*(int32_t *)data;
668 			error = VOP_BMAP(vp, ibn, NULL, &obn, NULL);
669 			*(int32_t *)data = (int32_t)obn;
670 			return error;
671 		}
672 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
673 			return (0);			/* XXX */
674 		/* fall into ... */
675 	case VFIFO:
676 	case VCHR:
677 	case VBLK:
678 		error = VOP_IOCTL(vp, com, data, fp->f_flag,
679 		    kauth_cred_get());
680 		if (error == 0 && com == TIOCSCTTY) {
681 			VREF(vp);
682 			mutex_enter(proc_lock);
683 			ovp = curproc->p_session->s_ttyvp;
684 			curproc->p_session->s_ttyvp = vp;
685 			mutex_exit(proc_lock);
686 			if (ovp != NULL)
687 				vrele(ovp);
688 		}
689 		return (error);
690 
691 	default:
692 		return (EPASSTHROUGH);
693 	}
694 }
695 
696 /*
697  * File table vnode poll routine.
698  */
699 static int
700 vn_poll(file_t *fp, int events)
701 {
702 
703 	return (VOP_POLL(fp->f_data, events));
704 }
705 
706 /*
707  * File table vnode kqfilter routine.
708  */
709 int
710 vn_kqfilter(file_t *fp, struct knote *kn)
711 {
712 
713 	return (VOP_KQFILTER(fp->f_data, kn));
714 }
715 
716 /*
717  * Check that the vnode is still valid, and if so
718  * acquire requested lock.
719  */
720 int
721 vn_lock(struct vnode *vp, int flags)
722 {
723 	int error;
724 
725 #if 0
726 	KASSERT(vp->v_usecount > 0 || (flags & LK_INTERLOCK) != 0
727 	    || (vp->v_iflag & VI_ONWORKLST) != 0);
728 #endif
729 	KASSERT((flags &
730 	    ~(LK_INTERLOCK|LK_SHARED|LK_EXCLUSIVE|LK_NOWAIT|LK_RETRY|
731 	    LK_CANRECURSE))
732 	    == 0);
733 
734 #ifdef DIAGNOSTIC
735 	if (wapbl_vphaswapbl(vp))
736 		WAPBL_JUNLOCK_ASSERT(wapbl_vptomp(vp));
737 #endif
738 
739 	do {
740 		/*
741 		 * XXX PR 37706 forced unmount of file systems is unsafe.
742 		 * Race between vclean() and this the remaining problem.
743 		 */
744 		if (vp->v_iflag & VI_XLOCK) {
745 			if ((flags & LK_INTERLOCK) == 0) {
746 				mutex_enter(&vp->v_interlock);
747 			}
748 			flags &= ~LK_INTERLOCK;
749 			if (flags & LK_NOWAIT) {
750 				mutex_exit(&vp->v_interlock);
751 				return EBUSY;
752 			}
753 			vwait(vp, VI_XLOCK);
754 			mutex_exit(&vp->v_interlock);
755 			error = ENOENT;
756 		} else {
757 			if ((flags & LK_INTERLOCK) != 0) {
758 				mutex_exit(&vp->v_interlock);
759 			}
760 			flags &= ~LK_INTERLOCK;
761 			error = VOP_LOCK(vp, (flags & ~LK_RETRY));
762 			if (error == 0 || error == EDEADLK || error == EBUSY)
763 				return (error);
764 		}
765 	} while (flags & LK_RETRY);
766 	return (error);
767 }
768 
769 /*
770  * File table vnode close routine.
771  */
772 static int
773 vn_closefile(file_t *fp)
774 {
775 
776 	return vn_close(fp->f_data, fp->f_flag, fp->f_cred);
777 }
778 
779 /*
780  * Enable LK_CANRECURSE on lock. Return prior status.
781  */
782 u_int
783 vn_setrecurse(struct vnode *vp)
784 {
785 	struct vnlock *lkp;
786 
787 	lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock);
788 	atomic_inc_uint(&lkp->vl_canrecurse);
789 
790 	return 0;
791 }
792 
793 /*
794  * Called when done with locksetrecurse.
795  */
796 void
797 vn_restorerecurse(struct vnode *vp, u_int flags)
798 {
799 	struct vnlock *lkp;
800 
801 	lkp = (vp->v_vnlock != NULL ? vp->v_vnlock : &vp->v_lock);
802 	atomic_dec_uint(&lkp->vl_canrecurse);
803 }
804 
805 /*
806  * Simplified in-kernel wrapper calls for extended attribute access.
807  * Both calls pass in a NULL credential, authorizing a "kernel" access.
808  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
809  */
810 int
811 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
812     const char *attrname, size_t *buflen, void *bf, struct lwp *l)
813 {
814 	struct uio auio;
815 	struct iovec aiov;
816 	int error;
817 
818 	aiov.iov_len = *buflen;
819 	aiov.iov_base = bf;
820 
821 	auio.uio_iov = &aiov;
822 	auio.uio_iovcnt = 1;
823 	auio.uio_rw = UIO_READ;
824 	auio.uio_offset = 0;
825 	auio.uio_resid = *buflen;
826 	UIO_SETUP_SYSSPACE(&auio);
827 
828 	if ((ioflg & IO_NODELOCKED) == 0)
829 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
830 
831 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL);
832 
833 	if ((ioflg & IO_NODELOCKED) == 0)
834 		VOP_UNLOCK(vp, 0);
835 
836 	if (error == 0)
837 		*buflen = *buflen - auio.uio_resid;
838 
839 	return (error);
840 }
841 
842 /*
843  * XXX Failure mode if partially written?
844  */
845 int
846 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
847     const char *attrname, size_t buflen, const void *bf, struct lwp *l)
848 {
849 	struct uio auio;
850 	struct iovec aiov;
851 	int error;
852 
853 	aiov.iov_len = buflen;
854 	aiov.iov_base = __UNCONST(bf);		/* XXXUNCONST kills const */
855 
856 	auio.uio_iov = &aiov;
857 	auio.uio_iovcnt = 1;
858 	auio.uio_rw = UIO_WRITE;
859 	auio.uio_offset = 0;
860 	auio.uio_resid = buflen;
861 	UIO_SETUP_SYSSPACE(&auio);
862 
863 	if ((ioflg & IO_NODELOCKED) == 0) {
864 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
865 	}
866 
867 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL);
868 
869 	if ((ioflg & IO_NODELOCKED) == 0) {
870 		VOP_UNLOCK(vp, 0);
871 	}
872 
873 	return (error);
874 }
875 
876 int
877 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
878     const char *attrname, struct lwp *l)
879 {
880 	int error;
881 
882 	if ((ioflg & IO_NODELOCKED) == 0) {
883 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
884 	}
885 
886 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL);
887 	if (error == EOPNOTSUPP)
888 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL);
889 
890 	if ((ioflg & IO_NODELOCKED) == 0) {
891 		VOP_UNLOCK(vp, 0);
892 	}
893 
894 	return (error);
895 }
896 
897 void
898 vn_ra_allocctx(struct vnode *vp)
899 {
900 	struct uvm_ractx *ra = NULL;
901 
902 	KASSERT(mutex_owned(&vp->v_interlock));
903 
904 	if (vp->v_type != VREG) {
905 		return;
906 	}
907 	if (vp->v_ractx != NULL) {
908 		return;
909 	}
910 	if (vp->v_ractx == NULL) {
911 		mutex_exit(&vp->v_interlock);
912 		ra = uvm_ra_allocctx();
913 		mutex_enter(&vp->v_interlock);
914 		if (ra != NULL && vp->v_ractx == NULL) {
915 			vp->v_ractx = ra;
916 			ra = NULL;
917 		}
918 	}
919 	if (ra != NULL) {
920 		uvm_ra_freectx(ra);
921 	}
922 }
923