Home | History | Annotate | Line # | Download | only in lfs
lfs_bio.c revision 1.121
      1  1.121  perseant /*	$NetBSD: lfs_bio.c,v 1.121 2012/01/02 22:10:44 perseant Exp $	*/
      2    1.2       cgd 
      3    1.7  perseant /*-
      4  1.108        ad  * Copyright (c) 1999, 2000, 2001, 2002, 2003, 2008 The NetBSD Foundation, Inc.
      5    1.7  perseant  * All rights reserved.
      6    1.7  perseant  *
      7    1.7  perseant  * This code is derived from software contributed to The NetBSD Foundation
      8    1.7  perseant  * by Konrad E. Schroder <perseant (at) hhhh.org>.
      9    1.7  perseant  *
     10    1.7  perseant  * Redistribution and use in source and binary forms, with or without
     11    1.7  perseant  * modification, are permitted provided that the following conditions
     12    1.7  perseant  * are met:
     13    1.7  perseant  * 1. Redistributions of source code must retain the above copyright
     14    1.7  perseant  *    notice, this list of conditions and the following disclaimer.
     15    1.7  perseant  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.7  perseant  *    notice, this list of conditions and the following disclaimer in the
     17    1.7  perseant  *    documentation and/or other materials provided with the distribution.
     18    1.7  perseant  *
     19    1.7  perseant  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20    1.7  perseant  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21    1.7  perseant  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22    1.7  perseant  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23    1.7  perseant  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24    1.7  perseant  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25    1.7  perseant  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26    1.7  perseant  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27    1.7  perseant  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28    1.7  perseant  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29    1.7  perseant  * POSSIBILITY OF SUCH DAMAGE.
     30    1.7  perseant  */
     31    1.1   mycroft /*
     32    1.1   mycroft  * Copyright (c) 1991, 1993
     33    1.1   mycroft  *	The Regents of the University of California.  All rights reserved.
     34    1.1   mycroft  *
     35    1.1   mycroft  * Redistribution and use in source and binary forms, with or without
     36    1.1   mycroft  * modification, are permitted provided that the following conditions
     37    1.1   mycroft  * are met:
     38    1.1   mycroft  * 1. Redistributions of source code must retain the above copyright
     39    1.1   mycroft  *    notice, this list of conditions and the following disclaimer.
     40    1.1   mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     41    1.1   mycroft  *    notice, this list of conditions and the following disclaimer in the
     42    1.1   mycroft  *    documentation and/or other materials provided with the distribution.
     43   1.72       agc  * 3. Neither the name of the University nor the names of its contributors
     44    1.1   mycroft  *    may be used to endorse or promote products derived from this software
     45    1.1   mycroft  *    without specific prior written permission.
     46    1.1   mycroft  *
     47    1.1   mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     48    1.1   mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     49    1.1   mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     50    1.1   mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     51    1.1   mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     52    1.1   mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     53    1.1   mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     54    1.1   mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     55    1.1   mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     56    1.1   mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     57    1.1   mycroft  * SUCH DAMAGE.
     58    1.1   mycroft  *
     59    1.6      fvdl  *	@(#)lfs_bio.c	8.10 (Berkeley) 6/10/95
     60    1.1   mycroft  */
     61   1.39     lukem 
     62   1.39     lukem #include <sys/cdefs.h>
     63  1.121  perseant __KERNEL_RCSID(0, "$NetBSD: lfs_bio.c,v 1.121 2012/01/02 22:10:44 perseant Exp $");
     64    1.1   mycroft 
     65    1.1   mycroft #include <sys/param.h>
     66    1.5  christos #include <sys/systm.h>
     67    1.1   mycroft #include <sys/proc.h>
     68    1.1   mycroft #include <sys/buf.h>
     69    1.1   mycroft #include <sys/vnode.h>
     70    1.1   mycroft #include <sys/resourcevar.h>
     71    1.1   mycroft #include <sys/mount.h>
     72    1.1   mycroft #include <sys/kernel.h>
     73   1.93      elad #include <sys/kauth.h>
     74    1.1   mycroft 
     75    1.1   mycroft #include <ufs/ufs/inode.h>
     76    1.1   mycroft #include <ufs/ufs/ufsmount.h>
     77    1.5  christos #include <ufs/ufs/ufs_extern.h>
     78    1.1   mycroft 
     79    1.1   mycroft #include <ufs/lfs/lfs.h>
     80    1.1   mycroft #include <ufs/lfs/lfs_extern.h>
     81    1.1   mycroft 
     82   1.58  perseant #include <uvm/uvm.h>
     83   1.58  perseant 
     84    1.1   mycroft /*
     85    1.1   mycroft  * LFS block write function.
     86    1.1   mycroft  *
     87    1.1   mycroft  * XXX
     88    1.1   mycroft  * No write cost accounting is done.
     89    1.1   mycroft  * This is almost certainly wrong for synchronous operations and NFS.
     90   1.71      yamt  *
     91  1.107        ad  * protected by lfs_lock.
     92    1.1   mycroft  */
     93   1.58  perseant int	locked_queue_count   = 0;	/* Count of locked-down buffers. */
     94   1.58  perseant long	locked_queue_bytes   = 0L;	/* Total size of locked buffers. */
     95   1.61  perseant int	lfs_subsys_pages     = 0L;	/* Total number LFS-written pages */
     96   1.78  perseant int	lfs_fs_pagetrip	     = 0;	/* # of pages to trip per-fs write */
     97   1.61  perseant int	lfs_writing	     = 0;	/* Set if already kicked off a writer
     98    1.1   mycroft 					   because of buffer space */
     99  1.121  perseant int	locked_queue_waiters = 0;	/* Number of processes waiting on lq */
    100  1.107        ad 
    101  1.107        ad /* Lock and condition variables for above. */
    102  1.107        ad kcondvar_t	locked_queue_cv;
    103  1.107        ad kcondvar_t	lfs_writing_cv;
    104  1.107        ad kmutex_t	lfs_lock;
    105   1.64  perseant 
    106    1.7  perseant extern int lfs_dostats;
    107    1.7  perseant 
    108   1.27  perseant /*
    109   1.51      yamt  * reserved number/bytes of locked buffers
    110   1.51      yamt  */
    111   1.51      yamt int locked_queue_rcount = 0;
    112   1.51      yamt long locked_queue_rbytes = 0L;
    113   1.51      yamt 
    114  1.117   mlelstv static int lfs_fits_buf(struct lfs *, int, int);
    115  1.117   mlelstv static int lfs_reservebuf(struct lfs *, struct vnode *vp, struct vnode *vp2,
    116   1.52      yamt     int, int);
    117  1.117   mlelstv static int lfs_reserveavail(struct lfs *, struct vnode *vp, struct vnode *vp2,
    118  1.117   mlelstv     int);
    119   1.51      yamt 
    120  1.117   mlelstv static int
    121   1.51      yamt lfs_fits_buf(struct lfs *fs, int n, int bytes)
    122   1.51      yamt {
    123   1.71      yamt 	int count_fit, bytes_fit;
    124   1.71      yamt 
    125   1.82  perseant 	ASSERT_NO_SEGLOCK(fs);
    126  1.107        ad 	KASSERT(mutex_owned(&lfs_lock));
    127   1.71      yamt 
    128   1.71      yamt 	count_fit =
    129  1.117   mlelstv 	    (locked_queue_count + locked_queue_rcount + n <= LFS_WAIT_BUFS);
    130   1.71      yamt 	bytes_fit =
    131  1.117   mlelstv 	    (locked_queue_bytes + locked_queue_rbytes + bytes <= LFS_WAIT_BYTES);
    132   1.51      yamt 
    133   1.80  perseant #ifdef DEBUG
    134   1.51      yamt 	if (!count_fit) {
    135   1.80  perseant 		DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit count: %d + %d + %d >= %d\n",
    136   1.80  perseant 		      locked_queue_count, locked_queue_rcount,
    137   1.80  perseant 		      n, LFS_WAIT_BUFS));
    138   1.51      yamt 	}
    139   1.51      yamt 	if (!bytes_fit) {
    140   1.80  perseant 		DLOG((DLOG_AVAIL, "lfs_fits_buf: no fit bytes: %ld + %ld + %d >= %ld\n",
    141   1.80  perseant 		      locked_queue_bytes, locked_queue_rbytes,
    142   1.80  perseant 		      bytes, LFS_WAIT_BYTES));
    143   1.51      yamt 	}
    144   1.80  perseant #endif /* DEBUG */
    145   1.51      yamt 
    146   1.51      yamt 	return (count_fit && bytes_fit);
    147   1.51      yamt }
    148   1.51      yamt 
    149   1.52      yamt /* ARGSUSED */
    150  1.117   mlelstv static int
    151   1.98  christos lfs_reservebuf(struct lfs *fs, struct vnode *vp,
    152   1.98  christos     struct vnode *vp2, int n, int bytes)
    153   1.51      yamt {
    154   1.82  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    155   1.51      yamt 	KASSERT(locked_queue_rcount >= 0);
    156   1.51      yamt 	KASSERT(locked_queue_rbytes >= 0);
    157   1.51      yamt 
    158  1.107        ad 	mutex_enter(&lfs_lock);
    159   1.51      yamt 	while (n > 0 && !lfs_fits_buf(fs, n, bytes)) {
    160   1.51      yamt 		int error;
    161   1.51      yamt 
    162   1.78  perseant 		lfs_flush(fs, 0, 0);
    163   1.51      yamt 
    164  1.121  perseant 		DLOG((DLOG_AVAIL, "lfs_reservebuf: waiting: count=%d, bytes=%ld\n",
    165  1.121  perseant 		      locked_queue_count, locked_queue_bytes));
    166  1.121  perseant 		++locked_queue_waiters;
    167  1.107        ad 		error = cv_timedwait_sig(&locked_queue_cv, &lfs_lock,
    168  1.107        ad 		    hz * LFS_BUFWAIT);
    169  1.121  perseant 		--locked_queue_waiters;
    170   1.71      yamt 		if (error && error != EWOULDBLOCK) {
    171  1.107        ad 			mutex_exit(&lfs_lock);
    172   1.51      yamt 			return error;
    173   1.71      yamt 		}
    174   1.51      yamt 	}
    175   1.51      yamt 
    176   1.51      yamt 	locked_queue_rcount += n;
    177   1.51      yamt 	locked_queue_rbytes += bytes;
    178   1.51      yamt 
    179  1.121  perseant 	if (n < 0 && locked_queue_waiters > 0) {
    180  1.121  perseant 		DLOG((DLOG_AVAIL, "lfs_reservebuf: broadcast: count=%d, bytes=%ld\n",
    181  1.121  perseant 		      locked_queue_count, locked_queue_bytes));
    182  1.117   mlelstv 		cv_broadcast(&locked_queue_cv);
    183  1.121  perseant 	}
    184  1.117   mlelstv 
    185  1.107        ad 	mutex_exit(&lfs_lock);
    186   1.71      yamt 
    187   1.51      yamt 	KASSERT(locked_queue_rcount >= 0);
    188   1.51      yamt 	KASSERT(locked_queue_rbytes >= 0);
    189   1.51      yamt 
    190   1.51      yamt 	return 0;
    191   1.51      yamt }
    192   1.51      yamt 
    193   1.51      yamt /*
    194   1.27  perseant  * Try to reserve some blocks, prior to performing a sensitive operation that
    195   1.27  perseant  * requires the vnode lock to be honored.  If there is not enough space, give
    196   1.27  perseant  * up the vnode lock temporarily and wait for the space to become available.
    197   1.27  perseant  *
    198   1.44  perseant  * Called with vp locked.  (Note nowever that if fsb < 0, vp is ignored.)
    199   1.46      yamt  *
    200   1.47      yamt  * XXX YAMT - it isn't safe to unlock vp here
    201   1.47      yamt  * because the node might be modified while we sleep.
    202   1.47      yamt  * (eg. cached states like i_offset might be stale,
    203   1.47      yamt  *  the vnode might be truncated, etc..)
    204   1.55      yamt  * maybe we should have a way to restart the vnodeop (EVOPRESTART?)
    205   1.55      yamt  * or rearrange vnodeop interface to leave vnode locking to file system
    206   1.55      yamt  * specific code so that each file systems can have their own vnode locking and
    207   1.55      yamt  * vnode re-using strategies.
    208   1.27  perseant  */
    209  1.117   mlelstv static int
    210   1.98  christos lfs_reserveavail(struct lfs *fs, struct vnode *vp,
    211   1.98  christos     struct vnode *vp2, int fsb)
    212   1.27  perseant {
    213   1.27  perseant 	CLEANERINFO *cip;
    214   1.27  perseant 	struct buf *bp;
    215   1.27  perseant 	int error, slept;
    216   1.27  perseant 
    217   1.82  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    218   1.27  perseant 	slept = 0;
    219  1.107        ad 	mutex_enter(&lfs_lock);
    220   1.78  perseant 	while (fsb > 0 && !lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail)) {
    221  1.107        ad 		mutex_exit(&lfs_lock);
    222   1.49      yamt #if 0
    223   1.49      yamt 		/*
    224   1.49      yamt 		 * XXX ideally, we should unlock vnodes here
    225   1.49      yamt 		 * because we might sleep very long time.
    226   1.49      yamt 		 */
    227  1.118   hannken 		VOP_UNLOCK(vp);
    228   1.52      yamt 		if (vp2 != NULL) {
    229  1.118   hannken 			VOP_UNLOCK(vp2);
    230   1.52      yamt 		}
    231   1.50      yamt #else
    232   1.50      yamt 		/*
    233   1.50      yamt 		 * XXX since we'll sleep for cleaner with vnode lock holding,
    234   1.55      yamt 		 * deadlock will occur if cleaner tries to lock the vnode.
    235   1.50      yamt 		 * (eg. lfs_markv -> lfs_fastvget -> getnewvnode -> vclean)
    236   1.50      yamt 		 */
    237   1.49      yamt #endif
    238   1.27  perseant 
    239   1.30  perseant 		if (!slept) {
    240   1.80  perseant 			DLOG((DLOG_AVAIL, "lfs_reserve: waiting for %ld (bfree = %d,"
    241   1.80  perseant 			      " est_bfree = %d)\n",
    242   1.80  perseant 			      fsb + fs->lfs_ravail + fs->lfs_favail,
    243   1.80  perseant 			      fs->lfs_bfree, LFS_EST_BFREE(fs)));
    244   1.80  perseant 		}
    245   1.27  perseant 		++slept;
    246   1.27  perseant 
    247   1.27  perseant 		/* Wake up the cleaner */
    248   1.27  perseant 		LFS_CLEANERINFO(cip, fs, bp);
    249   1.31  perseant 		LFS_SYNC_CLEANERINFO(cip, fs, bp, 0);
    250   1.94  perseant 		lfs_wakeup_cleaner(fs);
    251   1.79     perry 
    252  1.107        ad 		mutex_enter(&lfs_lock);
    253   1.82  perseant 		/* Cleaner might have run while we were reading, check again */
    254   1.82  perseant 		if (lfs_fits(fs, fsb + fs->lfs_ravail + fs->lfs_favail))
    255   1.82  perseant 			break;
    256   1.82  perseant 
    257  1.107        ad 		error = mtsleep(&fs->lfs_avail, PCATCH | PUSER, "lfs_reserve",
    258  1.107        ad 				0, &lfs_lock);
    259   1.49      yamt #if 0
    260   1.27  perseant 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    261   1.52      yamt 		vn_lock(vp2, LK_EXCLUSIVE | LK_RETRY); /* XXX use lockstatus */
    262   1.49      yamt #endif
    263   1.91  perseant 		if (error) {
    264  1.107        ad 			mutex_exit(&lfs_lock);
    265   1.27  perseant 			return error;
    266   1.91  perseant 		}
    267   1.27  perseant 	}
    268   1.80  perseant #ifdef DEBUG
    269   1.80  perseant 	if (slept) {
    270   1.80  perseant 		DLOG((DLOG_AVAIL, "lfs_reserve: woke up\n"));
    271   1.80  perseant 	}
    272   1.44  perseant #endif
    273   1.44  perseant 	fs->lfs_ravail += fsb;
    274  1.107        ad 	mutex_exit(&lfs_lock);
    275   1.51      yamt 
    276   1.27  perseant 	return 0;
    277   1.27  perseant }
    278   1.27  perseant 
    279   1.51      yamt #ifdef DIAGNOSTIC
    280   1.51      yamt int lfs_rescount;
    281   1.51      yamt int lfs_rescountdirop;
    282   1.51      yamt #endif
    283   1.51      yamt 
    284   1.51      yamt int
    285   1.52      yamt lfs_reserve(struct lfs *fs, struct vnode *vp, struct vnode *vp2, int fsb)
    286   1.51      yamt {
    287   1.51      yamt 	int error;
    288   1.51      yamt 	int cantwait;
    289   1.51      yamt 
    290   1.82  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    291   1.82  perseant 	if (vp2) {
    292   1.82  perseant 		/* Make sure we're not in the process of reclaiming vp2 */
    293  1.107        ad 		mutex_enter(&lfs_lock);
    294   1.82  perseant 		while(fs->lfs_flags & LFS_UNDIROP) {
    295  1.107        ad 			mtsleep(&fs->lfs_flags, PRIBIO + 1, "lfsrundirop", 0,
    296  1.107        ad 			    &lfs_lock);
    297   1.82  perseant 		}
    298  1.107        ad 		mutex_exit(&lfs_lock);
    299   1.82  perseant 	}
    300   1.82  perseant 
    301   1.52      yamt 	KASSERT(fsb < 0 || VOP_ISLOCKED(vp));
    302   1.52      yamt 	KASSERT(vp2 == NULL || fsb < 0 || VOP_ISLOCKED(vp2));
    303   1.55      yamt 	KASSERT(vp2 == NULL || !(VTOI(vp2)->i_flag & IN_ADIROP));
    304   1.55      yamt 	KASSERT(vp2 == NULL || vp2 != fs->lfs_unlockvp);
    305   1.52      yamt 
    306   1.54      yamt 	cantwait = (VTOI(vp)->i_flag & IN_ADIROP) || fs->lfs_unlockvp == vp;
    307   1.51      yamt #ifdef DIAGNOSTIC
    308   1.51      yamt 	if (cantwait) {
    309   1.51      yamt 		if (fsb > 0)
    310   1.51      yamt 			lfs_rescountdirop++;
    311   1.51      yamt 		else if (fsb < 0)
    312   1.51      yamt 			lfs_rescountdirop--;
    313   1.51      yamt 		if (lfs_rescountdirop < 0)
    314   1.51      yamt 			panic("lfs_rescountdirop");
    315   1.51      yamt 	}
    316   1.51      yamt 	else {
    317   1.51      yamt 		if (fsb > 0)
    318   1.51      yamt 			lfs_rescount++;
    319   1.51      yamt 		else if (fsb < 0)
    320   1.51      yamt 			lfs_rescount--;
    321   1.51      yamt 		if (lfs_rescount < 0)
    322   1.51      yamt 			panic("lfs_rescount");
    323   1.51      yamt 	}
    324   1.51      yamt #endif
    325   1.51      yamt 	if (cantwait)
    326   1.51      yamt 		return 0;
    327   1.53      yamt 
    328   1.53      yamt 	/*
    329   1.53      yamt 	 * XXX
    330   1.53      yamt 	 * vref vnodes here so that cleaner doesn't try to reuse them.
    331   1.55      yamt 	 * (see XXX comment in lfs_reserveavail)
    332   1.53      yamt 	 */
    333  1.116     pooka 	vhold(vp);
    334   1.53      yamt 	if (vp2 != NULL) {
    335  1.116     pooka 		vhold(vp2);
    336   1.53      yamt 	}
    337   1.51      yamt 
    338   1.52      yamt 	error = lfs_reserveavail(fs, vp, vp2, fsb);
    339   1.51      yamt 	if (error)
    340   1.52      yamt 		goto done;
    341   1.51      yamt 
    342   1.51      yamt 	/*
    343   1.51      yamt 	 * XXX just a guess. should be more precise.
    344   1.51      yamt 	 */
    345  1.117   mlelstv 	error = lfs_reservebuf(fs, vp, vp2, fsb, fsbtob(fs, fsb));
    346   1.51      yamt 	if (error)
    347   1.52      yamt 		lfs_reserveavail(fs, vp, vp2, -fsb);
    348   1.52      yamt 
    349   1.52      yamt done:
    350  1.116     pooka 	holdrele(vp);
    351   1.52      yamt 	if (vp2 != NULL) {
    352  1.116     pooka 		holdrele(vp2);
    353   1.52      yamt 	}
    354   1.51      yamt 
    355   1.51      yamt 	return error;
    356   1.51      yamt }
    357    1.1   mycroft 
    358    1.1   mycroft int
    359   1.36  perseant lfs_bwrite(void *v)
    360    1.5  christos {
    361    1.1   mycroft 	struct vop_bwrite_args /* {
    362  1.120   hannken 		struct vnode *a_vp;
    363    1.1   mycroft 		struct buf *a_bp;
    364    1.5  christos 	} */ *ap = v;
    365   1.17  augustss 	struct buf *bp = ap->a_bp;
    366    1.7  perseant 
    367    1.7  perseant #ifdef DIAGNOSTIC
    368   1.61  perseant 	if (VTOI(bp->b_vp)->i_lfs->lfs_ronly == 0 && (bp->b_flags & B_ASYNC)) {
    369    1.7  perseant 		panic("bawrite LFS buffer");
    370   1.16  perseant 	}
    371    1.7  perseant #endif /* DIAGNOSTIC */
    372   1.85  perseant 	return lfs_bwrite_ext(bp, 0);
    373    1.7  perseant }
    374    1.7  perseant 
    375   1.79     perry /*
    376   1.44  perseant  * Determine if there is enough room currently available to write fsb
    377   1.44  perseant  * blocks.  We need enough blocks for the new blocks, the current
    378   1.44  perseant  * inode blocks (including potentially the ifile inode), a summary block,
    379   1.44  perseant  * and the segment usage table, plus an ifile block.
    380    1.7  perseant  */
    381   1.32  perseant int
    382   1.36  perseant lfs_fits(struct lfs *fs, int fsb)
    383    1.7  perseant {
    384   1.26  perseant 	int needed;
    385   1.26  perseant 
    386   1.82  perseant 	ASSERT_NO_SEGLOCK(fs);
    387   1.36  perseant 	needed = fsb + btofsb(fs, fs->lfs_sumsize) +
    388   1.44  perseant 		 ((howmany(fs->lfs_uinodes + 1, INOPB(fs)) + fs->lfs_segtabsz +
    389  1.117   mlelstv 		   1) << (fs->lfs_bshift - fs->lfs_ffshift));
    390   1.27  perseant 
    391   1.26  perseant 	if (needed >= fs->lfs_avail) {
    392   1.80  perseant #ifdef DEBUG
    393   1.80  perseant 		DLOG((DLOG_AVAIL, "lfs_fits: no fit: fsb = %ld, uinodes = %ld, "
    394   1.80  perseant 		      "needed = %ld, avail = %ld\n",
    395   1.80  perseant 		      (long)fsb, (long)fs->lfs_uinodes, (long)needed,
    396   1.80  perseant 		      (long)fs->lfs_avail));
    397   1.26  perseant #endif
    398    1.7  perseant 		return 0;
    399    1.7  perseant 	}
    400    1.7  perseant 	return 1;
    401    1.7  perseant }
    402    1.7  perseant 
    403    1.7  perseant int
    404   1.44  perseant lfs_availwait(struct lfs *fs, int fsb)
    405   1.32  perseant {
    406   1.32  perseant 	int error;
    407   1.32  perseant 	CLEANERINFO *cip;
    408   1.32  perseant 	struct buf *cbp;
    409   1.63  perseant 
    410   1.82  perseant 	ASSERT_NO_SEGLOCK(fs);
    411   1.63  perseant 	/* Push cleaner blocks through regardless */
    412  1.107        ad 	mutex_enter(&lfs_lock);
    413   1.82  perseant 	if (LFS_SEGLOCK_HELD(fs) &&
    414   1.64  perseant 	    fs->lfs_sp->seg_flags & (SEGM_CLEAN | SEGM_FORCE_CKP)) {
    415  1.107        ad 		mutex_exit(&lfs_lock);
    416   1.63  perseant 		return 0;
    417   1.64  perseant 	}
    418  1.107        ad 	mutex_exit(&lfs_lock);
    419   1.32  perseant 
    420   1.44  perseant 	while (!lfs_fits(fs, fsb)) {
    421   1.32  perseant 		/*
    422   1.32  perseant 		 * Out of space, need cleaner to run.
    423   1.32  perseant 		 * Update the cleaner info, then wake it up.
    424   1.32  perseant 		 * Note the cleanerinfo block is on the ifile
    425   1.32  perseant 		 * so it CANT_WAIT.
    426   1.32  perseant 		 */
    427   1.32  perseant 		LFS_CLEANERINFO(cip, fs, cbp);
    428   1.32  perseant 		LFS_SYNC_CLEANERINFO(cip, fs, cbp, 0);
    429   1.79     perry 
    430   1.80  perseant #ifdef DEBUG
    431   1.80  perseant 		DLOG((DLOG_AVAIL, "lfs_availwait: out of available space, "
    432   1.80  perseant 		      "waiting on cleaner\n"));
    433   1.78  perseant #endif
    434   1.79     perry 
    435   1.94  perseant 		lfs_wakeup_cleaner(fs);
    436   1.32  perseant #ifdef DIAGNOSTIC
    437   1.82  perseant 		if (LFS_SEGLOCK_HELD(fs))
    438   1.32  perseant 			panic("lfs_availwait: deadlock");
    439   1.32  perseant #endif
    440   1.32  perseant 		error = tsleep(&fs->lfs_avail, PCATCH | PUSER, "cleaner", 0);
    441   1.32  perseant 		if (error)
    442   1.32  perseant 			return (error);
    443   1.32  perseant 	}
    444   1.32  perseant 	return 0;
    445   1.32  perseant }
    446   1.32  perseant 
    447   1.32  perseant int
    448   1.36  perseant lfs_bwrite_ext(struct buf *bp, int flags)
    449    1.7  perseant {
    450    1.1   mycroft 	struct lfs *fs;
    451    1.1   mycroft 	struct inode *ip;
    452  1.107        ad 	struct vnode *vp;
    453  1.107        ad 	int fsb;
    454   1.48      yamt 
    455  1.107        ad 	vp = bp->b_vp;
    456  1.107        ad 	fs = VFSTOUFS(vp->v_mount)->um_lfs;
    457   1.85  perseant 
    458   1.85  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    459  1.107        ad 	KASSERT(bp->b_cflags & BC_BUSY);
    460   1.58  perseant 	KASSERT(flags & BW_CLEAN || !LFS_IS_MALLOC_BUF(bp));
    461  1.109        ad 	KASSERT(((bp->b_oflags | bp->b_flags) & (BO_DELWRI|B_LOCKED))
    462  1.107        ad 	    != BO_DELWRI);
    463   1.48      yamt 
    464    1.1   mycroft 	/*
    465   1.85  perseant 	 * Don't write *any* blocks if we're mounted read-only, or
    466   1.85  perseant 	 * if we are "already unmounted".
    467   1.85  perseant 	 *
    468   1.16  perseant 	 * In particular the cleaner can't write blocks either.
    469   1.16  perseant 	 */
    470   1.85  perseant 	if (fs->lfs_ronly || (fs->lfs_pflags & LFS_PF_CLEAN)) {
    471  1.107        ad 		bp->b_oflags &= ~BO_DELWRI;
    472  1.121  perseant 		bp->b_flags |= B_READ; /* XXX is this right? --ks */
    473  1.103        ad 		bp->b_error = 0;
    474  1.107        ad 		mutex_enter(&bufcache_lock);
    475   1.32  perseant 		LFS_UNLOCK_BUF(bp);
    476   1.58  perseant 		if (LFS_IS_MALLOC_BUF(bp))
    477  1.107        ad 			bp->b_cflags &= ~BC_BUSY;
    478   1.16  perseant 		else
    479  1.107        ad 			brelsel(bp, 0);
    480  1.107        ad 		mutex_exit(&bufcache_lock);
    481   1.85  perseant 		return (fs->lfs_ronly ? EROFS : 0);
    482   1.16  perseant 	}
    483   1.16  perseant 
    484   1.16  perseant 	/*
    485    1.1   mycroft 	 * Set the delayed write flag and use reassignbuf to move the buffer
    486    1.1   mycroft 	 * from the clean list to the dirty one.
    487    1.1   mycroft 	 *
    488  1.109        ad 	 * Set the B_LOCKED flag and unlock the buffer, causing brelse to move
    489    1.1   mycroft 	 * the buffer onto the LOCKED free list.  This is necessary, otherwise
    490    1.1   mycroft 	 * getnewbuf() would try to reclaim the buffers using bawrite, which
    491    1.1   mycroft 	 * isn't going to work.
    492    1.1   mycroft 	 *
    493    1.1   mycroft 	 * XXX we don't let meta-data writes run out of space because they can
    494    1.1   mycroft 	 * come from the segment writer.  We need to make sure that there is
    495    1.1   mycroft 	 * enough space reserved so that there's room to write meta-data
    496    1.1   mycroft 	 * blocks.
    497    1.1   mycroft 	 */
    498  1.109        ad 	if ((bp->b_flags & B_LOCKED) == 0) {
    499  1.117   mlelstv 		fsb = numfrags(fs, bp->b_bcount);
    500   1.79     perry 
    501  1.107        ad 		ip = VTOI(vp);
    502  1.107        ad 		mutex_enter(&lfs_lock);
    503   1.48      yamt 		if (flags & BW_CLEAN) {
    504   1.26  perseant 			LFS_SET_UINO(ip, IN_CLEANING);
    505    1.7  perseant 		} else {
    506   1.36  perseant 			LFS_SET_UINO(ip, IN_MODIFIED);
    507    1.7  perseant 		}
    508  1.107        ad 		mutex_exit(&lfs_lock);
    509   1.36  perseant 		fs->lfs_avail -= fsb;
    510   1.32  perseant 
    511  1.107        ad 		mutex_enter(&bufcache_lock);
    512  1.119     rmind 		mutex_enter(vp->v_interlock);
    513  1.107        ad 		bp->b_oflags = (bp->b_oflags | BO_DELWRI) & ~BO_DONE;
    514   1.32  perseant 		LFS_LOCK_BUF(bp);
    515  1.107        ad 		bp->b_flags &= ~B_READ;
    516  1.103        ad 		bp->b_error = 0;
    517    1.1   mycroft 		reassignbuf(bp, bp->b_vp);
    518  1.119     rmind 		mutex_exit(vp->v_interlock);
    519  1.107        ad 	} else {
    520  1.107        ad 		mutex_enter(&bufcache_lock);
    521    1.1   mycroft 	}
    522   1.79     perry 
    523  1.107        ad 	if (bp->b_iodone != NULL)
    524  1.107        ad 		bp->b_cflags &= ~BC_BUSY;
    525    1.7  perseant 	else
    526  1.107        ad 		brelsel(bp, 0);
    527  1.107        ad 	mutex_exit(&bufcache_lock);
    528   1.79     perry 
    529    1.1   mycroft 	return (0);
    530    1.1   mycroft }
    531    1.1   mycroft 
    532   1.82  perseant /*
    533  1.107        ad  * Called and return with the lfs_lock held.
    534   1.82  perseant  */
    535   1.24  perseant void
    536   1.36  perseant lfs_flush_fs(struct lfs *fs, int flags)
    537   1.11  perseant {
    538   1.82  perseant 	ASSERT_NO_SEGLOCK(fs);
    539  1.107        ad 	KASSERT(mutex_owned(&lfs_lock));
    540   1.58  perseant 	if (fs->lfs_ronly)
    541   1.58  perseant 		return;
    542   1.11  perseant 
    543   1.82  perseant 	if (lfs_dostats)
    544   1.82  perseant 		++lfs_stats.flush_invoked;
    545   1.78  perseant 
    546  1.121  perseant 	fs->lfs_pdflush = 0;
    547  1.107        ad 	mutex_exit(&lfs_lock);
    548   1.67      yamt 	lfs_writer_enter(fs, "fldirop");
    549   1.58  perseant 	lfs_segwrite(fs->lfs_ivnode->v_mount, flags);
    550   1.82  perseant 	lfs_writer_leave(fs);
    551  1.107        ad 	mutex_enter(&lfs_lock);
    552   1.78  perseant 	fs->lfs_favail = 0; /* XXX */
    553   1.11  perseant }
    554   1.11  perseant 
    555    1.1   mycroft /*
    556   1.81  perseant  * This routine initiates segment writes when LFS is consuming too many
    557   1.81  perseant  * resources.  Ideally the pageout daemon would be able to direct LFS
    558   1.81  perseant  * more subtly.
    559   1.81  perseant  * XXX We have one static count of locked buffers;
    560   1.81  perseant  * XXX need to think more about the multiple filesystem case.
    561   1.71      yamt  *
    562  1.107        ad  * Called and return with lfs_lock held.
    563   1.83  perseant  * If fs != NULL, we hold the segment lock for fs.
    564    1.1   mycroft  */
    565    1.1   mycroft void
    566   1.78  perseant lfs_flush(struct lfs *fs, int flags, int only_onefs)
    567    1.1   mycroft {
    568   1.78  perseant 	extern u_int64_t locked_fakequeue_count;
    569   1.17  augustss 	struct mount *mp, *nmp;
    570   1.83  perseant 	struct lfs *tfs;
    571   1.67      yamt 
    572  1.107        ad 	KASSERT(mutex_owned(&lfs_lock));
    573   1.67      yamt 	KDASSERT(fs == NULL || !LFS_SEGLOCK_HELD(fs));
    574   1.79     perry 
    575   1.79     perry 	if (lfs_dostats)
    576    1.7  perseant 		++lfs_stats.write_exceeded;
    577   1.81  perseant 	/* XXX should we include SEGM_CKP here? */
    578   1.84  perseant 	if (lfs_writing && !(flags & SEGM_SYNC)) {
    579   1.80  perseant 		DLOG((DLOG_FLUSH, "lfs_flush: not flushing because another flush is active\n"));
    580    1.1   mycroft 		return;
    581   1.16  perseant 	}
    582   1.81  perseant 	while (lfs_writing)
    583  1.107        ad 		cv_wait(&lfs_writing_cv, &lfs_lock);
    584    1.1   mycroft 	lfs_writing = 1;
    585   1.79     perry 
    586  1.107        ad 	mutex_exit(&lfs_lock);
    587   1.58  perseant 
    588   1.78  perseant 	if (only_onefs) {
    589   1.83  perseant 		KASSERT(fs != NULL);
    590  1.114        ad 		if (vfs_busy(fs->lfs_ivnode->v_mount, NULL))
    591   1.78  perseant 			goto errout;
    592  1.107        ad 		mutex_enter(&lfs_lock);
    593   1.78  perseant 		lfs_flush_fs(fs, flags);
    594  1.107        ad 		mutex_exit(&lfs_lock);
    595  1.113        ad 		vfs_unbusy(fs->lfs_ivnode->v_mount, false, NULL);
    596   1.78  perseant 	} else {
    597   1.78  perseant 		locked_fakequeue_count = 0;
    598  1.105        ad 		mutex_enter(&mountlist_lock);
    599   1.78  perseant 		for (mp = CIRCLEQ_FIRST(&mountlist); mp != (void *)&mountlist;
    600   1.78  perseant 		     mp = nmp) {
    601  1.114        ad 			if (vfs_busy(mp, &nmp)) {
    602   1.80  perseant 				DLOG((DLOG_FLUSH, "lfs_flush: fs vfs_busy\n"));
    603   1.78  perseant 				continue;
    604   1.78  perseant 			}
    605  1.107        ad 			if (strncmp(&mp->mnt_stat.f_fstypename[0], MOUNT_LFS,
    606  1.102  christos 			    sizeof(mp->mnt_stat.f_fstypename)) == 0) {
    607   1.83  perseant 				tfs = VFSTOUFS(mp)->um_lfs;
    608  1.107        ad 				mutex_enter(&lfs_lock);
    609   1.83  perseant 				lfs_flush_fs(tfs, flags);
    610  1.107        ad 				mutex_exit(&lfs_lock);
    611   1.83  perseant 			}
    612  1.113        ad 			vfs_unbusy(mp, false, &nmp);
    613    1.6      fvdl 		}
    614  1.105        ad 		mutex_exit(&mountlist_lock);
    615    1.1   mycroft 	}
    616   1.43  perseant 	LFS_DEBUG_COUNTLOCKED("flush");
    617   1.78  perseant 	wakeup(&lfs_subsys_pages);
    618    1.7  perseant 
    619   1.78  perseant     errout:
    620  1.107        ad 	mutex_enter(&lfs_lock);
    621   1.71      yamt 	KASSERT(lfs_writing);
    622    1.1   mycroft 	lfs_writing = 0;
    623   1.60      yamt 	wakeup(&lfs_writing);
    624    1.1   mycroft }
    625    1.1   mycroft 
    626   1.41  perseant #define INOCOUNT(fs) howmany((fs)->lfs_uinodes, INOPB(fs))
    627   1.65      fvdl #define INOBYTES(fs) ((fs)->lfs_uinodes * sizeof (struct ufs1_dinode))
    628   1.41  perseant 
    629   1.70      yamt /*
    630   1.70      yamt  * make sure that we don't have too many locked buffers.
    631   1.70      yamt  * flush buffers if needed.
    632   1.70      yamt  */
    633    1.1   mycroft int
    634   1.98  christos lfs_check(struct vnode *vp, daddr_t blkno, int flags)
    635    1.1   mycroft {
    636    1.1   mycroft 	int error;
    637   1.10  perseant 	struct lfs *fs;
    638   1.20  perseant 	struct inode *ip;
    639   1.78  perseant 	extern pid_t lfs_writer_daemon;
    640   1.10  perseant 
    641    1.1   mycroft 	error = 0;
    642   1.20  perseant 	ip = VTOI(vp);
    643   1.79     perry 
    644    1.7  perseant 	/* If out of buffers, wait on writer */
    645    1.7  perseant 	/* XXX KS - if it's the Ifile, we're probably the cleaner! */
    646   1.20  perseant 	if (ip->i_number == LFS_IFILE_INUM)
    647   1.20  perseant 		return 0;
    648   1.20  perseant 	/* If we're being called from inside a dirop, don't sleep */
    649   1.20  perseant 	if (ip->i_flag & IN_ADIROP)
    650    1.7  perseant 		return 0;
    651    1.7  perseant 
    652   1.20  perseant 	fs = ip->i_lfs;
    653   1.20  perseant 
    654   1.82  perseant 	ASSERT_NO_SEGLOCK(fs);
    655   1.82  perseant 
    656   1.20  perseant 	/*
    657   1.20  perseant 	 * If we would flush below, but dirops are active, sleep.
    658   1.20  perseant 	 * Note that a dirop cannot ever reach this code!
    659   1.20  perseant 	 */
    660  1.107        ad 	mutex_enter(&lfs_lock);
    661   1.20  perseant 	while (fs->lfs_dirops > 0 &&
    662   1.41  perseant 	       (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    663   1.61  perseant 		locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    664   1.58  perseant 		lfs_subsys_pages > LFS_MAX_PAGES ||
    665   1.92  perseant 		fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    666   1.61  perseant 		lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0))
    667   1.20  perseant 	{
    668   1.20  perseant 		++fs->lfs_diropwait;
    669  1.107        ad 		mtsleep(&fs->lfs_writer, PRIBIO+1, "bufdirop", 0,
    670  1.107        ad 			&lfs_lock);
    671   1.20  perseant 		--fs->lfs_diropwait;
    672   1.20  perseant 	}
    673   1.10  perseant 
    674   1.80  perseant #ifdef DEBUG
    675   1.58  perseant 	if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS)
    676   1.80  perseant 		DLOG((DLOG_FLUSH, "lfs_check: lqc = %d, max %d\n",
    677   1.80  perseant 		      locked_queue_count + INOCOUNT(fs), LFS_MAX_BUFS));
    678   1.58  perseant 	if (locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES)
    679   1.80  perseant 		DLOG((DLOG_FLUSH, "lfs_check: lqb = %ld, max %ld\n",
    680   1.80  perseant 		      locked_queue_bytes + INOBYTES(fs), LFS_MAX_BYTES));
    681   1.58  perseant 	if (lfs_subsys_pages > LFS_MAX_PAGES)
    682   1.82  perseant 		DLOG((DLOG_FLUSH, "lfs_check: lssp = %d, max %d\n",
    683   1.82  perseant 		      lfs_subsys_pages, LFS_MAX_PAGES));
    684   1.78  perseant 	if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip)
    685   1.82  perseant 		DLOG((DLOG_FLUSH, "lfs_check: fssp = %d, trip at %d\n",
    686   1.82  perseant 		      fs->lfs_pages, lfs_fs_pagetrip));
    687   1.58  perseant 	if (lfs_dirvcount > LFS_MAX_DIROP)
    688   1.82  perseant 		DLOG((DLOG_FLUSH, "lfs_check: ldvc = %d, max %d\n",
    689   1.82  perseant 		      lfs_dirvcount, LFS_MAX_DIROP));
    690   1.92  perseant 	if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs))
    691   1.92  perseant 		DLOG((DLOG_FLUSH, "lfs_check: lfdvc = %d, max %d\n",
    692   1.92  perseant 		      fs->lfs_dirvcount, LFS_MAX_FSDIROP(fs)));
    693   1.58  perseant 	if (fs->lfs_diropwait > 0)
    694   1.82  perseant 		DLOG((DLOG_FLUSH, "lfs_check: ldvw = %d\n",
    695   1.82  perseant 		      fs->lfs_diropwait));
    696   1.58  perseant #endif
    697   1.64  perseant 
    698   1.99  perseant 	/* If there are too many pending dirops, we have to flush them. */
    699   1.99  perseant 	if (fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    700   1.99  perseant 	    lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
    701  1.121  perseant 		mutex_exit(&lfs_lock);
    702  1.121  perseant 		lfs_flush_dirops(fs);
    703  1.121  perseant 		mutex_enter(&lfs_lock);
    704  1.121  perseant 	} else if (locked_queue_count + INOCOUNT(fs) > LFS_MAX_BUFS ||
    705   1.41  perseant 	    locked_queue_bytes + INOBYTES(fs) > LFS_MAX_BYTES ||
    706   1.58  perseant 	    lfs_subsys_pages > LFS_MAX_PAGES ||
    707   1.92  perseant 	    fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    708   1.71      yamt 	    lfs_dirvcount > LFS_MAX_DIROP || fs->lfs_diropwait > 0) {
    709   1.78  perseant 		lfs_flush(fs, flags, 0);
    710   1.80  perseant 	} else if (lfs_fs_pagetrip && fs->lfs_pages > lfs_fs_pagetrip) {
    711   1.80  perseant 		/*
    712   1.80  perseant 		 * If we didn't flush the whole thing, some filesystems
    713   1.80  perseant 		 * still might want to be flushed.
    714   1.80  perseant 		 */
    715   1.78  perseant 		++fs->lfs_pdflush;
    716   1.78  perseant 		wakeup(&lfs_writer_daemon);
    717  1.107        ad 	}
    718   1.11  perseant 
    719  1.117   mlelstv 	while (locked_queue_count + INOCOUNT(fs) >= LFS_WAIT_BUFS ||
    720  1.117   mlelstv 		locked_queue_bytes + INOBYTES(fs) >= LFS_WAIT_BYTES ||
    721   1.58  perseant 		lfs_subsys_pages > LFS_WAIT_PAGES ||
    722   1.92  perseant 		fs->lfs_dirvcount > LFS_MAX_FSDIROP(fs) ||
    723   1.71      yamt 		lfs_dirvcount > LFS_MAX_DIROP) {
    724   1.82  perseant 
    725   1.40       chs 		if (lfs_dostats)
    726    1.7  perseant 			++lfs_stats.wait_exceeded;
    727   1.80  perseant 		DLOG((DLOG_AVAIL, "lfs_check: waiting: count=%d, bytes=%ld\n",
    728   1.80  perseant 		      locked_queue_count, locked_queue_bytes));
    729  1.121  perseant 		++locked_queue_waiters;
    730  1.107        ad 		error = cv_timedwait_sig(&locked_queue_cv, &lfs_lock,
    731  1.107        ad 		    hz * LFS_BUFWAIT);
    732  1.121  perseant 		--locked_queue_waiters;
    733   1.82  perseant 		if (error != EWOULDBLOCK)
    734   1.33  perseant 			break;
    735   1.82  perseant 
    736   1.18  perseant 		/*
    737   1.18  perseant 		 * lfs_flush might not flush all the buffers, if some of the
    738   1.33  perseant 		 * inodes were locked or if most of them were Ifile blocks
    739   1.61  perseant 		 * and we weren't asked to checkpoint.	Try flushing again
    740   1.33  perseant 		 * to keep us from blocking indefinitely.
    741   1.18  perseant 		 */
    742  1.117   mlelstv 		if (locked_queue_count + INOCOUNT(fs) >= LFS_MAX_BUFS ||
    743  1.117   mlelstv 		    locked_queue_bytes + INOBYTES(fs) >= LFS_MAX_BYTES) {
    744   1.78  perseant 			lfs_flush(fs, flags | SEGM_CKP, 0);
    745   1.18  perseant 		}
    746    1.7  perseant 	}
    747  1.107        ad 	mutex_exit(&lfs_lock);
    748    1.7  perseant 	return (error);
    749    1.7  perseant }
    750    1.1   mycroft 
    751    1.7  perseant /*
    752    1.7  perseant  * Allocate a new buffer header.
    753    1.7  perseant  */
    754   1.34  perseant struct buf *
    755   1.58  perseant lfs_newbuf(struct lfs *fs, struct vnode *vp, daddr_t daddr, size_t size, int type)
    756    1.7  perseant {
    757    1.7  perseant 	struct buf *bp;
    758    1.7  perseant 	size_t nbytes;
    759   1.79     perry 
    760   1.82  perseant 	ASSERT_MAYBE_SEGLOCK(fs);
    761   1.36  perseant 	nbytes = roundup(size, fsbtob(fs, 1));
    762   1.79     perry 
    763  1.107        ad 	bp = getiobuf(NULL, true);
    764   1.43  perseant 	if (nbytes) {
    765   1.58  perseant 		bp->b_data = lfs_malloc(fs, nbytes, type);
    766   1.58  perseant 		/* memset(bp->b_data, 0, nbytes); */
    767    1.7  perseant 	}
    768   1.79     perry #ifdef DIAGNOSTIC
    769   1.40       chs 	if (vp == NULL)
    770    1.7  perseant 		panic("vp is NULL in lfs_newbuf");
    771   1.40       chs 	if (bp == NULL)
    772    1.7  perseant 		panic("bp is NULL after malloc in lfs_newbuf");
    773    1.1   mycroft #endif
    774   1.57        pk 
    775    1.7  perseant 	bp->b_bufsize = size;
    776    1.7  perseant 	bp->b_bcount = size;
    777    1.7  perseant 	bp->b_lblkno = daddr;
    778    1.7  perseant 	bp->b_blkno = daddr;
    779    1.7  perseant 	bp->b_error = 0;
    780    1.7  perseant 	bp->b_resid = 0;
    781    1.7  perseant 	bp->b_iodone = lfs_callback;
    782  1.107        ad 	bp->b_cflags = BC_BUSY | BC_NOCACHE;
    783   1.76      yamt 	bp->b_private = fs;
    784   1.79     perry 
    785  1.107        ad 	mutex_enter(&bufcache_lock);
    786  1.119     rmind 	mutex_enter(vp->v_interlock);
    787  1.107        ad 	bgetvp(vp, bp);
    788  1.119     rmind 	mutex_exit(vp->v_interlock);
    789  1.107        ad 	mutex_exit(&bufcache_lock);
    790  1.107        ad 
    791    1.7  perseant 	return (bp);
    792    1.7  perseant }
    793    1.7  perseant 
    794    1.7  perseant void
    795   1.58  perseant lfs_freebuf(struct lfs *fs, struct buf *bp)
    796    1.7  perseant {
    797  1.107        ad 	struct vnode *vp;
    798   1.79     perry 
    799  1.107        ad 	if ((vp = bp->b_vp) != NULL) {
    800  1.107        ad 		mutex_enter(&bufcache_lock);
    801  1.119     rmind 		mutex_enter(vp->v_interlock);
    802    1.7  perseant 		brelvp(bp);
    803  1.119     rmind 		mutex_exit(vp->v_interlock);
    804  1.107        ad 		mutex_exit(&bufcache_lock);
    805  1.107        ad 	}
    806  1.107        ad 	if (!(bp->b_cflags & BC_INVAL)) { /* BC_INVAL indicates a "fake" buffer */
    807   1.58  perseant 		lfs_free(fs, bp->b_data, LFS_NB_UNKNOWN);
    808    1.7  perseant 		bp->b_data = NULL;
    809    1.1   mycroft 	}
    810   1.88      yamt 	putiobuf(bp);
    811    1.7  perseant }
    812    1.1   mycroft 
    813    1.7  perseant /*
    814   1.80  perseant  * Count buffers on the "locked" queue, and compare it to a pro-forma count.
    815   1.27  perseant  * Don't count malloced buffers, since they don't detract from the total.
    816    1.7  perseant  */
    817    1.7  perseant void
    818   1.98  christos lfs_countlocked(int *count, long *bytes, const char *msg)
    819    1.7  perseant {
    820   1.17  augustss 	struct buf *bp;
    821   1.17  augustss 	int n = 0;
    822   1.17  augustss 	long int size = 0L;
    823    1.7  perseant 
    824  1.107        ad 	mutex_enter(&bufcache_lock);
    825  1.110      matt 	TAILQ_FOREACH(bp, &bufqueues[BQ_LOCKED].bq_queue, b_freelist) {
    826  1.107        ad 		KASSERT(bp->b_iodone == NULL);
    827    1.7  perseant 		n++;
    828    1.7  perseant 		size += bp->b_bufsize;
    829   1.80  perseant #ifdef DIAGNOSTIC
    830   1.27  perseant 		if (n > nbuf)
    831   1.27  perseant 			panic("lfs_countlocked: this can't happen: more"
    832   1.27  perseant 			      " buffers locked than exist");
    833   1.27  perseant #endif
    834    1.7  perseant 	}
    835   1.80  perseant 	/*
    836   1.80  perseant 	 * Theoretically this function never really does anything.
    837   1.80  perseant 	 * Give a warning if we have to fix the accounting.
    838   1.80  perseant 	 */
    839   1.96  christos 	if (n != *count) {
    840   1.80  perseant 		DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted buf count"
    841   1.80  perseant 		      " from %d to %d\n", msg, *count, n));
    842   1.96  christos 	}
    843   1.96  christos 	if (size != *bytes) {
    844   1.80  perseant 		DLOG((DLOG_LLIST, "lfs_countlocked: %s: adjusted byte count"
    845   1.80  perseant 		      " from %ld to %ld\n", msg, *bytes, size));
    846   1.96  christos 	}
    847    1.7  perseant 	*count = n;
    848    1.7  perseant 	*bytes = size;
    849  1.107        ad 	mutex_exit(&bufcache_lock);
    850    1.7  perseant 	return;
    851    1.1   mycroft }
    852   1.95      yamt 
    853   1.95      yamt int
    854   1.95      yamt lfs_wait_pages(void)
    855   1.95      yamt {
    856   1.95      yamt 	int active, inactive;
    857   1.95      yamt 
    858   1.95      yamt 	uvm_estimatepageable(&active, &inactive);
    859   1.95      yamt 	return LFS_WAIT_RESOURCE(active + inactive + uvmexp.free, 1);
    860   1.95      yamt }
    861   1.95      yamt 
    862   1.95      yamt int
    863   1.95      yamt lfs_max_pages(void)
    864   1.95      yamt {
    865   1.95      yamt 	int active, inactive;
    866   1.95      yamt 
    867   1.95      yamt 	uvm_estimatepageable(&active, &inactive);
    868   1.95      yamt 	return LFS_MAX_RESOURCE(active + inactive + uvmexp.free, 1);
    869   1.95      yamt }
    870