xref: /csrg-svn/sys/ufs/lfs/lfs_bio.c (revision 55935)
151184Sbostic /*
251493Sbostic  * Copyright (c) 1991 Regents of the University of California.
351184Sbostic  * All rights reserved.
451184Sbostic  *
551184Sbostic  * %sccs.include.redist.c%
651184Sbostic  *
7*55935Sbostic  *	@(#)lfs_bio.c	7.15 (Berkeley) 08/21/92
851184Sbostic  */
951184Sbostic 
1051480Sbostic #include <sys/param.h>
1151480Sbostic #include <sys/proc.h>
1251480Sbostic #include <sys/buf.h>
1352081Sbostic #include <sys/vnode.h>
1451480Sbostic #include <sys/resourcevar.h>
1552081Sbostic #include <sys/mount.h>
1651184Sbostic 
1752081Sbostic #include <ufs/ufs/quota.h>
1852081Sbostic #include <ufs/ufs/inode.h>
1952081Sbostic #include <ufs/ufs/ufsmount.h>
2052081Sbostic 
2151493Sbostic #include <ufs/lfs/lfs.h>
2251493Sbostic #include <ufs/lfs/lfs_extern.h>
2351480Sbostic 
2452081Sbostic /*
2552081Sbostic  * LFS block write function.
2652081Sbostic  *
2752081Sbostic  * XXX
2852081Sbostic  * No write cost accounting is done.
2952081Sbostic  * This is almost certainly wrong for synchronous operations and NFS.
3052081Sbostic  */
3152081Sbostic int	locked_queue_count;		/* XXX Count of locked-down buffers. */
32*55935Sbostic int	lfs_writing;			/* Set if already kicked off a writer
33*55935Sbostic 					   because of buffer space */
34*55935Sbostic #define WRITE_THRESHHOLD	((nbuf >> 2) - 10)
35*55935Sbostic #define WAIT_THRESHHOLD		((nbuf >> 1) - 10)
3652081Sbostic 
3751480Sbostic int
3854621Smckusick lfs_bwrite(ap)
3954621Smckusick 	struct vop_bwrite_args /* {
4054621Smckusick 		struct buf *a_bp;
4154621Smckusick 	} */ *ap;
4251184Sbostic {
4353867Sheideman 	register struct buf *bp = ap->a_bp;
44*55935Sbostic 	struct lfs *fs;
45*55935Sbostic 	struct inode *ip;
4652347Sbostic 	int s;
4755807Sbostic 
4851851Sbostic 	/*
4952081Sbostic 	 * Set the delayed write flag and use reassignbuf to move the buffer
5052081Sbostic 	 * from the clean list to the dirty one.
5151851Sbostic 	 *
5252081Sbostic 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
5352081Sbostic 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
5452081Sbostic 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
5552081Sbostic 	 * isn't going to work.
5651851Sbostic 	 */
5753867Sheideman 	if (!(bp->b_flags & B_LOCKED)) {
58*55935Sbostic 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
59*55935Sbostic 		if (!LFS_FITS(fs, fsbtodb(fs, 1)) && !IS_IFILE(bp)) {
60*55935Sbostic 			bp->b_flags |= B_INVAL;
61*55935Sbostic 			brelse(bp);
62*55935Sbostic 			return (ENOSPC);
63*55935Sbostic 		}
64*55935Sbostic 		ip = VTOI((bp)->b_vp);
65*55935Sbostic 		if (!(ip->i_flag & IMOD))
66*55935Sbostic 			++fs->lfs_uinodes;
67*55935Sbostic 		ip->i_flag |= IMOD | ICHG | IUPD;			\
68*55935Sbostic 		fs->lfs_avail -= fsbtodb(fs, 1);
6952081Sbostic 		++locked_queue_count;
7053867Sheideman 		bp->b_flags |= B_DELWRI | B_LOCKED;
71*55935Sbostic 		bp->b_flags &= ~(B_READ | B_ERROR);
7253143Sstaelin 		s = splbio();
7353867Sheideman 		reassignbuf(bp, bp->b_vp);
7453143Sstaelin 		splx(s);
7553143Sstaelin 	}
7653867Sheideman 	brelse(bp);
7751480Sbostic 	return (0);
7851184Sbostic }
7952081Sbostic 
8052081Sbostic /*
8152081Sbostic  * XXX
8252081Sbostic  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
8352081Sbostic  * many locked down.  Eventually the pageout daemon will simply call LFS
8452325Sbostic  * when pages need to be reclaimed.  Note, we have one static count of locked
8552325Sbostic  * buffers, so we can't have more than a single file system.  To make this
8652325Sbostic  * work for multiple file systems, put the count into the mount structure.
8752081Sbostic  */
8852081Sbostic void
8952081Sbostic lfs_flush()
9052081Sbostic {
9152081Sbostic 	register struct mount *mp;
9252081Sbostic 
93*55935Sbostic 	if (lfs_writing)
9452081Sbostic 		return;
95*55935Sbostic 	lfs_writing = 1;
9652081Sbostic 	mp = rootfs;
9752081Sbostic 	do {
98*55935Sbostic 		/* The lock check below is to avoid races with unmount. */
9952081Sbostic 		if (mp->mnt_stat.f_type == MOUNT_LFS &&
100*55935Sbostic 		    (mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_UNMOUNT)) == 0 &&
101*55935Sbostic 		    !((((struct ufsmount *)mp->mnt_data))->ufsmount_u.lfs)->lfs_dirops ) {
10252347Sbostic 			/*
10352347Sbostic 			 * We set the queue to 0 here because we are about to
10452347Sbostic 			 * write all the dirty buffers we have.  If more come
10552347Sbostic 			 * in while we're writing the segment, they may not
10652347Sbostic 			 * get written, so we want the count to reflect these
10752347Sbostic 			 * new writes after the segwrite completes.
10852347Sbostic 			 */
10952081Sbostic 			lfs_segwrite(mp, 0);
110*55935Sbostic 		}
111*55935Sbostic 		mp = mp->mnt_next;
11252081Sbostic 	} while (mp != rootfs);
113*55935Sbostic 	lfs_writing = 0;
11452081Sbostic }
115*55935Sbostic 
116*55935Sbostic int
117*55935Sbostic lfs_check(vp, blkno)
118*55935Sbostic 	struct vnode *vp;
119*55935Sbostic 	daddr_t blkno;
120*55935Sbostic {
121*55935Sbostic 	extern int lfs_allclean_wakeup;
122*55935Sbostic 	int error;
123*55935Sbostic 
124*55935Sbostic 	if (incore(vp, blkno))
125*55935Sbostic 		return (0);
126*55935Sbostic 	if (locked_queue_count > WRITE_THRESHHOLD)
127*55935Sbostic 		lfs_flush();
128*55935Sbostic 	if (locked_queue_count > WAIT_THRESHHOLD)
129*55935Sbostic 		error = tsleep(&lfs_allclean_wakeup, PCATCH | PUSER,
130*55935Sbostic 		    "buffers", NULL);
131*55935Sbostic 	return (error);
132*55935Sbostic }
133