xref: /freebsd/sys/kern/vfs_vnops.c (revision 0fddbf874719b9bd50cf66ac26d1140bb3f2be69)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)vfs_vnops.c	8.2 (Berkeley) 1/21/94
39  * $FreeBSD$
40  */
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/proc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/bio.h>
53 #include <sys/buf.h>
54 #include <sys/filio.h>
55 #include <sys/ttycom.h>
56 #include <sys/conf.h>
57 
58 #include <machine/limits.h>
59 
60 static int vn_closefile __P((struct file *fp, struct proc *p));
61 static int vn_ioctl __P((struct file *fp, u_long com, caddr_t data,
62 		struct proc *p));
63 static int vn_read __P((struct file *fp, struct uio *uio,
64 		struct ucred *cred, int flags, struct proc *p));
65 static int vn_poll __P((struct file *fp, int events, struct ucred *cred,
66 		struct proc *p));
67 static int vn_kqfilter __P((struct file *fp, struct knote *kn));
68 static int vn_statfile __P((struct file *fp, struct stat *sb, struct proc *p));
69 static int vn_write __P((struct file *fp, struct uio *uio,
70 		struct ucred *cred, int flags, struct proc *p));
71 
72 struct 	fileops vnops = {
73 	vn_read, vn_write, vn_ioctl, vn_poll, vn_kqfilter,
74 	vn_statfile, vn_closefile
75 };
76 
77 /*
78  * Common code for vnode open operations.
79  * Check permissions, and call the VOP_OPEN or VOP_CREATE routine.
80  *
81  * Note that this does NOT free nameidata for the successful case,
82  * due to the NDINIT being done elsewhere.
83  */
84 int
85 vn_open(ndp, flagp, cmode)
86 	register struct nameidata *ndp;
87 	int *flagp, cmode;
88 {
89 	struct vnode *vp;
90 	struct mount *mp;
91 	struct proc *p = ndp->ni_cnd.cn_proc;
92 	struct ucred *cred = p->p_ucred;
93 	struct vattr vat;
94 	struct vattr *vap = &vat;
95 	int mode, fmode, error;
96 
97 restart:
98 	fmode = *flagp;
99 	if (fmode & O_CREAT) {
100 		ndp->ni_cnd.cn_nameiop = CREATE;
101 		ndp->ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF;
102 		if ((fmode & O_EXCL) == 0 && (fmode & O_NOFOLLOW) == 0)
103 			ndp->ni_cnd.cn_flags |= FOLLOW;
104 		bwillwrite();
105 		if ((error = namei(ndp)) != 0)
106 			return (error);
107 		if (ndp->ni_vp == NULL) {
108 			VATTR_NULL(vap);
109 			vap->va_type = VREG;
110 			vap->va_mode = cmode;
111 			if (fmode & O_EXCL)
112 				vap->va_vaflags |= VA_EXCLUSIVE;
113 			if (vn_start_write(ndp->ni_dvp, &mp, V_NOWAIT) != 0) {
114 				NDFREE(ndp, NDF_ONLY_PNBUF);
115 				vput(ndp->ni_dvp);
116 				if ((error = vn_start_write(NULL, &mp,
117 				    V_XSLEEP | PCATCH)) != 0)
118 					return (error);
119 				goto restart;
120 			}
121 			VOP_LEASE(ndp->ni_dvp, p, cred, LEASE_WRITE);
122 			error = VOP_CREATE(ndp->ni_dvp, &ndp->ni_vp,
123 					   &ndp->ni_cnd, vap);
124 			vput(ndp->ni_dvp);
125 			vn_finished_write(mp);
126 			if (error) {
127 				NDFREE(ndp, NDF_ONLY_PNBUF);
128 				return (error);
129 			}
130 			ASSERT_VOP_UNLOCKED(ndp->ni_dvp, "create");
131 			ASSERT_VOP_LOCKED(ndp->ni_vp, "create");
132 			fmode &= ~O_TRUNC;
133 			vp = ndp->ni_vp;
134 		} else {
135 			if (ndp->ni_dvp == ndp->ni_vp)
136 				vrele(ndp->ni_dvp);
137 			else
138 				vput(ndp->ni_dvp);
139 			ndp->ni_dvp = NULL;
140 			vp = ndp->ni_vp;
141 			if (fmode & O_EXCL) {
142 				error = EEXIST;
143 				goto bad;
144 			}
145 			fmode &= ~O_CREAT;
146 		}
147 	} else {
148 		ndp->ni_cnd.cn_nameiop = LOOKUP;
149 		ndp->ni_cnd.cn_flags =
150 		    ((fmode & O_NOFOLLOW) ? NOFOLLOW : FOLLOW) | LOCKLEAF;
151 		if ((error = namei(ndp)) != 0)
152 			return (error);
153 		vp = ndp->ni_vp;
154 	}
155 	if (vp->v_type == VLNK) {
156 		error = EMLINK;
157 		goto bad;
158 	}
159 	if (vp->v_type == VSOCK) {
160 		error = EOPNOTSUPP;
161 		goto bad;
162 	}
163 	if ((fmode & O_CREAT) == 0) {
164 		mode = 0;
165 		if (fmode & (FWRITE | O_TRUNC)) {
166 			if (vp->v_type == VDIR) {
167 				error = EISDIR;
168 				goto bad;
169 			}
170 			error = vn_writechk(vp);
171 			if (error)
172 				goto bad;
173 			mode |= VWRITE;
174 		}
175 		if (fmode & FREAD)
176 			mode |= VREAD;
177 		if (mode) {
178 		        error = VOP_ACCESS(vp, mode, cred, p);
179 			if (error)
180 				goto bad;
181 		}
182 	}
183 	if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0)
184 		goto bad;
185 	/*
186 	 * Make sure that a VM object is created for VMIO support.
187 	 */
188 	if (vn_canvmio(vp) == TRUE) {
189 		if ((error = vfs_object_create(vp, p, cred)) != 0)
190 			goto bad;
191 	}
192 
193 	if (fmode & FWRITE)
194 		vp->v_writecount++;
195 	*flagp = fmode;
196 	return (0);
197 bad:
198 	NDFREE(ndp, NDF_ONLY_PNBUF);
199 	vput(vp);
200 	*flagp = fmode;
201 	return (error);
202 }
203 
204 /*
205  * Check for write permissions on the specified vnode.
206  * Prototype text segments cannot be written.
207  */
208 int
209 vn_writechk(vp)
210 	register struct vnode *vp;
211 {
212 
213 	/*
214 	 * If there's shared text associated with
215 	 * the vnode, try to free it up once.  If
216 	 * we fail, we can't allow writing.
217 	 */
218 	if (vp->v_flag & VTEXT)
219 		return (ETXTBSY);
220 	return (0);
221 }
222 
223 /*
224  * Vnode close call
225  */
226 int
227 vn_close(vp, flags, cred, p)
228 	register struct vnode *vp;
229 	int flags;
230 	struct ucred *cred;
231 	struct proc *p;
232 {
233 	int error;
234 
235 	if (flags & FWRITE)
236 		vp->v_writecount--;
237 	error = VOP_CLOSE(vp, flags, cred, p);
238 	/*
239 	 * XXX - In certain instances VOP_CLOSE has to do the vrele
240 	 * itself. If the vrele has been done, it will return EAGAIN
241 	 * to indicate that the vrele should not be done again. When
242 	 * this happens, we just return success. The correct thing to
243 	 * do would be to have all VOP_CLOSE instances do the vrele.
244 	 */
245 	if (error == EAGAIN)
246 		return (0);
247 	vrele(vp);
248 	return (error);
249 }
250 
251 static __inline
252 int
253 sequential_heuristic(struct uio *uio, struct file *fp)
254 {
255 	/*
256 	 * Sequential heuristic - detect sequential operation
257 	 */
258 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
259 	    uio->uio_offset == fp->f_nextoff) {
260 		/*
261 		 * XXX we assume that the filesystem block size is
262 		 * the default.  Not true, but still gives us a pretty
263 		 * good indicator of how sequential the read operations
264 		 * are.
265 		 */
266 		fp->f_seqcount += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
267 		if (fp->f_seqcount >= 127)
268 			fp->f_seqcount = 127;
269 		return(fp->f_seqcount << 16);
270 	}
271 
272 	/*
273 	 * Not sequential, quick draw-down of seqcount
274 	 */
275 	if (fp->f_seqcount > 1)
276 		fp->f_seqcount = 1;
277 	else
278 		fp->f_seqcount = 0;
279 	return(0);
280 }
281 
282 /*
283  * Package up an I/O request on a vnode into a uio and do it.
284  */
285 int
286 vn_rdwr(rw, vp, base, len, offset, segflg, ioflg, cred, aresid, p)
287 	enum uio_rw rw;
288 	struct vnode *vp;
289 	caddr_t base;
290 	int len;
291 	off_t offset;
292 	enum uio_seg segflg;
293 	int ioflg;
294 	struct ucred *cred;
295 	int *aresid;
296 	struct proc *p;
297 {
298 	struct uio auio;
299 	struct iovec aiov;
300 	struct mount *mp;
301 	int error;
302 
303 	if ((ioflg & IO_NODELOCKED) == 0) {
304 		mp = NULL;
305 		if (rw == UIO_WRITE &&
306 		    vp->v_type != VCHR &&
307 		    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
308 			return (error);
309 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
310 	}
311 	auio.uio_iov = &aiov;
312 	auio.uio_iovcnt = 1;
313 	aiov.iov_base = base;
314 	aiov.iov_len = len;
315 	auio.uio_resid = len;
316 	auio.uio_offset = offset;
317 	auio.uio_segflg = segflg;
318 	auio.uio_rw = rw;
319 	auio.uio_procp = p;
320 	if (rw == UIO_READ) {
321 		error = VOP_READ(vp, &auio, ioflg, cred);
322 	} else {
323 		error = VOP_WRITE(vp, &auio, ioflg, cred);
324 	}
325 	if (aresid)
326 		*aresid = auio.uio_resid;
327 	else
328 		if (auio.uio_resid && error == 0)
329 			error = EIO;
330 	if ((ioflg & IO_NODELOCKED) == 0) {
331 		vn_finished_write(mp);
332 		VOP_UNLOCK(vp, 0, p);
333 	}
334 	return (error);
335 }
336 
337 /*
338  * File table vnode read routine.
339  */
340 static int
341 vn_read(fp, uio, cred, flags, p)
342 	struct file *fp;
343 	struct uio *uio;
344 	struct ucred *cred;
345 	struct proc *p;
346 	int flags;
347 {
348 	struct vnode *vp;
349 	int error, ioflag;
350 
351 	KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
352 	    uio->uio_procp, p));
353 	vp = (struct vnode *)fp->f_data;
354 	ioflag = 0;
355 	if (fp->f_flag & FNONBLOCK)
356 		ioflag |= IO_NDELAY;
357 	if (fp->f_flag & O_DIRECT)
358 		ioflag |= IO_DIRECT;
359 	VOP_LEASE(vp, p, cred, LEASE_READ);
360 	vn_lock(vp, LK_SHARED | LK_NOPAUSE | LK_RETRY, p);
361 	if ((flags & FOF_OFFSET) == 0)
362 		uio->uio_offset = fp->f_offset;
363 
364 	ioflag |= sequential_heuristic(uio, fp);
365 
366 	error = VOP_READ(vp, uio, ioflag, cred);
367 	if ((flags & FOF_OFFSET) == 0)
368 		fp->f_offset = uio->uio_offset;
369 	fp->f_nextoff = uio->uio_offset;
370 	VOP_UNLOCK(vp, 0, p);
371 	return (error);
372 }
373 
374 /*
375  * File table vnode write routine.
376  */
377 static int
378 vn_write(fp, uio, cred, flags, p)
379 	struct file *fp;
380 	struct uio *uio;
381 	struct ucred *cred;
382 	struct proc *p;
383 	int flags;
384 {
385 	struct vnode *vp;
386 	struct mount *mp;
387 	int error, ioflag;
388 
389 	KASSERT(uio->uio_procp == p, ("uio_procp %p is not p %p",
390 	    uio->uio_procp, p));
391 	vp = (struct vnode *)fp->f_data;
392 	if (vp->v_type == VREG)
393 		bwillwrite();
394 	vp = (struct vnode *)fp->f_data;	/* XXX needed? */
395 	ioflag = IO_UNIT;
396 	if (vp->v_type == VREG && (fp->f_flag & O_APPEND))
397 		ioflag |= IO_APPEND;
398 	if (fp->f_flag & FNONBLOCK)
399 		ioflag |= IO_NDELAY;
400 	if (fp->f_flag & O_DIRECT)
401 		ioflag |= IO_DIRECT;
402 	if ((fp->f_flag & O_FSYNC) ||
403 	    (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS)))
404 		ioflag |= IO_SYNC;
405 	mp = NULL;
406 	if (vp->v_type != VCHR &&
407 	    (error = vn_start_write(vp, &mp, V_WAIT | PCATCH)) != 0)
408 		return (error);
409 	VOP_LEASE(vp, p, cred, LEASE_WRITE);
410 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
411 	if ((flags & FOF_OFFSET) == 0)
412 		uio->uio_offset = fp->f_offset;
413 	ioflag |= sequential_heuristic(uio, fp);
414 	error = VOP_WRITE(vp, uio, ioflag, cred);
415 	if ((flags & FOF_OFFSET) == 0)
416 		fp->f_offset = uio->uio_offset;
417 	fp->f_nextoff = uio->uio_offset;
418 	VOP_UNLOCK(vp, 0, p);
419 	vn_finished_write(mp);
420 	return (error);
421 }
422 
423 /*
424  * File table vnode stat routine.
425  */
426 static int
427 vn_statfile(fp, sb, p)
428 	struct file *fp;
429 	struct stat *sb;
430 	struct proc *p;
431 {
432 	struct vnode *vp = (struct vnode *)fp->f_data;
433 
434 	return vn_stat(vp, sb, p);
435 }
436 
437 int
438 vn_stat(vp, sb, p)
439 	struct vnode *vp;
440 	register struct stat *sb;
441 	struct proc *p;
442 {
443 	struct vattr vattr;
444 	register struct vattr *vap;
445 	int error;
446 	u_short mode;
447 
448 	vap = &vattr;
449 	error = VOP_GETATTR(vp, vap, p->p_ucred, p);
450 	if (error)
451 		return (error);
452 
453 	/*
454 	 * Zero the spare stat fields
455 	 */
456 	sb->st_lspare = 0;
457 	sb->st_qspare[0] = 0;
458 	sb->st_qspare[1] = 0;
459 
460 	/*
461 	 * Copy from vattr table
462 	 */
463 	if (vap->va_fsid != VNOVAL)
464 		sb->st_dev = vap->va_fsid;
465 	else
466 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
467 	sb->st_ino = vap->va_fileid;
468 	mode = vap->va_mode;
469 	switch (vap->va_type) {
470 	case VREG:
471 		mode |= S_IFREG;
472 		break;
473 	case VDIR:
474 		mode |= S_IFDIR;
475 		break;
476 	case VBLK:
477 		mode |= S_IFBLK;
478 		break;
479 	case VCHR:
480 		mode |= S_IFCHR;
481 		break;
482 	case VLNK:
483 		mode |= S_IFLNK;
484 		/* This is a cosmetic change, symlinks do not have a mode. */
485 		if (vp->v_mount->mnt_flag & MNT_NOSYMFOLLOW)
486 			sb->st_mode &= ~ACCESSPERMS;	/* 0000 */
487 		else
488 			sb->st_mode |= ACCESSPERMS;	/* 0777 */
489 		break;
490 	case VSOCK:
491 		mode |= S_IFSOCK;
492 		break;
493 	case VFIFO:
494 		mode |= S_IFIFO;
495 		break;
496 	default:
497 		return (EBADF);
498 	};
499 	sb->st_mode = mode;
500 	sb->st_nlink = vap->va_nlink;
501 	sb->st_uid = vap->va_uid;
502 	sb->st_gid = vap->va_gid;
503 	sb->st_rdev = vap->va_rdev;
504 	if (vap->va_size > OFF_MAX)
505 		return (EOVERFLOW);
506 	sb->st_size = vap->va_size;
507 	sb->st_atimespec = vap->va_atime;
508 	sb->st_mtimespec = vap->va_mtime;
509 	sb->st_ctimespec = vap->va_ctime;
510 
511         /*
512 	 * According to www.opengroup.org, the meaning of st_blksize is
513 	 *   "a filesystem-specific preferred I/O block size for this
514 	 *    object.  In some filesystem types, this may vary from file
515 	 *    to file"
516 	 * Default to zero to catch bogus uses of this field.
517 	 */
518 
519 	if (vap->va_type == VREG) {
520 		sb->st_blksize = vap->va_blocksize;
521 	} else if (vn_isdisk(vp, NULL)) {
522 		sb->st_blksize = vp->v_rdev->si_bsize_best;
523 		if (sb->st_blksize < vp->v_rdev->si_bsize_phys)
524 			sb->st_blksize = vp->v_rdev->si_bsize_phys;
525 		if (sb->st_blksize < BLKDEV_IOSIZE)
526 			sb->st_blksize = BLKDEV_IOSIZE;
527 	} else {
528 		sb->st_blksize = 0;
529 	}
530 
531 	sb->st_flags = vap->va_flags;
532 	if (suser_xxx(p->p_ucred, 0, 0))
533 		sb->st_gen = 0;
534 	else
535 		sb->st_gen = vap->va_gen;
536 
537 #if (S_BLKSIZE == 512)
538 	/* Optimize this case */
539 	sb->st_blocks = vap->va_bytes >> 9;
540 #else
541 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
542 #endif
543 	return (0);
544 }
545 
546 /*
547  * File table vnode ioctl routine.
548  */
549 static int
550 vn_ioctl(fp, com, data, p)
551 	struct file *fp;
552 	u_long com;
553 	caddr_t data;
554 	struct proc *p;
555 {
556 	register struct vnode *vp = ((struct vnode *)fp->f_data);
557 	struct vattr vattr;
558 	int error;
559 
560 	switch (vp->v_type) {
561 
562 	case VREG:
563 	case VDIR:
564 		if (com == FIONREAD) {
565 			error = VOP_GETATTR(vp, &vattr, p->p_ucred, p);
566 			if (error)
567 				return (error);
568 			*(int *)data = vattr.va_size - fp->f_offset;
569 			return (0);
570 		}
571 		if (com == FIONBIO || com == FIOASYNC)	/* XXX */
572 			return (0);			/* XXX */
573 		/* fall into ... */
574 
575 	default:
576 #if 0
577 		return (ENOTTY);
578 #endif
579 	case VFIFO:
580 	case VCHR:
581 	case VBLK:
582 		if (com == FIODTYPE) {
583 			if (vp->v_type != VCHR && vp->v_type != VBLK)
584 				return (ENOTTY);
585 			*(int *)data = devsw(vp->v_rdev)->d_flags & D_TYPEMASK;
586 			return (0);
587 		}
588 		error = VOP_IOCTL(vp, com, data, fp->f_flag, p->p_ucred, p);
589 		if (error == 0 && com == TIOCSCTTY) {
590 
591 			/* Do nothing if reassigning same control tty */
592 			if (p->p_session->s_ttyvp == vp)
593 				return (0);
594 
595 			/* Get rid of reference to old control tty */
596 			if (p->p_session->s_ttyvp)
597 				vrele(p->p_session->s_ttyvp);
598 
599 			p->p_session->s_ttyvp = vp;
600 			VREF(vp);
601 		}
602 		return (error);
603 	}
604 }
605 
606 /*
607  * File table vnode poll routine.
608  */
609 static int
610 vn_poll(fp, events, cred, p)
611 	struct file *fp;
612 	int events;
613 	struct ucred *cred;
614 	struct proc *p;
615 {
616 
617 	return (VOP_POLL(((struct vnode *)fp->f_data), events, cred, p));
618 }
619 
620 /*
621  * Check that the vnode is still valid, and if so
622  * acquire requested lock.
623  */
624 int
625 #ifndef	DEBUG_LOCKS
626 vn_lock(vp, flags, p)
627 #else
628 debug_vn_lock(vp, flags, p, filename, line)
629 #endif
630 	struct vnode *vp;
631 	int flags;
632 	struct proc *p;
633 #ifdef	DEBUG_LOCKS
634 	const char *filename;
635 	int line;
636 #endif
637 {
638 	int error;
639 
640 	do {
641 		if ((flags & LK_INTERLOCK) == 0)
642 			mtx_lock(&vp->v_interlock);
643 		if ((vp->v_flag & VXLOCK) && vp->v_vxproc != curproc) {
644 			vp->v_flag |= VXWANT;
645 			msleep(vp, &vp->v_interlock, PINOD | PDROP,
646 			    "vn_lock", 0);
647 			error = ENOENT;
648 		} else {
649 			if (vp->v_vxproc != NULL)
650 				printf("VXLOCK interlock avoided in vn_lock\n");
651 #ifdef	DEBUG_LOCKS
652 			vp->filename = filename;
653 			vp->line = line;
654 #endif
655 			error = VOP_LOCK(vp,
656 				    flags | LK_NOPAUSE | LK_INTERLOCK, p);
657 			if (error == 0)
658 				return (error);
659 		}
660 		flags &= ~LK_INTERLOCK;
661 	} while (flags & LK_RETRY);
662 	return (error);
663 }
664 
665 /*
666  * File table vnode close routine.
667  */
668 static int
669 vn_closefile(fp, p)
670 	struct file *fp;
671 	struct proc *p;
672 {
673 
674 	fp->f_ops = &badfileops;
675 	return (vn_close(((struct vnode *)fp->f_data), fp->f_flag,
676 		fp->f_cred, p));
677 }
678 
679 /*
680  * Preparing to start a filesystem write operation. If the operation is
681  * permitted, then we bump the count of operations in progress and
682  * proceed. If a suspend request is in progress, we wait until the
683  * suspension is over, and then proceed.
684  */
685 int
686 vn_start_write(vp, mpp, flags)
687 	struct vnode *vp;
688 	struct mount **mpp;
689 	int flags;
690 {
691 	struct mount *mp;
692 	int error;
693 
694 	/*
695 	 * If a vnode is provided, get and return the mount point that
696 	 * to which it will write.
697 	 */
698 	if (vp != NULL) {
699 		if ((error = VOP_GETWRITEMOUNT(vp, mpp)) != 0) {
700 			*mpp = NULL;
701 			if (error != EOPNOTSUPP)
702 				return (error);
703 			return (0);
704 		}
705 	}
706 	if ((mp = *mpp) == NULL)
707 		return (0);
708 	/*
709 	 * Check on status of suspension.
710 	 */
711 	while ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0) {
712 		if (flags & V_NOWAIT)
713 			return (EWOULDBLOCK);
714 		error = tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH),
715 		    "suspfs", 0);
716 		if (error)
717 			return (error);
718 	}
719 	if (flags & V_XSLEEP)
720 		return (0);
721 	mp->mnt_writeopcount++;
722 	return (0);
723 }
724 
725 /*
726  * Secondary suspension. Used by operations such as vop_inactive
727  * routines that are needed by the higher level functions. These
728  * are allowed to proceed until all the higher level functions have
729  * completed (indicated by mnt_writeopcount dropping to zero). At that
730  * time, these operations are halted until the suspension is over.
731  */
732 int
733 vn_write_suspend_wait(vp, mp, flags)
734 	struct vnode *vp;
735 	struct mount *mp;
736 	int flags;
737 {
738 	int error;
739 
740 	if (vp != NULL) {
741 		if ((error = VOP_GETWRITEMOUNT(vp, &mp)) != 0) {
742 			if (error != EOPNOTSUPP)
743 				return (error);
744 			return (0);
745 		}
746 	}
747 	/*
748 	 * If we are not suspended or have not yet reached suspended
749 	 * mode, then let the operation proceed.
750 	 */
751 	if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPENDED) == 0)
752 		return (0);
753 	if (flags & V_NOWAIT)
754 		return (EWOULDBLOCK);
755 	/*
756 	 * Wait for the suspension to finish.
757 	 */
758 	return (tsleep(&mp->mnt_flag, (PUSER - 1) | (flags & PCATCH),
759 	    "suspfs", 0));
760 }
761 
762 /*
763  * Filesystem write operation has completed. If we are suspending and this
764  * operation is the last one, notify the suspender that the suspension is
765  * now in effect.
766  */
767 void
768 vn_finished_write(mp)
769 	struct mount *mp;
770 {
771 
772 	if (mp == NULL)
773 		return;
774 	mp->mnt_writeopcount--;
775 	if (mp->mnt_writeopcount < 0)
776 		panic("vn_finished_write: neg cnt");
777 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) != 0 &&
778 	    mp->mnt_writeopcount <= 0)
779 		wakeup(&mp->mnt_writeopcount);
780 }
781 
782 /*
783  * Request a filesystem to suspend write operations.
784  */
785 void
786 vfs_write_suspend(mp)
787 	struct mount *mp;
788 {
789 	struct proc *p = curproc;
790 
791 	if (mp->mnt_kern_flag & MNTK_SUSPEND)
792 		return;
793 	mp->mnt_kern_flag |= MNTK_SUSPEND;
794 	if (mp->mnt_writeopcount > 0)
795 		(void) tsleep(&mp->mnt_writeopcount, PUSER - 1, "suspwt", 0);
796 	VFS_SYNC(mp, MNT_WAIT, p->p_ucred, p);
797 	mp->mnt_kern_flag |= MNTK_SUSPENDED;
798 }
799 
800 /*
801  * Request a filesystem to resume write operations.
802  */
803 void
804 vfs_write_resume(mp)
805 	struct mount *mp;
806 {
807 
808 	if ((mp->mnt_kern_flag & MNTK_SUSPEND) == 0)
809 		return;
810 	mp->mnt_kern_flag &= ~(MNTK_SUSPEND | MNTK_SUSPENDED);
811 	wakeup(&mp->mnt_writeopcount);
812 	wakeup(&mp->mnt_flag);
813 }
814 
815 static int
816 vn_kqfilter(struct file *fp, struct knote *kn)
817 {
818 
819 	return (VOP_KQFILTER(((struct vnode *)fp->f_data), kn));
820 }
821 
822 /*
823  * Simplified in-kernel wrapper calls for extended attribute access.
824  * Both calls pass in a NULL credential, authorizing as "kernel" access.
825  * Set IO_NODELOCKED in ioflg if the vnode is already locked.
826  */
827 int
828 vn_extattr_get(struct vnode *vp, int ioflg, int attrnamespace,
829     const char *attrname, int *buflen, char *buf, struct proc *p)
830 {
831 	struct uio	auio;
832 	struct iovec	iov;
833 	int	error;
834 
835 	iov.iov_len = *buflen;
836 	iov.iov_base = buf;
837 
838 	auio.uio_iov = &iov;
839 	auio.uio_iovcnt = 1;
840 	auio.uio_rw = UIO_READ;
841 	auio.uio_segflg = UIO_SYSSPACE;
842 	auio.uio_procp = p;
843 	auio.uio_offset = 0;
844 	auio.uio_resid = *buflen;
845 
846 	if ((ioflg & IO_NODELOCKED) == 0)
847 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
848 
849 	/* authorize attribute retrieval as kernel */
850 	error = VOP_GETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p);
851 
852 	if ((ioflg & IO_NODELOCKED) == 0)
853 		VOP_UNLOCK(vp, 0, p);
854 
855 	if (error == 0) {
856 		*buflen = *buflen - auio.uio_resid;
857 	}
858 
859 	return (error);
860 }
861 
862 /*
863  * XXX failure mode if partially written?
864  */
865 int
866 vn_extattr_set(struct vnode *vp, int ioflg, int attrnamespace,
867     const char *attrname, int buflen, char *buf, struct proc *p)
868 {
869 	struct uio	auio;
870 	struct iovec	iov;
871 	struct mount	*mp;
872 	int	error;
873 
874 	iov.iov_len = buflen;
875 	iov.iov_base = buf;
876 
877 	auio.uio_iov = &iov;
878 	auio.uio_iovcnt = 1;
879 	auio.uio_rw = UIO_WRITE;
880 	auio.uio_segflg = UIO_SYSSPACE;
881 	auio.uio_procp = p;
882 	auio.uio_offset = 0;
883 	auio.uio_resid = buflen;
884 
885 	if ((ioflg & IO_NODELOCKED) == 0) {
886 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
887 			return (error);
888 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
889 	}
890 
891 	/* authorize attribute setting as kernel */
892 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, &auio, NULL, p);
893 
894 	if ((ioflg & IO_NODELOCKED) == 0) {
895 		vn_finished_write(mp);
896 		VOP_UNLOCK(vp, 0, p);
897 	}
898 
899 	return (error);
900 }
901 
902 int
903 vn_extattr_rm(struct vnode *vp, int ioflg, int attrnamespace,
904     const char *attrname, struct proc *p)
905 {
906 	struct mount	*mp;
907 	int	error;
908 
909 	if ((ioflg & IO_NODELOCKED) == 0) {
910 		if ((error = vn_start_write(vp, &mp, V_WAIT)) != 0)
911 			return (error);
912 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
913 	}
914 
915 	/* authorize attribute removal as kernel */
916 	error = VOP_SETEXTATTR(vp, attrnamespace, attrname, NULL, NULL, p);
917 
918 	if ((ioflg & IO_NODELOCKED) == 0) {
919 		vn_finished_write(mp);
920 		VOP_UNLOCK(vp, 0, p);
921 	}
922 
923 	return (error);
924 }
925