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