vfs_bio.c revision 33255
124143Sjoerg/*
224143Sjoerg * Copyright (c) 1994,1997 John S. Dyson
324143Sjoerg * All rights reserved.
447901Sn_hibma *
524143Sjoerg * Redistribution and use in source and binary forms, with or without
624143Sjoerg * modification, are permitted provided that the following conditions
724143Sjoerg * are met:
824143Sjoerg * 1. Redistributions of source code must retain the above copyright
938278Swosch *    notice immediately at the beginning of the file, without modification,
1038278Swosch *    this list of conditions, and the following disclaimer.
1124143Sjoerg * 2. Absolutely no warranty of function or purpose is made by the author
1224143Sjoerg *		John S. Dyson.
1324143Sjoerg *
1447901Sn_hibma * $Id: vfs_bio.c,v 1.150 1998/02/09 06:09:30 eivind Exp $
1524143Sjoerg */
1624143Sjoerg
1724143Sjoerg/*
1824143Sjoerg * this file contains a new buffer I/O scheme implementing a coherent
1924143Sjoerg * VM object and buffer cache scheme.  Pains have been taken to make
2024143Sjoerg * sure that the performance degradation associated with schemes such
2172951Srwatson * as this is not realized.
2224143Sjoerg *
2350477Speter * Author:  John S. Dyson
2424143Sjoerg * Significant help during the development and debugging phases
2524143Sjoerg * had been provided by David Greenman, also of the FreeBSD core team.
2624143Sjoerg */
2724143Sjoerg
28131621Sdes#include "opt_bounce.h"
29131621Sdes
30131621Sdes#define VMIO
31131621Sdes#include <sys/param.h>
32131621Sdes#include <sys/systm.h>
3324143Sjoerg#include <sys/sysproto.h>
3424143Sjoerg#include <sys/kernel.h>
3524143Sjoerg#include <sys/sysctl.h>
3624143Sjoerg#include <sys/proc.h>
3724143Sjoerg#include <sys/vnode.h>
38131621Sdes#include <sys/vmmeter.h>
39131621Sdes#include <sys/lock.h>
40131621Sdes#include <vm/vm.h>
41131626Sdes#include <vm/vm_param.h>
42131621Sdes#include <vm/vm_prot.h>
43131621Sdes#include <vm/vm_kern.h>
4424143Sjoerg#include <vm/vm_pageout.h>
45144636Sstefanf#include <vm/vm_page.h>
46144636Sstefanf#include <vm/vm_object.h>
4772951Srwatson#include <vm/vm_extern.h>
4824143Sjoerg#include <vm/vm_map.h>
4924143Sjoerg#include <sys/buf.h>
5024143Sjoerg#include <sys/mount.h>
5169375Sjhb#include <sys/malloc.h>
5272951Srwatson#include <sys/resourcevar.h>
5324143Sjoerg
5472951Srwatsonstatic MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer");
55146291Sobrien
56146291Sobrienstatic void vfs_update __P((void));
5772951Srwatsonstatic struct	proc *updateproc;
58145073Skeramidastatic struct kproc_desc up_kp = {
5992922Simp	"update",
6027340Speter	vfs_update,
61131402Salfred	&updateproc
62146291Sobrien};
6369375SjhbSYSINIT_KT(update, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
6424143Sjoerg
6572951Srwatsonstruct buf *buf;		/* buffer header pool */
6692922Simpstruct swqueue bswlist;
6724143Sjoerg
6824143Sjoergstatic int count_lock_queue __P((void));
6924143Sjoergstatic void vm_hold_free_pages(struct buf * bp, vm_offset_t from,
70158280Sbde		vm_offset_t to);
71131623Sdesstatic void vm_hold_load_pages(struct buf * bp, vm_offset_t from,
72131623Sdes		vm_offset_t to);
7324143Sjoergstatic void vfs_buf_set_valid(struct buf *bp, vm_ooffset_t foff,
7424143Sjoerg			      vm_offset_t off, vm_offset_t size,
7524143Sjoerg			      vm_page_t m);
7624143Sjoergstatic void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off,
7724143Sjoerg			       int pageno, vm_page_t m);
7824143Sjoergstatic void vfs_clean_pages(struct buf * bp);
7969896Smckusickstatic void vfs_setdirty(struct buf *bp);
8069896Smckusickstatic void vfs_vmio_release(struct buf *bp);
8124143Sjoergstatic void flushdirtybuffers(int slpflag, int slptimeo);
8224143Sjoerg
8369896Smckusickint needsbuffer;
8424143Sjoerg
85131402Salfred/*
86131402Salfred * Internal update daemon, process 3
87131402Salfred *	The variable vfs_update_wakeup allows for internal syncs.
88131402Salfred */
89131402Salfredint vfs_update_wakeup;
9024143Sjoerg
9124143Sjoerg
9224143Sjoerg/*
9324143Sjoerg * buffers base kva
9424143Sjoerg */
9524143Sjoerg
96131402Salfred/*
97158280Sbde * bogus page -- for I/O to/from partially complete buffers
98131402Salfred * this is a temporary solution to the problem, but it is not
99131402Salfred * really that bad.  it would be better to split the buffer
100158280Sbde * for input in the case of buffers partially already in memory,
101131402Salfred * but the code is intricate enough already.
102145073Skeramida */
103158280Sbdevm_page_t bogus_page;
10427340Speterstatic vm_offset_t bogus_offset;
105158280Sbde
10624143Sjoergstatic int bufspace, maxbufspace, vmiospace, maxvmiobufspace,
10727340Speter	bufmallocspace, maxbufmallocspace;
108158282Sbdeint numdirtybuffers;
10924143Sjoergstatic int lodirtybuffers, hidirtybuffers;
110145073Skeramidastatic int numfreebuffers, lofreebuffers, hifreebuffers;
111158280Sbdestatic int kvafreespace;
11227340Speter
113158280SbdeSYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD,
11424143Sjoerg	&numdirtybuffers, 0, "");
11527340SpeterSYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW,
116158282Sbde	&lodirtybuffers, 0, "");
11724143SjoergSYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW,
11824143Sjoerg	&hidirtybuffers, 0, "");
11924143SjoergSYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD,
12024143Sjoerg	&numfreebuffers, 0, "");
12124143SjoergSYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW,
12224143Sjoerg	&lofreebuffers, 0, "");
123158280SbdeSYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW,
124131623Sdes	&hifreebuffers, 0, "");
12524143SjoergSYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RW,
12624143Sjoerg	&maxbufspace, 0, "");
12724143SjoergSYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD,
12824143Sjoerg	&bufspace, 0, "");
12924143SjoergSYSCTL_INT(_vfs, OID_AUTO, maxvmiobufspace, CTLFLAG_RW,
13024143Sjoerg	&maxvmiobufspace, 0, "");
13124143SjoergSYSCTL_INT(_vfs, OID_AUTO, vmiospace, CTLFLAG_RD,
13224143Sjoerg	&vmiospace, 0, "");
13324143SjoergSYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW,
13424143Sjoerg	&maxbufmallocspace, 0, "");
13524143SjoergSYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD,
13624143Sjoerg	&bufmallocspace, 0, "");
13724143SjoergSYSCTL_INT(_vfs, OID_AUTO, kvafreespace, CTLFLAG_RD,
13872951Srwatson	&kvafreespace, 0, "");
13924143Sjoerg
14072951Srwatsonstatic LIST_HEAD(bufhashhdr, buf) bufhashtbl[BUFHSZ], invalhash;
14124143Sjoergstatic TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES];
14224143Sjoerg
14324143Sjoergextern int vm_swap_size;
14424143Sjoerg
14524143Sjoerg#define BUF_MAXUSE 24
14624143Sjoerg
14724143Sjoerg#define VFS_BIO_NEED_ANY 1
14824143Sjoerg#define VFS_BIO_NEED_LOWLIMIT 2
14924143Sjoerg#define VFS_BIO_NEED_FREE 4
15065557Sjasone
15124143Sjoerg/*
152131623Sdes * Initialize buffer headers and related structures.
153131623Sdes */
154131623Sdesvoid
15524143Sjoergbufinit()
15624143Sjoerg{
15724143Sjoerg	struct buf *bp;
15824143Sjoerg	int i;
15924143Sjoerg
16024143Sjoerg	TAILQ_INIT(&bswlist);
161131623Sdes	LIST_INIT(&invalhash);
16224143Sjoerg
16324143Sjoerg	/* first, make a null hash table */
16424143Sjoerg	for (i = 0; i < BUFHSZ; i++)
16524143Sjoerg		LIST_INIT(&bufhashtbl[i]);
16624143Sjoerg
16724143Sjoerg	/* next, make a null set of free lists */
168158280Sbde	for (i = 0; i < BUFFER_QUEUES; i++)
169158280Sbde		TAILQ_INIT(&bufqueues[i]);
17024143Sjoerg
17124143Sjoerg	/* finally, initialize each buffer header and stick on empty q */
17224143Sjoerg	for (i = 0; i < nbuf; i++) {
17324143Sjoerg		bp = &buf[i];
174131623Sdes		bzero(bp, sizeof *bp);
175131623Sdes		bp->b_flags = B_INVAL;	/* we're just an empty header */
17624143Sjoerg		bp->b_dev = NODEV;
17724143Sjoerg		bp->b_rcred = NOCRED;
17824143Sjoerg		bp->b_wcred = NOCRED;
17924143Sjoerg		bp->b_qindex = QUEUE_EMPTY;
18024143Sjoerg		bp->b_vnbufs.le_next = NOLIST;
18124143Sjoerg		bp->b_generation = 0;
18224143Sjoerg		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
18324143Sjoerg		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
18424143Sjoerg	}
18524143Sjoerg/*
186131402Salfred * maxbufspace is currently calculated to support all filesystem blocks
187131402Salfred * to be 8K.  If you happen to use a 16K filesystem, the size of the buffer
188131402Salfred * cache is still the same as it would be for 8K filesystems.  This
189131402Salfred * keeps the size of the buffer cache "in check" for big block filesystems.
19024143Sjoerg */
191131412Salfred	maxbufspace = (nbuf + 8) * DFLTBSIZE;
192131412Salfred/*
193131412Salfred * reserve 1/3 of the buffers for metadata (VDIR) which might not be VMIO'ed
194131412Salfred */
195131412Salfred	maxvmiobufspace = 2 * maxbufspace / 3;
19624143Sjoerg/*
19724143Sjoerg * Limit the amount of malloc memory since it is wired permanently into
19824143Sjoerg * the kernel space.  Even though this is accounted for in the buffer
19924143Sjoerg * allocation, we don't want the malloced region to grow uncontrolled.
20024143Sjoerg * The malloc scheme improves memory utilization significantly on average
20124143Sjoerg * (small) directories.
20224143Sjoerg */
20324143Sjoerg	maxbufmallocspace = maxbufspace / 20;
20424143Sjoerg
20524143Sjoerg/*
20624143Sjoerg * Remove the probability of deadlock conditions by limiting the
20738278Swosch * number of dirty buffers.
208131829Skeramida */
209145073Skeramida	hidirtybuffers = nbuf / 8 + 20;
210131829Skeramida	lodirtybuffers = nbuf / 16 + 10;
211133817Salfred	numdirtybuffers = 0;
212145073Skeramida	lofreebuffers = nbuf / 18 + 5;
213133817Salfred	hifreebuffers = 2 * lofreebuffers;
21438278Swosch	numfreebuffers = nbuf;
21538278Swosch	kvafreespace = 0;
21638278Swosch
217158282Sbde	bogus_offset = kmem_alloc_pageable(kernel_map, PAGE_SIZE);
218158282Sbde	bogus_page = vm_page_alloc(kernel_object,
219158282Sbde			((bogus_offset - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT),
220158282Sbde			VM_ALLOC_NORMAL);
221158282Sbde
22224143Sjoerg}
223131622Sdes
22424143Sjoerg/*
225131623Sdes * Free the kva allocation for a buffer
226131623Sdes * Must be called only at splbio or higher,
227131623Sdes *  as this is the only locking for buffer_map.
22824143Sjoerg */
229131623Sdesstatic void
230158280Sbdebfreekva(struct buf * bp)
231158280Sbde{
232158280Sbde	if (bp->b_kvasize == 0)
233158280Sbde		return;
234131623Sdes
235131623Sdes	vm_map_delete(buffer_map,
23627340Speter		(vm_offset_t) bp->b_kvabase,
237131623Sdes		(vm_offset_t) bp->b_kvabase + bp->b_kvasize);
238131623Sdes
239131623Sdes	bp->b_kvasize = 0;
240131623Sdes
241146291Sobrien}
242146291Sobrien
243146291Sobrien/*
244146291Sobrien * remove the buffer from the appropriate free list
24527390Speter */
246131626Sdesvoid
247131626Sdesbremfree(struct buf * bp)
248131626Sdes{
24924143Sjoerg	int s = splbio();
250131623Sdes
25124143Sjoerg	if (bp->b_qindex != QUEUE_NONE) {
252131623Sdes		if (bp->b_qindex == QUEUE_EMPTY) {
253131623Sdes			kvafreespace -= bp->b_kvasize;
25424143Sjoerg		}
255131623Sdes		TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
256131623Sdes		bp->b_qindex = QUEUE_NONE;
257131623Sdes	} else {
258131623Sdes#if !defined(MAX_PERF)
259158280Sbde		panic("bremfree: removing a buffer when not on a queue");
260158280Sbde#endif
261131623Sdes	}
262131623Sdes	if ((bp->b_flags & B_INVAL) ||
263131626Sdes		(bp->b_flags & (B_DELWRI|B_LOCKED)) == 0)
264131623Sdes		--numfreebuffers;
265131623Sdes	splx(s);
266131623Sdes}
26724143Sjoerg
268131623Sdes
269131623Sdes/*
27024143Sjoerg * Get a buffer with the specified data.  Look in the cache first.
271131623Sdes */
272131623Sdesint
273131623Sdesbread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
274131623Sdes    struct buf ** bpp)
275131623Sdes{
27638278Swosch	struct buf *bp;
277133817Salfred
27838278Swosch	bp = getblk(vp, blkno, size, 0, 0);
27924143Sjoerg	*bpp = bp;
280131623Sdes
281131623Sdes	/* if not found in cache, do some I/O */
28224143Sjoerg	if ((bp->b_flags & B_CACHE) == 0) {
28324143Sjoerg		if (curproc != NULL)
284131310Salfred			curproc->p_stats->p_ru.ru_inblock++;
285131622Sdes		bp->b_flags |= B_READ;
28624143Sjoerg		bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
287131623Sdes		if (bp->b_rcred == NOCRED) {
288131623Sdes			if (cred != NOCRED)
28924143Sjoerg				crhold(cred);
290131623Sdes			bp->b_rcred = cred;
291131623Sdes		}
292145073Skeramida		vfs_busy_pages(bp, 0);
293145073Skeramida		VOP_STRATEGY(bp);
294145073Skeramida		return (biowait(bp));
295145073Skeramida	}
296145073Skeramida	return (0);
297145073Skeramida}
298145073Skeramida
299145073Skeramida/*
300145073Skeramida * Operates like bread, but also starts asynchronous I/O on
301146342Skeramida * read-ahead blocks.
302146342Skeramida */
303146342Skeramidaint
304131623Sdesbreadn(struct vnode * vp, daddr_t blkno, int size,
305131623Sdes    daddr_t * rablkno, int *rabsize,
306131623Sdes    int cnt, struct ucred * cred, struct buf ** bpp)
307146342Skeramida{
308146342Skeramida	struct buf *bp, *rabp;
309131623Sdes	int i;
310131623Sdes	int rv = 0, readwait = 0;
311131623Sdes
312131623Sdes	*bpp = bp = getblk(vp, blkno, size, 0, 0);
31324143Sjoerg
31424143Sjoerg	/* if not found in cache, do some I/O */
31524143Sjoerg	if ((bp->b_flags & B_CACHE) == 0) {
31624143Sjoerg		if (curproc != NULL)
31724143Sjoerg			curproc->p_stats->p_ru.ru_inblock++;
31824143Sjoerg		bp->b_flags |= B_READ;
31924143Sjoerg		bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
320131622Sdes		if (bp->b_rcred == NOCRED) {
32124143Sjoerg			if (cred != NOCRED)
322131623Sdes				crhold(cred);
323131623Sdes			bp->b_rcred = cred;
324131623Sdes		}
325131623Sdes		vfs_busy_pages(bp, 0);
326131623Sdes		VOP_STRATEGY(bp);
327131626Sdes		++readwait;
32824143Sjoerg	}
329131623Sdes	for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
330131623Sdes		if (inmem(vp, *rablkno))
331131623Sdes			continue;
332131623Sdes		rabp = getblk(vp, *rablkno, *rabsize, 0, 0);
33324143Sjoerg
334131623Sdes		if ((rabp->b_flags & B_CACHE) == 0) {
335131626Sdes			if (curproc != NULL)
336131626Sdes				curproc->p_stats->p_ru.ru_inblock++;
33724143Sjoerg			rabp->b_flags |= B_READ | B_ASYNC;
338131623Sdes			rabp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
339131623Sdes			if (rabp->b_rcred == NOCRED) {
34024143Sjoerg				if (cred != NOCRED)
341131623Sdes					crhold(cred);
342131623Sdes				rabp->b_rcred = cred;
343131623Sdes			}
344131623Sdes			vfs_busy_pages(rabp, 0);
345131623Sdes			VOP_STRATEGY(rabp);
346131623Sdes		} else {
347131623Sdes			brelse(rabp);
34824143Sjoerg		}
349131623Sdes	}
350131623Sdes
351131623Sdes	if (readwait) {
352131623Sdes		rv = biowait(bp);
353131623Sdes	}
354131623Sdes	return (rv);
355131623Sdes}
356131623Sdes
357131623Sdes/*
358131623Sdes * Write, release buffer on completion.  (Done by iodone
359131623Sdes * if async.)
360131623Sdes */
361131623Sdesint
362131623Sdesbwrite(struct buf * bp)
363131623Sdes{
364131623Sdes	int oldflags = bp->b_flags;
36524143Sjoerg
366131623Sdes	if (bp->b_flags & B_INVAL) {
367131623Sdes		brelse(bp);
368131623Sdes		return (0);
369131623Sdes	}
370131623Sdes#if !defined(MAX_PERF)
37124143Sjoerg	if (!(bp->b_flags & B_BUSY))
372131623Sdes		panic("bwrite: buffer is not busy???");
373131623Sdes#endif
374131623Sdes
375131623Sdes	bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI);
376131623Sdes	bp->b_flags |= B_WRITEINPROG;
37724143Sjoerg
378131623Sdes	if ((oldflags & B_DELWRI) == B_DELWRI) {
379131623Sdes		--numdirtybuffers;
38024143Sjoerg		reassignbuf(bp, bp->b_vp);
381131623Sdes	}
382131623Sdes
383131623Sdes	bp->b_vp->v_numoutput++;
384131623Sdes	vfs_busy_pages(bp, 1);
385131623Sdes	if (curproc != NULL)
386131623Sdes		curproc->p_stats->p_ru.ru_oublock++;
387131623Sdes	VOP_STRATEGY(bp);
388131623Sdes
389131623Sdes	if ((oldflags & B_ASYNC) == 0) {
39024143Sjoerg		int rtval = biowait(bp);
39124143Sjoerg
392131623Sdes		if (oldflags & B_DELWRI) {
393131623Sdes			reassignbuf(bp, bp->b_vp);
394131623Sdes		}
395131623Sdes		brelse(bp);
39624143Sjoerg		return (rtval);
39724143Sjoerg	}
398131623Sdes	return (0);
399131623Sdes}
400131623Sdes
401131623Sdesinline void
402131623Sdesvfs_bio_need_satisfy(void) {
40342447Sobrien	++numfreebuffers;
404131623Sdes	if (!needsbuffer)
405131623Sdes		return;
406131623Sdes	if (numdirtybuffers < lodirtybuffers) {
407131623Sdes		needsbuffer &= ~(VFS_BIO_NEED_ANY | VFS_BIO_NEED_LOWLIMIT);
408131623Sdes	} else {
409131623Sdes		needsbuffer &= ~VFS_BIO_NEED_ANY;
410131623Sdes	}
411131623Sdes	if (numfreebuffers >= hifreebuffers) {
412131623Sdes		needsbuffer &= ~VFS_BIO_NEED_FREE;
413131623Sdes	}
414131623Sdes	wakeup(&needsbuffer);
415131623Sdes}
416131623Sdes
41724143Sjoerg/*
41824143Sjoerg * Delayed write. (Buffer is marked dirty).
419132015Salfred */
420132015Salfredvoid
421132955Salfredbdwrite(struct buf * bp)
422132955Salfred{
423132955Salfred
424132955Salfred#if !defined(MAX_PERF)
425132955Salfred	if ((bp->b_flags & B_BUSY) == 0) {
426132955Salfred		panic("bdwrite: buffer is not busy");
427132955Salfred	}
428132955Salfred#endif
429132955Salfred
430131402Salfred	if (bp->b_flags & B_INVAL) {
431131402Salfred		brelse(bp);
432131402Salfred		return;
433131402Salfred	}
434131402Salfred	if (bp->b_flags & B_TAPE) {
435132955Salfred		bawrite(bp);
436132955Salfred		return;
437132955Salfred	}
438132955Salfred	bp->b_flags &= ~(B_READ|B_RELBUF);
439131402Salfred	if ((bp->b_flags & B_DELWRI) == 0) {
440131402Salfred		bp->b_flags |= B_DONE | B_DELWRI;
441132955Salfred		reassignbuf(bp, bp->b_vp);
442132015Salfred		++numdirtybuffers;
443132015Salfred	}
444132955Salfred
445132015Salfred	/*
446132015Salfred	 * This bmap keeps the system from needing to do the bmap later,
447132955Salfred	 * perhaps when the system is attempting to do a sync.  Since it
448132955Salfred	 * is likely that the indirect block -- or whatever other datastructure
449132955Salfred	 * that the filesystem needs is still in memory now, it is a good
450132955Salfred	 * thing to do this.  Note also, that if the pageout daemon is
451132955Salfred	 * requesting a sync -- there might not be enough memory to do
452132955Salfred	 * the bmap then...  So, this is important to do.
453132955Salfred	 */
454131402Salfred	if (bp->b_lblkno == bp->b_blkno) {
455131626Sdes		VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
456132015Salfred	}
457132015Salfred
458131402Salfred	/*
459132015Salfred	 * Set the *dirty* buffer range based upon the VM system dirty pages.
460131402Salfred	 */
461132015Salfred	vfs_setdirty(bp);
462132015Salfred
463131402Salfred	/*
464132015Salfred	 * We need to do this here to satisfy the vnode_pager and the
465132015Salfred	 * pageout daemon, so that it thinks that the pages have been
466131402Salfred	 * "cleaned".  Note that since the pages are in a delayed write
467131402Salfred	 * buffer -- the VFS layer "will" see that the pages get written
468131402Salfred	 * out on the next sync, or perhaps the cluster will be completed.
469132955Salfred	 */
470132955Salfred	vfs_clean_pages(bp);
471132955Salfred	bqrelse(bp);
472132955Salfred
473131402Salfred	if (numdirtybuffers >= hidirtybuffers)
474158280Sbde		flushdirtybuffers(0, 0);
475158280Sbde
476131402Salfred	return;
477131402Salfred}
478131402Salfred
479131402Salfred/*
480131402Salfred * Asynchronous write.
481131402Salfred * Start output on a buffer, but do not wait for it to complete.
482131402Salfred * The buffer is released when the output completes.
483131402Salfred */
484131402Salfredvoid
485131402Salfredbawrite(struct buf * bp)
486131412Salfred{
487131412Salfred	bp->b_flags |= B_ASYNC;
488131412Salfred	(void) VOP_BWRITE(bp);
489133817Salfred}
490133817Salfred
491131402Salfred/*
492131402Salfred * Ordered write.
493131402Salfred * Start output on a buffer, but only wait for it to complete if the
494131402Salfred * output device cannot guarantee ordering in some other way.  Devices
495131402Salfred * that can perform asynchronous ordered writes will set the B_ASYNC
496131402Salfred * flag in their strategy routine.
497131402Salfred * The buffer is released when the output completes.
498132955Salfred */
499132955Salfredint
500132955Salfredbowrite(struct buf * bp)
501131412Salfred{
502131412Salfred	/*
503131412Salfred	 * XXX Add in B_ASYNC once the SCSI
504131412Salfred	 *     layer can deal with ordered
505131412Salfred	 *     writes properly.
506133817Salfred	 */
507131412Salfred	bp->b_flags |= B_ORDERED;
508131412Salfred	return (VOP_BWRITE(bp));
50924143Sjoerg}
51024143Sjoerg
511131310Salfred/*
512131622Sdes * Release a buffer.
513131626Sdes */
51424143Sjoergvoid
515131623Sdesbrelse(struct buf * bp)
516131623Sdes{
517131623Sdes	int s;
518133817Salfred
519131623Sdes	if (bp->b_flags & B_CLUSTER) {
520131623Sdes		relpbuf(bp);
521131623Sdes		return;
522131623Sdes	}
52324143Sjoerg	/* anyone need a "free" block? */
524131623Sdes	s = splbio();
525131623Sdes
526131623Sdes	/* anyone need this block? */
527131623Sdes	if (bp->b_flags & B_WANTED) {
528131623Sdes		bp->b_flags &= ~(B_WANTED | B_AGE);
529131623Sdes		wakeup(bp);
53024143Sjoerg	}
531131623Sdes
532131623Sdes	if (bp->b_flags & B_LOCKED)
533131623Sdes		bp->b_flags &= ~B_ERROR;
534131623Sdes
535131623Sdes	if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) ||
536131626Sdes	    (bp->b_bufsize <= 0)) {
537131623Sdes		bp->b_flags |= B_INVAL;
538131626Sdes		if (bp->b_flags & B_DELWRI)
539131623Sdes			--numdirtybuffers;
540131623Sdes		bp->b_flags &= ~(B_DELWRI | B_CACHE);
541131623Sdes		if (((bp->b_flags & B_VMIO) == 0) && bp->b_vp) {
542131623Sdes			if (bp->b_bufsize)
543131623Sdes				allocbuf(bp, 0);
544131623Sdes			brelvp(bp);
545131623Sdes		}
546131623Sdes	}
547131623Sdes
548131626Sdes	/*
549158280Sbde	 * VMIO buffer rundown.  It is not very necessary to keep a VMIO buffer
550158280Sbde	 * constituted, so the B_INVAL flag is used to *invalidate* the buffer,
551131623Sdes	 * but the VM object is kept around.  The B_NOCACHE flag is used to
552131623Sdes	 * invalidate the pages in the VM object.
553131402Salfred	 *
554131623Sdes	 * If the buffer is a partially filled NFS buffer, keep it
555131623Sdes	 * since invalidating it now will lose informatio.  The valid
556131626Sdes	 * flags in the vm_pages have only DEV_BSIZE resolution but
557131623Sdes	 * the b_validoff, b_validend fields have byte resolution.
558131623Sdes	 * This can avoid unnecessary re-reads of the buffer.
559131623Sdes	 * XXX this seems to cause performance problems.
560131623Sdes	 */
561131623Sdes	if ((bp->b_flags & B_VMIO)
562131623Sdes	    && !(bp->b_vp->v_tag == VT_NFS &&
56324143Sjoerg		 bp->b_vp->v_type != VBLK &&
564131623Sdes		 (bp->b_flags & B_DELWRI) != 0)
565131623Sdes#ifdef notdef
566132024Sdes	    && (bp->b_vp->v_tag != VT_NFS
567131623Sdes		|| bp->b_vp->v_type == VBLK
568131623Sdes		|| (bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR))
569131623Sdes		|| bp->b_validend == 0
57024143Sjoerg		|| (bp->b_validoff == 0
571131623Sdes		    && bp->b_validend == bp->b_bufsize))
572131623Sdes#endif
573131623Sdes	    ) {
574131623Sdes		vm_ooffset_t foff;
575131623Sdes		vm_object_t obj;
576131623Sdes		int i, resid;
577131623Sdes		vm_page_t m;
578131623Sdes		struct vnode *vp;
579131626Sdes		int iototal = bp->b_bufsize;
580131619Sdes
581131623Sdes		vp = bp->b_vp;
582132024Sdes
583132024Sdes#if !defined(MAX_PERF)
584131619Sdes		if (!vp)
585131623Sdes			panic("brelse: missing vp");
586131623Sdes#endif
587131623Sdes
588131619Sdes		if (bp->b_npages) {
589131623Sdes			vm_pindex_t poff;
590131623Sdes			obj = (vm_object_t) vp->v_object;
591131623Sdes			if (vp->v_type == VBLK)
592131619Sdes				foff = ((vm_ooffset_t) bp->b_lblkno) << DEV_BSHIFT;
593158280Sbde			else
594158280Sbde				foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
595131623Sdes			poff = OFF_TO_IDX(foff);
596131623Sdes			for (i = 0; i < bp->b_npages; i++) {
597131623Sdes				m = bp->b_pages[i];
598131623Sdes				if (m == bogus_page) {
599131626Sdes					m = vm_page_lookup(obj, poff + i);
600131619Sdes#if !defined(MAX_PERF)
601131623Sdes					if (!m) {
602131623Sdes						panic("brelse: page missing\n");
603131623Sdes					}
604131619Sdes#endif
605131623Sdes					bp->b_pages[i] = m;
606159520Sse					pmap_qenter(trunc_page(bp->b_data),
607159520Sse						bp->b_pages, bp->b_npages);
608131623Sdes				}
609131623Sdes				resid = IDX_TO_OFF(m->pindex+1) - foff;
610131619Sdes				if (resid > iototal)
611131623Sdes					resid = iototal;
612131623Sdes				if (resid > 0) {
613131623Sdes					/*
614131619Sdes					 * Don't invalidate the page if the local machine has already
615131623Sdes					 * modified it.  This is the lesser of two evils, and should
616158280Sbde					 * be fixed.
617131623Sdes					 */
618131619Sdes					if (bp->b_flags & (B_NOCACHE | B_ERROR)) {
619131623Sdes						vm_page_test_dirty(m);
620131623Sdes						if (m->dirty == 0) {
621131623Sdes							vm_page_set_invalid(m, (vm_offset_t) foff, resid);
622131623Sdes							if (m->valid == 0)
623131623Sdes								vm_page_protect(m, VM_PROT_NONE);
624131623Sdes						}
625131626Sdes					}
626131626Sdes					if (resid >= PAGE_SIZE) {
627131623Sdes						if ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
628131623Sdes							bp->b_flags |= B_INVAL;
629131623Sdes						}
630131623Sdes					} else {
631131623Sdes						if (!vm_page_is_valid(m,
632131623Sdes							(((vm_offset_t) bp->b_data) & PAGE_MASK), resid)) {
633131623Sdes							bp->b_flags |= B_INVAL;
634131623Sdes						}
635131623Sdes					}
636131623Sdes				}
637131626Sdes				foff += resid;
63824143Sjoerg				iototal -= resid;
639131623Sdes			}
640131623Sdes		}
641131623Sdes		if (bp->b_flags & (B_INVAL | B_RELBUF))
64224143Sjoerg			vfs_vmio_release(bp);
643131623Sdes	} else if (bp->b_flags & B_VMIO) {
644131623Sdes		if (bp->b_flags & (B_INVAL | B_RELBUF))
645131623Sdes			vfs_vmio_release(bp);
646131623Sdes	}
64724143Sjoerg
64824143Sjoerg#if !defined(MAX_PERF)
649131626Sdes	if (bp->b_qindex != QUEUE_NONE)
65024143Sjoerg		panic("brelse: free buffer onto another queue???");
651131310Salfred#endif
652131626Sdes
65324143Sjoerg	/* enqueue */
654131623Sdes	/* buffers with no memory */
655131623Sdes	if (bp->b_bufsize == 0) {
656131623Sdes		bp->b_flags |= B_INVAL;
657131623Sdes		bp->b_qindex = QUEUE_EMPTY;
658131623Sdes		TAILQ_INSERT_HEAD(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
659131623Sdes		LIST_REMOVE(bp, b_hash);
660131623Sdes		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
661131623Sdes		bp->b_dev = NODEV;
662131623Sdes		kvafreespace += bp->b_kvasize;
663145155Skeramida		bp->b_generation++;
66424143Sjoerg
665131623Sdes	/* buffers with junk contents */
666131623Sdes	} else if (bp->b_flags & (B_ERROR | B_INVAL | B_NOCACHE | B_RELBUF)) {
667131623Sdes		bp->b_flags |= B_INVAL;
668131623Sdes		bp->b_qindex = QUEUE_AGE;
669131620Sdes		TAILQ_INSERT_HEAD(&bufqueues[QUEUE_AGE], bp, b_freelist);
670131623Sdes		LIST_REMOVE(bp, b_hash);
671131623Sdes		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
672131623Sdes		bp->b_dev = NODEV;
673131623Sdes		bp->b_generation++;
674131623Sdes
675158280Sbde	/* buffers that are locked */
676158280Sbde	} else if (bp->b_flags & B_LOCKED) {
677158280Sbde		bp->b_qindex = QUEUE_LOCKED;
678131626Sdes		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
679131626Sdes
680131626Sdes	/* buffers with stale but valid contents */
681131626Sdes	} else if (bp->b_flags & B_AGE) {
682131626Sdes		bp->b_qindex = QUEUE_AGE;
683131626Sdes		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_AGE], bp, b_freelist);
684131623Sdes
685131623Sdes	/* buffers with valid and quite potentially reuseable contents */
68624143Sjoerg	} else {
687131623Sdes		bp->b_qindex = QUEUE_LRU;
688131623Sdes		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
689131623Sdes	}
69024143Sjoerg
691131623Sdes	if ((bp->b_flags & B_INVAL) ||
69224143Sjoerg		(bp->b_flags & (B_LOCKED|B_DELWRI)) == 0) {
693131623Sdes		if (bp->b_flags & B_DELWRI) {
694131623Sdes			--numdirtybuffers;
69524143Sjoerg			bp->b_flags &= ~B_DELWRI;
696131623Sdes		}
697131623Sdes		vfs_bio_need_satisfy();
69824143Sjoerg	}
699131623Sdes
700131623Sdes	/* unlock */
701131623Sdes	bp->b_flags &= ~(B_ORDERED | B_WANTED | B_BUSY |
702131623Sdes				B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
703131623Sdes	splx(s);
704104388Sjhb}
705131623Sdes
706131623Sdes/*
707131623Sdes * Release a buffer.
708131623Sdes */
709131623Sdesvoid
71024143Sjoergbqrelse(struct buf * bp)
711131623Sdes{
712131623Sdes	int s;
713131623Sdes
714131623Sdes	s = splbio();
715131623Sdes
71624143Sjoerg	/* anyone need this block? */
71743720Sfenner	if (bp->b_flags & B_WANTED) {
718131623Sdes		bp->b_flags &= ~(B_WANTED | B_AGE);
719131623Sdes		wakeup(bp);
720131626Sdes	}
721131623Sdes
722131623Sdes#if !defined(MAX_PERF)
723131623Sdes	if (bp->b_qindex != QUEUE_NONE)
724131623Sdes		panic("bqrelse: free buffer onto another queue???");
72524143Sjoerg#endif
726131623Sdes
727131623Sdes	if (bp->b_flags & B_LOCKED) {
728131623Sdes		bp->b_flags &= ~B_ERROR;
729158280Sbde		bp->b_qindex = QUEUE_LOCKED;
730158280Sbde		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
731158280Sbde		/* buffers with stale but valid contents */
732158280Sbde	} else {
733131623Sdes		bp->b_qindex = QUEUE_LRU;
734133817Salfred		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
735133817Salfred	}
736131623Sdes
737131623Sdes	if ((bp->b_flags & (B_LOCKED|B_DELWRI)) == 0) {
738131623Sdes		vfs_bio_need_satisfy();
739131623Sdes	}
740131623Sdes
741131623Sdes	/* unlock */
742131402Salfred	bp->b_flags &= ~(B_ORDERED | B_WANTED | B_BUSY |
743131623Sdes		B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
744131623Sdes	splx(s);
745158280Sbde}
746133817Salfred
747133817Salfredstatic void
748131626Sdesvfs_vmio_release(bp)
749131626Sdes	struct buf *bp;
750131626Sdes{
751131626Sdes	int i;
752131626Sdes	vm_page_t m;
753131623Sdes
754131623Sdes	for (i = 0; i < bp->b_npages; i++) {
755131623Sdes		m = bp->b_pages[i];
756131623Sdes		bp->b_pages[i] = NULL;
757131623Sdes		vm_page_unwire(m);
758145073Skeramida		/*
759131623Sdes		 * We don't mess with busy pages, it is
760145073Skeramida		 * the responsibility of the process that
761145073Skeramida		 * busied the pages to deal with them.
762145073Skeramida		 */
763145073Skeramida		if ((m->flags & PG_BUSY) || (m->busy != 0))
764145073Skeramida			continue;
765145073Skeramida
766145073Skeramida		if (m->wire_count == 0) {
767145073Skeramida
76869896Smckusick			if (m->flags & PG_WANTED) {
769158280Sbde				m->flags &= ~PG_WANTED;
770145073Skeramida				wakeup(m);
77172377Sjake			}
772158282Sbde
77337100Sdt			/*
77469896Smckusick			 * If this is an async free -- we cannot place
77524143Sjoerg			 * pages onto the cache queue.  If it is an
77669896Smckusick			 * async free, then we don't modify any queues.
77724143Sjoerg			 * This is probably in error (for perf reasons),
778146342Skeramida			 * and we will eventually need to build
779158280Sbde			 * a more complete infrastructure to support I/O
78069896Smckusick			 * rundown.
78124143Sjoerg			 */
782131623Sdes			if ((bp->b_flags & B_ASYNC) == 0) {
783131623Sdes
78424143Sjoerg			/*
78524143Sjoerg			 * In the case of sync buffer frees, we can do pretty much
786131310Salfred			 * anything to any of the memory queues.  Specifically,
787158282Sbde			 * the cache queue is okay to be modified.
78824143Sjoerg			 */
789131623Sdes				if (m->valid) {
790131310Salfred					if(m->dirty == 0)
791131623Sdes						vm_page_test_dirty(m);
792131623Sdes					/*
793131623Sdes					 * this keeps pressure off of the process memory
794131623Sdes					 */
795131623Sdes					if (m->dirty == 0 && m->hold_count == 0)
796131623Sdes						vm_page_cache(m);
797158280Sbde					else
798158280Sbde						vm_page_deactivate(m);
799131623Sdes				} else if (m->hold_count == 0) {
800131623Sdes					m->flags |= PG_BUSY;
80124143Sjoerg					vm_page_protect(m, VM_PROT_NONE);
80272951Srwatson					vm_page_free(m);
803158282Sbde				}
804158282Sbde			} else {
805158282Sbde				/*
806158282Sbde				 * If async, then at least we clear the
807158282Sbde				 * act_count.
808158282Sbde				 */
809158282Sbde				m->act_count = 0;
810158282Sbde			}
811158282Sbde		}
812158282Sbde	}
813158282Sbde	bufspace -= bp->b_bufsize;
814158282Sbde	vmiospace -= bp->b_bufsize;
815158282Sbde	pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
816158282Sbde	bp->b_npages = 0;
817158282Sbde	bp->b_bufsize = 0;
818158282Sbde	bp->b_flags &= ~B_VMIO;
819158282Sbde	if (bp->b_vp)
820158282Sbde		brelvp(bp);
821158282Sbde}
82238278Swosch
82324143Sjoerg/*
824158282Sbde * Check to see if a block is currently memory resident.
825131622Sdes */
826131402Salfredstruct buf *
827131623Sdesgbincore(struct vnode * vp, daddr_t blkno)
828131623Sdes{
829131402Salfred	struct buf *bp;
830131623Sdes	struct bufhashhdr *bh;
831131623Sdes
832131402Salfred	bh = BUFHASH(vp, blkno);
833131623Sdes	bp = bh->lh_first;
834131402Salfred
835131402Salfred	/* Search hash chain */
83624143Sjoerg	while (bp != NULL) {
83724143Sjoerg		/* hit */
83824143Sjoerg		if (bp->b_vp == vp && bp->b_lblkno == blkno &&
839131620Sdes		    (bp->b_flags & B_INVAL) == 0) {
840131620Sdes			break;
841131620Sdes		}
842131620Sdes		bp = bp->b_hash.le_next;
843131620Sdes	}
844131620Sdes	return (bp);
84524143Sjoerg}
84624143Sjoerg
847158280Sbde/*
848131623Sdes * this routine implements clustered async writes for
849131623Sdes * clearing out B_DELWRI buffers...  This is much better
850131623Sdes * than the old way of writing only one buffer at a time.
851131623Sdes */
852131623Sdesint
853131623Sdesvfs_bio_awrite(struct buf * bp)
854131623Sdes{
85524143Sjoerg	int i;
85638278Swosch	daddr_t lblkno = bp->b_lblkno;
857131620Sdes	struct vnode *vp = bp->b_vp;
858146343Skeramida	int s;
859146343Skeramida	int ncl;
860146343Skeramida	struct buf *bpa;
861158280Sbde	int nwritten;
862158280Sbde	int size;
863158280Sbde	int maxcl;
864158280Sbde
865146343Skeramida	s = splbio();
866146343Skeramida	/*
867146343Skeramida	 * right now we support clustered writing only to regular files
868146343Skeramida	 */
869131626Sdes	if ((vp->v_type == VREG) &&
87038278Swosch	    (vp->v_mount != 0) && /* Only on nodes that have the size info */
871131626Sdes	    (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
872131628Sdes
873131626Sdes		size = vp->v_mount->mnt_stat.f_iosize;
874131626Sdes		maxcl = MAXPHYS / size;
875131626Sdes
87638278Swosch		for (i = 1; i < maxcl; i++) {
877131626Sdes			if ((bpa = gbincore(vp, lblkno + i)) &&
878131626Sdes			    ((bpa->b_flags & (B_BUSY | B_DELWRI | B_CLUSTEROK | B_INVAL)) ==
879131626Sdes			    (B_DELWRI | B_CLUSTEROK)) &&
880131626Sdes			    (bpa->b_bufsize == size)) {
881131626Sdes				if ((bpa->b_blkno == bpa->b_lblkno) ||
88238278Swosch				    (bpa->b_blkno != bp->b_blkno + ((i * size) >> DEV_BSHIFT)))
883131626Sdes					break;
884131628Sdes			} else {
885131626Sdes				break;
886131626Sdes			}
887131626Sdes		}
88838278Swosch		ncl = i;
889145073Skeramida		/*
890145073Skeramida		 * this is a possible cluster write
891145073Skeramida		 */
892145073Skeramida		if (ncl != 1) {
893145073Skeramida			nwritten = cluster_wbuild(vp, size, lblkno, ncl);
894145073Skeramida			splx(s);
895131626Sdes			return nwritten;
896131628Sdes		}
897131626Sdes	}
898131626Sdes#if 0
899131626Sdes   	else if ((vp->v_flag & VOBJBUF) && (vp->v_type == VBLK) &&
90038278Swosch		((size = bp->b_bufsize) >= PAGE_SIZE)) {
901131626Sdes		maxcl = MAXPHYS / size;
902131628Sdes		for (i = 1; i < maxcl; i++) {
903131626Sdes			if ((bpa = gbincore(vp, lblkno + i)) &&
904131626Sdes			    ((bpa->b_flags & (B_BUSY | B_DELWRI | B_CLUSTEROK | B_INVAL)) ==
905131626Sdes			    (B_DELWRI | B_CLUSTEROK)) &&
90638278Swosch			    (bpa->b_bufsize == size)) {
90738278Swosch				    if (bpa->b_blkno !=
90838278Swosch						bp->b_blkno + ((i * size) >> DEV_BSHIFT))
90924143Sjoerg							break;
91038278Swosch			} else {
911131626Sdes				break;
91238278Swosch			}
913131626Sdes		}
91438278Swosch		ncl = i;
91524143Sjoerg		/*
916131626Sdes		 * this is a possible cluster write
917131626Sdes		 */
91824143Sjoerg		if (ncl != 1) {
919131626Sdes			nwritten = cluster_wbuild(vp, size, lblkno, ncl);
920131626Sdes			splx(s);
921131626Sdes			return nwritten;
922131626Sdes		}
923131626Sdes	}
924131626Sdes#endif
92524143Sjoerg
926131626Sdes	bremfree(bp);
92724143Sjoerg	splx(s);
92824143Sjoerg	/*
92938278Swosch	 * default (old) behavior, writing out only one block
930158280Sbde	 */
931158280Sbde	bp->b_flags |= B_BUSY | B_ASYNC;
932158280Sbde	nwritten = bp->b_bufsize;
93324143Sjoerg	(void) VOP_BWRITE(bp);
934158280Sbde	return nwritten;
935158280Sbde}
936158280Sbde
937158280Sbde
938158280Sbde/*
939158280Sbde * Find a buffer header which is available for use.
940158280Sbde */
941133817Salfredstatic struct buf *
942131623Sdesgetnewbuf(struct vnode *vp, daddr_t blkno,
943131623Sdes	int slpflag, int slptimeo, int size, int maxsize)
944131623Sdes{
945131623Sdes	struct buf *bp, *bp1;
946131623Sdes	int nbyteswritten = 0;
947145073Skeramida	vm_offset_t addr;
948133817Salfred	static int writerecursion = 0;
949133817Salfred
950133817Salfredstart:
951133817Salfred	if (bufspace >= maxbufspace)
952133817Salfred		goto trytofreespace;
953133817Salfred
954131623Sdes	/* can we constitute a new buffer? */
95538278Swosch	if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]))) {
95638278Swosch#if !defined(MAX_PERF)
95738278Swosch		if (bp->b_qindex != QUEUE_EMPTY)
95838278Swosch			panic("getnewbuf: inconsistent EMPTY queue, qindex=%d",
95938278Swosch			    bp->b_qindex);
960131626Sdes#endif
96138278Swosch		bp->b_flags |= B_BUSY;
962131626Sdes		bremfree(bp);
963131626Sdes		goto fillbuf;
96438278Swosch	}
965131626Sdestrytofreespace:
966131626Sdes	/*
967131626Sdes	 * We keep the file I/O from hogging metadata I/O
968131626Sdes	 * This is desirable because file data is cached in the
969131626Sdes	 * VM/Buffer cache even if a buffer is freed.
970131626Sdes	 */
97138278Swosch	if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]))) {
972131626Sdes#if !defined(MAX_PERF)
97338278Swosch		if (bp->b_qindex != QUEUE_AGE)
97438278Swosch			panic("getnewbuf: inconsistent AGE queue, qindex=%d",
97538278Swosch			    bp->b_qindex);
97638278Swosch#endif
97738278Swosch	} else if ((bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]))) {
978131626Sdes#if !defined(MAX_PERF)
97938278Swosch		if (bp->b_qindex != QUEUE_LRU)
980131626Sdes			panic("getnewbuf: inconsistent LRU queue, qindex=%d",
981131626Sdes			    bp->b_qindex);
98238278Swosch#endif
983131626Sdes	}
984131626Sdes	if (!bp) {
985131626Sdes		/* wait for a free buffer of any kind */
986131626Sdes		needsbuffer |= VFS_BIO_NEED_ANY;
987131626Sdes		do
988131626Sdes			tsleep(&needsbuffer, (PRIBIO + 1) | slpflag, "newbuf",
98938278Swosch			    slptimeo);
990131626Sdes		while (needsbuffer & VFS_BIO_NEED_ANY);
99138278Swosch		return (0);
99238278Swosch	}
99338278Swosch
99438278Swosch#if defined(DIAGNOSTIC)
99538278Swosch	if (bp->b_flags & B_BUSY) {
996131626Sdes		panic("getnewbuf: busy buffer on free list\n");
997131310Salfred	}
998131626Sdes#endif
999131626Sdes
100038278Swosch	/*
1001131626Sdes	 * We are fairly aggressive about freeing VMIO buffers, but since
1002131626Sdes	 * the buffering is intact without buffer headers, there is not
1003131626Sdes	 * much loss.  We gain by maintaining non-VMIOed metadata in buffers.
1004131626Sdes	 */
1005131626Sdes	if ((bp->b_qindex == QUEUE_LRU) && (bp->b_usecount > 0)) {
1006131626Sdes		if ((bp->b_flags & B_VMIO) == 0 ||
100738278Swosch			(vmiospace < maxvmiobufspace)) {
1008131626Sdes			--bp->b_usecount;
1009131623Sdes			TAILQ_REMOVE(&bufqueues[QUEUE_LRU], bp, b_freelist);
1010131310Salfred			if (TAILQ_FIRST(&bufqueues[QUEUE_LRU]) != NULL) {
1011131626Sdes				TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
101238278Swosch				goto start;
101338278Swosch			}
1014131626Sdes			TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
101538278Swosch		}
1016131626Sdes	}
1017131626Sdes
101838278Swosch
1019131626Sdes	/* if we are a delayed write, convert to an async write */
1020131626Sdes	if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) {
1021131626Sdes
1022131626Sdes		/*
1023131626Sdes		 * If our delayed write is likely to be used soon, then
1024131626Sdes		 * recycle back onto the LRU queue.
102538278Swosch		 */
1026131626Sdes		if (vp && (bp->b_vp == vp) && (bp->b_qindex == QUEUE_LRU) &&
102738278Swosch			(bp->b_lblkno >= blkno) && (maxsize > 0)) {
1028145073Skeramida
1029145073Skeramida			if (bp->b_usecount > 0) {
1030145073Skeramida				if (bp->b_lblkno < blkno + (MAXPHYS / maxsize)) {
1031145073Skeramida
1032145073Skeramida					TAILQ_REMOVE(&bufqueues[QUEUE_LRU], bp, b_freelist);
1033145073Skeramida
1034145073Skeramida					if (TAILQ_FIRST(&bufqueues[QUEUE_LRU]) != NULL) {
1035145073Skeramida						TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
1036145073Skeramida						bp->b_usecount--;
1037145073Skeramida						goto start;
1038145073Skeramida					}
1039145073Skeramida					TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
1040145073Skeramida				}
1041145073Skeramida			}
1042145073Skeramida		}
1043145073Skeramida
1044145073Skeramida		/*
1045145073Skeramida		 * Certain layered filesystems can recursively re-enter the vfs_bio
1046158280Sbde		 * code, due to delayed writes.  This helps keep the system from
104738278Swosch		 * deadlocking.
1048158280Sbde		 */
1049131829Skeramida		if (writerecursion > 0) {
1050131402Salfred			bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]);
1051131829Skeramida			while (bp) {
1052131829Skeramida				if ((bp->b_flags & B_DELWRI) == 0)
1053131829Skeramida					break;
1054131626Sdes				bp = TAILQ_NEXT(bp, b_freelist);
1055131829Skeramida			}
1056131402Salfred			if (bp == NULL) {
1057131626Sdes				bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]);
1058131626Sdes				while (bp) {
1059131402Salfred					if ((bp->b_flags & B_DELWRI) == 0)
1060131626Sdes						break;
1061131402Salfred					bp = TAILQ_NEXT(bp, b_freelist);
1062131829Skeramida				}
1063131829Skeramida			}
1064131829Skeramida			if (bp == NULL)
1065131829Skeramida				panic("getnewbuf: cannot get buffer, infinite recursion failure");
1066131829Skeramida		} else {
1067131829Skeramida			++writerecursion;
1068131829Skeramida			nbyteswritten += vfs_bio_awrite(bp);
1069131829Skeramida			--writerecursion;
1070131829Skeramida			if (!slpflag && !slptimeo) {
1071133817Salfred				return (0);
1072133817Salfred			}
1073131829Skeramida			goto start;
1074131829Skeramida		}
1075131829Skeramida	}
1076131829Skeramida
1077131829Skeramida	if (bp->b_flags & B_WANTED) {
1078131829Skeramida		bp->b_flags &= ~B_WANTED;
1079131829Skeramida		wakeup(bp);
1080131829Skeramida	}
1081131829Skeramida	bremfree(bp);
1082131829Skeramida	bp->b_flags |= B_BUSY;
1083131829Skeramida
1084133817Salfred	if (bp->b_flags & B_VMIO) {
1085133817Salfred		bp->b_flags &= ~B_ASYNC;
1086131829Skeramida		vfs_vmio_release(bp);
1087131829Skeramida	}
1088131829Skeramida
1089131829Skeramida	if (bp->b_vp)
1090131829Skeramida		brelvp(bp);
1091131829Skeramida
1092131829Skeramidafillbuf:
1093131829Skeramida	bp->b_generation++;
1094131829Skeramida
1095131829Skeramida	/* we are not free, nor do we contain interesting data */
1096131829Skeramida	if (bp->b_rcred != NOCRED) {
1097133817Salfred		crfree(bp->b_rcred);
1098133817Salfred		bp->b_rcred = NOCRED;
1099131829Skeramida	}
1100131829Skeramida	if (bp->b_wcred != NOCRED) {
1101131829Skeramida		crfree(bp->b_wcred);
1102131829Skeramida		bp->b_wcred = NOCRED;
1103133817Salfred	}
1104133817Salfred
1105133817Salfred	LIST_REMOVE(bp, b_hash);
1106133817Salfred	LIST_INSERT_HEAD(&invalhash, bp, b_hash);
1107133817Salfred	if (bp->b_bufsize) {
1108133817Salfred		allocbuf(bp, 0);
1109133817Salfred	}
1110133817Salfred	bp->b_flags = B_BUSY;
1111133817Salfred	bp->b_dev = NODEV;
1112133817Salfred	bp->b_vp = NULL;
1113133817Salfred	bp->b_blkno = bp->b_lblkno = 0;
1114133817Salfred	bp->b_iodone = 0;
1115133817Salfred	bp->b_error = 0;
1116133817Salfred	bp->b_resid = 0;
1117133817Salfred	bp->b_bcount = 0;
1118133817Salfred	bp->b_npages = 0;
1119133817Salfred	bp->b_dirtyoff = bp->b_dirtyend = 0;
1120133817Salfred	bp->b_validoff = bp->b_validend = 0;
1121133817Salfred	bp->b_usecount = 5;
1122133817Salfred
1123133817Salfred	maxsize = (maxsize + PAGE_MASK) & ~PAGE_MASK;
1124133817Salfred
1125133817Salfred	/*
1126133817Salfred	 * we assume that buffer_map is not at address 0
1127133817Salfred	 */
1128131829Skeramida	addr = 0;
1129131829Skeramida	if (maxsize != bp->b_kvasize) {
113024143Sjoerg		bfreekva(bp);
113124143Sjoerg
113224143Sjoergfindkvaspace:
113324143Sjoerg		/*
113424143Sjoerg		 * See if we have buffer kva space
113524143Sjoerg		 */
113624143Sjoerg		if (vm_map_findspace(buffer_map,
113724143Sjoerg			vm_map_min(buffer_map), maxsize, &addr)) {
113824143Sjoerg			if (kvafreespace > 0) {
113924143Sjoerg				int totfree = 0, freed;
1140131310Salfred				do {
1141131622Sdes					freed = 0;
114224143Sjoerg					for (bp1 = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1143131623Sdes						bp1 != NULL; bp1 = TAILQ_NEXT(bp1, b_freelist)) {
1144131623Sdes						if (bp1->b_kvasize != 0) {
1145131623Sdes							totfree += bp1->b_kvasize;
114624143Sjoerg							freed = bp1->b_kvasize;
1147131623Sdes							bremfree(bp1);
1148131623Sdes							bfreekva(bp1);
1149131626Sdes							brelse(bp1);
1150131623Sdes							break;
1151131623Sdes						}
1152131623Sdes					}
115324143Sjoerg				} while (freed);
1154131623Sdes				/*
115524143Sjoerg				 * if we found free space, then retry with the same buffer.
115624143Sjoerg				 */
1157158282Sbde				if (totfree)
1158131622Sdes					goto findkvaspace;
115924143Sjoerg			}
116043053Sdillon			bp->b_flags |= B_INVAL;
116143053Sdillon			brelse(bp);
116243053Sdillon			goto trytofreespace;
116324143Sjoerg		}
116443053Sdillon	}
116543053Sdillon
116624143Sjoerg	/*
116743053Sdillon	 * See if we are below are allocated minimum
116824143Sjoerg	 */
116943053Sdillon	if (bufspace >= (maxbufspace + nbyteswritten)) {
117043697Sdillon		bp->b_flags |= B_INVAL;
1171131623Sdes		brelse(bp);
117224143Sjoerg		goto trytofreespace;
117343053Sdillon	}
117443053Sdillon
117524143Sjoerg	/*
1176131626Sdes	 * create a map entry for the buffer -- in essence
1177131623Sdes	 * reserving the kva space.
117843053Sdillon	 */
1179	if (addr) {
1180		vm_map_insert(buffer_map, NULL, 0,
1181			addr, addr + maxsize,
1182			VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
1183
1184		bp->b_kvabase = (caddr_t) addr;
1185		bp->b_kvasize = maxsize;
1186	}
1187	bp->b_data = bp->b_kvabase;
1188
1189	return (bp);
1190}
1191
1192static void
1193waitfreebuffers(int slpflag, int slptimeo) {
1194	while (numfreebuffers < hifreebuffers) {
1195		flushdirtybuffers(slpflag, slptimeo);
1196		if (numfreebuffers < hifreebuffers)
1197			break;
1198		needsbuffer |= VFS_BIO_NEED_FREE;
1199		if (tsleep(&needsbuffer, PRIBIO|slpflag, "biofre", slptimeo))
1200			break;
1201	}
1202}
1203
1204static void
1205flushdirtybuffers(int slpflag, int slptimeo) {
1206	int s;
1207	static pid_t flushing = 0;
1208
1209	s = splbio();
1210
1211	if (flushing) {
1212		if (flushing == curproc->p_pid) {
1213			splx(s);
1214			return;
1215		}
1216		while (flushing) {
1217			if (tsleep(&flushing, PRIBIO|slpflag, "biofls", slptimeo)) {
1218				splx(s);
1219				return;
1220			}
1221		}
1222	}
1223	flushing = curproc->p_pid;
1224
1225	while (numdirtybuffers > lodirtybuffers) {
1226		struct buf *bp;
1227		needsbuffer |= VFS_BIO_NEED_LOWLIMIT;
1228		bp = TAILQ_FIRST(&bufqueues[QUEUE_AGE]);
1229		if (bp == NULL)
1230			bp = TAILQ_FIRST(&bufqueues[QUEUE_LRU]);
1231
1232		while (bp && ((bp->b_flags & B_DELWRI) == 0)) {
1233			bp = TAILQ_NEXT(bp, b_freelist);
1234		}
1235
1236		if (bp) {
1237			vfs_bio_awrite(bp);
1238			continue;
1239		}
1240		break;
1241	}
1242
1243	flushing = 0;
1244	wakeup(&flushing);
1245	splx(s);
1246}
1247
1248/*
1249 * Check to see if a block is currently memory resident.
1250 */
1251struct buf *
1252incore(struct vnode * vp, daddr_t blkno)
1253{
1254	struct buf *bp;
1255
1256	int s = splbio();
1257	bp = gbincore(vp, blkno);
1258	splx(s);
1259	return (bp);
1260}
1261
1262/*
1263 * Returns true if no I/O is needed to access the
1264 * associated VM object.  This is like incore except
1265 * it also hunts around in the VM system for the data.
1266 */
1267
1268int
1269inmem(struct vnode * vp, daddr_t blkno)
1270{
1271	vm_object_t obj;
1272	vm_offset_t toff, tinc;
1273	vm_page_t m;
1274	vm_ooffset_t off;
1275
1276	if (incore(vp, blkno))
1277		return 1;
1278	if (vp->v_mount == NULL)
1279		return 0;
1280	if ((vp->v_object == NULL) || (vp->v_flag & VOBJBUF) == 0)
1281		return 0;
1282
1283	obj = vp->v_object;
1284	tinc = PAGE_SIZE;
1285	if (tinc > vp->v_mount->mnt_stat.f_iosize)
1286		tinc = vp->v_mount->mnt_stat.f_iosize;
1287	off = blkno * vp->v_mount->mnt_stat.f_iosize;
1288
1289	for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
1290
1291		m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
1292		if (!m)
1293			return 0;
1294		if (vm_page_is_valid(m, (vm_offset_t) (toff + off), tinc) == 0)
1295			return 0;
1296	}
1297	return 1;
1298}
1299
1300/*
1301 * now we set the dirty range for the buffer --
1302 * for NFS -- if the file is mapped and pages have
1303 * been written to, let it know.  We want the
1304 * entire range of the buffer to be marked dirty if
1305 * any of the pages have been written to for consistancy
1306 * with the b_validoff, b_validend set in the nfs write
1307 * code, and used by the nfs read code.
1308 */
1309static void
1310vfs_setdirty(struct buf *bp) {
1311	int i;
1312	vm_object_t object;
1313	vm_offset_t boffset, offset;
1314	/*
1315	 * We qualify the scan for modified pages on whether the
1316	 * object has been flushed yet.  The OBJ_WRITEABLE flag
1317	 * is not cleared simply by protecting pages off.
1318	 */
1319	if ((bp->b_flags & B_VMIO) &&
1320		((object = bp->b_pages[0]->object)->flags & (OBJ_WRITEABLE|OBJ_CLEANING))) {
1321		/*
1322		 * test the pages to see if they have been modified directly
1323		 * by users through the VM system.
1324		 */
1325		for (i = 0; i < bp->b_npages; i++)
1326			vm_page_test_dirty(bp->b_pages[i]);
1327
1328		/*
1329		 * scan forwards for the first page modified
1330		 */
1331		for (i = 0; i < bp->b_npages; i++) {
1332			if (bp->b_pages[i]->dirty) {
1333				break;
1334			}
1335		}
1336		boffset = (i << PAGE_SHIFT);
1337		if (boffset < bp->b_dirtyoff) {
1338			bp->b_dirtyoff = boffset;
1339		}
1340
1341		/*
1342		 * scan backwards for the last page modified
1343		 */
1344		for (i = bp->b_npages - 1; i >= 0; --i) {
1345			if (bp->b_pages[i]->dirty) {
1346				break;
1347			}
1348		}
1349		boffset = (i + 1);
1350		offset = boffset + bp->b_pages[0]->pindex;
1351		if (offset >= object->size)
1352			boffset = object->size - bp->b_pages[0]->pindex;
1353		if (bp->b_dirtyend < (boffset << PAGE_SHIFT))
1354			bp->b_dirtyend = (boffset << PAGE_SHIFT);
1355	}
1356}
1357
1358/*
1359 * Get a block given a specified block and offset into a file/device.
1360 */
1361struct buf *
1362getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo)
1363{
1364	struct buf *bp;
1365	int s;
1366	struct bufhashhdr *bh;
1367	int maxsize;
1368	int generation;
1369
1370	if (vp->v_mount) {
1371		maxsize = vp->v_mount->mnt_stat.f_iosize;
1372		/*
1373		 * This happens on mount points.
1374		 */
1375		if (maxsize < size)
1376			maxsize = size;
1377	} else {
1378		maxsize = size;
1379	}
1380
1381#if !defined(MAX_PERF)
1382	if (size > MAXBSIZE)
1383		panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
1384#endif
1385
1386	s = splbio();
1387loop:
1388	if (numfreebuffers < lofreebuffers) {
1389		waitfreebuffers(slpflag, slptimeo);
1390	}
1391
1392	if ((bp = gbincore(vp, blkno))) {
1393loop1:
1394		generation = bp->b_generation;
1395		if (bp->b_flags & B_BUSY) {
1396			bp->b_flags |= B_WANTED;
1397			if (bp->b_usecount < BUF_MAXUSE)
1398				++bp->b_usecount;
1399			if (!tsleep(bp,
1400				(PRIBIO + 1) | slpflag, "getblk", slptimeo)) {
1401				if (bp->b_generation != generation)
1402					goto loop;
1403				goto loop1;
1404			} else {
1405				splx(s);
1406				return (struct buf *) NULL;
1407			}
1408		}
1409		bp->b_flags |= B_BUSY | B_CACHE;
1410		bremfree(bp);
1411
1412		/*
1413		 * check for size inconsistancies (note that they shouldn't
1414		 * happen but do when filesystems don't handle the size changes
1415		 * correctly.) We are conservative on metadata and don't just
1416		 * extend the buffer but write and re-constitute it.
1417		 */
1418
1419		if (bp->b_bcount != size) {
1420			bp->b_generation++;
1421			if ((bp->b_flags & B_VMIO) && (size <= bp->b_kvasize)) {
1422				allocbuf(bp, size);
1423			} else {
1424				bp->b_flags |= B_NOCACHE;
1425				VOP_BWRITE(bp);
1426				goto loop;
1427			}
1428		}
1429
1430		if (bp->b_usecount < BUF_MAXUSE)
1431			++bp->b_usecount;
1432		splx(s);
1433		return (bp);
1434	} else {
1435		vm_object_t obj;
1436
1437		if ((bp = getnewbuf(vp, blkno,
1438			slpflag, slptimeo, size, maxsize)) == 0) {
1439			if (slpflag || slptimeo) {
1440				splx(s);
1441				return NULL;
1442			}
1443			goto loop;
1444		}
1445
1446		/*
1447		 * This code is used to make sure that a buffer is not
1448		 * created while the getnewbuf routine is blocked.
1449		 * Normally the vnode is locked so this isn't a problem.
1450		 * VBLK type I/O requests, however, don't lock the vnode.
1451		 */
1452		if (!VOP_ISLOCKED(vp) && gbincore(vp, blkno)) {
1453			bp->b_flags |= B_INVAL;
1454			brelse(bp);
1455			goto loop;
1456		}
1457
1458		/*
1459		 * Insert the buffer into the hash, so that it can
1460		 * be found by incore.
1461		 */
1462		bp->b_blkno = bp->b_lblkno = blkno;
1463		bgetvp(vp, bp);
1464		LIST_REMOVE(bp, b_hash);
1465		bh = BUFHASH(vp, blkno);
1466		LIST_INSERT_HEAD(bh, bp, b_hash);
1467
1468		if ((obj = vp->v_object) && (vp->v_flag & VOBJBUF)) {
1469			bp->b_flags |= (B_VMIO | B_CACHE);
1470#if defined(VFS_BIO_DEBUG)
1471			if (vp->v_type != VREG && vp->v_type != VBLK)
1472				printf("getblk: vmioing file type %d???\n", vp->v_type);
1473#endif
1474		} else {
1475			bp->b_flags &= ~B_VMIO;
1476		}
1477		splx(s);
1478
1479		allocbuf(bp, size);
1480#ifdef	PC98
1481		/*
1482		 * 1024byte/sector support
1483		 */
1484#define B_XXX2 0x8000000
1485		if (vp->v_flag & 0x10000) bp->b_flags |= B_XXX2;
1486#endif
1487		return (bp);
1488	}
1489}
1490
1491/*
1492 * Get an empty, disassociated buffer of given size.
1493 */
1494struct buf *
1495geteblk(int size)
1496{
1497	struct buf *bp;
1498	int s;
1499
1500	s = splbio();
1501	while ((bp = getnewbuf(0, (daddr_t) 0, 0, 0, size, MAXBSIZE)) == 0);
1502	splx(s);
1503	allocbuf(bp, size);
1504	bp->b_flags |= B_INVAL;
1505	return (bp);
1506}
1507
1508
1509/*
1510 * This code constitutes the buffer memory from either anonymous system
1511 * memory (in the case of non-VMIO operations) or from an associated
1512 * VM object (in the case of VMIO operations).
1513 *
1514 * Note that this code is tricky, and has many complications to resolve
1515 * deadlock or inconsistant data situations.  Tread lightly!!!
1516 *
1517 * Modify the length of a buffer's underlying buffer storage without
1518 * destroying information (unless, of course the buffer is shrinking).
1519 */
1520int
1521allocbuf(struct buf * bp, int size)
1522{
1523
1524	int s;
1525	int newbsize, mbsize;
1526	int i;
1527
1528#if !defined(MAX_PERF)
1529	if (!(bp->b_flags & B_BUSY))
1530		panic("allocbuf: buffer not busy");
1531
1532	if (bp->b_kvasize < size)
1533		panic("allocbuf: buffer too small");
1534#endif
1535
1536	if ((bp->b_flags & B_VMIO) == 0) {
1537		caddr_t origbuf;
1538		int origbufsize;
1539		/*
1540		 * Just get anonymous memory from the kernel
1541		 */
1542		mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
1543#if !defined(NO_B_MALLOC)
1544		if (bp->b_flags & B_MALLOC)
1545			newbsize = mbsize;
1546		else
1547#endif
1548			newbsize = round_page(size);
1549
1550		if (newbsize < bp->b_bufsize) {
1551#if !defined(NO_B_MALLOC)
1552			/*
1553			 * malloced buffers are not shrunk
1554			 */
1555			if (bp->b_flags & B_MALLOC) {
1556				if (newbsize) {
1557					bp->b_bcount = size;
1558				} else {
1559					free(bp->b_data, M_BIOBUF);
1560					bufspace -= bp->b_bufsize;
1561					bufmallocspace -= bp->b_bufsize;
1562					bp->b_data = bp->b_kvabase;
1563					bp->b_bufsize = 0;
1564					bp->b_bcount = 0;
1565					bp->b_flags &= ~B_MALLOC;
1566				}
1567				return 1;
1568			}
1569#endif
1570			vm_hold_free_pages(
1571			    bp,
1572			    (vm_offset_t) bp->b_data + newbsize,
1573			    (vm_offset_t) bp->b_data + bp->b_bufsize);
1574		} else if (newbsize > bp->b_bufsize) {
1575#if !defined(NO_B_MALLOC)
1576			/*
1577			 * We only use malloced memory on the first allocation.
1578			 * and revert to page-allocated memory when the buffer grows.
1579			 */
1580			if ( (bufmallocspace < maxbufmallocspace) &&
1581				(bp->b_bufsize == 0) &&
1582				(mbsize <= PAGE_SIZE/2)) {
1583
1584				bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
1585				bp->b_bufsize = mbsize;
1586				bp->b_bcount = size;
1587				bp->b_flags |= B_MALLOC;
1588				bufspace += mbsize;
1589				bufmallocspace += mbsize;
1590				return 1;
1591			}
1592#endif
1593			origbuf = NULL;
1594			origbufsize = 0;
1595#if !defined(NO_B_MALLOC)
1596			/*
1597			 * If the buffer is growing on it's other-than-first allocation,
1598			 * then we revert to the page-allocation scheme.
1599			 */
1600			if (bp->b_flags & B_MALLOC) {
1601				origbuf = bp->b_data;
1602				origbufsize = bp->b_bufsize;
1603				bp->b_data = bp->b_kvabase;
1604				bufspace -= bp->b_bufsize;
1605				bufmallocspace -= bp->b_bufsize;
1606				bp->b_bufsize = 0;
1607				bp->b_flags &= ~B_MALLOC;
1608				newbsize = round_page(newbsize);
1609			}
1610#endif
1611			vm_hold_load_pages(
1612			    bp,
1613			    (vm_offset_t) bp->b_data + bp->b_bufsize,
1614			    (vm_offset_t) bp->b_data + newbsize);
1615#if !defined(NO_B_MALLOC)
1616			if (origbuf) {
1617				bcopy(origbuf, bp->b_data, origbufsize);
1618				free(origbuf, M_BIOBUF);
1619			}
1620#endif
1621		}
1622	} else {
1623		vm_page_t m;
1624		int desiredpages;
1625
1626		newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
1627		desiredpages = (round_page(newbsize) >> PAGE_SHIFT);
1628
1629#if !defined(NO_B_MALLOC)
1630		if (bp->b_flags & B_MALLOC)
1631			panic("allocbuf: VMIO buffer can't be malloced");
1632#endif
1633
1634		if (newbsize < bp->b_bufsize) {
1635			if (desiredpages < bp->b_npages) {
1636				for (i = desiredpages; i < bp->b_npages; i++) {
1637					/*
1638					 * the page is not freed here -- it
1639					 * is the responsibility of vnode_pager_setsize
1640					 */
1641					m = bp->b_pages[i];
1642#if defined(DIAGNOSTIC)
1643					if (m == bogus_page)
1644						panic("allocbuf: bogus page found");
1645#endif
1646					s = splvm();
1647					while ((m->flags & PG_BUSY) || (m->busy != 0)) {
1648						m->flags |= PG_WANTED;
1649						tsleep(m, PVM, "biodep", 0);
1650					}
1651					splx(s);
1652
1653					bp->b_pages[i] = NULL;
1654					vm_page_unwire(m);
1655				}
1656				pmap_qremove((vm_offset_t) trunc_page(bp->b_data) +
1657				    (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages));
1658				bp->b_npages = desiredpages;
1659			}
1660		} else if (newbsize > bp->b_bufsize) {
1661			vm_object_t obj;
1662			vm_offset_t tinc, toff;
1663			vm_ooffset_t off;
1664			vm_pindex_t objoff;
1665			int pageindex, curbpnpages;
1666			struct vnode *vp;
1667			int bsize;
1668
1669			vp = bp->b_vp;
1670
1671			if (vp->v_type == VBLK)
1672				bsize = DEV_BSIZE;
1673			else
1674				bsize = vp->v_mount->mnt_stat.f_iosize;
1675
1676			if (bp->b_npages < desiredpages) {
1677				obj = vp->v_object;
1678				tinc = PAGE_SIZE;
1679				if (tinc > bsize)
1680					tinc = bsize;
1681				off = (vm_ooffset_t) bp->b_lblkno * bsize;
1682				curbpnpages = bp->b_npages;
1683		doretry:
1684				bp->b_flags |= B_CACHE;
1685				bp->b_validoff = bp->b_validend = 0;
1686				for (toff = 0; toff < newbsize; toff += tinc) {
1687					int bytesinpage;
1688
1689					pageindex = toff >> PAGE_SHIFT;
1690					objoff = OFF_TO_IDX(off + toff);
1691					if (pageindex < curbpnpages) {
1692
1693						m = bp->b_pages[pageindex];
1694#ifdef VFS_BIO_DIAG
1695						if (m->pindex != objoff)
1696							panic("allocbuf: page changed offset??!!!?");
1697#endif
1698						bytesinpage = tinc;
1699						if (tinc > (newbsize - toff))
1700							bytesinpage = newbsize - toff;
1701						if (bp->b_flags & B_CACHE)
1702							vfs_buf_set_valid(bp, off, toff, bytesinpage, m);
1703						continue;
1704					}
1705					m = vm_page_lookup(obj, objoff);
1706					if (!m) {
1707						m = vm_page_alloc(obj, objoff, VM_ALLOC_NORMAL);
1708						if (!m) {
1709							VM_WAIT;
1710							vm_pageout_deficit += (desiredpages - bp->b_npages);
1711							goto doretry;
1712						}
1713						/*
1714						 * Normally it is unwise to clear PG_BUSY without
1715						 * PAGE_WAKEUP -- but it is okay here, as there is
1716						 * no chance for blocking between here and vm_page_alloc
1717						 */
1718						m->flags &= ~PG_BUSY;
1719						vm_page_wire(m);
1720						bp->b_flags &= ~B_CACHE;
1721					} else if (m->flags & PG_BUSY) {
1722						s = splvm();
1723						if (m->flags & PG_BUSY) {
1724							m->flags |= PG_WANTED;
1725							tsleep(m, PVM, "pgtblk", 0);
1726						}
1727						splx(s);
1728						goto doretry;
1729					} else {
1730						if ((curproc != pageproc) &&
1731							((m->queue - m->pc) == PQ_CACHE) &&
1732						    ((cnt.v_free_count + cnt.v_cache_count) <
1733								(cnt.v_free_min + cnt.v_cache_min))) {
1734							pagedaemon_wakeup();
1735						}
1736						bytesinpage = tinc;
1737						if (tinc > (newbsize - toff))
1738							bytesinpage = newbsize - toff;
1739						if (bp->b_flags & B_CACHE)
1740							vfs_buf_set_valid(bp, off, toff, bytesinpage, m);
1741						vm_page_wire(m);
1742					}
1743					bp->b_pages[pageindex] = m;
1744					curbpnpages = pageindex + 1;
1745				}
1746				if (vp->v_tag == VT_NFS &&
1747				    vp->v_type != VBLK) {
1748					if (bp->b_dirtyend > 0) {
1749						bp->b_validoff = min(bp->b_validoff, bp->b_dirtyoff);
1750						bp->b_validend = max(bp->b_validend, bp->b_dirtyend);
1751					}
1752					if (bp->b_validend == 0)
1753						bp->b_flags &= ~B_CACHE;
1754				}
1755				bp->b_data = (caddr_t) trunc_page(bp->b_data);
1756				bp->b_npages = curbpnpages;
1757				pmap_qenter((vm_offset_t) bp->b_data,
1758					bp->b_pages, bp->b_npages);
1759				((vm_offset_t) bp->b_data) |= off & PAGE_MASK;
1760			}
1761		}
1762	}
1763	if (bp->b_flags & B_VMIO)
1764		vmiospace += (newbsize - bp->b_bufsize);
1765	bufspace += (newbsize - bp->b_bufsize);
1766	bp->b_bufsize = newbsize;
1767	bp->b_bcount = size;
1768	return 1;
1769}
1770
1771/*
1772 * Wait for buffer I/O completion, returning error status.
1773 */
1774int
1775biowait(register struct buf * bp)
1776{
1777	int s;
1778
1779	s = splbio();
1780	while ((bp->b_flags & B_DONE) == 0)
1781#if defined(NO_SCHEDULE_MODS)
1782		tsleep(bp, PRIBIO, "biowait", 0);
1783#else
1784		if (bp->b_flags & B_READ)
1785			tsleep(bp, PRIBIO, "biord", 0);
1786		else
1787			tsleep(bp, curproc->p_usrpri, "biowr", 0);
1788#endif
1789	splx(s);
1790	if (bp->b_flags & B_EINTR) {
1791		bp->b_flags &= ~B_EINTR;
1792		return (EINTR);
1793	}
1794	if (bp->b_flags & B_ERROR) {
1795		return (bp->b_error ? bp->b_error : EIO);
1796	} else {
1797		return (0);
1798	}
1799}
1800
1801/*
1802 * Finish I/O on a buffer, calling an optional function.
1803 * This is usually called from interrupt level, so process blocking
1804 * is not *a good idea*.
1805 */
1806void
1807biodone(register struct buf * bp)
1808{
1809	int s;
1810
1811	s = splbio();
1812
1813#if !defined(MAX_PERF)
1814	if (!(bp->b_flags & B_BUSY))
1815		panic("biodone: buffer not busy");
1816#endif
1817
1818	if (bp->b_flags & B_DONE) {
1819		splx(s);
1820#if !defined(MAX_PERF)
1821		printf("biodone: buffer already done\n");
1822#endif
1823		return;
1824	}
1825	bp->b_flags |= B_DONE;
1826
1827	if ((bp->b_flags & B_READ) == 0) {
1828		vwakeup(bp);
1829	}
1830#ifdef BOUNCE_BUFFERS
1831	if (bp->b_flags & B_BOUNCE)
1832		vm_bounce_free(bp);
1833#endif
1834
1835	/* call optional completion function if requested */
1836	if (bp->b_flags & B_CALL) {
1837		bp->b_flags &= ~B_CALL;
1838		(*bp->b_iodone) (bp);
1839		splx(s);
1840		return;
1841	}
1842	if (bp->b_flags & B_VMIO) {
1843		int i, resid;
1844		vm_ooffset_t foff;
1845		vm_page_t m;
1846		vm_object_t obj;
1847		int iosize;
1848		struct vnode *vp = bp->b_vp;
1849
1850		obj = vp->v_object;
1851
1852#if defined(VFS_BIO_DEBUG)
1853		if (vp->v_usecount == 0) {
1854			panic("biodone: zero vnode ref count");
1855		}
1856
1857		if (vp->v_object == NULL) {
1858			panic("biodone: missing VM object");
1859		}
1860
1861		if ((vp->v_flag & VOBJBUF) == 0) {
1862			panic("biodone: vnode is not setup for merged cache");
1863		}
1864#endif
1865
1866		if (vp->v_type == VBLK)
1867			foff = (vm_ooffset_t) DEV_BSIZE * bp->b_lblkno;
1868		else
1869			foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
1870#if !defined(MAX_PERF)
1871		if (!obj) {
1872			panic("biodone: no object");
1873		}
1874#endif
1875#if defined(VFS_BIO_DEBUG)
1876		if (obj->paging_in_progress < bp->b_npages) {
1877			printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
1878			    obj->paging_in_progress, bp->b_npages);
1879		}
1880#endif
1881		iosize = bp->b_bufsize;
1882		for (i = 0; i < bp->b_npages; i++) {
1883			int bogusflag = 0;
1884			m = bp->b_pages[i];
1885			if (m == bogus_page) {
1886				bogusflag = 1;
1887				m = vm_page_lookup(obj, OFF_TO_IDX(foff));
1888				if (!m) {
1889#if defined(VFS_BIO_DEBUG)
1890					printf("biodone: page disappeared\n");
1891#endif
1892					--obj->paging_in_progress;
1893					continue;
1894				}
1895				bp->b_pages[i] = m;
1896				pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
1897			}
1898#if defined(VFS_BIO_DEBUG)
1899			if (OFF_TO_IDX(foff) != m->pindex) {
1900				printf("biodone: foff(%d)/m->pindex(%d) mismatch\n", foff, m->pindex);
1901			}
1902#endif
1903			resid = IDX_TO_OFF(m->pindex + 1) - foff;
1904			if (resid > iosize)
1905				resid = iosize;
1906			/*
1907			 * In the write case, the valid and clean bits are
1908			 * already changed correctly, so we only need to do this
1909			 * here in the read case.
1910			 */
1911			if ((bp->b_flags & B_READ) && !bogusflag && resid > 0) {
1912				vfs_page_set_valid(bp, foff, i, m);
1913			}
1914
1915			/*
1916			 * when debugging new filesystems or buffer I/O methods, this
1917			 * is the most common error that pops up.  if you see this, you
1918			 * have not set the page busy flag correctly!!!
1919			 */
1920			if (m->busy == 0) {
1921#if !defined(MAX_PERF)
1922				printf("biodone: page busy < 0, "
1923				    "pindex: %d, foff: 0x(%x,%x), "
1924				    "resid: %d, index: %d\n",
1925				    (int) m->pindex, (int)(foff >> 32),
1926						(int) foff & 0xffffffff, resid, i);
1927#endif
1928				if (vp->v_type != VBLK)
1929#if !defined(MAX_PERF)
1930					printf(" iosize: %ld, lblkno: %d, flags: 0x%lx, npages: %d\n",
1931					    bp->b_vp->v_mount->mnt_stat.f_iosize,
1932					    (int) bp->b_lblkno,
1933					    bp->b_flags, bp->b_npages);
1934				else
1935					printf(" VDEV, lblkno: %d, flags: 0x%lx, npages: %d\n",
1936					    (int) bp->b_lblkno,
1937					    bp->b_flags, bp->b_npages);
1938				printf(" valid: 0x%x, dirty: 0x%x, wired: %d\n",
1939				    m->valid, m->dirty, m->wire_count);
1940#endif
1941				panic("biodone: page busy < 0\n");
1942			}
1943			--m->busy;
1944			if ((m->busy == 0) && (m->flags & PG_WANTED)) {
1945				m->flags &= ~PG_WANTED;
1946				wakeup(m);
1947			}
1948			--obj->paging_in_progress;
1949			foff += resid;
1950			iosize -= resid;
1951		}
1952		if (obj && obj->paging_in_progress == 0 &&
1953		    (obj->flags & OBJ_PIPWNT)) {
1954			obj->flags &= ~OBJ_PIPWNT;
1955			wakeup(obj);
1956		}
1957	}
1958	/*
1959	 * For asynchronous completions, release the buffer now. The brelse
1960	 * checks for B_WANTED and will do the wakeup there if necessary - so
1961	 * no need to do a wakeup here in the async case.
1962	 */
1963
1964	if (bp->b_flags & B_ASYNC) {
1965		if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR | B_RELBUF)) != 0)
1966			brelse(bp);
1967		else
1968			bqrelse(bp);
1969	} else {
1970		bp->b_flags &= ~B_WANTED;
1971		wakeup(bp);
1972	}
1973	splx(s);
1974}
1975
1976static int
1977count_lock_queue()
1978{
1979	int count;
1980	struct buf *bp;
1981
1982	count = 0;
1983	for (bp = TAILQ_FIRST(&bufqueues[QUEUE_LOCKED]);
1984	    bp != NULL;
1985	    bp = TAILQ_NEXT(bp, b_freelist))
1986		count++;
1987	return (count);
1988}
1989
1990static int vfs_update_interval = 30;
1991
1992static void
1993vfs_update()
1994{
1995	while (1) {
1996		tsleep(&vfs_update_wakeup, PUSER, "update",
1997		    hz * vfs_update_interval);
1998		vfs_update_wakeup = 0;
1999		sync(curproc, NULL);
2000	}
2001}
2002
2003static int
2004sysctl_kern_updateinterval SYSCTL_HANDLER_ARGS
2005{
2006	int error = sysctl_handle_int(oidp,
2007		oidp->oid_arg1, oidp->oid_arg2, req);
2008	if (!error)
2009		wakeup(&vfs_update_wakeup);
2010	return error;
2011}
2012
2013SYSCTL_PROC(_kern, KERN_UPDATEINTERVAL, update, CTLTYPE_INT|CTLFLAG_RW,
2014	&vfs_update_interval, 0, sysctl_kern_updateinterval, "I", "");
2015
2016
2017/*
2018 * This routine is called in lieu of iodone in the case of
2019 * incomplete I/O.  This keeps the busy status for pages
2020 * consistant.
2021 */
2022void
2023vfs_unbusy_pages(struct buf * bp)
2024{
2025	int i;
2026
2027	if (bp->b_flags & B_VMIO) {
2028		struct vnode *vp = bp->b_vp;
2029		vm_object_t obj = vp->v_object;
2030		vm_ooffset_t foff;
2031
2032		foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
2033
2034		for (i = 0; i < bp->b_npages; i++) {
2035			vm_page_t m = bp->b_pages[i];
2036
2037			if (m == bogus_page) {
2038				m = vm_page_lookup(obj, OFF_TO_IDX(foff) + i);
2039#if !defined(MAX_PERF)
2040				if (!m) {
2041					panic("vfs_unbusy_pages: page missing\n");
2042				}
2043#endif
2044				bp->b_pages[i] = m;
2045				pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
2046			}
2047			--obj->paging_in_progress;
2048			--m->busy;
2049			if ((m->busy == 0) && (m->flags & PG_WANTED)) {
2050				m->flags &= ~PG_WANTED;
2051				wakeup(m);
2052			}
2053		}
2054		if (obj->paging_in_progress == 0 &&
2055		    (obj->flags & OBJ_PIPWNT)) {
2056			obj->flags &= ~OBJ_PIPWNT;
2057			wakeup(obj);
2058		}
2059	}
2060}
2061
2062/*
2063 * Set NFS' b_validoff and b_validend fields from the valid bits
2064 * of a page.  If the consumer is not NFS, and the page is not
2065 * valid for the entire range, clear the B_CACHE flag to force
2066 * the consumer to re-read the page.
2067 */
2068static void
2069vfs_buf_set_valid(struct buf *bp,
2070		  vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
2071		  vm_page_t m)
2072{
2073	if (bp->b_vp->v_tag == VT_NFS && bp->b_vp->v_type != VBLK) {
2074		vm_offset_t svalid, evalid;
2075		int validbits = m->valid;
2076
2077		/*
2078		 * This only bothers with the first valid range in the
2079		 * page.
2080		 */
2081		svalid = off;
2082		while (validbits && !(validbits & 1)) {
2083			svalid += DEV_BSIZE;
2084			validbits >>= 1;
2085		}
2086		evalid = svalid;
2087		while (validbits & 1) {
2088			evalid += DEV_BSIZE;
2089			validbits >>= 1;
2090		}
2091		/*
2092		 * Make sure this range is contiguous with the range
2093		 * built up from previous pages.  If not, then we will
2094		 * just use the range from the previous pages.
2095		 */
2096		if (svalid == bp->b_validend) {
2097			bp->b_validoff = min(bp->b_validoff, svalid);
2098			bp->b_validend = max(bp->b_validend, evalid);
2099		}
2100	} else if (!vm_page_is_valid(m,
2101				     (vm_offset_t) ((foff + off) & PAGE_MASK),
2102				     size)) {
2103		bp->b_flags &= ~B_CACHE;
2104	}
2105}
2106
2107/*
2108 * Set the valid bits in a page, taking care of the b_validoff,
2109 * b_validend fields which NFS uses to optimise small reads.  Off is
2110 * the offset within the file and pageno is the page index within the buf.
2111 */
2112static void
2113vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m)
2114{
2115	struct vnode *vp = bp->b_vp;
2116	vm_ooffset_t soff, eoff;
2117
2118	soff = off;
2119	eoff = off + min(PAGE_SIZE, bp->b_bufsize);
2120	vm_page_set_invalid(m,
2121			    (vm_offset_t) (soff & PAGE_MASK),
2122			    (vm_offset_t) (eoff - soff));
2123	if (vp->v_tag == VT_NFS && vp->v_type != VBLK) {
2124		vm_ooffset_t sv, ev;
2125		off = off - pageno * PAGE_SIZE;
2126		sv = off + ((bp->b_validoff + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1));
2127		ev = off + (bp->b_validend & ~(DEV_BSIZE - 1));
2128		soff = max(sv, soff);
2129		eoff = min(ev, eoff);
2130	}
2131	if (eoff > soff)
2132		vm_page_set_validclean(m,
2133				       (vm_offset_t) (soff & PAGE_MASK),
2134				       (vm_offset_t) (eoff - soff));
2135}
2136
2137/*
2138 * This routine is called before a device strategy routine.
2139 * It is used to tell the VM system that paging I/O is in
2140 * progress, and treat the pages associated with the buffer
2141 * almost as being PG_BUSY.  Also the object paging_in_progress
2142 * flag is handled to make sure that the object doesn't become
2143 * inconsistant.
2144 */
2145void
2146vfs_busy_pages(struct buf * bp, int clear_modify)
2147{
2148	int i,s;
2149
2150	if (bp->b_flags & B_VMIO) {
2151		struct vnode *vp = bp->b_vp;
2152		vm_object_t obj = vp->v_object;
2153		vm_ooffset_t foff;
2154
2155		if (vp->v_type == VBLK)
2156			foff = (vm_ooffset_t) DEV_BSIZE * bp->b_lblkno;
2157		else
2158			foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
2159
2160		vfs_setdirty(bp);
2161
2162retry:
2163		for (i = 0; i < bp->b_npages; i++) {
2164			vm_page_t m = bp->b_pages[i];
2165
2166			if (m && (m->flags & PG_BUSY)) {
2167				s = splvm();
2168				while (m->flags & PG_BUSY) {
2169					m->flags |= PG_WANTED;
2170					tsleep(m, PVM, "vbpage", 0);
2171				}
2172				splx(s);
2173				goto retry;
2174			}
2175		}
2176
2177		for (i = 0; i < bp->b_npages; i++, foff += PAGE_SIZE) {
2178			vm_page_t m = bp->b_pages[i];
2179
2180			if ((bp->b_flags & B_CLUSTER) == 0) {
2181				obj->paging_in_progress++;
2182				m->busy++;
2183			}
2184
2185			vm_page_protect(m, VM_PROT_NONE);
2186			if (clear_modify)
2187				vfs_page_set_valid(bp, foff, i, m);
2188			else if (bp->b_bcount >= PAGE_SIZE) {
2189				if (m->valid && (bp->b_flags & B_CACHE) == 0) {
2190					bp->b_pages[i] = bogus_page;
2191					pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
2192				}
2193			}
2194		}
2195	}
2196}
2197
2198/*
2199 * Tell the VM system that the pages associated with this buffer
2200 * are clean.  This is used for delayed writes where the data is
2201 * going to go to disk eventually without additional VM intevention.
2202 */
2203void
2204vfs_clean_pages(struct buf * bp)
2205{
2206	int i;
2207
2208	if (bp->b_flags & B_VMIO) {
2209		struct vnode *vp = bp->b_vp;
2210		vm_ooffset_t foff;
2211
2212		if (vp->v_type == VBLK)
2213			foff = (vm_ooffset_t) DEV_BSIZE * bp->b_lblkno;
2214		else
2215			foff = (vm_ooffset_t) vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
2216		for (i = 0; i < bp->b_npages; i++, foff += PAGE_SIZE) {
2217			vm_page_t m = bp->b_pages[i];
2218
2219			vfs_page_set_valid(bp, foff, i, m);
2220		}
2221	}
2222}
2223
2224void
2225vfs_bio_clrbuf(struct buf *bp) {
2226	int i;
2227	if( bp->b_flags & B_VMIO) {
2228		if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE)) {
2229			int mask;
2230			mask = 0;
2231			for(i=0;i<bp->b_bufsize;i+=DEV_BSIZE)
2232				mask |= (1 << (i/DEV_BSIZE));
2233			if( bp->b_pages[0]->valid != mask) {
2234				bzero(bp->b_data, bp->b_bufsize);
2235			}
2236			bp->b_pages[0]->valid = mask;
2237			bp->b_resid = 0;
2238			return;
2239		}
2240		for(i=0;i<bp->b_npages;i++) {
2241			if( bp->b_pages[i]->valid == VM_PAGE_BITS_ALL)
2242				continue;
2243			if( bp->b_pages[i]->valid == 0) {
2244				if ((bp->b_pages[i]->flags & PG_ZERO) == 0) {
2245					bzero(bp->b_data + (i << PAGE_SHIFT), PAGE_SIZE);
2246				}
2247			} else {
2248				int j;
2249				for(j=0;j<PAGE_SIZE/DEV_BSIZE;j++) {
2250					if( (bp->b_pages[i]->valid & (1<<j)) == 0)
2251						bzero(bp->b_data + (i << PAGE_SHIFT) + j * DEV_BSIZE, DEV_BSIZE);
2252				}
2253			}
2254			/* bp->b_pages[i]->valid = VM_PAGE_BITS_ALL; */
2255		}
2256		bp->b_resid = 0;
2257	} else {
2258		clrbuf(bp);
2259	}
2260}
2261
2262/*
2263 * vm_hold_load_pages and vm_hold_unload pages get pages into
2264 * a buffers address space.  The pages are anonymous and are
2265 * not associated with a file object.
2266 */
2267void
2268vm_hold_load_pages(struct buf * bp, vm_offset_t from, vm_offset_t to)
2269{
2270	vm_offset_t pg;
2271	vm_page_t p;
2272	int index;
2273
2274	to = round_page(to);
2275	from = round_page(from);
2276	index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT;
2277
2278	for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
2279
2280tryagain:
2281
2282		p = vm_page_alloc(kernel_object,
2283			((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT),
2284		    VM_ALLOC_NORMAL);
2285		if (!p) {
2286			vm_pageout_deficit += (to - from) >> PAGE_SHIFT;
2287			VM_WAIT;
2288			goto tryagain;
2289		}
2290		vm_page_wire(p);
2291		pmap_kenter(pg, VM_PAGE_TO_PHYS(p));
2292		bp->b_pages[index] = p;
2293		PAGE_WAKEUP(p);
2294	}
2295	bp->b_npages = index;
2296}
2297
2298void
2299vm_hold_free_pages(struct buf * bp, vm_offset_t from, vm_offset_t to)
2300{
2301	vm_offset_t pg;
2302	vm_page_t p;
2303	int index, newnpages;
2304
2305	from = round_page(from);
2306	to = round_page(to);
2307	newnpages = index = (from - trunc_page(bp->b_data)) >> PAGE_SHIFT;
2308
2309	for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
2310		p = bp->b_pages[index];
2311		if (p && (index < bp->b_npages)) {
2312#if !defined(MAX_PERF)
2313			if (p->busy) {
2314				printf("vm_hold_free_pages: blkno: %d, lblkno: %d\n",
2315					bp->b_blkno, bp->b_lblkno);
2316			}
2317#endif
2318			bp->b_pages[index] = NULL;
2319			pmap_kremove(pg);
2320			p->flags |= PG_BUSY;
2321			vm_page_unwire(p);
2322			vm_page_free(p);
2323		}
2324	}
2325	bp->b_npages = newnpages;
2326}
2327
2328
2329#include "opt_ddb.h"
2330#ifdef DDB
2331#include <ddb/ddb.h>
2332
2333DB_SHOW_COMMAND(buffer, db_show_buffer)
2334{
2335	/* get args */
2336	struct buf *bp = (struct buf *)addr;
2337
2338	if (!have_addr) {
2339		db_printf("usage: show buffer <addr>\n");
2340		return;
2341	}
2342
2343	db_printf("b_proc = %p,\nb_flags = 0x%b\n", (void *)bp->b_proc,
2344		  bp->b_flags, "\20\40bounce\37cluster\36vmio\35ram\34ordered"
2345		  "\33paging\32xxx\31writeinprog\30wanted\27relbuf\26tape"
2346		  "\25read\24raw\23phys\22clusterok\21malloc\20nocache"
2347		  "\17locked\16inval\15gathered\14error\13eintr\12done\11dirty"
2348		  "\10delwri\7call\6cache\5busy\4bad\3async\2needcommit\1age");
2349	db_printf("b_error = %d, b_bufsize = %ld, b_bcount = %ld, "
2350		  "b_resid = %ld\nb_dev = 0x%x, b_data = %p, "
2351		  "b_blkno = %d, b_pblkno = %d\n",
2352		  bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
2353		  bp->b_dev, bp->b_data, bp->b_blkno, bp->b_pblkno);
2354	if (bp->b_npages) {
2355		int i;
2356		db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
2357		for (i = 0; i < bp->b_npages; i++) {
2358			vm_page_t m;
2359			m = bp->b_pages[i];
2360			db_printf("(0x%x, 0x%x, 0x%x)", m->object, m->pindex,
2361				VM_PAGE_TO_PHYS(m));
2362			if ((i + 1) < bp->b_npages)
2363				db_printf(",");
2364		}
2365		db_printf("\n");
2366	}
2367}
2368#endif /* DDB */
2369