Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.24
      1 /*	$NetBSD: lfs_bio.c,v 1.24 2000/06/27 20:57:13 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) * NBPG)
    119 #define LFS_WAIT_BYTES      (((bufpages >> 1) - (bufpages >> 3) - 10) * NBPG)
    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 	struct buf *bp = ap->a_bp;
    141 	struct inode *ip;
    142 
    143 	ip = VTOI(bp->b_vp);
    144 
    145 #ifdef DIAGNOSTIC
    146         if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
    147 		panic("bawrite LFS buffer");
    148 	}
    149 #endif /* DIAGNOSTIC */
    150 	return lfs_bwrite_ext(bp,0);
    151 }
    152 
    153 /*
    154  * Determine if there is enough room currently available to write db
    155  * disk blocks.  We need enough blocks for the new blocks, the current
    156  * inode blocks, a summary block, plus potentially the ifile inode and
    157  * the segment usage table, plus an ifile page.
    158  */
    159 inline static int
    160 lfs_fits(struct lfs *fs, int db)
    161 {
    162 	if(((db + (fs->lfs_uinodes + INOPB((fs))) /
    163 	     INOPB(fs) + fsbtodb(fs, 1) + LFS_SUMMARY_SIZE / DEV_BSIZE +
    164 	     fs->lfs_segtabsz)) >= fs->lfs_avail)
    165 	{
    166 		return 0;
    167 	}
    168 
    169 	/*
    170 	 * Also check the number of segments available for writing.
    171 	 * If you don't do this here, it is possible for the *cleaner* to
    172 	 * cause us to become starved of segments, by flushing the pending
    173 	 * block list.
    174 	 *
    175 	 * XXX the old lfs_markv did not have this problem.
    176 	 */
    177 	if (fs->lfs_nclean <= MIN_FREE_SEGS)
    178 		return 0;
    179 
    180 	return 1;
    181 }
    182 
    183 int
    184 lfs_bwrite_ext(bp, flags)
    185 	struct buf *bp;
    186 	int flags;
    187 {
    188 	struct lfs *fs;
    189 	struct inode *ip;
    190 	int db, error, s;
    191 
    192 	/*
    193 	 * Don't write *any* blocks if we're mounted read-only.
    194 	 * In particular the cleaner can't write blocks either.
    195 	 */
    196         if(VTOI(bp->b_vp)->i_lfs->lfs_ronly) {
    197 		bp->b_flags &= ~(B_DELWRI|B_LOCKED|B_READ|B_ERROR);
    198 		if(bp->b_flags & B_CALL)
    199 			bp->b_flags &= ~B_BUSY;
    200 		else
    201 			brelse(bp);
    202 		return EROFS;
    203 	}
    204 
    205 	/*
    206 	 * Set the delayed write flag and use reassignbuf to move the buffer
    207 	 * from the clean list to the dirty one.
    208 	 *
    209 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    210 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    211 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    212 	 * isn't going to work.
    213 	 *
    214 	 * XXX we don't let meta-data writes run out of space because they can
    215 	 * come from the segment writer.  We need to make sure that there is
    216 	 * enough space reserved so that there's room to write meta-data
    217 	 * blocks.
    218 	 */
    219 	if (!(bp->b_flags & B_LOCKED)) {
    220 		fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
    221 		db = fragstodb(fs, numfrags(fs, bp->b_bcount));
    222 #ifdef DEBUG_LFS
    223 		if(CANT_WAIT(bp,flags)) {
    224 			if(((db + (fs->lfs_uinodes + INOPB((fs))) / INOPB(fs)
    225 			     + fsbtodb(fs, 1)
    226 			     + LFS_SUMMARY_SIZE / DEV_BSIZE
    227 			     + fs->lfs_segtabsz)) >= fs->lfs_avail)
    228 			{
    229 				printf("A");
    230 			}
    231 			if (fs->lfs_nclean <= MIN_FREE_SEGS-1)
    232 				printf("M");
    233 		}
    234 #endif
    235 		while (!lfs_fits(fs, db) && !CANT_WAIT(bp,flags)) {
    236 			/* Out of space, need cleaner to run */
    237 
    238 			wakeup(&lfs_allclean_wakeup);
    239 			wakeup(&fs->lfs_nextseg);
    240 			error = tsleep(&fs->lfs_avail, PCATCH | PUSER,
    241 			    "cleaner", 0);
    242 			if (error) {
    243 				/* printf("lfs_bwrite: error in tsleep"); */
    244 				brelse(bp);
    245 				return (error);
    246 			}
    247 		}
    248 
    249 		ip = VTOI(bp->b_vp);
    250 		if (bp->b_flags & B_CALL) {
    251 			if(!(ip->i_flag & IN_CLEANING))
    252 				++fs->lfs_uinodes;
    253 			ip->i_flag |= IN_CLEANING;
    254 		} else {
    255 			if(!(ip->i_flag & (IN_MODIFIED | IN_ACCESSED)))
    256 				++fs->lfs_uinodes;
    257 			ip->i_flag |= IN_CHANGE | IN_MODIFIED | IN_UPDATE;
    258 		}
    259 		fs->lfs_avail -= db;
    260 		++locked_queue_count;
    261 		locked_queue_bytes += bp->b_bufsize;
    262 		s = splbio();
    263 		bp->b_flags |= B_DELWRI | B_LOCKED;
    264 		bp->b_flags &= ~(B_READ | B_ERROR);
    265 		reassignbuf(bp, bp->b_vp);
    266 		splx(s);
    267 
    268 	}
    269 
    270 	if(bp->b_flags & B_CALL)
    271 		bp->b_flags &= ~B_BUSY;
    272 	else
    273 		brelse(bp);
    274 
    275 	return (0);
    276 }
    277 
    278 void
    279 lfs_flush_fs(fs, flags)
    280 	struct lfs *fs;
    281 	int flags;
    282 {
    283 	if(fs->lfs_ronly == 0 && fs->lfs_dirops == 0)
    284 	{
    285 		/* disallow dirops during flush */
    286 		fs->lfs_writer++;
    287 
    288 		/*
    289 		 * We set the queue to 0 here because we
    290 		 * are about to write all the dirty
    291 		 * buffers we have.  If more come in
    292 		 * while we're writing the segment, they
    293 		 * may not get written, so we want the
    294 		 * count to reflect these new writes
    295 		 * after the segwrite completes.
    296 		 */
    297 		if(lfs_dostats)
    298 			++lfs_stats.flush_invoked;
    299 		lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    300 
    301 		/* XXX KS - allow dirops again */
    302 		if(--fs->lfs_writer==0)
    303 			wakeup(&fs->lfs_dirops);
    304 	}
    305 }
    306 
    307 /*
    308  * XXX
    309  * This routine flushes buffers out of the B_LOCKED queue when LFS has too
    310  * many locked down.  Eventually the pageout daemon will simply call LFS
    311  * when pages need to be reclaimed.  Note, we have one static count of locked
    312  * buffers, so we can't have more than a single file system.  To make this
    313  * work for multiple file systems, put the count into the mount structure.
    314  */
    315 void
    316 lfs_flush(fs, flags)
    317 	struct lfs *fs;
    318 	int flags;
    319 {
    320 	struct mount *mp, *nmp;
    321 
    322 	if(lfs_dostats)
    323 		++lfs_stats.write_exceeded;
    324 	if (lfs_writing && flags==0) {/* XXX flags */
    325 #ifdef DEBUG_LFS
    326 		printf("lfs_flush: not flushing because another flush is active\n");
    327 #endif
    328 		return;
    329 	}
    330 	lfs_writing = 1;
    331 
    332 	simple_lock(&mountlist_slock);
    333 	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
    334 		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock)) {
    335 			nmp = mp->mnt_list.cqe_next;
    336 			continue;
    337 		}
    338 		if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS, MFSNAMELEN)==0)
    339 			lfs_flush_fs(((struct ufsmount *)mp->mnt_data)->ufsmount_u.lfs, flags);
    340 		simple_lock(&mountlist_slock);
    341 		nmp = mp->mnt_list.cqe_next;
    342 		vfs_unbusy(mp);
    343 	}
    344 	simple_unlock(&mountlist_slock);
    345 
    346 	lfs_countlocked(&locked_queue_count,&locked_queue_bytes);
    347 	wakeup(&locked_queue_count);
    348 
    349 	lfs_writing = 0;
    350 }
    351 
    352 int
    353 lfs_check(vp, blkno, flags)
    354 	struct vnode *vp;
    355 	ufs_daddr_t blkno;
    356 	int flags;
    357 {
    358 	int error;
    359 	struct lfs *fs;
    360 	struct inode *ip;
    361 	extern int lfs_dirvcount;
    362 
    363 	error = 0;
    364 	ip = VTOI(vp);
    365 
    366 	/* If out of buffers, wait on writer */
    367 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    368 	if (ip->i_number == LFS_IFILE_INUM)
    369 		return 0;
    370 	/* If we're being called from inside a dirop, don't sleep */
    371 	if (ip->i_flag & IN_ADIROP)
    372 		return 0;
    373 
    374 	fs = ip->i_lfs;
    375 
    376 	/*
    377 	 * If we would flush below, but dirops are active, sleep.
    378 	 * Note that a dirop cannot ever reach this code!
    379 	 */
    380 	while (fs->lfs_dirops > 0 &&
    381 	       (locked_queue_count > LFS_MAX_BUFS ||
    382                 locked_queue_bytes > LFS_MAX_BYTES ||
    383                 lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0))
    384 	{
    385 		++fs->lfs_diropwait;
    386 		tsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0);
    387 		--fs->lfs_diropwait;
    388 	}
    389 
    390 	if (locked_queue_count > LFS_MAX_BUFS ||
    391 	    locked_queue_bytes > LFS_MAX_BYTES ||
    392 	    lfs_dirvcount > LFS_MAXDIROP || fs->lfs_diropwait > 0)
    393 	{
    394 		++fs->lfs_writer;
    395 		lfs_flush(fs, flags);
    396 		if(--fs->lfs_writer==0)
    397 			wakeup(&fs->lfs_dirops);
    398 	}
    399 
    400 	while  (locked_queue_count > LFS_WAIT_BUFS
    401 		|| locked_queue_bytes > LFS_WAIT_BYTES)
    402 	{
    403 		if(lfs_dostats)
    404 			++lfs_stats.wait_exceeded;
    405 #ifdef DEBUG_LFS
    406 		printf("lfs_check: waiting: count=%d, bytes=%ld\n",
    407 			locked_queue_count, locked_queue_bytes);
    408 #endif
    409 		error = tsleep(&locked_queue_count, PCATCH | PUSER,
    410 			       "buffers", hz * LFS_BUFWAIT);
    411 		/*
    412 		 * lfs_flush might not flush all the buffers, if some of the
    413 		 * inodes were locked.  Try flushing again to keep us from
    414 		 * blocking indefinitely.
    415 		 */
    416 		if (locked_queue_count > LFS_MAX_BUFS ||
    417 		    locked_queue_bytes > LFS_MAX_BYTES)
    418 		{
    419 			++fs->lfs_writer;
    420 			lfs_flush(fs, flags);
    421 			if(--fs->lfs_writer==0)
    422 				wakeup(&fs->lfs_dirops);
    423 		}
    424 	}
    425 	return (error);
    426 }
    427 
    428 /*
    429  * Allocate a new buffer header.
    430  */
    431 struct buf *
    432 lfs_newbuf(vp, daddr, size)
    433 	struct vnode *vp;
    434 	ufs_daddr_t daddr;
    435 	size_t size;
    436 {
    437 	struct buf *bp;
    438 	size_t nbytes;
    439 	int s;
    440 
    441 	nbytes = roundup(size, DEV_BSIZE);
    442 
    443 	bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
    444 	bzero(bp, sizeof(struct buf));
    445 	if (nbytes)
    446 		bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
    447 	if(nbytes) {
    448 		bzero(bp->b_data, nbytes);
    449 	}
    450 #ifdef DIAGNOSTIC
    451 	if(vp==NULL)
    452 		panic("vp is NULL in lfs_newbuf");
    453 	if(bp==NULL)
    454 		panic("bp is NULL after malloc in lfs_newbuf");
    455 #endif
    456 	s = splbio();
    457 	bgetvp(vp, bp);
    458 	splx(s);
    459 
    460 	bp->b_bufsize = size;
    461 	bp->b_bcount = size;
    462 	bp->b_lblkno = daddr;
    463 	bp->b_blkno = daddr;
    464 	bp->b_error = 0;
    465 	bp->b_resid = 0;
    466 	bp->b_iodone = lfs_callback;
    467 	bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
    468 
    469 	return (bp);
    470 }
    471 
    472 void
    473 lfs_freebuf(bp)
    474 	struct buf *bp;
    475 {
    476 	int s;
    477 
    478 	s = splbio();
    479 	if(bp->b_vp)
    480 		brelvp(bp);
    481 	splx(s);
    482 	if (!(bp->b_flags & B_INVAL)) { /* B_INVAL indicates a "fake" buffer */
    483 		free(bp->b_data, M_SEGMENT);
    484 		bp->b_data = NULL;
    485 	}
    486 	free(bp, M_SEGMENT);
    487 }
    488 
    489 /*
    490  * Definitions for the buffer free lists.
    491  */
    492 #define BQUEUES		4		/* number of free buffer queues */
    493 
    494 #define BQ_LOCKED	0		/* super-blocks &c */
    495 #define BQ_LRU		1		/* lru, useful buffers */
    496 #define BQ_AGE		2		/* rubbish */
    497 #define BQ_EMPTY	3		/* buffer headers with no memory */
    498 
    499 extern TAILQ_HEAD(bqueues, buf) bufqueues[BQUEUES];
    500 
    501 /*
    502  * Return a count of buffers on the "locked" queue.
    503  */
    504 void
    505 lfs_countlocked(count, bytes)
    506 	int *count;
    507 	long *bytes;
    508 {
    509 	struct buf *bp;
    510 	int n = 0;
    511 	long int size = 0L;
    512 
    513 	for (bp = bufqueues[BQ_LOCKED].tqh_first; bp;
    514 	    bp = bp->b_freelist.tqe_next) {
    515 		n++;
    516 		size += bp->b_bufsize;
    517 	}
    518 	*count = n;
    519 	*bytes = size;
    520 	return;
    521 }
    522