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