xref: /freebsd/sys/kern/vfs_vnops.c (revision 531c890b8aecbf157fe3491503b5ca62c0b01093)
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 	do {
866 		if ((flags & LK_INTERLOCK) == 0)
867 			VI_LOCK(vp);
868 		if ((flags & LK_NOWAIT || (flags & LK_TYPE_MASK) == 0) &&
869 		    vp->v_iflag & VI_DOOMED) {
870 			VI_UNLOCK(vp);
871 			return (ENOENT);
872 		}
873 		/*
874 		 * Just polling to check validity.
875 		 */
876 		if ((flags & LK_TYPE_MASK) == 0) {
877 			VI_UNLOCK(vp);
878 			return (0);
879 		}
880 		/*
881 		 * lockmgr drops interlock before it will return for
882 		 * any reason.  So force the code above to relock it.
883 		 */
884 		error = VOP_LOCK1(vp, flags | LK_INTERLOCK, file, line);
885 		flags &= ~LK_INTERLOCK;
886 		KASSERT((flags & LK_RETRY) == 0 || error == 0,
887 		    ("LK_RETRY set with incompatible flags %d\n", flags));
888 		/*
889 		 * Callers specify LK_RETRY if they wish to get dead vnodes.
890 		 * If RETRY is not set, we return ENOENT instead.
891 		 */
892 		if (error == 0 && vp->v_iflag & VI_DOOMED &&
893 		    (flags & LK_RETRY) == 0) {
894 			VOP_UNLOCK(vp, 0);
895 			error = ENOENT;
896 			break;
897 		}
898 	} while (flags & LK_RETRY && error != 0);
899 	return (error);
900 }
901 
902 /*
903  * File table vnode close routine.
904  */
905 static int
906 vn_closefile(fp, td)
907 	struct file *fp;
908 	struct thread *td;
909 {
910 	struct vnode *vp;
911 	struct flock lf;
912 	int vfslocked;
913 	int error;
914 
915 	vp = fp->f_vnode;
916 
917 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
918 	if (fp->f_type == DTYPE_VNODE && fp->f_flag & FHASLOCK) {
919 		lf.l_whence = SEEK_SET;
920 		lf.l_start = 0;
921 		lf.l_len = 0;
922 		lf.l_type = F_UNLCK;
923 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
924 	}
925 
926 	fp->f_ops = &badfileops;
927 
928 	error = vn_close(vp, fp->f_flag, fp->f_cred, td);
929 	VFS_UNLOCK_GIANT(vfslocked);
930 	return (error);
931 }
932 
933 /*
934  * Preparing to start a filesystem write operation. If the operation is
935  * permitted, then we bump the count of operations in progress and
936  * proceed. If a suspend request is in progress, we wait until the
937  * suspension is over, and then proceed.
938  */
939 int
940 vn_start_write(vp, mpp, flags)
941 	struct vnode *vp;
942 	struct mount **mpp;
943 	int flags;
944 {
945 	struct mount *mp;
946 	int error;
947 
948 	error = 0;
949 	/*
950 	 * If a vnode is provided, get and return the mount point that
951 	 * to which it will write.
952 	 */
953 	if (vp != NULL) {
954 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
955 			*mpp = NULL;
956 			if (error != EOPNOTSUPP)
957 				return (error);
958 			return (0);
959 		}
960 	}
961 	if ((mp = *mpp) == NULL)
962 		return (0);
963 	MNT_ILOCK(mp);
964 	if (vp == NULL)
965 		MNT_REF(mp);
966 	/*
967 	 * Check on status of suspension.
968 	 */
969 	while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
970 		if (flags & V_NOWAIT) {
971 			error = EWOULDBLOCK;
972 			goto unlock;
973 		}
974 		error = msleep(&mp->mnt_flag, MNT_MTX(mp),
975 		    (PUSER - 1) | (flags & PCATCH), "suspfs", 0);
976 		if (error)
977 			goto unlock;
978 	}
979 	if (flags & V_XSLEEP)
980 		goto unlock;
981 	mp->mnt_writeopcount++;
982 unlock:
983 	MNT_REL(mp);
984 	MNT_IUNLOCK(mp);
985 	return (error);
986 }
987 
988 /*
989  * Secondary suspension. Used by operations such as vop_inactive
990  * routines that are needed by the higher level functions. These
991  * are allowed to proceed until all the higher level functions have
992  * completed (indicated by mnt_writeopcount dropping to zero). At that
993  * time, these operations are halted until the suspension is over.
994  */
995 int
996 vn_write_suspend_wait(vp, mp, flags)
997 	struct vnode *vp;
998 	struct mount *mp;
999 	int flags;
1000 {
1001 	int error;
1002 
1003 	if (vp != NULL) {
1004 		if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
1005 			if (error != EOPNOTSUPP)
1006 				return (error);
1007 			return (0);
1008 		}
1009 	}
1010 	/*
1011 	 * If we are not suspended or have not yet reached suspended
1012 	 * mode, then let the operation proceed.
1013 	 */
1014 	if (mp == NULL)
1015 		return (0);
1016 	MNT_ILOCK(mp);
1017 	if (vp == NULL)
1018 		MNT_REF(mp);
1019 	if ((mp->mnt_kern_flag & MNTK_SUSPENDED) == 0) {
1020 		MNT_REL(mp);
1021 		MNT_IUNLOCK(mp);
1022 		return (0);
1023 	}
1024 	if (flags & V_NOWAIT) {
1025 		MNT_REL(mp);
1026 		MNT_IUNLOCK(mp);
1027 		return (EWOULDBLOCK);
1028 	}
1029 	/*
1030 	 * Wait for the suspension to finish.
1031 	 */
1032 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1033 	    (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1034 	vfs_rel(mp);
1035 	return (error);
1036 }
1037 
1038 /*
1039  * Secondary suspension. Used by operations such as vop_inactive
1040  * routines that are needed by the higher level functions. These
1041  * are allowed to proceed until all the higher level functions have
1042  * completed (indicated by mnt_writeopcount dropping to zero). At that
1043  * time, these operations are halted until the suspension is over.
1044  */
1045 int
1046 vn_start_secondary_write(vp, mpp, flags)
1047 	struct vnode *vp;
1048 	struct mount **mpp;
1049 	int flags;
1050 {
1051 	struct mount *mp;
1052 	int error;
1053 
1054  retry:
1055 	if (vp != NULL) {
1056 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
1057 			*mpp = NULL;
1058 			if (error != EOPNOTSUPP)
1059 				return (error);
1060 			return (0);
1061 		}
1062 	}
1063 	/*
1064 	 * If we are not suspended or have not yet reached suspended
1065 	 * mode, then let the operation proceed.
1066 	 */
1067 	if ((mp = *mpp) == NULL)
1068 		return (0);
1069 	MNT_ILOCK(mp);
1070 	if (vp == NULL)
1071 		MNT_REF(mp);
1072 	if ((mp->mnt_kern_flag & (MNTK_SUSPENDED | MNTK_SUSPEND2)) == 0) {
1073 		mp->mnt_secondary_writes++;
1074 		mp->mnt_secondary_accwrites++;
1075 		MNT_REL(mp);
1076 		MNT_IUNLOCK(mp);
1077 		return (0);
1078 	}
1079 	if (flags & V_NOWAIT) {
1080 		MNT_REL(mp);
1081 		MNT_IUNLOCK(mp);
1082 		return (EWOULDBLOCK);
1083 	}
1084 	/*
1085 	 * Wait for the suspension to finish.
1086 	 */
1087 	error = msleep(&mp->mnt_flag, MNT_MTX(mp),
1088 		       (PUSER - 1) | (flags & PCATCH) | PDROP, "suspfs", 0);
1089 	vfs_rel(mp);
1090 	if (error == 0)
1091 		goto retry;
1092 	return (error);
1093 }
1094 
1095 /*
1096  * Filesystem write operation has completed. If we are suspending and this
1097  * operation is the last one, notify the suspender that the suspension is
1098  * now in effect.
1099  */
1100 void
1101 vn_finished_write(mp)
1102 	struct mount *mp;
1103 {
1104 	if (mp == NULL)
1105 		return;
1106 	MNT_ILOCK(mp);
1107 	mp->mnt_writeopcount--;
1108 	if (mp->mnt_writeopcount < 0)
1109 		panic("vn_finished_write: neg cnt");
1110 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1111 	    mp->mnt_writeopcount <= 0)
1112 		wakeup(&mp->mnt_writeopcount);
1113 	MNT_IUNLOCK(mp);
1114 }
1115 
1116 
1117 /*
1118  * Filesystem secondary write operation has completed. If we are
1119  * suspending and this operation is the last one, notify the suspender
1120  * that the suspension is now in effect.
1121  */
1122 void
1123 vn_finished_secondary_write(mp)
1124 	struct mount *mp;
1125 {
1126 	if (mp == NULL)
1127 		return;
1128 	MNT_ILOCK(mp);
1129 	mp->mnt_secondary_writes--;
1130 	if (mp->mnt_secondary_writes < 0)
1131 		panic("vn_finished_secondary_write: neg cnt");
1132 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
1133 	    mp->mnt_secondary_writes <= 0)
1134 		wakeup(&mp->mnt_secondary_writes);
1135 	MNT_IUNLOCK(mp);
1136 }
1137 
1138 
1139 
1140 /*
1141  * Request a filesystem to suspend write operations.
1142  */
1143 int
1144 vfs_write_suspend(mp)
1145 	struct mount *mp;
1146 {
1147 	struct thread *td = curthread;
1148 	int error;
1149 
1150 	MNT_ILOCK(mp);
1151 	if (mp->mnt_kern_flag & MNTK_SUSPEND) {
1152 		MNT_IUNLOCK(mp);
1153 		return (0);
1154 	}
1155 	mp->mnt_kern_flag |= MNTK_SUSPEND;
1156 	if (mp->mnt_writeopcount > 0)
1157 		(void) msleep(&mp->mnt_writeopcount,
1158 		    MNT_MTX(mp), (PUSER - 1)|PDROP, "suspwt", 0);
1159 	else
1160 		MNT_IUNLOCK(mp);
1161 	if ((error = VFS_SYNC(mp, MNT_SUSPEND, td)) != 0)
1162 		vfs_write_resume(mp);
1163 	return (error);
1164 }
1165 
1166 /*
1167  * Request a filesystem to resume write operations.
1168  */
1169 void
1170 vfs_write_resume(mp)
1171 	struct mount *mp;
1172 {
1173 
1174 	MNT_ILOCK(mp);
1175 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
1176 		mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPEND2 |
1177 				       MNTK_SUSPENDED);
1178 		wakeup(&mp->mnt_writeopcount);
1179 		wakeup(&mp->mnt_flag);
1180 	}
1181 	MNT_IUNLOCK(mp);
1182 }
1183 
1184 /*
1185  * Implement kqueues for files by translating it to vnode operation.
1186  */
1187 static int
1188 vn_kqfilter(struct file *fp, struct knote *kn)
1189 {
1190 	int vfslocked;
1191 	int error;
1192 
1193 	vfslocked = VFS_LOCK_GIANT(fp->f_vnode->v_mount);
1194 	error = VOP_KQFILTER(fp->f_vnode, kn);
1195 	VFS_UNLOCK_GIANT(vfslocked);
1196 
1197 	return error;
1198 }
1199 
1200 /*
1201  * Simplified in-kernel wrapper calls for extended attribute access.
1202  * Both calls pass in a NULL credential, authorizing as "kernel" access.
1203  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
1204  */
1205 int
1206 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
1207     const char *attrname, int *buflen, char *buf, struct thread *td)
1208 {
1209 	struct uio	auio;
1210 	struct iovec	iov;
1211 	int	error;
1212 
1213 	iov.iov_len = *buflen;
1214 	iov.iov_base = buf;
1215 
1216 	auio.uio_iov = &iov;
1217 	auio.uio_iovcnt = 1;
1218 	auio.uio_rw = UIO_READ;
1219 	auio.uio_segflg = UIO_SYSSPACE;
1220 	auio.uio_td = td;
1221 	auio.uio_offset = 0;
1222 	auio.uio_resid = *buflen;
1223 
1224 	if ((ioflg & IO_NODELOCKED) == 0)
1225 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1226 
1227 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1228 
1229 	/* authorize attribute retrieval as kernel */
1230 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, NULL,
1231 	    td);
1232 
1233 	if ((ioflg & IO_NODELOCKED) == 0)
1234 		VOP_UNLOCK(vp, 0);
1235 
1236 	if (error == 0) {
1237 		*buflen = *buflen - auio.uio_resid;
1238 	}
1239 
1240 	return (error);
1241 }
1242 
1243 /*
1244  * XXX failure mode if partially written?
1245  */
1246 int
1247 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
1248     const char *attrname, int buflen, char *buf, struct thread *td)
1249 {
1250 	struct uio	auio;
1251 	struct iovec	iov;
1252 	struct mount	*mp;
1253 	int	error;
1254 
1255 	iov.iov_len = buflen;
1256 	iov.iov_base = buf;
1257 
1258 	auio.uio_iov = &iov;
1259 	auio.uio_iovcnt = 1;
1260 	auio.uio_rw = UIO_WRITE;
1261 	auio.uio_segflg = UIO_SYSSPACE;
1262 	auio.uio_td = td;
1263 	auio.uio_offset = 0;
1264 	auio.uio_resid = buflen;
1265 
1266 	if ((ioflg & IO_NODELOCKED) == 0) {
1267 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1268 			return (error);
1269 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1270 	}
1271 
1272 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1273 
1274 	/* authorize attribute setting as kernel */
1275 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, td);
1276 
1277 	if ((ioflg & IO_NODELOCKED) == 0) {
1278 		vn_finished_write(mp);
1279 		VOP_UNLOCK(vp, 0);
1280 	}
1281 
1282 	return (error);
1283 }
1284 
1285 int
1286 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
1287     const char *attrname, struct thread *td)
1288 {
1289 	struct mount	*mp;
1290 	int	error;
1291 
1292 	if ((ioflg & IO_NODELOCKED) == 0) {
1293 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
1294 			return (error);
1295 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1296 	}
1297 
1298 	ASSERT_VOP_LOCKED(vp, "IO_NODELOCKED with no vp lock held");
1299 
1300 	/* authorize attribute removal as kernel */
1301 	error = VOP_DELETEEXTATTR(vp, attrnamespace, attrname, NULL, td);
1302 	if (error == EOPNOTSUPP)
1303 		error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL,
1304 		    NULL, td);
1305 
1306 	if ((ioflg & IO_NODELOCKED) == 0) {
1307 		vn_finished_write(mp);
1308 		VOP_UNLOCK(vp, 0);
1309 	}
1310 
1311 	return (error);
1312 }
1313