xref: /dragonfly/sys/kern/vfs_vnops.c (revision 0bb9290e)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/kern/vfs_vnops.c,v 1.87.2.13 2002/12/29 18:19:53 dillon Exp $
40  * $DragonFly: src/sys/kern/vfs_vnops.c,v 1.44 2006/08/12 00:26:20 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/fcntl.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <sys/proc.h>
49 #include <sys/mount.h>
50 #include <sys/nlookup.h>
51 #include <sys/vnode.h>
52 #include <sys/buf.h>
53 #include <sys/filio.h>
54 #include <sys/ttycom.h>
55 #include <sys/conf.h>
56 #include <sys/syslog.h>
57 
58 static int vn_closefile (struct file *fp);
59 static int vn_ioctl (struct file *fp, u_long com, caddr_t data,
60 		struct ucred *cred);
61 static int vn_read (struct file *fp, struct uio *uio,
62 		struct ucred *cred, int flags);
63 static int svn_read (struct file *fp, struct uio *uio,
64 		struct ucred *cred, int flags);
65 static int vn_poll (struct file *fp, int events, struct ucred *cred);
66 static int vn_kqfilter (struct file *fp, struct knote *kn);
67 static int vn_statfile (struct file *fp, struct stat *sb, struct ucred *cred);
68 static int vn_write (struct file *fp, struct uio *uio,
69 		struct ucred *cred, int flags);
70 static int svn_write (struct file *fp, struct uio *uio,
71 		struct ucred *cred, int flags);
72 
73 struct fileops vnode_fileops = {
74 	.fo_read = vn_read,
75 	.fo_write = vn_write,
76 	.fo_ioctl = vn_ioctl,
77 	.fo_poll = vn_poll,
78 	.fo_kqfilter = vn_kqfilter,
79 	.fo_stat = vn_statfile,
80 	.fo_close = vn_closefile,
81 	.fo_shutdown = nofo_shutdown
82 };
83 
84 struct fileops specvnode_fileops = {
85 	.fo_read = svn_read,
86 	.fo_write = svn_write,
87 	.fo_ioctl = vn_ioctl,
88 	.fo_poll = vn_poll,
89 	.fo_kqfilter = vn_kqfilter,
90 	.fo_stat = vn_statfile,
91 	.fo_close = vn_closefile,
92 	.fo_shutdown = nofo_shutdown
93 };
94 
95 /*
96  * Shortcut the device read/write.  This avoids a lot of vnode junk.
97  * Basically the specfs vnops for read and write take the locked vnode,
98  * unlock it (because we can't hold the vnode locked while reading or writing
99  * a device which may block indefinitely), issues the device operation, then
100  * relock the vnode before returning, plus other junk.  This bypasses all
101  * of that and just does the device operation.
102  */
103 void
104 vn_setspecops(struct file *fp)
105 {
106 	if (vfs_fastdev && fp->f_ops == &vnode_fileops) {
107 		fp->f_ops = &specvnode_fileops;
108 	}
109 }
110 
111 /*
112  * Common code for vnode open operations.  Check permissions, and call
113  * the VOP_NOPEN or VOP_NCREATE routine.
114  *
115  * The caller is responsible for setting up nd with nlookup_init() and
116  * for cleaning it up with nlookup_done(), whether we return an error
117  * or not.
118  *
119  * On success nd->nl_open_vp will hold a referenced and, if requested,
120  * locked vnode.  A locked vnode is requested via NLC_LOCKVP.  If fp
121  * is non-NULL the vnode will be installed in the file pointer.
122  *
123  * NOTE: The vnode is referenced just once on return whether or not it
124  * is also installed in the file pointer.
125  */
126 int
127 vn_open(struct nlookupdata *nd, struct file *fp, int fmode, int cmode)
128 {
129 	struct vnode *vp;
130 	struct ucred *cred = nd->nl_cred;
131 	struct vattr vat;
132 	struct vattr *vap = &vat;
133 	struct namecache *ncp;
134 	int mode, error;
135 
136 	/*
137 	 * Lookup the path and create or obtain the vnode.  After a
138 	 * successful lookup a locked nd->nl_ncp will be returned.
139 	 *
140 	 * The result of this section should be a locked vnode.
141 	 *
142 	 * XXX with only a little work we should be able to avoid locking
143 	 * the vnode if FWRITE, O_CREAT, and O_TRUNC are *not* set.
144 	 */
145 	if (fmode & O_CREAT) {
146 		/*
147 		 * CONDITIONAL CREATE FILE CASE
148 		 *
149 		 * Setting NLC_CREATE causes a negative hit to store
150 		 * the negative hit ncp and not return an error.  Then
151 		 * nc_error or nc_vp may be checked to see if the ncp
152 		 * represents a negative hit.  NLC_CREATE also requires
153 		 * write permission on the governing directory or EPERM
154 		 * is returned.
155 		 */
156 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
157 			nd->nl_flags |= NLC_FOLLOW;
158 		nd->nl_flags |= NLC_CREATE;
159 		bwillwrite();
160 		error = nlookup(nd);
161 	} else {
162 		/*
163 		 * NORMAL OPEN FILE CASE
164 		 */
165 		error = nlookup(nd);
166 	}
167 
168 	if (error)
169 		return (error);
170 	ncp = nd->nl_ncp;
171 
172 	/*
173 	 * split case to allow us to re-resolve and retry the ncp in case
174 	 * we get ESTALE.
175 	 */
176 again:
177 	if (fmode & O_CREAT) {
178 		if (ncp->nc_vp == NULL) {
179 			VATTR_NULL(vap);
180 			vap->va_type = VREG;
181 			vap->va_mode = cmode;
182 			if (fmode & O_EXCL)
183 				vap->va_vaflags |= VA_EXCLUSIVE;
184 			error = VOP_NCREATE(ncp, &vp, nd->nl_cred, vap);
185 			if (error)
186 				return (error);
187 			fmode &= ~O_TRUNC;
188 			/* locked vnode is returned */
189 		} else {
190 			if (fmode & O_EXCL) {
191 				error = EEXIST;
192 			} else {
193 				error = cache_vget(ncp, cred,
194 						    LK_EXCLUSIVE, &vp);
195 			}
196 			if (error)
197 				return (error);
198 			fmode &= ~O_CREAT;
199 		}
200 	} else {
201 		error = cache_vget(ncp, cred, LK_EXCLUSIVE, &vp);
202 		if (error)
203 			return (error);
204 	}
205 
206 	/*
207 	 * We have a locked vnode and ncp now.  Note that the ncp will
208 	 * be cleaned up by the caller if nd->nl_ncp is left intact.
209 	 */
210 	if (vp->v_type == VLNK) {
211 		error = EMLINK;
212 		goto bad;
213 	}
214 	if (vp->v_type == VSOCK) {
215 		error = EOPNOTSUPP;
216 		goto bad;
217 	}
218 	if ((fmode & O_CREAT) == 0) {
219 		mode = 0;
220 		if (fmode & (FWRITE | O_TRUNC)) {
221 			if (vp->v_type == VDIR) {
222 				error = EISDIR;
223 				goto bad;
224 			}
225 			error = vn_writechk(vp);
226 			if (error) {
227 				/*
228 				 * Special stale handling, re-resolve the
229 				 * vnode.
230 				 */
231 				if (error == ESTALE) {
232 					vput(vp);
233 					vp = NULL;
234 					cache_setunresolved(ncp);
235 					error = cache_resolve(ncp, cred);
236 					if (error == 0)
237 						goto again;
238 				}
239 				goto bad;
240 			}
241 			mode |= VWRITE;
242 		}
243 		if (fmode & FREAD)
244 			mode |= VREAD;
245 		if (mode) {
246 		        error = VOP_ACCESS(vp, mode, cred);
247 			if (error) {
248 				/*
249 				 * Special stale handling, re-resolve the
250 				 * vnode.
251 				 */
252 				if (error == ESTALE) {
253 					vput(vp);
254 					vp = NULL;
255 					cache_setunresolved(ncp);
256 					error = cache_resolve(ncp, cred);
257 					if (error == 0)
258 						goto again;
259 				}
260 				goto bad;
261 			}
262 		}
263 	}
264 	if (fmode & O_TRUNC) {
265 		vn_unlock(vp);				/* XXX */
266 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
267 		VATTR_NULL(vap);
268 		vap->va_size = 0;
269 		error = VOP_SETATTR(vp, vap, cred);
270 		if (error)
271 			goto bad;
272 	}
273 
274 	/*
275 	 * Setup the fp so VOP_OPEN can override it.  No descriptor has been
276 	 * associated with the fp yet so we own it clean.  f_ncp inherits
277 	 * nl_ncp .
278 	 */
279 	if (fp) {
280 		if (vp->v_type == VDIR) {
281 			fp->f_ncp = nd->nl_ncp;
282 			nd->nl_ncp = NULL;
283 			cache_unlock(fp->f_ncp);
284 		}
285 	}
286 
287 	/*
288 	 * Get rid of nl_ncp.  vn_open does not return it (it returns the
289 	 * vnode or the file pointer).  Note: we can't leave nl_ncp locked
290 	 * through the VOP_OPEN anyway since the VOP_OPEN may block, e.g.
291 	 * on /dev/ttyd0
292 	 */
293 	if (nd->nl_ncp) {
294 		cache_put(nd->nl_ncp);
295 		nd->nl_ncp = NULL;
296 	}
297 
298 	error = VOP_OPEN(vp, fmode, cred, fp);
299 	if (error) {
300 		/*
301 		 * setting f_ops to &badfileops will prevent the descriptor
302 		 * code from trying to close and release the vnode, since
303 		 * the open failed we do not want to call close.
304 		 */
305 		if (fp) {
306 			fp->f_data = NULL;
307 			fp->f_ops = &badfileops;
308 		}
309 		goto bad;
310 	}
311 
312 #if 0
313 	/*
314 	 * Assert that VREG files have been setup for vmio.
315 	 */
316 	KASSERT(vp->v_type != VREG || vp->v_object != NULL,
317 		("vn_open: regular file was not VMIO enabled!"));
318 #endif
319 
320 	/*
321 	 * Return the vnode.  XXX needs some cleaning up.  The vnode is
322 	 * only returned in the fp == NULL case.
323 	 */
324 	if (fp == NULL) {
325 		nd->nl_open_vp = vp;
326 		nd->nl_vp_fmode = fmode;
327 		if ((nd->nl_flags & NLC_LOCKVP) == 0)
328 			vn_unlock(vp);
329 	} else {
330 		vput(vp);
331 	}
332 	return (0);
333 bad:
334 	if (vp)
335 		vput(vp);
336 	return (error);
337 }
338 
339 /*
340  * Check for write permissions on the specified vnode.
341  * Prototype text segments cannot be written.
342  */
343 int
344 vn_writechk(vp)
345 	struct vnode *vp;
346 {
347 
348 	/*
349 	 * If there's shared text associated with
350 	 * the vnode, try to free it up once.  If
351 	 * we fail, we can't allow writing.
352 	 */
353 	if (vp->v_flag & VTEXT)
354 		return (ETXTBSY);
355 	return (0);
356 }
357 
358 /*
359  * Vnode close call
360  */
361 int
362 vn_close(struct vnode *vp, int flags)
363 {
364 	int error;
365 
366 	if ((error = vn_lock(vp, LK_EXCLUSIVE | LK_RETRY)) == 0) {
367 		error = VOP_CLOSE(vp, flags);
368 		vn_unlock(vp);
369 	}
370 	vrele(vp);
371 	return (error);
372 }
373 
374 static __inline
375 int
376 sequential_heuristic(struct uio *uio, struct file *fp)
377 {
378 	/*
379 	 * Sequential heuristic - detect sequential operation
380 	 */
381 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
382 	    uio->uio_offset == fp->f_nextoff) {
383 		int tmpseq = fp->f_seqcount;
384 		/*
385 		 * XXX we assume that the filesystem block size is
386 		 * the default.  Not true, but still gives us a pretty
387 		 * good indicator of how sequential the read operations
388 		 * are.
389 		 */
390 		tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
391 		if (tmpseq > IO_SEQMAX)
392 			tmpseq = IO_SEQMAX;
393 		fp->f_seqcount = tmpseq;
394 		return(fp->f_seqcount << IO_SEQSHIFT);
395 	}
396 
397 	/*
398 	 * Not sequential, quick draw-down of seqcount
399 	 */
400 	if (fp->f_seqcount > 1)
401 		fp->f_seqcount = 1;
402 	else
403 		fp->f_seqcount = 0;
404 	return(0);
405 }
406 
407 /*
408  * Package up an I/O request on a vnode into a uio and do it.
409  */
410 int
411 vn_rdwr(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
412 	off_t offset, enum uio_seg segflg, int ioflg,
413 	struct ucred *cred, int *aresid)
414 {
415 	struct uio auio;
416 	struct iovec aiov;
417 	int error;
418 
419 	if ((ioflg & IO_NODELOCKED) == 0)
420 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
421 	auio.uio_iov = &aiov;
422 	auio.uio_iovcnt = 1;
423 	aiov.iov_base = base;
424 	aiov.iov_len = len;
425 	auio.uio_resid = len;
426 	auio.uio_offset = offset;
427 	auio.uio_segflg = segflg;
428 	auio.uio_rw = rw;
429 	auio.uio_td = curthread;
430 	if (rw == UIO_READ) {
431 		error = VOP_READ(vp, &auio, ioflg, cred);
432 	} else {
433 		error = VOP_WRITE(vp, &auio, ioflg, cred);
434 	}
435 	if (aresid)
436 		*aresid = auio.uio_resid;
437 	else
438 		if (auio.uio_resid && error == 0)
439 			error = EIO;
440 	if ((ioflg & IO_NODELOCKED) == 0)
441 		vn_unlock(vp);
442 	return (error);
443 }
444 
445 /*
446  * Package up an I/O request on a vnode into a uio and do it.  The I/O
447  * request is split up into smaller chunks and we try to avoid saturating
448  * the buffer cache while potentially holding a vnode locked, so we
449  * check bwillwrite() before calling vn_rdwr().  We also call uio_yield()
450  * to give other processes a chance to lock the vnode (either other processes
451  * core'ing the same binary, or unrelated processes scanning the directory).
452  */
453 int
454 vn_rdwr_inchunks(enum uio_rw rw, struct vnode *vp, caddr_t base, int len,
455 		 off_t offset, enum uio_seg segflg, int ioflg,
456 		 struct ucred *cred, int *aresid)
457 {
458 	int error = 0;
459 
460 	do {
461 		int chunk;
462 
463 		/*
464 		 * Force `offset' to a multiple of MAXBSIZE except possibly
465 		 * for the first chunk, so that filesystems only need to
466 		 * write full blocks except possibly for the first and last
467 		 * chunks.
468 		 */
469 		chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
470 
471 		if (chunk > len)
472 			chunk = len;
473 		if (rw != UIO_READ && vp->v_type == VREG)
474 			bwillwrite();
475 		error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
476 			    ioflg, cred, aresid);
477 		len -= chunk;	/* aresid calc already includes length */
478 		if (error)
479 			break;
480 		offset += chunk;
481 		base += chunk;
482 		uio_yield();
483 	} while (len);
484 	if (aresid)
485 		*aresid += len;
486 	return (error);
487 }
488 
489 /*
490  * MPALMOSTSAFE - acquires mplock
491  */
492 static int
493 vn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
494 {
495 	struct vnode *vp;
496 	int error, ioflag;
497 
498 	get_mplock();
499 	KASSERT(uio->uio_td == curthread,
500 		("uio_td %p is not td %p", uio->uio_td, curthread));
501 	vp = (struct vnode *)fp->f_data;
502 
503 	ioflag = 0;
504 	if (flags & O_FBLOCKING) {
505 		/* ioflag &= ~IO_NDELAY; */
506 	} else if (flags & O_FNONBLOCKING) {
507 		ioflag |= IO_NDELAY;
508 	} else if (fp->f_flag & FNONBLOCK) {
509 		ioflag |= IO_NDELAY;
510 	}
511 	if (flags & O_FBUFFERED) {
512 		/* ioflag &= ~IO_DIRECT; */
513 	} else if (flags & O_FUNBUFFERED) {
514 		ioflag |= IO_DIRECT;
515 	} else if (fp->f_flag & O_DIRECT) {
516 		ioflag |= IO_DIRECT;
517 	}
518 	vn_lock(vp, LK_SHARED | LK_RETRY);
519 	if ((flags & O_FOFFSET) == 0)
520 		uio->uio_offset = fp->f_offset;
521 
522 	ioflag |= sequential_heuristic(uio, fp);
523 
524 	error = VOP_READ(vp, uio, ioflag, cred);
525 	if ((flags & O_FOFFSET) == 0)
526 		fp->f_offset = uio->uio_offset;
527 	fp->f_nextoff = uio->uio_offset;
528 	vn_unlock(vp);
529 	rel_mplock();
530 	return (error);
531 }
532 
533 /*
534  * Device-optimized file table vnode read routine.
535  *
536  * This bypasses the VOP table and talks directly to the device.  Most
537  * filesystems just route to specfs and can make this optimization.
538  *
539  * MPALMOSTSAFE - acquires mplock
540  */
541 static int
542 svn_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
543 {
544 	struct vnode *vp;
545 	int ioflag;
546 	int error;
547 	dev_t dev;
548 
549 	get_mplock();
550 	KASSERT(uio->uio_td == curthread,
551 		("uio_td %p is not td %p", uio->uio_td, curthread));
552 
553 	vp = (struct vnode *)fp->f_data;
554 	if (vp == NULL || vp->v_type == VBAD) {
555 		error = EBADF;
556 		goto done;
557 	}
558 
559 	if ((dev = vp->v_rdev) == NULL) {
560 		error = EBADF;
561 		goto done;
562 	}
563 	reference_dev(dev);
564 
565 	if (uio->uio_resid == 0) {
566 		error = 0;
567 		goto done;
568 	}
569 	if ((flags & O_FOFFSET) == 0)
570 		uio->uio_offset = fp->f_offset;
571 
572 	ioflag = 0;
573 	if (flags & O_FBLOCKING) {
574 		/* ioflag &= ~IO_NDELAY; */
575 	} else if (flags & O_FNONBLOCKING) {
576 		ioflag |= IO_NDELAY;
577 	} else if (fp->f_flag & FNONBLOCK) {
578 		ioflag |= IO_NDELAY;
579 	}
580 	if (flags & O_FBUFFERED) {
581 		/* ioflag &= ~IO_DIRECT; */
582 	} else if (flags & O_FUNBUFFERED) {
583 		ioflag |= IO_DIRECT;
584 	} else if (fp->f_flag & O_DIRECT) {
585 		ioflag |= IO_DIRECT;
586 	}
587 	ioflag |= sequential_heuristic(uio, fp);
588 
589 	error = dev_dread(dev, uio, ioflag);
590 
591 	release_dev(dev);
592 	if ((flags & O_FOFFSET) == 0)
593 		fp->f_offset = uio->uio_offset;
594 	fp->f_nextoff = uio->uio_offset;
595 done:
596 	rel_mplock();
597 	return (error);
598 }
599 
600 /*
601  * MPALMOSTSAFE - acquires mplock
602  */
603 static int
604 vn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
605 {
606 	struct vnode *vp;
607 	int error, ioflag;
608 
609 	get_mplock();
610 	KASSERT(uio->uio_td == curthread,
611 		("uio_procp %p is not p %p", uio->uio_td, curthread));
612 	vp = (struct vnode *)fp->f_data;
613 	if (vp->v_type == VREG)
614 		bwillwrite();
615 	vp = (struct vnode *)fp->f_data;	/* XXX needed? */
616 
617 	ioflag = IO_UNIT;
618 	if (vp->v_type == VREG &&
619 	   ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
620 		ioflag |= IO_APPEND;
621 	}
622 
623 	if (flags & O_FBLOCKING) {
624 		/* ioflag &= ~IO_NDELAY; */
625 	} else if (flags & O_FNONBLOCKING) {
626 		ioflag |= IO_NDELAY;
627 	} else if (fp->f_flag & FNONBLOCK) {
628 		ioflag |= IO_NDELAY;
629 	}
630 	if (flags & O_FBUFFERED) {
631 		/* ioflag &= ~IO_DIRECT; */
632 	} else if (flags & O_FUNBUFFERED) {
633 		ioflag |= IO_DIRECT;
634 	} else if (fp->f_flag & O_DIRECT) {
635 		ioflag |= IO_DIRECT;
636 	}
637 	if (flags & O_FASYNCWRITE) {
638 		/* ioflag &= ~IO_SYNC; */
639 	} else if (flags & O_FSYNCWRITE) {
640 		ioflag |= IO_SYNC;
641 	} else if (fp->f_flag & O_FSYNC) {
642 		ioflag |= IO_SYNC;
643 	}
644 
645 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
646 		ioflag |= IO_SYNC;
647 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
648 	if ((flags & O_FOFFSET) == 0)
649 		uio->uio_offset = fp->f_offset;
650 	ioflag |= sequential_heuristic(uio, fp);
651 	error = VOP_WRITE(vp, uio, ioflag, cred);
652 	if ((flags & O_FOFFSET) == 0)
653 		fp->f_offset = uio->uio_offset;
654 	fp->f_nextoff = uio->uio_offset;
655 	vn_unlock(vp);
656 	rel_mplock();
657 	return (error);
658 }
659 
660 /*
661  * Device-optimized file table vnode write routine.
662  *
663  * This bypasses the VOP table and talks directly to the device.  Most
664  * filesystems just route to specfs and can make this optimization.
665  *
666  * MPALMOSTSAFE - acquires mplock
667  */
668 static int
669 svn_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
670 {
671 	struct vnode *vp;
672 	int ioflag;
673 	int error;
674 	dev_t dev;
675 
676 	get_mplock();
677 	KASSERT(uio->uio_td == curthread,
678 		("uio_procp %p is not p %p", uio->uio_td, curthread));
679 
680 	vp = (struct vnode *)fp->f_data;
681 	if (vp == NULL || vp->v_type == VBAD) {
682 		error = EBADF;
683 		goto done;
684 	}
685 	if (vp->v_type == VREG)
686 		bwillwrite();
687 	vp = (struct vnode *)fp->f_data;	/* XXX needed? */
688 
689 	if ((dev = vp->v_rdev) == NULL) {
690 		error = EBADF;
691 		goto done;
692 	}
693 	reference_dev(dev);
694 
695 	if ((flags & O_FOFFSET) == 0)
696 		uio->uio_offset = fp->f_offset;
697 
698 	ioflag = IO_UNIT;
699 	if (vp->v_type == VREG &&
700 	   ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
701 		ioflag |= IO_APPEND;
702 	}
703 
704 	if (flags & O_FBLOCKING) {
705 		/* ioflag &= ~IO_NDELAY; */
706 	} else if (flags & O_FNONBLOCKING) {
707 		ioflag |= IO_NDELAY;
708 	} else if (fp->f_flag & FNONBLOCK) {
709 		ioflag |= IO_NDELAY;
710 	}
711 	if (flags & O_FBUFFERED) {
712 		/* ioflag &= ~IO_DIRECT; */
713 	} else if (flags & O_FUNBUFFERED) {
714 		ioflag |= IO_DIRECT;
715 	} else if (fp->f_flag & O_DIRECT) {
716 		ioflag |= IO_DIRECT;
717 	}
718 	if (flags & O_FASYNCWRITE) {
719 		/* ioflag &= ~IO_SYNC; */
720 	} else if (flags & O_FSYNCWRITE) {
721 		ioflag |= IO_SYNC;
722 	} else if (fp->f_flag & O_FSYNC) {
723 		ioflag |= IO_SYNC;
724 	}
725 
726 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
727 		ioflag |= IO_SYNC;
728 	ioflag |= sequential_heuristic(uio, fp);
729 
730 	error = dev_dwrite(dev, uio, ioflag);
731 
732 	release_dev(dev);
733 	if ((flags & O_FOFFSET) == 0)
734 		fp->f_offset = uio->uio_offset;
735 	fp->f_nextoff = uio->uio_offset;
736 done:
737 	rel_mplock();
738 	return (error);
739 }
740 
741 /*
742  * MPALMOSTSAFE - acquires mplock
743  */
744 static int
745 vn_statfile(struct file *fp, struct stat *sb, struct ucred *cred)
746 {
747 	struct vnode *vp;
748 	int error;
749 
750 	get_mplock();
751 	vp = (struct vnode *)fp->f_data;
752 	error = vn_stat(vp, sb, cred);
753 	rel_mplock();
754 	return (error);
755 }
756 
757 int
758 vn_stat(struct vnode *vp, struct stat *sb, struct ucred *cred)
759 {
760 	struct vattr vattr;
761 	struct vattr *vap;
762 	int error;
763 	u_short mode;
764 	dev_t dev;
765 
766 	vap = &vattr;
767 	error = VOP_GETATTR(vp, vap);
768 	if (error)
769 		return (error);
770 
771 	/*
772 	 * Zero the spare stat fields
773 	 */
774 	sb->st_lspare = 0;
775 	sb->st_qspare = 0;
776 
777 	/*
778 	 * Copy from vattr table
779 	 */
780 	if (vap->va_fsid != VNOVAL)
781 		sb->st_dev = vap->va_fsid;
782 	else
783 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
784 	sb->st_ino = vap->va_fileid;
785 	mode = vap->va_mode;
786 	switch (vap->va_type) {
787 	case VREG:
788 		mode |= S_IFREG;
789 		break;
790 	case VDIR:
791 		mode |= S_IFDIR;
792 		break;
793 	case VBLK:
794 		mode |= S_IFBLK;
795 		break;
796 	case VCHR:
797 		mode |= S_IFCHR;
798 		break;
799 	case VLNK:
800 		mode |= S_IFLNK;
801 		/* This is a cosmetic change, symlinks do not have a mode. */
802 		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
803 			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
804 		else
805 			sb->st_mode |= ACCESSPERMS;	/* 0777 */
806 		break;
807 	case VSOCK:
808 		mode |= S_IFSOCK;
809 		break;
810 	case VFIFO:
811 		mode |= S_IFIFO;
812 		break;
813 	default:
814 		return (EBADF);
815 	};
816 	sb->st_mode = mode;
817 	sb->st_nlink = vap->va_nlink;
818 	sb->st_uid = vap->va_uid;
819 	sb->st_gid = vap->va_gid;
820 	sb->st_rdev = vap->va_rdev;
821 	sb->st_size = vap->va_size;
822 	sb->st_atimespec = vap->va_atime;
823 	sb->st_mtimespec = vap->va_mtime;
824 	sb->st_ctimespec = vap->va_ctime;
825 
826 	/*
827 	 * A VCHR and VBLK device may track the last access and last modified
828 	 * time independantly of the filesystem.  This is particularly true
829 	 * because device read and write calls may bypass the filesystem.
830 	 */
831 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
832 		if ((dev = vp->v_rdev) != NULL) {
833 			if (dev->si_lastread) {
834 				sb->st_atimespec.tv_sec = dev->si_lastread;
835 				sb->st_atimespec.tv_nsec = 0;
836 			}
837 			if (dev->si_lastwrite) {
838 				sb->st_atimespec.tv_sec = dev->si_lastwrite;
839 				sb->st_atimespec.tv_nsec = 0;
840 			}
841 		}
842 	}
843 
844         /*
845 	 * According to www.opengroup.org, the meaning of st_blksize is
846 	 *   "a filesystem-specific preferred I/O block size for this
847 	 *    object.  In some filesystem types, this may vary from file
848 	 *    to file"
849 	 * Default to PAGE_SIZE after much discussion.
850 	 */
851 
852 	if (vap->va_type == VREG) {
853 		sb->st_blksize = vap->va_blocksize;
854 	} else if (vn_isdisk(vp, NULL)) {
855 		/*
856 		 * XXX this is broken.  If the device is not yet open (aka
857 		 * stat() call, aka v_rdev == NULL), how are we supposed
858 		 * to get a valid block size out of it?
859 		 */
860 		dev_t dev;
861 
862 		if ((dev = vp->v_rdev) == NULL)
863 			dev = udev2dev(vp->v_udev, vp->v_type == VBLK);
864 		sb->st_blksize = dev->si_bsize_best;
865 		if (sb->st_blksize < dev->si_bsize_phys)
866 			sb->st_blksize = dev->si_bsize_phys;
867 		if (sb->st_blksize < BLKDEV_IOSIZE)
868 			sb->st_blksize = BLKDEV_IOSIZE;
869 	} else {
870 		sb->st_blksize = PAGE_SIZE;
871 	}
872 
873 	sb->st_flags = vap->va_flags;
874 	if (suser_cred(cred, 0))
875 		sb->st_gen = 0;
876 	else
877 		sb->st_gen = vap->va_gen;
878 
879 #if (S_BLKSIZE == 512)
880 	/* Optimize this case */
881 	sb->st_blocks = vap->va_bytes >> 9;
882 #else
883 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
884 #endif
885 	sb->st_fsmid = vap->va_fsmid;
886 	return (0);
887 }
888 
889 /*
890  * MPALMOSTSAFE - acquires mplock
891  */
892 static int
893 vn_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
894 {
895 	struct vnode *vp = ((struct vnode *)fp->f_data);
896 	struct vnode *ovp;
897 	struct vattr vattr;
898 	int error;
899 
900 	get_mplock();
901 
902 	switch (vp->v_type) {
903 	case VREG:
904 	case VDIR:
905 		if (com == FIONREAD) {
906 			if ((error = VOP_GETATTR(vp, &vattr)) != 0)
907 				break;
908 			*(int *)data = vattr.va_size - fp->f_offset;
909 			error = 0;
910 			break;
911 		}
912 		if (com == FIOASYNC) {				/* XXX */
913 			error = 0;				/* XXX */
914 			break;
915 		}
916 		/* fall into ... */
917 	default:
918 #if 0
919 		return (ENOTTY);
920 #endif
921 	case VFIFO:
922 	case VCHR:
923 	case VBLK:
924 		if (com == FIODTYPE) {
925 			if (vp->v_type != VCHR && vp->v_type != VBLK) {
926 				error = ENOTTY;
927 				break;
928 			}
929 			*(int *)data = dev_dflags(vp->v_rdev) & D_TYPEMASK;
930 			error = 0;
931 			break;
932 		}
933 		error = VOP_IOCTL(vp, com, data, fp->f_flag, ucred);
934 		if (error == 0 && com == TIOCSCTTY) {
935 			struct proc *p = curthread->td_proc;
936 			struct session *sess;
937 
938 			if (p == NULL) {
939 				error = ENOTTY;
940 				break;
941 			}
942 
943 			sess = p->p_session;
944 			/* Do nothing if reassigning same control tty */
945 			if (sess->s_ttyvp == vp) {
946 				error = 0;
947 				break;
948 			}
949 
950 			/* Get rid of reference to old control tty */
951 			ovp = sess->s_ttyvp;
952 			vref(vp);
953 			sess->s_ttyvp = vp;
954 			if (ovp)
955 				vrele(ovp);
956 		}
957 		break;
958 	}
959 	rel_mplock();
960 	return (error);
961 }
962 
963 /*
964  * MPALMOSTSAFE - acquires mplock
965  */
966 static int
967 vn_poll(struct file *fp, int events, struct ucred *cred)
968 {
969 	int error;
970 
971 	get_mplock();
972 	error = VOP_POLL(((struct vnode *)fp->f_data), events, cred);
973 	rel_mplock();
974 	return (error);
975 }
976 
977 /*
978  * Check that the vnode is still valid, and if so
979  * acquire requested lock.
980  */
981 int
982 #ifndef	DEBUG_LOCKS
983 vn_lock(struct vnode *vp, int flags)
984 #else
985 debug_vn_lock(struct vnode *vp, int flags, const char *filename, int line)
986 #endif
987 {
988 	int error;
989 
990 	do {
991 #ifdef	DEBUG_LOCKS
992 		vp->filename = filename;
993 		vp->line = line;
994 		error = debuglockmgr(&vp->v_lock, flags,
995 				     "vn_lock", filename, line);
996 #else
997 		error = lockmgr(&vp->v_lock, flags);
998 #endif
999 		if (error == 0)
1000 			break;
1001 	} while (flags & LK_RETRY);
1002 
1003 	/*
1004 	 * Because we (had better!) have a ref on the vnode, once it
1005 	 * goes to VRECLAIMED state it will not be recycled until all
1006 	 * refs go away.  So we can just check the flag.
1007 	 */
1008 	if (error == 0 && (vp->v_flag & VRECLAIMED)) {
1009 		lockmgr(&vp->v_lock, LK_RELEASE);
1010 		error = ENOENT;
1011 	}
1012 	return (error);
1013 }
1014 
1015 void
1016 vn_unlock(struct vnode *vp)
1017 {
1018 	lockmgr(&vp->v_lock, LK_RELEASE);
1019 }
1020 
1021 int
1022 vn_islocked(struct vnode *vp)
1023 {
1024 	return (lockstatus(&vp->v_lock, curthread));
1025 }
1026 
1027 /*
1028  * MPALMOSTSAFE - acquires mplock
1029  */
1030 static int
1031 vn_closefile(struct file *fp)
1032 {
1033 	int error;
1034 
1035 	get_mplock();
1036 	fp->f_ops = &badfileops;
1037 	error = vn_close(((struct vnode *)fp->f_data), fp->f_flag);
1038 	rel_mplock();
1039 	return(error);
1040 }
1041 
1042 /*
1043  * MPALMOSTSAFE - acquires mplock
1044  */
1045 static int
1046 vn_kqfilter(struct file *fp, struct knote *kn)
1047 {
1048 	int error;
1049 
1050 	get_mplock();
1051 	error = VOP_KQFILTER(((struct vnode *)fp->f_data), kn);
1052 	rel_mplock();
1053 	return (error);
1054 }
1055