xref: /csrg-svn/sys/ufs/lfs/lfs_bio.c (revision 56052)
151184Sbostic /*
251493Sbostic  * Copyright (c) 1991 Regents of the University of California.
351184Sbostic  * All rights reserved.
451184Sbostic  *
551184Sbostic  * %sccs.include.redist.c%
651184Sbostic  *
7*56052Sbostic  *	@(#)lfs_bio.c	7.16 (Berkeley) 08/26/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  */
31*56052Sbostic int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
3252081Sbostic int	locked_queue_count;		/* XXX Count of locked-down buffers. */
3355935Sbostic int	lfs_writing;			/* Set if already kicked off a writer
3455935Sbostic 					   because of buffer space */
3555935Sbostic #define WRITE_THRESHHOLD	((nbuf >> 2) - 10)
3655935Sbostic #define WAIT_THRESHHOLD		((nbuf >> 1) - 10)
3752081Sbostic 
3851480Sbostic int
3954621Smckusick lfs_bwrite(ap)
4054621Smckusick 	struct vop_bwrite_args /* {
4154621Smckusick 		struct buf *a_bp;
4254621Smckusick 	} */ *ap;
4351184Sbostic {
4453867Sheideman 	register struct buf *bp = ap->a_bp;
4555935Sbostic 	struct lfs *fs;
4655935Sbostic 	struct inode *ip;
4752347Sbostic 	int s;
4855807Sbostic 
4951851Sbostic 	/*
5052081Sbostic 	 * Set the delayed write flag and use reassignbuf to move the buffer
5152081Sbostic 	 * from the clean list to the dirty one.
5251851Sbostic 	 *
5352081Sbostic 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
5452081Sbostic 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
5552081Sbostic 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
5652081Sbostic 	 * isn't going to work.
5751851Sbostic 	 */
5853867Sheideman 	if (!(bp->b_flags & B_LOCKED)) {
5955935Sbostic 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
6055935Sbostic 		if (!LFS_FITS(fs, fsbtodb(fs, 1)) && !IS_IFILE(bp)) {
6155935Sbostic 			bp->b_flags |= B_INVAL;
6255935Sbostic 			brelse(bp);
63*56052Sbostic 			wakeup(&lfs_allclean_wakeup);
6455935Sbostic 			return (ENOSPC);
6555935Sbostic 		}
6655935Sbostic 		ip = VTOI((bp)->b_vp);
6755935Sbostic 		if (!(ip->i_flag & IMOD))
6855935Sbostic 			++fs->lfs_uinodes;
6955935Sbostic 		ip->i_flag |= IMOD | ICHG | IUPD;			\
7055935Sbostic 		fs->lfs_avail -= fsbtodb(fs, 1);
7152081Sbostic 		++locked_queue_count;
7253867Sheideman 		bp->b_flags |= B_DELWRI | B_LOCKED;
7355935Sbostic 		bp->b_flags &= ~(B_READ | B_ERROR);
7453143Sstaelin 		s = splbio();
7553867Sheideman 		reassignbuf(bp, bp->b_vp);
7653143Sstaelin 		splx(s);
7753143Sstaelin 	}
7853867Sheideman 	brelse(bp);
7951480Sbostic 	return (0);
8051184Sbostic }
8152081Sbostic 
8252081Sbostic /*
8352081Sbostic  * XXX
8452081Sbostic  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
8552081Sbostic  * many locked down.  Eventually the pageout daemon will simply call LFS
8652325Sbostic  * when pages need to be reclaimed.  Note, we have one static count of locked
8752325Sbostic  * buffers, so we can't have more than a single file system.  To make this
8852325Sbostic  * work for multiple file systems, put the count into the mount structure.
8952081Sbostic  */
9052081Sbostic void
9152081Sbostic lfs_flush()
9252081Sbostic {
9352081Sbostic 	register struct mount *mp;
9452081Sbostic 
9555935Sbostic 	if (lfs_writing)
9652081Sbostic 		return;
9755935Sbostic 	lfs_writing = 1;
9852081Sbostic 	mp = rootfs;
9952081Sbostic 	do {
10055935Sbostic 		/* The lock check below is to avoid races with unmount. */
10152081Sbostic 		if (mp->mnt_stat.f_type == MOUNT_LFS &&
10255935Sbostic 		    (mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_UNMOUNT)) == 0 &&
10355935Sbostic 		    !((((struct ufsmount *)mp->mnt_data))->ufsmount_u.lfs)->lfs_dirops ) {
10452347Sbostic 			/*
10552347Sbostic 			 * We set the queue to 0 here because we are about to
10652347Sbostic 			 * write all the dirty buffers we have.  If more come
10752347Sbostic 			 * in while we're writing the segment, they may not
10852347Sbostic 			 * get written, so we want the count to reflect these
10952347Sbostic 			 * new writes after the segwrite completes.
11052347Sbostic 			 */
11152081Sbostic 			lfs_segwrite(mp, 0);
11255935Sbostic 		}
11355935Sbostic 		mp = mp->mnt_next;
11452081Sbostic 	} while (mp != rootfs);
11555935Sbostic 	lfs_writing = 0;
11652081Sbostic }
11755935Sbostic 
11855935Sbostic int
11955935Sbostic lfs_check(vp, blkno)
12055935Sbostic 	struct vnode *vp;
12155935Sbostic 	daddr_t blkno;
12255935Sbostic {
12355935Sbostic 	extern int lfs_allclean_wakeup;
12455935Sbostic 	int error;
12555935Sbostic 
12655935Sbostic 	if (incore(vp, blkno))
12755935Sbostic 		return (0);
12855935Sbostic 	if (locked_queue_count > WRITE_THRESHHOLD)
12955935Sbostic 		lfs_flush();
13055935Sbostic 	if (locked_queue_count > WAIT_THRESHHOLD)
13155935Sbostic 		error = tsleep(&lfs_allclean_wakeup, PCATCH | PUSER,
13255935Sbostic 		    "buffers", NULL);
13355935Sbostic 	return (error);
13455935Sbostic }
135