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