xref: /freebsd/sys/kern/vfs_vnops.c (revision 7dfd9569a2f0637fb9a48157b1c1bfe5709faee3)
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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_mac.h"
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/kdb.h>
47 #include <sys/stat.h>
48 #include <sys/proc.h>
49 #include <sys/limits.h>
50 #include <sys/lock.h>
51 #include <sys/mac.h>
52 #include <sys/mount.h>
53 #include <sys/mutex.h>
54 #include <sys/namei.h>
55 #include <sys/vnode.h>
56 #include <sys/bio.h>
57 #include <sys/buf.h>
58 #include <sys/filio.h>
59 #include <sys/sx.h>
60 #include <sys/ttycom.h>
61 #include <sys/conf.h>
62 #include <sys/syslog.h>
63 #include <sys/unistd.h>
64 
65 static fo_rdwr_t	vn_read;
66 static fo_rdwr_t	vn_write;
67 static fo_ioctl_t	vn_ioctl;
68 static fo_poll_t	vn_poll;
69 static fo_kqfilter_t	vn_kqfilter;
70 static fo_stat_t	vn_statfile;
71 static fo_close_t	vn_closefile;
72 
73 struct 	fileops vnops = {
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_flags = DFLAG_PASSABLE | DFLAG_SEEKABLE
82 };
83 
84 int
85 vn_open(ndp, flagp, cmode, fdidx)
86 	struct nameidata *ndp;
87 	int *flagp, cmode, fdidx;
88 {
89 	struct thread *td = ndp->ni_cnd.cn_thread;
90 
91 	return (vn_open_cred(ndp, flagp, cmode, td->td_ucred, fdidx));
92 }
93 
94 /*
95  * Common code for vnode open operations.
96  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
97  *
98  * Note that this does NOT free nameidata for the successful case,
99  * due to the NDINIT being done elsewhere.
100  */
101 int
102 vn_open_cred(ndp, flagp, cmode, cred, fdidx)
103 	struct nameidata *ndp;
104 	int *flagp, cmode;
105 	struct ucred *cred;
106 	int fdidx;
107 {
108 	struct vnode *vp;
109 	struct mount *mp;
110 	struct thread *td = ndp->ni_cnd.cn_thread;
111 	struct vattr vat;
112 	struct vattr *vap = &vat;
113 	int mode, fmode, error;
114 	int vfslocked, mpsafe;
115 
116 	mpsafe = ndp->ni_cnd.cn_flags & MPSAFE;
117 restart:
118 	vfslocked = 0;
119 	fmode = *flagp;
120 	if (fmode & O_CREAT) {
121 		ndp->ni_cnd.cn_nameiop = CREATE;
122 		ndp->ni_cnd.cn_flags = ISOPEN | LOCKPARENT | LOCKLEAF |
123 		    MPSAFE | AUDITVNODE1;
124 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
125 			ndp->ni_cnd.cn_flags |= FOLLOW;
126 		bwillwrite();
127 		if ((error = namei(ndp)) != 0)
128 			return (error);
129 		vfslocked = NDHASGIANT(ndp);
130 		if (!mpsafe)
131 			ndp->ni_cnd.cn_flags &= ~MPSAFE;
132 		if (ndp->ni_vp == NULL) {
133 			VATTR_NULL(vap);
134 			vap->va_type = VREG;
135 			vap->va_mode = cmode;
136 			if (fmode & O_EXCL)
137 				vap->va_vaflags |= VA_EXCLUSIVE;
138 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
139 				NDFREE(ndp, NDF_ONLY_PNBUF);
140 				vput(ndp->ni_dvp);
141 				VFS_UNLOCK_GIANT(vfslocked);
142 				if ((error = vn_start_write(NULL, &mp,
143 				    V_XSLEEP | PCATCH)) != 0)
144 					return (error);
145 				goto restart;
146 			}
147 #ifdef MAC
148 			error = mac_check_vnode_create(cred, ndp->ni_dvp,
149 			    &ndp->ni_cnd, vap);
150 			if (error == 0) {
151 #endif
152 				VOP_LEASE(ndp->ni_dvp, td, cred, LEASE_WRITE);
153 				error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
154 						   &ndp->ni_cnd, vap);
155 #ifdef MAC
156 			}
157 #endif
158 			vput(ndp->ni_dvp);
159 			vn_finished_write(mp);
160 			if (error) {
161 				VFS_UNLOCK_GIANT(vfslocked);
162 				NDFREE(ndp, NDF_ONLY_PNBUF);
163 				return (error);
164 			}
165 			fmode &= ~O_TRUNC;
166 			vp = ndp->ni_vp;
167 		} else {
168 			if (ndp->ni_dvp == ndp->ni_vp)
169 				vrele(ndp->ni_dvp);
170 			else
171 				vput(ndp->ni_dvp);
172 			ndp->ni_dvp = NULL;
173 			vp = ndp->ni_vp;
174 			if (fmode & O_EXCL) {
175 				error = EEXIST;
176 				goto bad;
177 			}
178 			fmode &= ~O_CREAT;
179 		}
180 	} else {
181 		ndp->ni_cnd.cn_nameiop = LOOKUP;
182 		ndp->ni_cnd.cn_flags = ISOPEN |
183 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) |
184 		    LOCKSHARED | LOCKLEAF | MPSAFE | AUDITVNODE1;
185 		if ((error = namei(ndp)) != 0)
186 			return (error);
187 		if (!mpsafe)
188 			ndp->ni_cnd.cn_flags &= ~MPSAFE;
189 		vfslocked = NDHASGIANT(ndp);
190 		vp = ndp->ni_vp;
191 	}
192 	if (vp->v_type == VLNK) {
193 		error = EMLINK;
194 		goto bad;
195 	}
196 	if (vp->v_type == VSOCK) {
197 		error = EOPNOTSUPP;
198 		goto bad;
199 	}
200 	mode = 0;
201 	if (fmode & (FWRITE | O_TRUNC)) {
202 		if (vp->v_type == VDIR) {
203 			error = EISDIR;
204 			goto bad;
205 		}
206 		mode |= VWRITE;
207 	}
208 	if (fmode & FREAD)
209 		mode |= VREAD;
210 	if (fmode & O_APPEND)
211 		mode |= VAPPEND;
212 #ifdef MAC
213 	error = mac_check_vnode_open(cred, vp, mode);
214 	if (error)
215 		goto bad;
216 #endif
217 	if ((fmode & O_CREAT) == 0) {
218 		if (mode & VWRITE) {
219 			error = vn_writechk(vp);
220 			if (error)
221 				goto bad;
222 		}
223 		if (mode) {
224 		        error = VOP_ACCESS(vp, mode, cred, td);
225 			if (error)
226 				goto bad;
227 		}
228 	}
229 	if ((error = VOP_OPEN(vp, fmode, cred, td, fdidx)) != 0)
230 		goto bad;
231 
232 	if (fmode & FWRITE)
233 		vp->v_writecount++;
234 	*flagp = fmode;
235 	ASSERT_VOP_LOCKED(vp, "vn_open_cred");
236 	if (!mpsafe)
237 		VFS_UNLOCK_GIANT(vfslocked);
238 	return (0);
239 bad:
240 	NDFREE(ndp, NDF_ONLY_PNBUF);
241 	vput(vp);
242 	VFS_UNLOCK_GIANT(vfslocked);
243 	*flagp = fmode;
244 	ndp->ni_vp = NULL;
245 	return (error);
246 }
247 
248 /*
249  * Check for write permissions on the specified vnode.
250  * Prototype text segments cannot be written.
251  */
252 int
253 vn_writechk(vp)
254 	register struct vnode *vp;
255 {
256 
257 	ASSERT_VOP_LOCKED(vp, "vn_writechk");
258 	/*
259 	 * If there's shared text associated with
260 	 * the vnode, try to free it up once.  If
261 	 * we fail, we can't allow writing.
262 	 */
263 	if (vp->v_vflag & VV_TEXT)
264 		return (ETXTBSY);
265 
266 	return (0);
267 }
268 
269 /*
270  * Vnode close call
271  */
272 int
273 vn_close(vp, flags, file_cred, td)
274 	register struct vnode *vp;
275 	int flags;
276 	struct ucred *file_cred;
277 	struct thread *td;
278 {
279 	struct mount *mp;
280 	int error;
281 
282 	VFS_ASSERT_GIANT(vp->v_mount);
283 
284 	vn_start_write(vp, &mp, V_WAIT);
285 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
286 	if (flags & FWRITE)
287 		vp->v_writecount--;
288 	error = VOP_CLOSE(vp, flags, file_cred, td);
289 	vput(vp);
290 	vn_finished_write(mp);
291 	return (error);
292 }
293 
294 /*
295  * Sequential heuristic - detect sequential operation
296  */
297 static __inline
298 int
299 sequential_heuristic(struct uio *uio, struct file *fp)
300 {
301 
302 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
303 	    uio->uio_offset == fp->f_nextoff) {
304 		/*
305 		 * XXX we assume that the filesystem block size is
306 		 * the default.  Not true, but still gives us a pretty
307 		 * good indicator of how sequential the read operations
308 		 * are.
309 		 */
310 		fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
311 		if (fp->f_seqcount > IO_SEQMAX)
312 			fp->f_seqcount = IO_SEQMAX;
313 		return(fp->f_seqcount << IO_SEQSHIFT);
314 	}
315 
316 	/*
317 	 * Not sequential, quick draw-down of seqcount
318 	 */
319 	if (fp->f_seqcount > 1)
320 		fp->f_seqcount = 1;
321 	else
322 		fp->f_seqcount = 0;
323 	return(0);
324 }
325 
326 /*
327  * Package up an I/O request on a vnode into a uio and do it.
328  */
329 int
330 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, active_cred, file_cred,
331     aresid, td)
332 	enum uio_rw rw;
333 	struct vnode *vp;
334 	void *base;
335 	int len;
336 	off_t offset;
337 	enum uio_seg segflg;
338 	int ioflg;
339 	struct ucred *active_cred;
340 	struct ucred *file_cred;
341 	int *aresid;
342 	struct thread *td;
343 {
344 	struct uio auio;
345 	struct iovec aiov;
346 	struct mount *mp;
347 	struct ucred *cred;
348 	int error;
349 
350 	VFS_ASSERT_GIANT(vp->v_mount);
351 
352 	if ((ioflg & IO_NODELOCKED) == 0) {
353 		mp = NULL;
354 		if (rw == UIO_WRITE) {
355 			if (vp->v_type != VCHR &&
356 			    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH))
357 			    != 0)
358 				return (error);
359 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
360 		} else {
361 			/*
362 			 * XXX This should be LK_SHARED but I don't trust VFS
363 			 * enough to leave it like that until it has been
364 			 * reviewed further.
365 			 */
366 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
367 		}
368 
369 	}
370 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
371 	auio.uio_iov = &aiov;
372 	auio.uio_iovcnt = 1;
373 	aiov.iov_base = base;
374 	aiov.iov_len = len;
375 	auio.uio_resid = len;
376 	auio.uio_offset = offset;
377 	auio.uio_segflg = segflg;
378 	auio.uio_rw = rw;
379 	auio.uio_td = td;
380 	error = 0;
381 #ifdef MAC
382 	if ((ioflg & IO_NOMACCHECK) == 0) {
383 		if (rw == UIO_READ)
384 			error = mac_check_vnode_read(active_cred, file_cred,
385 			    vp);
386 		else
387 			error = mac_check_vnode_write(active_cred, file_cred,
388 			    vp);
389 	}
390 #endif
391 	if (error == 0) {
392 		if (file_cred)
393 			cred = file_cred;
394 		else
395 			cred = active_cred;
396 		if (rw == UIO_READ)
397 			error = VOP_READ(vp, &auio, ioflg, cred);
398 		else
399 			error = VOP_WRITE(vp, &auio, ioflg, cred);
400 	}
401 	if (aresid)
402 		*aresid = auio.uio_resid;
403 	else
404 		if (auio.uio_resid && error == 0)
405 			error = EIO;
406 	if ((ioflg & IO_NODELOCKED) == 0) {
407 		if (rw == UIO_WRITE && vp->v_type != VCHR)
408 			vn_finished_write(mp);
409 		VOP_UNLOCK(vp, 0, td);
410 	}
411 	return (error);
412 }
413 
414 /*
415  * Package up an I/O request on a vnode into a uio and do it.  The I/O
416  * request is split up into smaller chunks and we try to avoid saturating
417  * the buffer cache while potentially holding a vnode locked, so we
418  * check bwillwrite() before calling vn_rdwr().  We also call uio_yield()
419  * to give other processes a chance to lock the vnode (either other processes
420  * core'ing the same binary, or unrelated processes scanning the directory).
421  */
422 int
423 vn_rdwr_inchunks(rw, vp, base, len, offset, segflg, ioflg, active_cred,
424     file_cred, aresid, td)
425 	enum uio_rw rw;
426 	struct vnode *vp;
427 	void *base;
428 	size_t len;
429 	off_t offset;
430 	enum uio_seg segflg;
431 	int ioflg;
432 	struct ucred *active_cred;
433 	struct ucred *file_cred;
434 	size_t *aresid;
435 	struct thread *td;
436 {
437 	int error = 0;
438 	int iaresid;
439 
440 	VFS_ASSERT_GIANT(vp->v_mount);
441 
442 	do {
443 		int chunk;
444 
445 		/*
446 		 * Force `offset' to a multiple of MAXBSIZE except possibly
447 		 * for the first chunk, so that filesystems only need to
448 		 * write full blocks except possibly for the first and last
449 		 * chunks.
450 		 */
451 		chunk = MAXBSIZE - (uoff_t)offset % MAXBSIZE;
452 
453 		if (chunk > len)
454 			chunk = len;
455 		if (rw != UIO_READ && vp->v_type == VREG)
456 			bwillwrite();
457 		iaresid = 0;
458 		error = vn_rdwr(rw, vp, base, chunk, offset, segflg,
459 		    ioflg, active_cred, file_cred, &iaresid, td);
460 		len -= chunk;	/* aresid calc already includes length */
461 		if (error)
462 			break;
463 		offset += chunk;
464 		base = (char *)base + chunk;
465 		uio_yield();
466 	} while (len);
467 	if (aresid)
468 		*aresid = len + iaresid;
469 	return (error);
470 }
471 
472 /*
473  * File table vnode read routine.
474  */
475 static int
476 vn_read(fp, uio, active_cred, flags, td)
477 	struct file *fp;
478 	struct uio *uio;
479 	struct ucred *active_cred;
480 	struct thread *td;
481 	int flags;
482 {
483 	struct vnode *vp;
484 	int error, ioflag;
485 	int vfslocked;
486 
487 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
488 	    uio->uio_td, td));
489 	vp = fp->f_vnode;
490 	ioflag = 0;
491 	if (fp->f_flag & FNONBLOCK)
492 		ioflag |= IO_NDELAY;
493 	if (fp->f_flag & O_DIRECT)
494 		ioflag |= IO_DIRECT;
495 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
496 	VOP_LEASE(vp, td, fp->f_cred, LEASE_READ);
497 	/*
498 	 * According to McKusick the vn lock is protecting f_offset here.
499 	 * Once this field has it's own lock we can acquire this shared.
500 	 */
501 	if ((flags & FOF_OFFSET) == 0) {
502 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
503 		uio->uio_offset = fp->f_offset;
504 	} else
505 		vn_lock(vp, LK_SHARED | LK_RETRY, td);
506 
507 	ioflag |= sequential_heuristic(uio, fp);
508 
509 #ifdef MAC
510 	error = mac_check_vnode_read(active_cred, fp->f_cred, vp);
511 	if (error == 0)
512 #endif
513 		error = VOP_READ(vp, uio, ioflag, fp->f_cred);
514 	if ((flags & FOF_OFFSET) == 0)
515 		fp->f_offset = uio->uio_offset;
516 	fp->f_nextoff = uio->uio_offset;
517 	VOP_UNLOCK(vp, 0, td);
518 	VFS_UNLOCK_GIANT(vfslocked);
519 	return (error);
520 }
521 
522 /*
523  * File table vnode write routine.
524  */
525 static int
526 vn_write(fp, uio, active_cred, flags, td)
527 	struct file *fp;
528 	struct uio *uio;
529 	struct ucred *active_cred;
530 	struct thread *td;
531 	int flags;
532 {
533 	struct vnode *vp;
534 	struct mount *mp;
535 	int error, ioflag;
536 	int vfslocked;
537 
538 	KASSERT(uio->uio_td == td, ("uio_td %p is not td %p",
539 	    uio->uio_td, td));
540 	vp = fp->f_vnode;
541 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
542 	if (vp->v_type == VREG)
543 		bwillwrite();
544 	ioflag = IO_UNIT;
545 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
546 		ioflag |= IO_APPEND;
547 	if (fp->f_flag & FNONBLOCK)
548 		ioflag |= IO_NDELAY;
549 	if (fp->f_flag & O_DIRECT)
550 		ioflag |= IO_DIRECT;
551 	if ((fp->f_flag & O_FSYNC) ||
552 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
553 		ioflag |= IO_SYNC;
554 	mp = NULL;
555 	if (vp->v_type != VCHR &&
556 	    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
557 		goto unlock;
558 	VOP_LEASE(vp, td, fp->f_cred, LEASE_WRITE);
559 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
560 	if ((flags & FOF_OFFSET) == 0)
561 		uio->uio_offset = fp->f_offset;
562 	ioflag |= sequential_heuristic(uio, fp);
563 #ifdef MAC
564 	error = mac_check_vnode_write(active_cred, fp->f_cred, vp);
565 	if (error == 0)
566 #endif
567 		error = VOP_WRITE(vp, uio, ioflag, fp->f_cred);
568 	if ((flags & FOF_OFFSET) == 0)
569 		fp->f_offset = uio->uio_offset;
570 	fp->f_nextoff = uio->uio_offset;
571 	VOP_UNLOCK(vp, 0, td);
572 	if (vp->v_type != VCHR)
573 		vn_finished_write(mp);
574 unlock:
575 	VFS_UNLOCK_GIANT(vfslocked);
576 	return (error);
577 }
578 
579 /*
580  * File table vnode stat routine.
581  */
582 static int
583 vn_statfile(fp, sb, active_cred, td)
584 	struct file *fp;
585 	struct stat *sb;
586 	struct ucred *active_cred;
587 	struct thread *td;
588 {
589 	struct vnode *vp = fp->f_vnode;
590 	int vfslocked;
591 	int error;
592 
593 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
594 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
595 	error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
596 	VOP_UNLOCK(vp, 0, td);
597 	VFS_UNLOCK_GIANT(vfslocked);
598 
599 	return (error);
600 }
601 
602 /*
603  * Stat a vnode; implementation for the stat syscall
604  */
605 int
606 vn_stat(vp, sb, active_cred, file_cred, td)
607 	struct vnode *vp;
608 	register struct stat *sb;
609 	struct ucred *active_cred;
610 	struct ucred *file_cred;
611 	struct thread *td;
612 {
613 	struct vattr vattr;
614 	register struct vattr *vap;
615 	int error;
616 	u_short mode;
617 
618 #ifdef MAC
619 	error = mac_check_vnode_stat(active_cred, file_cred, vp);
620 	if (error)
621 		return (error);
622 #endif
623 
624 	vap = &vattr;
625 	error = VOP_GETATTR(vp, vap, active_cred, td);
626 	if (error)
627 		return (error);
628 
629 	/*
630 	 * Zero the spare stat fields
631 	 */
632 	bzero(sb, sizeof *sb);
633 
634 	/*
635 	 * Copy from vattr table
636 	 */
637 	if (vap->va_fsid != VNOVAL)
638 		sb->st_dev = vap->va_fsid;
639 	else
640 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
641 	sb->st_ino = vap->va_fileid;
642 	mode = vap->va_mode;
643 	switch (vap->va_type) {
644 	case VREG:
645 		mode |= S_IFREG;
646 		break;
647 	case VDIR:
648 		mode |= S_IFDIR;
649 		break;
650 	case VBLK:
651 		mode |= S_IFBLK;
652 		break;
653 	case VCHR:
654 		mode |= S_IFCHR;
655 		break;
656 	case VLNK:
657 		mode |= S_IFLNK;
658 		/* This is a cosmetic change, symlinks do not have a mode. */
659 		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
660 			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
661 		else
662 			sb->st_mode |= ACCESSPERMS;	/* 0777 */
663 		break;
664 	case VSOCK:
665 		mode |= S_IFSOCK;
666 		break;
667 	case VFIFO:
668 		mode |= S_IFIFO;
669 		break;
670 	default:
671 		return (EBADF);
672 	};
673 	sb->st_mode = mode;
674 	sb->st_nlink = vap->va_nlink;
675 	sb->st_uid = vap->va_uid;
676 	sb->st_gid = vap->va_gid;
677 	sb->st_rdev = vap->va_rdev;
678 	if (vap->va_size > OFF_MAX)
679 		return (EOVERFLOW);
680 	sb->st_size = vap->va_size;
681 	sb->st_atimespec = vap->va_atime;
682 	sb->st_mtimespec = vap->va_mtime;
683 	sb->st_ctimespec = vap->va_ctime;
684 	sb->st_birthtimespec = vap->va_birthtime;
685 
686         /*
687 	 * According to www.opengroup.org, the meaning of st_blksize is
688 	 *   "a filesystem-specific preferred I/O block size for this
689 	 *    object.  In some filesystem types, this may vary from file
690 	 *    to file"
691 	 * Default to PAGE_SIZE after much discussion.
692 	 * XXX: min(PAGE_SIZE, vp->v_bufobj.bo_bsize) may be more correct.
693 	 */
694 
695 	sb->st_blksize = PAGE_SIZE;
696 
697 	sb->st_flags = vap->va_flags;
698 	if (suser(td))
699 		sb->st_gen = 0;
700 	else
701 		sb->st_gen = vap->va_gen;
702 
703 #if (S_BLKSIZE == 512)
704 	/* Optimize this case */
705 	sb->st_blocks = vap->va_bytes >> 9;
706 #else
707 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
708 #endif
709 	return (0);
710 }
711 
712 /*
713  * File table vnode ioctl routine.
714  */
715 static int
716 vn_ioctl(fp, com, data, active_cred, td)
717 	struct file *fp;
718 	u_long com;
719 	void *data;
720 	struct ucred *active_cred;
721 	struct thread *td;
722 {
723 	struct vnode *vp = fp->f_vnode;
724 	struct vattr vattr;
725 	int vfslocked;
726 	int error;
727 
728 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
729 	error = ENOTTY;
730 	switch (vp->v_type) {
731 	case VREG:
732 	case VDIR:
733 		if (com == FIONREAD) {
734 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
735 			error = VOP_GETATTR(vp, &vattr, active_cred, td);
736 			VOP_UNLOCK(vp, 0, td);
737 			if (!error)
738 				*(int *)data = vattr.va_size - fp->f_offset;
739 		}
740 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
741 			error = 0;
742 		else
743 			error = VOP_IOCTL(vp, com, data, fp->f_flag,
744 			    active_cred, td);
745 		break;
746 
747 	default:
748 		break;
749 	}
750 	VFS_UNLOCK_GIANT(vfslocked);
751 	return (error);
752 }
753 
754 /*
755  * File table vnode poll routine.
756  */
757 static int
758 vn_poll(fp, events, active_cred, td)
759 	struct file *fp;
760 	int events;
761 	struct ucred *active_cred;
762 	struct thread *td;
763 {
764 	struct vnode *vp;
765 	int vfslocked;
766 	int error;
767 
768 	vp = fp->f_vnode;
769 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
770 #ifdef MAC
771 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
772 	error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
773 	VOP_UNLOCK(vp, 0, td);
774 	if (!error)
775 #endif
776 
777 	error = VOP_POLL(vp, events, fp->f_cred, td);
778 	VFS_UNLOCK_GIANT(vfslocked);
779 	return (error);
780 }
781 
782 /*
783  * Check that the vnode is still valid, and if so
784  * acquire requested lock.
785  */
786 int
787 vn_lock(vp, flags, td)
788 	struct vnode *vp;
789 	int flags;
790 	struct thread *td;
791 {
792 	int error;
793 
794 	do {
795 		if ((flags & LK_INTERLOCK) == 0)
796 			VI_LOCK(vp);
797 		if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
798 		    vp->v_iflag & VI_DOOMED) {
799 			VI_UNLOCK(vp);
800 			return (ENOENT);
801 		}
802 		/*
803 		 * Just polling to check validity.
804 		 */
805 		if ((flags & LK_TYPE_MASK) == 0) {
806 			VI_UNLOCK(vp);
807 			return (0);
808 		}
809 		/*
810 		 * lockmgr drops interlock before it will return for
811 		 * any reason.  So force the code above to relock it.
812 		 */
813 		error = VOP_LOCK(vp, flags | LK_INTERLOCK, td);
814 		flags &= ~LK_INTERLOCK;
815 		KASSERT((flags & LK_RETRY) == 0 || error == 0,
816 		    ("LK_RETRY set with incompatible flags %d\n", flags));
817 		/*
818 		 * Callers specify LK_RETRY if they wish to get dead vnodes.
819 		 * If RETRY is not set, we return ENOENT instead.
820 		 */
821 		if (error == 0 && vp->v_iflag & VI_DOOMED &&
822 		    (flags & LK_RETRY) == 0) {
823 			VOP_UNLOCK(vp, 0, td);
824 			error = ENOENT;
825 			break;
826 		}
827 	} while (flags & LK_RETRY && error != 0);
828 	return (error);
829 }
830 
831 /*
832  * File table vnode close routine.
833  */
834 static int
835 vn_closefile(fp, td)
836 	struct file *fp;
837 	struct thread *td;
838 {
839 	struct vnode *vp;
840 	struct flock lf;
841 	int vfslocked;
842 	int error;
843 
844 	vp = fp->f_vnode;
845 
846 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
847 	if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
848 		lf.l_whence = SEEK_SET;
849 		lf.l_start = 0;
850 		lf.l_len = 0;
851 		lf.l_type = F_UNLCK;
852 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
853 	}
854 
855 	fp->f_ops = &badfileops;
856 
857 	error = vn_close(vp, fp->f_flag, fp->f_cred, td);
858 	VFS_UNLOCK_GIANT(vfslocked);
859 	return (error);
860 }
861 
862 /*
863  * Preparing to start a filesystem write operation. If the operation is
864  * permitted, then we bump the count of operations in progress and
865  * proceed. If a suspend request is in progress, we wait until the
866  * suspension is over, and then proceed.
867  */
868 int
869 vn_start_write(vp, mpp, flags)
870 	struct vnode *vp;
871 	struct mount **mpp;
872 	int flags;
873 {
874 	struct mount *mp;
875 	int error;
876 
877 	error = 0;
878 	/*
879 	 * If a vnode is provided, get and return the mount point that
880 	 * to which it will write.
881 	 */
882 	if (vp != NULL) {
883 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
884 			*mpp = NULL;
885 			if (error != EOPNOTSUPP)
886 				return (error);
887 			return (0);
888 		}
889 	}
890 	if ((mp = *mpp) == NULL)
891 		return (0);
892 	MNT_ILOCK(mp);
893 	if (vp == NULL)
894 		MNT_REF(mp);
895 	/*
896 	 * Check on status of suspension.
897 	 */
898 	while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
899 		if (flags & V_NOWAIT) {
900 			error = EWOULDBLOCK;
901 			goto unlock;
902 		}
903 		error = msleep(&mp->mnt_flag, MNT_MTX(mp),
904 		    (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
905 		if (error)
906 			goto unlock;
907 	}
908 	if (flags & V_XSLEEP)
909 		goto unlock;
910 	mp->mnt_writeopcount++;
911 unlock:
912 	MNT_REL(mp);
913 	MNT_IUNLOCK(mp);
914 	return (error);
915 }
916 
917 /*
918  * Secondary suspension. Used by operations such as vop_inactive
919  * routines that are needed by the higher level functions. These
920  * are allowed to proceed until all the higher level functions have
921  * completed (indicated by mnt_writeopcount dropping to zero). At that
922  * time, these operations are halted until the suspension is over.
923  */
924 int
925 vn_write_suspend_wait(vp, mp, flags)
926 	struct vnode *vp;
927 	struct mount *mp;
928 	int flags;
929 {
930 	int error;
931 
932 	if (vp != NULL) {
933 		if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
934 			if (error != EOPNOTSUPP)
935 				return (error);
936 			return (0);
937 		}
938 	}
939 	/*
940 	 * If we are not suspended or have not yet reached suspended
941 	 * mode, then let the operation proceed.
942 	 */
943 	if (mp == NULL)
944 		return (0);
945 	MNT_ILOCK(mp);
946 	if (vp == NULL)
947 		MNT_REF(mp);
948 	if ((mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) {
949 		MNT_REL(mp);
950 		MNT_IUNLOCK(mp);
951 		return (0);
952 	}
953 	if (flags & V_NOWAIT) {
954 		MNT_REL(mp);
955 		MNT_IUNLOCK(mp);
956 		return (EWOULDBLOCK);
957 	}
958 	/*
959 	 * Wait for the suspension to finish.
960 	 */
961 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
962 	    (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
963 	vfs_rel(mp);
964 	return (error);
965 }
966 
967 /*
968  * Secondary suspension. Used by operations such as vop_inactive
969  * routines that are needed by the higher level functions. These
970  * are allowed to proceed until all the higher level functions have
971  * completed (indicated by mnt_writeopcount dropping to zero). At that
972  * time, these operations are halted until the suspension is over.
973  */
974 int
975 vn_start_secondary_write(vp, mpp, flags)
976 	struct vnode *vp;
977 	struct mount **mpp;
978 	int flags;
979 {
980 	struct mount *mp;
981 	int error;
982 
983  retry:
984 	if (vp != NULL) {
985 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
986 			*mpp = NULL;
987 			if (error != EOPNOTSUPP)
988 				return (error);
989 			return (0);
990 		}
991 	}
992 	/*
993 	 * If we are not suspended or have not yet reached suspended
994 	 * mode, then let the operation proceed.
995 	 */
996 	if ((mp = *mpp) == NULL)
997 		return (0);
998 	MNT_ILOCK(mp);
999 	if (vp == NULL)
1000 		MNT_REF(mp);
1001 	if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1002 		mp->mnt_secondary_writes++;
1003 		mp->mnt_secondary_accwrites++;
1004 		MNT_REL(mp);
1005 		MNT_IUNLOCK(mp);
1006 		return (0);
1007 	}
1008 	if (flags & V_NOWAIT) {
1009 		MNT_REL(mp);
1010 		MNT_IUNLOCK(mp);
1011 		return (EWOULDBLOCK);
1012 	}
1013 	/*
1014 	 * Wait for the suspension to finish.
1015 	 */
1016 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1017 		       (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1018 	vfs_rel(mp);
1019 	if (error == 0)
1020 		goto retry;
1021 	return (error);
1022 }
1023 
1024 /*
1025  * Filesystem write operation has completed. If we are suspending and this
1026  * operation is the last one, notify the suspender that the suspension is
1027  * now in effect.
1028  */
1029 void
1030 vn_finished_write(mp)
1031 	struct mount *mp;
1032 {
1033 	if (mp == NULL)
1034 		return;
1035 	MNT_ILOCK(mp);
1036 	mp->mnt_writeopcount--;
1037 	if (mp->mnt_writeopcount < 0)
1038 		panic("vn_finished_write: neg cnt");
1039 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1040 	    mp->mnt_writeopcount <= 0)
1041 		wakeup(&mp->mnt_writeopcount);
1042 	MNT_IUNLOCK(mp);
1043 }
1044 
1045 
1046 /*
1047  * Filesystem secondary write operation has completed. If we are
1048  * suspending and this operation is the last one, notify the suspender
1049  * that the suspension is now in effect.
1050  */
1051 void
1052 vn_finished_secondary_write(mp)
1053 	struct mount *mp;
1054 {
1055 	if (mp == NULL)
1056 		return;
1057 	MNT_ILOCK(mp);
1058 	mp->mnt_secondary_writes--;
1059 	if (mp->mnt_secondary_writes < 0)
1060 		panic("vn_finished_secondary_write: neg cnt");
1061 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1062 	    mp->mnt_secondary_writes <= 0)
1063 		wakeup(&mp->mnt_secondary_writes);
1064 	MNT_IUNLOCK(mp);
1065 }
1066 
1067 
1068 
1069 /*
1070  * Request a filesystem to suspend write operations.
1071  */
1072 int
1073 vfs_write_suspend(mp)
1074 	struct mount *mp;
1075 {
1076 	struct thread *td = curthread;
1077 	int error;
1078 
1079 	error = 0;
1080 	MNT_ILOCK(mp);
1081 	if (mp->mnt_kern_flag & MNTK_SUSPEND)
1082 		goto unlock;
1083 	mp->mnt_kern_flag |= MNTK_SUSPEND;
1084 	if (mp->mnt_writeopcount > 0)
1085 		(void) msleep(&mp->mnt_writeopcount,
1086 		    MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1087 	else
1088 		MNT_IUNLOCK(mp);
1089 	if ((error = VFS_SYNC(mp, MNT_SUSPEND, td)) != 0) {
1090 		vfs_write_resume(mp);
1091 		return (error);
1092 	}
1093 	MNT_ILOCK(mp);
1094 unlock:
1095 	MNT_IUNLOCK(mp);
1096 	return (error);
1097 }
1098 
1099 /*
1100  * Request a filesystem to resume write operations.
1101  */
1102 void
1103 vfs_write_resume(mp)
1104 	struct mount *mp;
1105 {
1106 
1107 	MNT_ILOCK(mp);
1108 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1109 		mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1110 				       MNTK_SUSPENDED);
1111 		wakeup(&mp->mnt_writeopcount);
1112 		wakeup(&mp->mnt_flag);
1113 	}
1114 	MNT_IUNLOCK(mp);
1115 }
1116 
1117 /*
1118  * Implement kqueues for files by translating it to vnode operation.
1119  */
1120 static int
1121 vn_kqfilter(struct file *fp, struct knote *kn)
1122 {
1123 	int vfslocked;
1124 	int error;
1125 
1126 	vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1127 	error = VOP_KQFILTER(fp->f_vnode, kn);
1128 	VFS_UNLOCK_GIANT(vfslocked);
1129 
1130 	return error;
1131 }
1132 
1133 /*
1134  * Simplified in-kernel wrapper calls for extended attribute access.
1135  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1136  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1137  */
1138 int
1139 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1140     const char *attrname, int *buflen, char *buf, struct thread *td)
1141 {
1142 	struct uio	auio;
1143 	struct iovec	iov;
1144 	int	error;
1145 
1146 	iov.iov_len = *buflen;
1147 	iov.iov_base = buf;
1148 
1149 	auio.uio_iov = &iov;
1150 	auio.uio_iovcnt = 1;
1151 	auio.uio_rw = UIO_READ;
1152 	auio.uio_segflg = UIO_SYSSPACE;
1153 	auio.uio_td = td;
1154 	auio.uio_offset = 0;
1155 	auio.uio_resid = *buflen;
1156 
1157 	if ((ioflg & IO_NODELOCKED) == 0)
1158 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1159 
1160 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1161 
1162 	/* authorize attribute retrieval as kernel */
1163 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1164 	    td);
1165 
1166 	if ((ioflg & IO_NODELOCKED) == 0)
1167 		VOP_UNLOCK(vp, 0, td);
1168 
1169 	if (error == 0) {
1170 		*buflen = *buflen - auio.uio_resid;
1171 	}
1172 
1173 	return (error);
1174 }
1175 
1176 /*
1177  * XXX failure mode if partially written?
1178  */
1179 int
1180 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1181     const char *attrname, int buflen, char *buf, struct thread *td)
1182 {
1183 	struct uio	auio;
1184 	struct iovec	iov;
1185 	struct mount	*mp;
1186 	int	error;
1187 
1188 	iov.iov_len = buflen;
1189 	iov.iov_base = buf;
1190 
1191 	auio.uio_iov = &iov;
1192 	auio.uio_iovcnt = 1;
1193 	auio.uio_rw = UIO_WRITE;
1194 	auio.uio_segflg = UIO_SYSSPACE;
1195 	auio.uio_td = td;
1196 	auio.uio_offset = 0;
1197 	auio.uio_resid = buflen;
1198 
1199 	if ((ioflg & IO_NODELOCKED) == 0) {
1200 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1201 			return (error);
1202 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1203 	}
1204 
1205 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1206 
1207 	/* authorize attribute setting as kernel */
1208 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1209 
1210 	if ((ioflg & IO_NODELOCKED) == 0) {
1211 		vn_finished_write(mp);
1212 		VOP_UNLOCK(vp, 0, td);
1213 	}
1214 
1215 	return (error);
1216 }
1217 
1218 int
1219 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1220     const char *attrname, struct thread *td)
1221 {
1222 	struct mount	*mp;
1223 	int	error;
1224 
1225 	if ((ioflg & IO_NODELOCKED) == 0) {
1226 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1227 			return (error);
1228 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1229 	}
1230 
1231 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1232 
1233 	/* authorize attribute removal as kernel */
1234 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1235 	if (error == EOPNOTSUPP)
1236 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1237 		    NULL, td);
1238 
1239 	if ((ioflg & IO_NODELOCKED) == 0) {
1240 		vn_finished_write(mp);
1241 		VOP_UNLOCK(vp, 0, td);
1242 	}
1243 
1244 	return (error);
1245 }
1246