Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.1
      1 /*
      2  * Copyright (c) 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)lfs_bio.c	8.4 (Berkeley) 12/30/93
     34  *	$Id: lfs_bio.c,v 1.1 1994/06/08 11:42:28 mycroft Exp $
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/proc.h>
     39 #include <sys/buf.h>
     40 #include <sys/vnode.h>
     41 #include <sys/resourcevar.h>
     42 #include <sys/mount.h>
     43 #include <sys/kernel.h>
     44 
     45 #include <ufs/ufs/quota.h>
     46 #include <ufs/ufs/inode.h>
     47 #include <ufs/ufs/ufsmount.h>
     48 
     49 #include <ufs/lfs/lfs.h>
     50 #include <ufs/lfs/lfs_extern.h>
     51 
     52 /*
     53  * LFS block write function.
     54  *
     55  * XXX
     56  * No write cost accounting is done.
     57  * This is almost certainly wrong for synchronous operations and NFS.
     58  */
     59 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
     60 int	locked_queue_count;		/* XXX Count of locked-down buffers. */
     61 int	lfs_writing;			/* Set if already kicked off a writer
     62 					   because of buffer space */
     63 /*
     64 #define WRITE_THRESHHOLD	((nbuf >> 2) - 10)
     65 #define WAIT_THRESHHOLD		((nbuf >> 1) - 10)
     66 */
     67 #define WAIT_THRESHHOLD         (nbuf - (nbuf >> 2) - 10)
     68 #define WRITE_THRESHHOLD        ((nbuf >> 1) - 10)
     69 #define LFS_BUFWAIT	2
     70 
     71 int
     72 lfs_bwrite(ap)
     73 	struct vop_bwrite_args /* {
     74 		struct buf *a_bp;
     75 	} */ *ap;
     76 {
     77 	register struct buf *bp = ap->a_bp;
     78 	struct lfs *fs;
     79 	struct inode *ip;
     80 	int error, s;
     81 
     82 	/*
     83 	 * Set the delayed write flag and use reassignbuf to move the buffer
     84 	 * from the clean list to the dirty one.
     85 	 *
     86 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
     87 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
     88 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
     89 	 * isn't going to work.
     90 	 *
     91 	 * XXX we don't let meta-data writes run out of space because they can
     92 	 * come from the segment writer.  We need to make sure that there is
     93 	 * enough space reserved so that there's room to write meta-data
     94 	 * blocks.
     95 	 */
     96 	if (!(bp->b_flags & B_LOCKED)) {
     97 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
     98 		while (!LFS_FITS(fs, fsbtodb(fs, 1)) && !IS_IFILE(bp) &&
     99 		    bp->b_lblkno > 0) {
    100 			/* Out of space, need cleaner to run */
    101 			wakeup(&lfs_allclean_wakeup);
    102 			if (error = tsleep(&fs->lfs_avail, PCATCH | PUSER,
    103 			    "cleaner", NULL)) {
    104 				brelse(bp);
    105 				return (error);
    106 			}
    107 		}
    108 		ip = VTOI((bp)->b_vp);
    109 		if (!(ip->i_flag & IN_MODIFIED))
    110 			++fs->lfs_uinodes;
    111 		ip->i_flag |= IN_CHANGE | IN_MODIFIED | IN_UPDATE;
    112 		fs->lfs_avail -= fsbtodb(fs, 1);
    113 		++locked_queue_count;
    114 		bp->b_flags |= B_DELWRI | B_LOCKED;
    115 		bp->b_flags &= ~(B_READ | B_ERROR);
    116 		s = splbio();
    117 		reassignbuf(bp, bp->b_vp);
    118 		splx(s);
    119 	}
    120 	brelse(bp);
    121 	return (0);
    122 }
    123 
    124 /*
    125  * XXX
    126  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
    127  * many locked down.  Eventually the pageout daemon will simply call LFS
    128  * when pages need to be reclaimed.  Note, we have one static count of locked
    129  * buffers, so we can't have more than a single file system.  To make this
    130  * work for multiple file systems, put the count into the mount structure.
    131  */
    132 void
    133 lfs_flush()
    134 {
    135 	register struct mount *mp;
    136 
    137 #ifdef DOSTATS
    138 	++lfs_stats.write_exceeded;
    139 #endif
    140 	if (lfs_writing)
    141 		return;
    142 	lfs_writing = 1;
    143 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
    144 		/* The lock check below is to avoid races with unmount. */
    145 		if (!strcmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS) &&
    146 		    (mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_UNMOUNT)) == 0 &&
    147 		    !((((struct ufsmount *)mp->mnt_data))->ufsmount_u.lfs)->lfs_dirops ) {
    148 			/*
    149 			 * We set the queue to 0 here because we are about to
    150 			 * write all the dirty buffers we have.  If more come
    151 			 * in while we're writing the segment, they may not
    152 			 * get written, so we want the count to reflect these
    153 			 * new writes after the segwrite completes.
    154 			 */
    155 #ifdef DOSTATS
    156 			++lfs_stats.flush_invoked;
    157 #endif
    158 			lfs_segwrite(mp, 0);
    159 		}
    160 	}
    161 	lfs_writing = 0;
    162 }
    163 
    164 int
    165 lfs_check(vp, blkno)
    166 	struct vnode *vp;
    167 	daddr_t blkno;
    168 {
    169 	extern int lfs_allclean_wakeup;
    170 	int error;
    171 
    172 	error = 0;
    173 	if (incore(vp, blkno))
    174 		return (0);
    175 	if (locked_queue_count > WRITE_THRESHHOLD)
    176 		lfs_flush();
    177 
    178 	/* If out of buffers, wait on writer */
    179 	while (locked_queue_count > WAIT_THRESHHOLD) {
    180 #ifdef DOSTATS
    181 	    ++lfs_stats.wait_exceeded;
    182 #endif
    183 	    error = tsleep(&locked_queue_count, PCATCH | PUSER, "buffers",
    184 	        hz * LFS_BUFWAIT);
    185 	}
    186 
    187 	return (error);
    188 }
    189