Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.7
      1 /*	$NetBSD: lfs_bio.c,v 1.7 1999/03/10 00:20:00 perseant Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      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 NetBSD
     21  *      Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 /*
     39  * Copyright (c) 1991, 1993
     40  *	The Regents of the University of California.  All rights reserved.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)lfs_bio.c	8.10 (Berkeley) 6/10/95
     71  */
     72 
     73 #include <sys/param.h>
     74 #include <sys/systm.h>
     75 #include <sys/proc.h>
     76 #include <sys/buf.h>
     77 #include <sys/vnode.h>
     78 #include <sys/resourcevar.h>
     79 #include <sys/mount.h>
     80 #include <sys/kernel.h>
     81 
     82 #include <ufs/ufs/quota.h>
     83 #include <ufs/ufs/inode.h>
     84 #include <ufs/ufs/ufsmount.h>
     85 #include <ufs/ufs/ufs_extern.h>
     86 
     87 #include <sys/malloc.h>
     88 #include <ufs/lfs/lfs.h>
     89 #include <ufs/lfs/lfs_extern.h>
     90 
     91 /* Macros to clear/set/test flags. */
     92 # define	SET(t, f)	(t) |= (f)
     93 # define	CLR(t, f)	(t) &= ~(f)
     94 # define	ISSET(t, f)	((t) & (f))
     95 
     96 /*
     97  * LFS block write function.
     98  *
     99  * XXX
    100  * No write cost accounting is done.
    101  * This is almost certainly wrong for synchronous operations and NFS.
    102  */
    103 int	lfs_allclean_wakeup;		/* Cleaner wakeup address. */
    104 int	locked_queue_count   = 0;	/* XXX Count of locked-down buffers. */
    105 long	locked_queue_bytes   = 0L;	/* XXX Total size of locked buffers. */
    106 int	lfs_writing          = 0;	/* Set if already kicked off a writer
    107 					   because of buffer space */
    108 extern int lfs_dostats;
    109 
    110 
    111 /*
    112  * #define WRITE_THRESHHOLD    ((nbuf >> 1) - 10)
    113  * #define WAIT_THRESHHOLD     (nbuf - (nbuf >> 2) - 10)
    114  */
    115 #define LFS_MAX_BUFS        ((nbuf >> 2) - 10)
    116 #define LFS_WAIT_BUFS       ((nbuf >> 1) - (nbuf >> 3) - 10)
    117 /* These are new ... is LFS taking up too much memory in its buffers? */
    118 #define LFS_MAX_BYTES       (((bufpages >> 2) - 10) * CLBYTES)
    119 #define LFS_WAIT_BYTES      (((bufpages >> 1) - (bufpages >> 3) - 10) * CLBYTES)
    120 #define LFS_BUFWAIT         2
    121 /*
    122  *
    123  * XXX we don't let meta-data writes run out of space because they can
    124  * come from the segment writer.  We need to make sure that there is
    125  * enough space reserved so that there's room to write meta-data
    126  * blocks.
    127  *
    128  * Also, we don't let blocks that have come to us from the cleaner
    129  * run out of space.
    130  */
    131 #define CANT_WAIT(BP,F) (IS_IFILE((BP)) || (BP)->b_lblkno<0 || ((F) & BW_CLEAN))
    132 
    133 int
    134 lfs_bwrite(v)
    135 	void *v;
    136 {
    137 	struct vop_bwrite_args /* {
    138 		struct buf *a_bp;
    139 	} */ *ap = v;
    140 	register struct buf *bp = ap->a_bp;
    141 
    142 #ifdef DIAGNOSTIC
    143 	if(bp->b_flags & B_ASYNC)
    144 		panic("bawrite LFS buffer");
    145 #endif /* DIAGNOSTIC */
    146 	return lfs_bwrite_ext(bp,0);
    147 }
    148 
    149 /*
    150  * Determine if there is enough room currently available to write db
    151  * disk blocks.  We need enough blocks for the new blocks, the current
    152  * inode blocks, a summary block, plus potentially the ifile inode and
    153  * the segment usage table, plus an ifile page.
    154  */
    155 inline static int lfs_fits(struct lfs *fs, int db)
    156 {
    157 	if(((db + (fs->lfs_uinodes + INOPB((fs))) /
    158 	     INOPB(fs) + fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE +
    159 	     fs->lfs_segtabsz)) >= fs->lfs_avail)
    160 	{
    161 		return 0;
    162 	}
    163 
    164 	/*
    165 	 * Also check the number of segments available for writing.
    166 	 * If you don't do this here, it is possible for the *cleaner* to
    167 	 * cause us to become starved of segments, by flushing the pending
    168 	 * block list.
    169 	 *
    170 	 * XXX the old lfs_markv did not have this problem.
    171 	 */
    172 	if (fs->lfs_nclean <= MIN_FREE_SEGS)
    173 		return 0;
    174 
    175 	return 1;
    176 }
    177 
    178 int
    179 lfs_bwrite_ext(bp, flags)
    180 	struct buf *bp;
    181 	int flags;
    182 {
    183 	struct lfs *fs;
    184 	struct inode *ip;
    185 	int db, error, s;
    186 
    187 	/*
    188 	 * Set the delayed write flag and use reassignbuf to move the buffer
    189 	 * from the clean list to the dirty one.
    190 	 *
    191 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    192 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    193 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    194 	 * isn't going to work.
    195 	 *
    196 	 * XXX we don't let meta-data writes run out of space because they can
    197 	 * come from the segment writer.  We need to make sure that there is
    198 	 * enough space reserved so that there's room to write meta-data
    199 	 * blocks.
    200 	 */
    201 	if (!(bp->b_flags & B_LOCKED)) {
    202 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
    203 		db = fragstodb(fs, numfrags(fs, bp->b_bcount));
    204 #ifdef DEBUG_LFS
    205 		if(CANT_WAIT(bp,flags)) {
    206 			if(((db + (fs->lfs_uinodes + INOPB((fs))) / INOPB(fs)
    207 			     + fsbtodb(fs, 1)
    208 			     + LFS_SUMMARY_SIZE / DEV_BSIZE
    209 			     + fs->lfs_segtabsz)) >= fs->lfs_avail)
    210 			{
    211 				printf("A");
    212 			}
    213 			if (fs->lfs_nclean <= MIN_FREE_SEGS-1)
    214 				printf("M");
    215 		}
    216 #endif
    217 		while (!lfs_fits(fs, db) && !CANT_WAIT(bp,flags)) {
    218 			/* Out of space, need cleaner to run */
    219 
    220 			wakeup(&lfs_allclean_wakeup);
    221 			wakeup(&fs->lfs_nextseg);
    222 			error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", NULL);
    223 			if (error) {
    224 				/* printf("lfs_bwrite: error in tsleep"); */
    225 				brelse(bp);
    226 				return (error);
    227 			}
    228 		}
    229 
    230 		ip = VTOI(bp->b_vp);
    231 #ifndef LFS_STINGY_CLEAN
    232 		if (!(ip->i_flag & IN_MODIFIED))
    233 			++fs->lfs_uinodes;
    234 		ip->i_flag |= IN_CHANGE | IN_MODIFIED | IN_UPDATE;
    235 #else /* LFS_STINGY_CLEAN */
    236 		if ((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL))
    237 		{
    238 			if(!(ip->i_flag & IN_CLEANING))
    239 				++fs->lfs_uinodes;
    240 			ip->i_flag |= IN_CLEANING;
    241 		} else {
    242 			if(!(ip->i_flag & IN_MODIFIED))
    243 				++fs->lfs_uinodes;
    244 			ip->i_flag |= IN_CHANGE | IN_MODIFIED | IN_UPDATE;
    245 		}
    246 #endif /* LFS_STINGY_CLEAN */
    247 		fs->lfs_avail -= db;
    248 		++locked_queue_count;
    249 		locked_queue_bytes += bp->b_bufsize;
    250 #ifdef LFS_HONOR_RDONLY
    251 		/*
    252 		 * XXX KS - Don't write blocks if we're mounted ro.
    253 		 * Placement here means that the cleaner can't write
    254 		 * blocks either.
    255 		 */
    256 	        if(VTOI(bp->b_vp)->i_lfs->lfs_ronly)
    257 			bp->b_flags &= ~(B_DELWRI|B_LOCKED);
    258 		else
    259 #endif
    260 			bp->b_flags |= B_DELWRI | B_LOCKED;
    261 		bp->b_flags &= ~(B_READ | B_ERROR);
    262 		s = splbio();
    263 		/* XXX - KS - if it's fake and there's another in core, forget it */
    264 		if((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL)
    265 		   && incore(bp->b_vp,bp->b_lblkno))
    266 		{
    267 #ifdef DIAGNOSTIC
    268 			printf("lfs_bwrite_ext: dropping fake buffer %d/%d",VTOI(bp->b_vp)->i_number, bp->b_lblkno);
    269 #endif
    270 			lfs_freebuf(bp);
    271 			splx(s);
    272 			return 0;
    273 		}
    274 		reassignbuf(bp, bp->b_vp);
    275 		splx(s);
    276 	}
    277 
    278 	if((bp->b_flags & (B_CALL|B_INVAL)) == (B_CALL|B_INVAL))
    279 		bp->b_flags &= ~B_BUSY;
    280 	else
    281 		brelse(bp);
    282 
    283 	return (0);
    284 }
    285 
    286 /*
    287  * XXX
    288  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
    289  * many locked down.  Eventually the pageout daemon will simply call LFS
    290  * when pages need to be reclaimed.  Note, we have one static count of locked
    291  * buffers, so we can't have more than a single file system.  To make this
    292  * work for multiple file systems, put the count into the mount structure.
    293  */
    294 void
    295 lfs_flush(fs, flags)
    296 	struct lfs *fs;
    297 	int flags;
    298 {
    299 	register struct mount *mp, *nmp;
    300 	struct lfs *lfsp;
    301 
    302 	if(lfs_dostats)
    303 		++lfs_stats.write_exceeded;
    304 	if (lfs_writing && flags==0) /* XXX flags */
    305 		return;
    306 	lfs_writing = 1;
    307 
    308 	simple_lock(&mountlist_slock);
    309 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    310 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    311 			nmp = mp->mnt_list.cqe_next;
    312 			continue;
    313 		}
    314 		if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN)==0)
    315 		{
    316 			lfsp = ((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs;
    317 			if((mp->mnt_flag & MNT_RDONLY) == 0 &&
    318 			   lfsp->lfs_dirops==0)
    319 			{
    320 				/* disallow dirops during flush */
    321 				lfsp->lfs_writer++;
    322 
    323 				/*
    324 				 * We set the queue to 0 here because we
    325 				 * are about to write all the dirty
    326 				 * buffers we have.  If more come in
    327 				 * while we're writing the segment, they
    328 				 * may not get written, so we want the
    329 				 * count to reflect these new writes
    330 				 * after the segwrite completes.
    331 				 */
    332 				if(lfs_dostats)
    333 					++lfs_stats.flush_invoked;
    334 				lfs_segwrite(mp, flags);
    335 
    336 				/* XXX KS - allow dirops again */
    337 				if(--lfsp->lfs_writer==0)
    338 					wakeup(&lfsp->lfs_dirops);
    339 			}
    340 		}
    341 		simple_lock(&mountlist_slock);
    342 		nmp = mp->mnt_list.cqe_next;
    343 		vfs_unbusy(mp);
    344 	}
    345 	simple_unlock(&mountlist_slock);
    346 
    347 	lfs_countlocked(&locked_queue_count,&locked_queue_bytes);
    348 	wakeup(&locked_queue_count);
    349 
    350 	lfs_writing = 0;
    351 }
    352 
    353 int
    354 lfs_check(vp, blkno, flags)
    355 	struct vnode *vp;
    356 	ufs_daddr_t blkno;
    357 	int flags;
    358 {
    359 	int error;
    360 
    361 	error = 0;
    362 	if (incore(vp, blkno))
    363 		return (0);
    364 
    365 	/* If out of buffers, wait on writer */
    366 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    367 	if (VTOI(vp)->i_number == LFS_IFILE_INUM)
    368 		return 0;
    369 
    370 	if (locked_queue_count > LFS_MAX_BUFS
    371 	    || locked_queue_bytes > LFS_MAX_BYTES)
    372 	{
    373 		lfs_flush(VTOI(vp)->i_lfs, flags);
    374 	}
    375 	while  (locked_queue_count > LFS_WAIT_BUFS
    376 		|| locked_queue_bytes > LFS_WAIT_BYTES)
    377 	{
    378 		if(lfs_dostats)
    379 			++lfs_stats.wait_exceeded;
    380 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    381 			       "buffers", hz * LFS_BUFWAIT);
    382 	}
    383 	return (error);
    384 }
    385 
    386 /*
    387  * Allocate a new buffer header.
    388  */
    389 struct buf *
    390 lfs_newbuf(vp, daddr, size)
    391 	struct vnode *vp;
    392 	ufs_daddr_t daddr;
    393 	size_t size;
    394 {
    395 	struct buf *bp;
    396 	size_t nbytes;
    397 	int s;
    398 
    399 	nbytes = roundup(size, DEV_BSIZE);
    400 
    401 	bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
    402 	bzero(bp, sizeof(struct buf));
    403 	if (nbytes)
    404 		bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
    405 	if(nbytes) {
    406 		bzero(bp->b_data, nbytes);
    407 	}
    408 #ifdef DIAGNOSTIC
    409 	if(vp==NULL)
    410 		panic("vp is NULL in lfs_newbuf");
    411 	if(bp==NULL)
    412 		panic("bp is NULL after malloc in lfs_newbuf");
    413 #endif
    414 	s = splbio();
    415 	bgetvp(vp, bp);
    416 	splx(s);
    417 
    418 	bp->b_bufsize = size;
    419 	bp->b_bcount = size;
    420 	bp->b_lblkno = daddr;
    421 	bp->b_blkno = daddr;
    422 	bp->b_error = 0;
    423 	bp->b_resid = 0;
    424 	bp->b_iodone = lfs_callback;
    425 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
    426 
    427 	return (bp);
    428 }
    429 
    430 void
    431 lfs_freebuf(bp)
    432 	struct buf *bp;
    433 {
    434 	int s;
    435 
    436 	s = splbio();
    437 	if(bp->b_vp)
    438 		brelvp(bp);
    439 	splx(s);
    440 	if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
    441 		free(bp->b_data, M_SEGMENT);
    442 		bp->b_data = NULL;
    443 	}
    444 	free(bp, M_SEGMENT);
    445 }
    446 
    447 /*
    448  * Definitions for the buffer free lists.
    449  */
    450 #define BQUEUES		4		/* number of free buffer queues */
    451 
    452 #define BQ_LOCKED	0		/* super-blocks &c */
    453 #define BQ_LRU		1		/* lru, useful buffers */
    454 #define BQ_AGE		2		/* rubbish */
    455 #define BQ_EMPTY	3		/* buffer headers with no memory */
    456 
    457 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    458 
    459 /*
    460  * Return a count of buffers on the "locked" queue.
    461  */
    462 void
    463 lfs_countlocked(count, bytes)
    464 	int *count;
    465 	long *bytes;
    466 {
    467 	register struct buf *bp;
    468 	register int n = 0;
    469 	register long int size = 0L;
    470 
    471 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
    472 	    bp = bp->b_freelist.tqe_next) {
    473 		n++;
    474 		size += bp->b_bufsize;
    475 	}
    476 	*count = n;
    477 	*bytes = size;
    478 	return;
    479 }
    480