xref: /freebsd/sys/kern/vfs_vnops.c (revision 4f29da19bd44f0e99f021510460a81bf754c21d2)
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)
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 	vn_finished_write(mp);
573 unlock:
574 	VFS_UNLOCK_GIANT(vfslocked);
575 	return (error);
576 }
577 
578 /*
579  * File table vnode stat routine.
580  */
581 static int
582 vn_statfile(fp, sb, active_cred, td)
583 	struct file *fp;
584 	struct stat *sb;
585 	struct ucred *active_cred;
586 	struct thread *td;
587 {
588 	struct vnode *vp = fp->f_vnode;
589 	int vfslocked;
590 	int error;
591 
592 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
593 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
594 	error = vn_stat(vp, sb, active_cred, fp->f_cred, td);
595 	VOP_UNLOCK(vp, 0, td);
596 	VFS_UNLOCK_GIANT(vfslocked);
597 
598 	return (error);
599 }
600 
601 /*
602  * Stat a vnode; implementation for the stat syscall
603  */
604 int
605 vn_stat(vp, sb, active_cred, file_cred, td)
606 	struct vnode *vp;
607 	register struct stat *sb;
608 	struct ucred *active_cred;
609 	struct ucred *file_cred;
610 	struct thread *td;
611 {
612 	struct vattr vattr;
613 	register struct vattr *vap;
614 	int error;
615 	u_short mode;
616 
617 #ifdef MAC
618 	error = mac_check_vnode_stat(active_cred, file_cred, vp);
619 	if (error)
620 		return (error);
621 #endif
622 
623 	vap = &vattr;
624 	error = VOP_GETATTR(vp, vap, active_cred, td);
625 	if (error)
626 		return (error);
627 
628 	/*
629 	 * Zero the spare stat fields
630 	 */
631 	bzero(sb, sizeof *sb);
632 
633 	/*
634 	 * Copy from vattr table
635 	 */
636 	if (vap->va_fsid != VNOVAL)
637 		sb->st_dev = vap->va_fsid;
638 	else
639 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
640 	sb->st_ino = vap->va_fileid;
641 	mode = vap->va_mode;
642 	switch (vap->va_type) {
643 	case VREG:
644 		mode |= S_IFREG;
645 		break;
646 	case VDIR:
647 		mode |= S_IFDIR;
648 		break;
649 	case VBLK:
650 		mode |= S_IFBLK;
651 		break;
652 	case VCHR:
653 		mode |= S_IFCHR;
654 		break;
655 	case VLNK:
656 		mode |= S_IFLNK;
657 		/* This is a cosmetic change, symlinks do not have a mode. */
658 		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
659 			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
660 		else
661 			sb->st_mode |= ACCESSPERMS;	/* 0777 */
662 		break;
663 	case VSOCK:
664 		mode |= S_IFSOCK;
665 		break;
666 	case VFIFO:
667 		mode |= S_IFIFO;
668 		break;
669 	default:
670 		return (EBADF);
671 	};
672 	sb->st_mode = mode;
673 	sb->st_nlink = vap->va_nlink;
674 	sb->st_uid = vap->va_uid;
675 	sb->st_gid = vap->va_gid;
676 	sb->st_rdev = vap->va_rdev;
677 	if (vap->va_size > OFF_MAX)
678 		return (EOVERFLOW);
679 	sb->st_size = vap->va_size;
680 	sb->st_atimespec = vap->va_atime;
681 	sb->st_mtimespec = vap->va_mtime;
682 	sb->st_ctimespec = vap->va_ctime;
683 	sb->st_birthtimespec = vap->va_birthtime;
684 
685         /*
686 	 * According to www.opengroup.org, the meaning of st_blksize is
687 	 *   "a filesystem-specific preferred I/O block size for this
688 	 *    object.  In some filesystem types, this may vary from file
689 	 *    to file"
690 	 * Default to PAGE_SIZE after much discussion.
691 	 * XXX: min(PAGE_SIZE, vp->v_bufobj.bo_bsize) may be more correct.
692 	 */
693 
694 	sb->st_blksize = PAGE_SIZE;
695 
696 	sb->st_flags = vap->va_flags;
697 	if (suser(td))
698 		sb->st_gen = 0;
699 	else
700 		sb->st_gen = vap->va_gen;
701 
702 #if (S_BLKSIZE == 512)
703 	/* Optimize this case */
704 	sb->st_blocks = vap->va_bytes >> 9;
705 #else
706 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
707 #endif
708 	return (0);
709 }
710 
711 /*
712  * File table vnode ioctl routine.
713  */
714 static int
715 vn_ioctl(fp, com, data, active_cred, td)
716 	struct file *fp;
717 	u_long com;
718 	void *data;
719 	struct ucred *active_cred;
720 	struct thread *td;
721 {
722 	struct vnode *vp = fp->f_vnode;
723 	struct vattr vattr;
724 	int vfslocked;
725 	int error;
726 
727 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
728 	error = ENOTTY;
729 	switch (vp->v_type) {
730 	case VREG:
731 	case VDIR:
732 		if (com == FIONREAD) {
733 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
734 			error = VOP_GETATTR(vp, &vattr, active_cred, td);
735 			VOP_UNLOCK(vp, 0, td);
736 			if (!error)
737 				*(int *)data = vattr.va_size - fp->f_offset;
738 		}
739 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
740 			error = 0;
741 		else
742 			error = VOP_IOCTL(vp, com, data, fp->f_flag,
743 			    active_cred, td);
744 		break;
745 
746 	default:
747 		break;
748 	}
749 	VFS_UNLOCK_GIANT(vfslocked);
750 	return (error);
751 }
752 
753 /*
754  * File table vnode poll routine.
755  */
756 static int
757 vn_poll(fp, events, active_cred, td)
758 	struct file *fp;
759 	int events;
760 	struct ucred *active_cred;
761 	struct thread *td;
762 {
763 	struct vnode *vp;
764 	int vfslocked;
765 	int error;
766 
767 	vp = fp->f_vnode;
768 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
769 #ifdef MAC
770 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
771 	error = mac_check_vnode_poll(active_cred, fp->f_cred, vp);
772 	VOP_UNLOCK(vp, 0, td);
773 	if (!error)
774 #endif
775 
776 	error = VOP_POLL(vp, events, fp->f_cred, td);
777 	VFS_UNLOCK_GIANT(vfslocked);
778 	return (error);
779 }
780 
781 /*
782  * Check that the vnode is still valid, and if so
783  * acquire requested lock.
784  */
785 int
786 vn_lock(vp, flags, td)
787 	struct vnode *vp;
788 	int flags;
789 	struct thread *td;
790 {
791 	int error;
792 
793 	do {
794 		if ((flags & LK_INTERLOCK) == 0)
795 			VI_LOCK(vp);
796 		if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
797 		    vp->v_iflag & VI_DOOMED) {
798 			VI_UNLOCK(vp);
799 			return (ENOENT);
800 		}
801 		/*
802 		 * Just polling to check validity.
803 		 */
804 		if ((flags & LK_TYPE_MASK) == 0) {
805 			VI_UNLOCK(vp);
806 			return (0);
807 		}
808 		/*
809 		 * lockmgr drops interlock before it will return for
810 		 * any reason.  So force the code above to relock it.
811 		 */
812 		error = VOP_LOCK(vp, flags | LK_INTERLOCK, td);
813 		flags &= ~LK_INTERLOCK;
814 		KASSERT((flags & LK_RETRY) == 0 || error == 0,
815 		    ("LK_RETRY set with incompatible flags %d\n", flags));
816 		/*
817 		 * Callers specify LK_RETRY if they wish to get dead vnodes.
818 		 * If RETRY is not set, we return ENOENT instead.
819 		 */
820 		if (error == 0 && vp->v_iflag & VI_DOOMED &&
821 		    (flags & LK_RETRY) == 0) {
822 			VOP_UNLOCK(vp, 0, td);
823 			error = ENOENT;
824 			break;
825 		}
826 	} while (flags & LK_RETRY && error != 0);
827 	return (error);
828 }
829 
830 /*
831  * File table vnode close routine.
832  */
833 static int
834 vn_closefile(fp, td)
835 	struct file *fp;
836 	struct thread *td;
837 {
838 	struct vnode *vp;
839 	struct flock lf;
840 	int vfslocked;
841 	int error;
842 
843 	vp = fp->f_vnode;
844 
845 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
846 	if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
847 		lf.l_whence = SEEK_SET;
848 		lf.l_start = 0;
849 		lf.l_len = 0;
850 		lf.l_type = F_UNLCK;
851 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
852 	}
853 
854 	fp->f_ops = &badfileops;
855 
856 	error = vn_close(vp, fp->f_flag, fp->f_cred, td);
857 	VFS_UNLOCK_GIANT(vfslocked);
858 	return (error);
859 }
860 
861 /*
862  * Preparing to start a filesystem write operation. If the operation is
863  * permitted, then we bump the count of operations in progress and
864  * proceed. If a suspend request is in progress, we wait until the
865  * suspension is over, and then proceed.
866  */
867 int
868 vn_start_write(vp, mpp, flags)
869 	struct vnode *vp;
870 	struct mount **mpp;
871 	int flags;
872 {
873 	struct mount *mp;
874 	int error;
875 
876 	error = 0;
877 	/*
878 	 * If a vnode is provided, get and return the mount point that
879 	 * to which it will write.
880 	 */
881 	if (vp != NULL) {
882 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
883 			*mpp = NULL;
884 			if (error != EOPNOTSUPP)
885 				return (error);
886 			return (0);
887 		}
888 	}
889 	if ((mp = *mpp) == NULL)
890 		return (0);
891 	MNT_ILOCK(mp);
892 	if (vp == NULL)
893 		MNT_REF(mp);
894 	/*
895 	 * Check on status of suspension.
896 	 */
897 	while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
898 		if (flags & V_NOWAIT) {
899 			error = EWOULDBLOCK;
900 			goto unlock;
901 		}
902 		error = msleep(&mp->mnt_flag, MNT_MTX(mp),
903 		    (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
904 		if (error)
905 			goto unlock;
906 	}
907 	if (flags & V_XSLEEP)
908 		goto unlock;
909 	mp->mnt_writeopcount++;
910 unlock:
911 	MNT_REL(mp);
912 	MNT_IUNLOCK(mp);
913 	return (error);
914 }
915 
916 /*
917  * Secondary suspension. Used by operations such as vop_inactive
918  * routines that are needed by the higher level functions. These
919  * are allowed to proceed until all the higher level functions have
920  * completed (indicated by mnt_writeopcount dropping to zero). At that
921  * time, these operations are halted until the suspension is over.
922  */
923 int
924 vn_write_suspend_wait(vp, mp, flags)
925 	struct vnode *vp;
926 	struct mount *mp;
927 	int flags;
928 {
929 	int error;
930 
931 	if (vp != NULL) {
932 		if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
933 			if (error != EOPNOTSUPP)
934 				return (error);
935 			return (0);
936 		}
937 	}
938 	/*
939 	 * If we are not suspended or have not yet reached suspended
940 	 * mode, then let the operation proceed.
941 	 */
942 	if (mp == NULL)
943 		return (0);
944 	MNT_ILOCK(mp);
945 	if (vp == NULL)
946 		MNT_REF(mp);
947 	if ((mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) {
948 		MNT_REL(mp);
949 		MNT_IUNLOCK(mp);
950 		return (0);
951 	}
952 	if (flags & V_NOWAIT) {
953 		MNT_REL(mp);
954 		MNT_IUNLOCK(mp);
955 		return (EWOULDBLOCK);
956 	}
957 	/*
958 	 * Wait for the suspension to finish.
959 	 */
960 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
961 	    (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
962 	vfs_rel(mp);
963 	return (error);
964 }
965 
966 /*
967  * Secondary suspension. Used by operations such as vop_inactive
968  * routines that are needed by the higher level functions. These
969  * are allowed to proceed until all the higher level functions have
970  * completed (indicated by mnt_writeopcount dropping to zero). At that
971  * time, these operations are halted until the suspension is over.
972  */
973 int
974 vn_start_secondary_write(vp, mpp, flags)
975 	struct vnode *vp;
976 	struct mount **mpp;
977 	int flags;
978 {
979 	struct mount *mp;
980 	int error;
981 
982  retry:
983 	if (vp != NULL) {
984 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
985 			*mpp = NULL;
986 			if (error != EOPNOTSUPP)
987 				return (error);
988 			return (0);
989 		}
990 	}
991 	/*
992 	 * If we are not suspended or have not yet reached suspended
993 	 * mode, then let the operation proceed.
994 	 */
995 	if ((mp = *mpp) == NULL)
996 		return (0);
997 	MNT_ILOCK(mp);
998 	if (vp == NULL)
999 		MNT_REF(mp);
1000 	if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1001 		mp->mnt_secondary_writes++;
1002 		mp->mnt_secondary_accwrites++;
1003 		MNT_REL(mp);
1004 		MNT_IUNLOCK(mp);
1005 		return (0);
1006 	}
1007 	if (flags & V_NOWAIT) {
1008 		MNT_REL(mp);
1009 		MNT_IUNLOCK(mp);
1010 		return (EWOULDBLOCK);
1011 	}
1012 	/*
1013 	 * Wait for the suspension to finish.
1014 	 */
1015 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1016 		       (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1017 	vfs_rel(mp);
1018 	if (error == 0)
1019 		goto retry;
1020 	return (error);
1021 }
1022 
1023 /*
1024  * Filesystem write operation has completed. If we are suspending and this
1025  * operation is the last one, notify the suspender that the suspension is
1026  * now in effect.
1027  */
1028 void
1029 vn_finished_write(mp)
1030 	struct mount *mp;
1031 {
1032 	if (mp == NULL)
1033 		return;
1034 	MNT_ILOCK(mp);
1035 	mp->mnt_writeopcount--;
1036 	if (mp->mnt_writeopcount < 0)
1037 		panic("vn_finished_write: neg cnt");
1038 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1039 	    mp->mnt_writeopcount <= 0)
1040 		wakeup(&mp->mnt_writeopcount);
1041 	MNT_IUNLOCK(mp);
1042 }
1043 
1044 
1045 /*
1046  * Filesystem secondary write operation has completed. If we are
1047  * suspending and this operation is the last one, notify the suspender
1048  * that the suspension is now in effect.
1049  */
1050 void
1051 vn_finished_secondary_write(mp)
1052 	struct mount *mp;
1053 {
1054 	if (mp == NULL)
1055 		return;
1056 	MNT_ILOCK(mp);
1057 	mp->mnt_secondary_writes--;
1058 	if (mp->mnt_secondary_writes < 0)
1059 		panic("vn_finished_secondary_write: neg cnt");
1060 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1061 	    mp->mnt_secondary_writes <= 0)
1062 		wakeup(&mp->mnt_secondary_writes);
1063 	MNT_IUNLOCK(mp);
1064 }
1065 
1066 
1067 
1068 /*
1069  * Request a filesystem to suspend write operations.
1070  */
1071 int
1072 vfs_write_suspend(mp)
1073 	struct mount *mp;
1074 {
1075 	struct thread *td = curthread;
1076 	int error;
1077 
1078 	error = 0;
1079 	MNT_ILOCK(mp);
1080 	if (mp->mnt_kern_flag & MNTK_SUSPEND)
1081 		goto unlock;
1082 	mp->mnt_kern_flag |= MNTK_SUSPEND;
1083 	if (mp->mnt_writeopcount > 0)
1084 		(void) msleep(&mp->mnt_writeopcount,
1085 		    MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1086 	else
1087 		MNT_IUNLOCK(mp);
1088 	if ((error = VFS_SYNC(mp, MNT_SUSPEND, td)) != 0) {
1089 		vfs_write_resume(mp);
1090 		return (error);
1091 	}
1092 	MNT_ILOCK(mp);
1093 unlock:
1094 	MNT_IUNLOCK(mp);
1095 	return (error);
1096 }
1097 
1098 /*
1099  * Request a filesystem to resume write operations.
1100  */
1101 void
1102 vfs_write_resume(mp)
1103 	struct mount *mp;
1104 {
1105 
1106 	MNT_ILOCK(mp);
1107 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1108 		mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1109 				       MNTK_SUSPENDED);
1110 		wakeup(&mp->mnt_writeopcount);
1111 		wakeup(&mp->mnt_flag);
1112 	}
1113 	MNT_IUNLOCK(mp);
1114 }
1115 
1116 /*
1117  * Implement kqueues for files by translating it to vnode operation.
1118  */
1119 static int
1120 vn_kqfilter(struct file *fp, struct knote *kn)
1121 {
1122 	int vfslocked;
1123 	int error;
1124 
1125 	vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1126 	error = VOP_KQFILTER(fp->f_vnode, kn);
1127 	VFS_UNLOCK_GIANT(vfslocked);
1128 
1129 	return error;
1130 }
1131 
1132 /*
1133  * Simplified in-kernel wrapper calls for extended attribute access.
1134  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1135  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1136  */
1137 int
1138 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1139     const char *attrname, int *buflen, char *buf, struct thread *td)
1140 {
1141 	struct uio	auio;
1142 	struct iovec	iov;
1143 	int	error;
1144 
1145 	iov.iov_len = *buflen;
1146 	iov.iov_base = buf;
1147 
1148 	auio.uio_iov = &iov;
1149 	auio.uio_iovcnt = 1;
1150 	auio.uio_rw = UIO_READ;
1151 	auio.uio_segflg = UIO_SYSSPACE;
1152 	auio.uio_td = td;
1153 	auio.uio_offset = 0;
1154 	auio.uio_resid = *buflen;
1155 
1156 	if ((ioflg & IO_NODELOCKED) == 0)
1157 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1158 
1159 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1160 
1161 	/* authorize attribute retrieval as kernel */
1162 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1163 	    td);
1164 
1165 	if ((ioflg & IO_NODELOCKED) == 0)
1166 		VOP_UNLOCK(vp, 0, td);
1167 
1168 	if (error == 0) {
1169 		*buflen = *buflen - auio.uio_resid;
1170 	}
1171 
1172 	return (error);
1173 }
1174 
1175 /*
1176  * XXX failure mode if partially written?
1177  */
1178 int
1179 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1180     const char *attrname, int buflen, char *buf, struct thread *td)
1181 {
1182 	struct uio	auio;
1183 	struct iovec	iov;
1184 	struct mount	*mp;
1185 	int	error;
1186 
1187 	iov.iov_len = buflen;
1188 	iov.iov_base = buf;
1189 
1190 	auio.uio_iov = &iov;
1191 	auio.uio_iovcnt = 1;
1192 	auio.uio_rw = UIO_WRITE;
1193 	auio.uio_segflg = UIO_SYSSPACE;
1194 	auio.uio_td = td;
1195 	auio.uio_offset = 0;
1196 	auio.uio_resid = buflen;
1197 
1198 	if ((ioflg & IO_NODELOCKED) == 0) {
1199 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1200 			return (error);
1201 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1202 	}
1203 
1204 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1205 
1206 	/* authorize attribute setting as kernel */
1207 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1208 
1209 	if ((ioflg & IO_NODELOCKED) == 0) {
1210 		vn_finished_write(mp);
1211 		VOP_UNLOCK(vp, 0, td);
1212 	}
1213 
1214 	return (error);
1215 }
1216 
1217 int
1218 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1219     const char *attrname, struct thread *td)
1220 {
1221 	struct mount	*mp;
1222 	int	error;
1223 
1224 	if ((ioflg & IO_NODELOCKED) == 0) {
1225 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1226 			return (error);
1227 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1228 	}
1229 
1230 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1231 
1232 	/* authorize attribute removal as kernel */
1233 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1234 	if (error == EOPNOTSUPP)
1235 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1236 		    NULL, td);
1237 
1238 	if ((ioflg & IO_NODELOCKED) == 0) {
1239 		vn_finished_write(mp);
1240 		VOP_UNLOCK(vp, 0, td);
1241 	}
1242 
1243 	return (error);
1244 }
1245