vfs_bio.c revision 65557
1/*
2 * Copyright (c) 1994,1997 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice immediately at the beginning of the file, without modification,
10 *    this list of conditions, and the following disclaimer.
11 * 2. Absolutely no warranty of function or purpose is made by the author
12 *		John S. Dyson.
13 *
14 * $FreeBSD: head/sys/kern/vfs_bio.c 65557 2000-09-07 01:33:02Z jasone $
15 */
16
17/*
18 * this file contains a new buffer I/O scheme implementing a coherent
19 * VM object and buffer cache scheme.  Pains have been taken to make
20 * sure that the performance degradation associated with schemes such
21 * as this is not realized.
22 *
23 * Author:  John S. Dyson
24 * Significant help during the development and debugging phases
25 * had been provided by David Greenman, also of the FreeBSD core team.
26 *
27 * see man buf(9) for more info.
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bio.h>
33#include <sys/buf.h>
34#include <sys/eventhandler.h>
35#include <sys/lock.h>
36#include <sys/malloc.h>
37#include <sys/mount.h>
38#include <sys/kernel.h>
39#include <sys/kthread.h>
40#include <sys/ktr.h>
41#include <sys/proc.h>
42#include <sys/reboot.h>
43#include <sys/resourcevar.h>
44#include <sys/sysctl.h>
45#include <sys/vmmeter.h>
46#include <sys/vnode.h>
47#include <vm/vm.h>
48#include <vm/vm_param.h>
49#include <vm/vm_kern.h>
50#include <vm/vm_pageout.h>
51#include <vm/vm_page.h>
52#include <vm/vm_object.h>
53#include <vm/vm_extern.h>
54#include <vm/vm_map.h>
55
56#include <machine/mutex.h>
57
58static MALLOC_DEFINE(M_BIOBUF, "BIO buffer", "BIO buffer");
59
60struct	bio_ops bioops;		/* I/O operation notification */
61
62struct buf *buf;		/* buffer header pool */
63struct swqueue bswlist;
64struct simplelock buftimelock;	/* Interlock on setting prio and timo */
65
66static void vm_hold_free_pages(struct buf * bp, vm_offset_t from,
67		vm_offset_t to);
68static void vm_hold_load_pages(struct buf * bp, vm_offset_t from,
69		vm_offset_t to);
70static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off,
71			       int pageno, vm_page_t m);
72static void vfs_clean_pages(struct buf * bp);
73static void vfs_setdirty(struct buf *bp);
74static void vfs_vmio_release(struct buf *bp);
75static void vfs_backgroundwritedone(struct buf *bp);
76static int flushbufqueues(void);
77
78static int bd_request;
79
80static void buf_daemon __P((void));
81/*
82 * bogus page -- for I/O to/from partially complete buffers
83 * this is a temporary solution to the problem, but it is not
84 * really that bad.  it would be better to split the buffer
85 * for input in the case of buffers partially already in memory,
86 * but the code is intricate enough already.
87 */
88vm_page_t bogus_page;
89int runningbufspace;
90int vmiodirenable = FALSE;
91static vm_offset_t bogus_offset;
92
93static int bufspace, maxbufspace,
94	bufmallocspace, maxbufmallocspace, lobufspace, hibufspace;
95static int bufreusecnt, bufdefragcnt, buffreekvacnt;
96static int maxbdrun;
97static int needsbuffer;
98static int numdirtybuffers, hidirtybuffers;
99static int numfreebuffers, lofreebuffers, hifreebuffers;
100static int getnewbufcalls;
101static int getnewbufrestarts;
102
103SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD,
104	&numdirtybuffers, 0, "");
105SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW,
106	&hidirtybuffers, 0, "");
107SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD,
108	&numfreebuffers, 0, "");
109SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW,
110	&lofreebuffers, 0, "");
111SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW,
112	&hifreebuffers, 0, "");
113SYSCTL_INT(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD,
114	&runningbufspace, 0, "");
115SYSCTL_INT(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD,
116	&maxbufspace, 0, "");
117SYSCTL_INT(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD,
118	&hibufspace, 0, "");
119SYSCTL_INT(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD,
120	&lobufspace, 0, "");
121SYSCTL_INT(_vfs, OID_AUTO, bufspace, CTLFLAG_RD,
122	&bufspace, 0, "");
123SYSCTL_INT(_vfs, OID_AUTO, maxbdrun, CTLFLAG_RW,
124	&maxbdrun, 0, "");
125SYSCTL_INT(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW,
126	&maxbufmallocspace, 0, "");
127SYSCTL_INT(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD,
128	&bufmallocspace, 0, "");
129SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW,
130	&getnewbufcalls, 0, "");
131SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW,
132	&getnewbufrestarts, 0, "");
133SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW,
134	&vmiodirenable, 0, "");
135SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW,
136	&bufdefragcnt, 0, "");
137SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW,
138	&buffreekvacnt, 0, "");
139SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW,
140	&bufreusecnt, 0, "");
141
142static int bufhashmask;
143static LIST_HEAD(bufhashhdr, buf) *bufhashtbl, invalhash;
144struct bqueues bufqueues[BUFFER_QUEUES] = { { 0 } };
145char *buf_wmesg = BUF_WMESG;
146
147extern int vm_swap_size;
148
149#define VFS_BIO_NEED_ANY	0x01	/* any freeable buffer */
150#define VFS_BIO_NEED_DIRTYFLUSH	0x02	/* waiting for dirty buffer flush */
151#define VFS_BIO_NEED_FREE	0x04	/* wait for free bufs, hi hysteresis */
152#define VFS_BIO_NEED_BUFSPACE	0x08	/* wait for buf space, lo hysteresis */
153
154/*
155 * Buffer hash table code.  Note that the logical block scans linearly, which
156 * gives us some L1 cache locality.
157 */
158
159static __inline
160struct bufhashhdr *
161bufhash(struct vnode *vnp, daddr_t bn)
162{
163	return(&bufhashtbl[(((uintptr_t)(vnp) >> 7) + (int)bn) & bufhashmask]);
164}
165
166/*
167 *	numdirtywakeup:
168 *
169 *	If someone is blocked due to there being too many dirty buffers,
170 *	and numdirtybuffers is now reasonable, wake them up.
171 */
172
173static __inline void
174numdirtywakeup(void)
175{
176	if (numdirtybuffers < hidirtybuffers) {
177		if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
178			needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
179			wakeup(&needsbuffer);
180		}
181	}
182}
183
184/*
185 *	bufspacewakeup:
186 *
187 *	Called when buffer space is potentially available for recovery.
188 *	getnewbuf() will block on this flag when it is unable to free
189 *	sufficient buffer space.  Buffer space becomes recoverable when
190 *	bp's get placed back in the queues.
191 */
192
193static __inline void
194bufspacewakeup(void)
195{
196	/*
197	 * If someone is waiting for BUF space, wake them up.  Even
198	 * though we haven't freed the kva space yet, the waiting
199	 * process will be able to now.
200	 */
201	if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
202		needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
203		wakeup(&needsbuffer);
204	}
205}
206
207/*
208 *	bufcountwakeup:
209 *
210 *	Called when a buffer has been added to one of the free queues to
211 *	account for the buffer and to wakeup anyone waiting for free buffers.
212 *	This typically occurs when large amounts of metadata are being handled
213 *	by the buffer cache ( else buffer space runs out first, usually ).
214 */
215
216static __inline void
217bufcountwakeup(void)
218{
219	++numfreebuffers;
220	if (needsbuffer) {
221		needsbuffer &= ~VFS_BIO_NEED_ANY;
222		if (numfreebuffers >= hifreebuffers)
223			needsbuffer &= ~VFS_BIO_NEED_FREE;
224		wakeup(&needsbuffer);
225	}
226}
227
228/*
229 *	vfs_buf_test_cache:
230 *
231 *	Called when a buffer is extended.  This function clears the B_CACHE
232 *	bit if the newly extended portion of the buffer does not contain
233 *	valid data.
234 */
235static __inline__
236void
237vfs_buf_test_cache(struct buf *bp,
238		  vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
239		  vm_page_t m)
240{
241	if (bp->b_flags & B_CACHE) {
242		int base = (foff + off) & PAGE_MASK;
243		if (vm_page_is_valid(m, base, size) == 0)
244			bp->b_flags &= ~B_CACHE;
245	}
246}
247
248static __inline__
249void
250bd_wakeup(int dirtybuflevel)
251{
252	if (numdirtybuffers >= dirtybuflevel && bd_request == 0) {
253		bd_request = 1;
254		wakeup(&bd_request);
255	}
256}
257
258/*
259 * bd_speedup - speedup the buffer cache flushing code
260 */
261
262static __inline__
263void
264bd_speedup(void)
265{
266	bd_wakeup(1);
267}
268
269/*
270 * Initialize buffer headers and related structures.
271 */
272
273caddr_t
274bufhashinit(caddr_t vaddr)
275{
276	/* first, make a null hash table */
277	for (bufhashmask = 8; bufhashmask < nbuf / 4; bufhashmask <<= 1)
278		;
279	bufhashtbl = (void *)vaddr;
280	vaddr = vaddr + sizeof(*bufhashtbl) * bufhashmask;
281	--bufhashmask;
282	return(vaddr);
283}
284
285void
286bufinit(void)
287{
288	struct buf *bp;
289	int i;
290
291	TAILQ_INIT(&bswlist);
292	LIST_INIT(&invalhash);
293	simple_lock_init(&buftimelock);
294
295	for (i = 0; i <= bufhashmask; i++)
296		LIST_INIT(&bufhashtbl[i]);
297
298	/* next, make a null set of free lists */
299	for (i = 0; i < BUFFER_QUEUES; i++)
300		TAILQ_INIT(&bufqueues[i]);
301
302	/* finally, initialize each buffer header and stick on empty q */
303	for (i = 0; i < nbuf; i++) {
304		bp = &buf[i];
305		bzero(bp, sizeof *bp);
306		bp->b_flags = B_INVAL;	/* we're just an empty header */
307		bp->b_dev = NODEV;
308		bp->b_rcred = NOCRED;
309		bp->b_wcred = NOCRED;
310		bp->b_qindex = QUEUE_EMPTY;
311		bp->b_xflags = 0;
312		LIST_INIT(&bp->b_dep);
313		BUF_LOCKINIT(bp);
314		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
315		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
316	}
317
318	/*
319	 * maxbufspace is the absolute maximum amount of buffer space we are
320	 * allowed to reserve in KVM and in real terms.  The absolute maximum
321	 * is nominally used by buf_daemon.  hibufspace is the nominal maximum
322	 * used by most other processes.  The differential is required to
323	 * ensure that buf_daemon is able to run when other processes might
324	 * be blocked waiting for buffer space.
325	 *
326	 * maxbufspace is based on BKVASIZE.  Allocating buffers larger then
327	 * this may result in KVM fragmentation which is not handled optimally
328	 * by the system.
329	 */
330	maxbufspace = nbuf * BKVASIZE;
331	hibufspace = imax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
332	lobufspace = hibufspace - MAXBSIZE;
333
334/*
335 * Limit the amount of malloc memory since it is wired permanently into
336 * the kernel space.  Even though this is accounted for in the buffer
337 * allocation, we don't want the malloced region to grow uncontrolled.
338 * The malloc scheme improves memory utilization significantly on average
339 * (small) directories.
340 */
341	maxbufmallocspace = hibufspace / 20;
342
343/*
344 * Reduce the chance of a deadlock occuring by limiting the number
345 * of delayed-write dirty buffers we allow to stack up.
346 */
347	hidirtybuffers = nbuf / 4 + 20;
348	numdirtybuffers = 0;
349/*
350 * To support extreme low-memory systems, make sure hidirtybuffers cannot
351 * eat up all available buffer space.  This occurs when our minimum cannot
352 * be met.  We try to size hidirtybuffers to 3/4 our buffer space assuming
353 * BKVASIZE'd (8K) buffers.
354 */
355	while (hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
356		hidirtybuffers >>= 1;
357	}
358
359/*
360 * Try to keep the number of free buffers in the specified range,
361 * and give special processes (e.g. like buf_daemon) access to an
362 * emergency reserve.
363 */
364	lofreebuffers = nbuf / 18 + 5;
365	hifreebuffers = 2 * lofreebuffers;
366	numfreebuffers = nbuf;
367
368/*
369 * Maximum number of async ops initiated per buf_daemon loop.  This is
370 * somewhat of a hack at the moment, we really need to limit ourselves
371 * based on the number of bytes of I/O in-transit that were initiated
372 * from buf_daemon.
373 */
374	if ((maxbdrun = nswbuf / 4) < 4)
375		maxbdrun = 4;
376
377	bogus_offset = kmem_alloc_pageable(kernel_map, PAGE_SIZE);
378	bogus_page = vm_page_alloc(kernel_object,
379			((bogus_offset - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT),
380			VM_ALLOC_NORMAL);
381	cnt.v_wire_count++;
382
383}
384
385/*
386 * bfreekva() - free the kva allocation for a buffer.
387 *
388 *	Must be called at splbio() or higher as this is the only locking for
389 *	buffer_map.
390 *
391 *	Since this call frees up buffer space, we call bufspacewakeup().
392 */
393static void
394bfreekva(struct buf * bp)
395{
396	if (bp->b_kvasize) {
397		++buffreekvacnt;
398		bufspace -= bp->b_kvasize;
399		vm_map_delete(buffer_map,
400		    (vm_offset_t) bp->b_kvabase,
401		    (vm_offset_t) bp->b_kvabase + bp->b_kvasize
402		);
403		bp->b_kvasize = 0;
404		bufspacewakeup();
405	}
406}
407
408/*
409 *	bremfree:
410 *
411 *	Remove the buffer from the appropriate free list.
412 */
413void
414bremfree(struct buf * bp)
415{
416	int s = splbio();
417	int old_qindex = bp->b_qindex;
418
419	if (bp->b_qindex != QUEUE_NONE) {
420		KASSERT(BUF_REFCNT(bp) == 1, ("bremfree: bp %p not locked",bp));
421		TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
422		bp->b_qindex = QUEUE_NONE;
423		runningbufspace += bp->b_bufsize;
424	} else {
425		if (BUF_REFCNT(bp) <= 1)
426			panic("bremfree: removing a buffer not on a queue");
427	}
428
429	/*
430	 * Fixup numfreebuffers count.  If the buffer is invalid or not
431	 * delayed-write, and it was on the EMPTY, LRU, or AGE queues,
432	 * the buffer was free and we must decrement numfreebuffers.
433	 */
434	if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
435		switch(old_qindex) {
436		case QUEUE_DIRTY:
437		case QUEUE_CLEAN:
438		case QUEUE_EMPTY:
439		case QUEUE_EMPTYKVA:
440			--numfreebuffers;
441			break;
442		default:
443			break;
444		}
445	}
446	splx(s);
447}
448
449
450/*
451 * Get a buffer with the specified data.  Look in the cache first.  We
452 * must clear BIO_ERROR and B_INVAL prior to initiating I/O.  If B_CACHE
453 * is set, the buffer is valid and we do not have to do anything ( see
454 * getblk() ).
455 */
456int
457bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
458    struct buf ** bpp)
459{
460	struct buf *bp;
461
462	bp = getblk(vp, blkno, size, 0, 0);
463	*bpp = bp;
464
465	/* if not found in cache, do some I/O */
466	if ((bp->b_flags & B_CACHE) == 0) {
467		if (curproc != idleproc)
468			curproc->p_stats->p_ru.ru_inblock++;
469		KASSERT(!(bp->b_flags & B_ASYNC), ("bread: illegal async bp %p", bp));
470		bp->b_iocmd = BIO_READ;
471		bp->b_flags &= ~B_INVAL;
472		bp->b_ioflags &= ~BIO_ERROR;
473		if (bp->b_rcred == NOCRED) {
474			if (cred != NOCRED)
475				crhold(cred);
476			bp->b_rcred = cred;
477		}
478		vfs_busy_pages(bp, 0);
479		VOP_STRATEGY(vp, bp);
480		return (bufwait(bp));
481	}
482	return (0);
483}
484
485/*
486 * Operates like bread, but also starts asynchronous I/O on
487 * read-ahead blocks.  We must clear BIO_ERROR and B_INVAL prior
488 * to initiating I/O . If B_CACHE is set, the buffer is valid
489 * and we do not have to do anything.
490 */
491int
492breadn(struct vnode * vp, daddr_t blkno, int size,
493    daddr_t * rablkno, int *rabsize,
494    int cnt, struct ucred * cred, struct buf ** bpp)
495{
496	struct buf *bp, *rabp;
497	int i;
498	int rv = 0, readwait = 0;
499
500	*bpp = bp = getblk(vp, blkno, size, 0, 0);
501
502	/* if not found in cache, do some I/O */
503	if ((bp->b_flags & B_CACHE) == 0) {
504		if (curproc != idleproc)
505			curproc->p_stats->p_ru.ru_inblock++;
506		bp->b_iocmd = BIO_READ;
507		bp->b_flags &= ~B_INVAL;
508		bp->b_ioflags &= ~BIO_ERROR;
509		if (bp->b_rcred == NOCRED) {
510			if (cred != NOCRED)
511				crhold(cred);
512			bp->b_rcred = cred;
513		}
514		vfs_busy_pages(bp, 0);
515		VOP_STRATEGY(vp, bp);
516		++readwait;
517	}
518
519	for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
520		if (inmem(vp, *rablkno))
521			continue;
522		rabp = getblk(vp, *rablkno, *rabsize, 0, 0);
523
524		if ((rabp->b_flags & B_CACHE) == 0) {
525			if (curproc != idleproc)
526				curproc->p_stats->p_ru.ru_inblock++;
527			rabp->b_flags |= B_ASYNC;
528			rabp->b_flags &= ~B_INVAL;
529			rabp->b_ioflags &= ~BIO_ERROR;
530			rabp->b_iocmd = BIO_READ;
531			if (rabp->b_rcred == NOCRED) {
532				if (cred != NOCRED)
533					crhold(cred);
534				rabp->b_rcred = cred;
535			}
536			vfs_busy_pages(rabp, 0);
537			BUF_KERNPROC(rabp);
538			VOP_STRATEGY(vp, rabp);
539		} else {
540			brelse(rabp);
541		}
542	}
543
544	if (readwait) {
545		rv = bufwait(bp);
546	}
547	return (rv);
548}
549
550/*
551 * Write, release buffer on completion.  (Done by iodone
552 * if async).  Do not bother writing anything if the buffer
553 * is invalid.
554 *
555 * Note that we set B_CACHE here, indicating that buffer is
556 * fully valid and thus cacheable.  This is true even of NFS
557 * now so we set it generally.  This could be set either here
558 * or in biodone() since the I/O is synchronous.  We put it
559 * here.
560 */
561int
562bwrite(struct buf * bp)
563{
564	int oldflags, s;
565	struct buf *newbp;
566
567	if (bp->b_flags & B_INVAL) {
568		brelse(bp);
569		return (0);
570	}
571
572	oldflags = bp->b_flags;
573
574	if (BUF_REFCNT(bp) == 0)
575		panic("bwrite: buffer is not busy???");
576	s = splbio();
577	/*
578	 * If a background write is already in progress, delay
579	 * writing this block if it is asynchronous. Otherwise
580	 * wait for the background write to complete.
581	 */
582	if (bp->b_xflags & BX_BKGRDINPROG) {
583		if (bp->b_flags & B_ASYNC) {
584			splx(s);
585			bdwrite(bp);
586			return (0);
587		}
588		bp->b_xflags |= BX_BKGRDWAIT;
589		tsleep(&bp->b_xflags, PRIBIO, "biord", 0);
590		if (bp->b_xflags & BX_BKGRDINPROG)
591			panic("bwrite: still writing");
592	}
593
594	/* Mark the buffer clean */
595	bundirty(bp);
596
597	/*
598	 * If this buffer is marked for background writing and we
599	 * do not have to wait for it, make a copy and write the
600	 * copy so as to leave this buffer ready for further use.
601	 */
602	if ((bp->b_xflags & BX_BKGRDWRITE) && (bp->b_flags & B_ASYNC)) {
603		if (bp->b_iodone != NULL) {
604			printf("bp->b_iodone = %p\n", bp->b_iodone);
605			panic("bwrite: need chained iodone");
606		}
607
608		/* get a new block */
609		newbp = geteblk(bp->b_bufsize);
610
611		/* set it to be identical to the old block */
612		memcpy(newbp->b_data, bp->b_data, bp->b_bufsize);
613		bgetvp(bp->b_vp, newbp);
614		newbp->b_lblkno = bp->b_lblkno;
615		newbp->b_blkno = bp->b_blkno;
616		newbp->b_offset = bp->b_offset;
617		newbp->b_iodone = vfs_backgroundwritedone;
618		newbp->b_flags |= B_ASYNC;
619		newbp->b_flags &= ~B_INVAL;
620
621		/* move over the dependencies */
622		if (LIST_FIRST(&bp->b_dep) != NULL)
623			buf_movedeps(bp, newbp);
624
625		/*
626		 * Initiate write on the copy, release the original to
627		 * the B_LOCKED queue so that it cannot go away until
628		 * the background write completes. If not locked it could go
629		 * away and then be reconstituted while it was being written.
630		 * If the reconstituted buffer were written, we could end up
631		 * with two background copies being written at the same time.
632		 */
633		bp->b_xflags |= BX_BKGRDINPROG;
634		bp->b_flags |= B_LOCKED;
635		bqrelse(bp);
636		bp = newbp;
637	}
638
639	bp->b_flags &= ~B_DONE;
640	bp->b_ioflags &= ~BIO_ERROR;
641	bp->b_flags |= B_WRITEINPROG | B_CACHE;
642	bp->b_iocmd = BIO_WRITE;
643
644	bp->b_vp->v_numoutput++;
645	vfs_busy_pages(bp, 1);
646	if (curproc != idleproc)
647		curproc->p_stats->p_ru.ru_oublock++;
648	splx(s);
649	if (oldflags & B_ASYNC)
650		BUF_KERNPROC(bp);
651	BUF_STRATEGY(bp);
652
653	if ((oldflags & B_ASYNC) == 0) {
654		int rtval = bufwait(bp);
655		brelse(bp);
656		return (rtval);
657	}
658
659	return (0);
660}
661
662/*
663 * Complete a background write started from bwrite.
664 */
665static void
666vfs_backgroundwritedone(bp)
667	struct buf *bp;
668{
669	struct buf *origbp;
670
671	/*
672	 * Find the original buffer that we are writing.
673	 */
674	if ((origbp = gbincore(bp->b_vp, bp->b_lblkno)) == NULL)
675		panic("backgroundwritedone: lost buffer");
676	/*
677	 * Process dependencies then return any unfinished ones.
678	 */
679	if (LIST_FIRST(&bp->b_dep) != NULL)
680		buf_complete(bp);
681	if (LIST_FIRST(&bp->b_dep) != NULL)
682		buf_movedeps(bp, origbp);
683	/*
684	 * Clear the BX_BKGRDINPROG flag in the original buffer
685	 * and awaken it if it is waiting for the write to complete.
686	 */
687	origbp->b_xflags &= ~BX_BKGRDINPROG;
688	if (origbp->b_xflags & BX_BKGRDWAIT) {
689		origbp->b_xflags &= ~BX_BKGRDWAIT;
690		wakeup(&origbp->b_xflags);
691	}
692	/*
693	 * Clear the B_LOCKED flag and remove it from the locked
694	 * queue if it currently resides there.
695	 */
696	origbp->b_flags &= ~B_LOCKED;
697	if (BUF_LOCK(origbp, LK_EXCLUSIVE | LK_NOWAIT) == 0) {
698		bremfree(origbp);
699		bqrelse(origbp);
700	}
701	/*
702	 * This buffer is marked B_NOCACHE, so when it is released
703	 * by biodone, it will be tossed. We mark it with BIO_READ
704	 * to avoid biodone doing a second vwakeup.
705	 */
706	bp->b_flags |= B_NOCACHE;
707	bp->b_iocmd = BIO_READ;
708	bp->b_flags &= ~(B_CACHE | B_DONE);
709	bp->b_iodone = 0;
710	bufdone(bp);
711}
712
713/*
714 * Delayed write. (Buffer is marked dirty).  Do not bother writing
715 * anything if the buffer is marked invalid.
716 *
717 * Note that since the buffer must be completely valid, we can safely
718 * set B_CACHE.  In fact, we have to set B_CACHE here rather then in
719 * biodone() in order to prevent getblk from writing the buffer
720 * out synchronously.
721 */
722void
723bdwrite(struct buf * bp)
724{
725	if (BUF_REFCNT(bp) == 0)
726		panic("bdwrite: buffer is not busy");
727
728	if (bp->b_flags & B_INVAL) {
729		brelse(bp);
730		return;
731	}
732	bdirty(bp);
733
734	/*
735	 * Set B_CACHE, indicating that the buffer is fully valid.  This is
736	 * true even of NFS now.
737	 */
738	bp->b_flags |= B_CACHE;
739
740	/*
741	 * This bmap keeps the system from needing to do the bmap later,
742	 * perhaps when the system is attempting to do a sync.  Since it
743	 * is likely that the indirect block -- or whatever other datastructure
744	 * that the filesystem needs is still in memory now, it is a good
745	 * thing to do this.  Note also, that if the pageout daemon is
746	 * requesting a sync -- there might not be enough memory to do
747	 * the bmap then...  So, this is important to do.
748	 */
749	if (bp->b_lblkno == bp->b_blkno) {
750		VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
751	}
752
753	/*
754	 * Set the *dirty* buffer range based upon the VM system dirty pages.
755	 */
756	vfs_setdirty(bp);
757
758	/*
759	 * We need to do this here to satisfy the vnode_pager and the
760	 * pageout daemon, so that it thinks that the pages have been
761	 * "cleaned".  Note that since the pages are in a delayed write
762	 * buffer -- the VFS layer "will" see that the pages get written
763	 * out on the next sync, or perhaps the cluster will be completed.
764	 */
765	vfs_clean_pages(bp);
766	bqrelse(bp);
767
768	/*
769	 * Wakeup the buffer flushing daemon if we have saturated the
770	 * buffer cache.
771	 */
772
773	bd_wakeup(hidirtybuffers);
774
775	/*
776	 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
777	 * due to the softdep code.
778	 */
779}
780
781/*
782 *	bdirty:
783 *
784 *	Turn buffer into delayed write request.  We must clear BIO_READ and
785 *	B_RELBUF, and we must set B_DELWRI.  We reassign the buffer to
786 *	itself to properly update it in the dirty/clean lists.  We mark it
787 *	B_DONE to ensure that any asynchronization of the buffer properly
788 *	clears B_DONE ( else a panic will occur later ).
789 *
790 *	bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
791 *	might have been set pre-getblk().  Unlike bwrite/bdwrite, bdirty()
792 *	should only be called if the buffer is known-good.
793 *
794 *	Since the buffer is not on a queue, we do not update the numfreebuffers
795 *	count.
796 *
797 *	Must be called at splbio().
798 *	The buffer must be on QUEUE_NONE.
799 */
800void
801bdirty(bp)
802	struct buf *bp;
803{
804	KASSERT(bp->b_qindex == QUEUE_NONE, ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
805	bp->b_flags &= ~(B_RELBUF);
806	bp->b_iocmd = BIO_WRITE;
807
808	if ((bp->b_flags & B_DELWRI) == 0) {
809		bp->b_flags |= B_DONE | B_DELWRI;
810		reassignbuf(bp, bp->b_vp);
811		++numdirtybuffers;
812		bd_wakeup(hidirtybuffers);
813	}
814}
815
816/*
817 *	bundirty:
818 *
819 *	Clear B_DELWRI for buffer.
820 *
821 *	Since the buffer is not on a queue, we do not update the numfreebuffers
822 *	count.
823 *
824 *	Must be called at splbio().
825 *	The buffer must be on QUEUE_NONE.
826 */
827
828void
829bundirty(bp)
830	struct buf *bp;
831{
832	KASSERT(bp->b_qindex == QUEUE_NONE, ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
833
834	if (bp->b_flags & B_DELWRI) {
835		bp->b_flags &= ~B_DELWRI;
836		reassignbuf(bp, bp->b_vp);
837		--numdirtybuffers;
838		numdirtywakeup();
839	}
840	/*
841	 * Since it is now being written, we can clear its deferred write flag.
842	 */
843	bp->b_flags &= ~B_DEFERRED;
844}
845
846/*
847 *	bawrite:
848 *
849 *	Asynchronous write.  Start output on a buffer, but do not wait for
850 *	it to complete.  The buffer is released when the output completes.
851 *
852 *	bwrite() ( or the VOP routine anyway ) is responsible for handling
853 *	B_INVAL buffers.  Not us.
854 */
855void
856bawrite(struct buf * bp)
857{
858	bp->b_flags |= B_ASYNC;
859	(void) BUF_WRITE(bp);
860}
861
862/*
863 *	bowrite:
864 *
865 *	Ordered write.  Start output on a buffer, and flag it so that the
866 *	device will write it in the order it was queued.  The buffer is
867 *	released when the output completes.  bwrite() ( or the VOP routine
868 *	anyway ) is responsible for handling B_INVAL buffers.
869 */
870int
871bowrite(struct buf * bp)
872{
873	bp->b_ioflags |= BIO_ORDERED;
874	bp->b_flags |= B_ASYNC;
875	return (BUF_WRITE(bp));
876}
877
878/*
879 *	bwillwrite:
880 *
881 *	Called prior to the locking of any vnodes when we are expecting to
882 *	write.  We do not want to starve the buffer cache with too many
883 *	dirty buffers so we block here.  By blocking prior to the locking
884 *	of any vnodes we attempt to avoid the situation where a locked vnode
885 *	prevents the various system daemons from flushing related buffers.
886 */
887
888void
889bwillwrite(void)
890{
891	int slop = hidirtybuffers / 10;
892
893	if (numdirtybuffers > hidirtybuffers + slop) {
894		int s;
895
896		s = splbio();
897		while (numdirtybuffers > hidirtybuffers) {
898			bd_wakeup(hidirtybuffers);
899			needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
900			tsleep(&needsbuffer, (PRIBIO + 4), "flswai", 0);
901		}
902		splx(s);
903	}
904}
905
906/*
907 *	brelse:
908 *
909 *	Release a busy buffer and, if requested, free its resources.  The
910 *	buffer will be stashed in the appropriate bufqueue[] allowing it
911 *	to be accessed later as a cache entity or reused for other purposes.
912 */
913void
914brelse(struct buf * bp)
915{
916	int s;
917
918	KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
919
920	s = splbio();
921
922	if (bp->b_flags & B_LOCKED)
923		bp->b_ioflags &= ~BIO_ERROR;
924
925	if (bp->b_iocmd == BIO_WRITE &&
926	    (bp->b_ioflags & BIO_ERROR) &&
927	    !(bp->b_flags & B_INVAL)) {
928		/*
929		 * Failed write, redirty.  Must clear BIO_ERROR to prevent
930		 * pages from being scrapped.  If B_INVAL is set then
931		 * this case is not run and the next case is run to
932		 * destroy the buffer.  B_INVAL can occur if the buffer
933		 * is outside the range supported by the underlying device.
934		 */
935		bp->b_ioflags &= ~BIO_ERROR;
936		bdirty(bp);
937	} else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
938	    (bp->b_ioflags & BIO_ERROR) ||
939	    bp->b_iocmd == BIO_DELETE || (bp->b_bufsize <= 0)) {
940		/*
941		 * Either a failed I/O or we were asked to free or not
942		 * cache the buffer.
943		 */
944		bp->b_flags |= B_INVAL;
945		if (LIST_FIRST(&bp->b_dep) != NULL)
946			buf_deallocate(bp);
947		if (bp->b_flags & B_DELWRI) {
948			--numdirtybuffers;
949			numdirtywakeup();
950		}
951		bp->b_flags &= ~(B_DELWRI | B_CACHE);
952		if ((bp->b_flags & B_VMIO) == 0) {
953			if (bp->b_bufsize)
954				allocbuf(bp, 0);
955			if (bp->b_vp)
956				brelvp(bp);
957		}
958	}
959
960	/*
961	 * We must clear B_RELBUF if B_DELWRI is set.  If vfs_vmio_release()
962	 * is called with B_DELWRI set, the underlying pages may wind up
963	 * getting freed causing a previous write (bdwrite()) to get 'lost'
964	 * because pages associated with a B_DELWRI bp are marked clean.
965	 *
966	 * We still allow the B_INVAL case to call vfs_vmio_release(), even
967	 * if B_DELWRI is set.
968	 */
969
970	if (bp->b_flags & B_DELWRI)
971		bp->b_flags &= ~B_RELBUF;
972
973	/*
974	 * VMIO buffer rundown.  It is not very necessary to keep a VMIO buffer
975	 * constituted, not even NFS buffers now.  Two flags effect this.  If
976	 * B_INVAL, the struct buf is invalidated but the VM object is kept
977	 * around ( i.e. so it is trivial to reconstitute the buffer later ).
978	 *
979	 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
980	 * invalidated.  BIO_ERROR cannot be set for a failed write unless the
981	 * buffer is also B_INVAL because it hits the re-dirtying code above.
982	 *
983	 * Normally we can do this whether a buffer is B_DELWRI or not.  If
984	 * the buffer is an NFS buffer, it is tracking piecemeal writes or
985	 * the commit state and we cannot afford to lose the buffer. If the
986	 * buffer has a background write in progress, we need to keep it
987	 * around to prevent it from being reconstituted and starting a second
988	 * background write.
989	 */
990	if ((bp->b_flags & B_VMIO)
991	    && !(bp->b_vp->v_tag == VT_NFS &&
992		 !vn_isdisk(bp->b_vp, NULL) &&
993		 (bp->b_flags & B_DELWRI) &&
994		 (bp->b_xflags & BX_BKGRDINPROG))
995	    ) {
996
997		int i, j, resid;
998		vm_page_t m;
999		off_t foff;
1000		vm_pindex_t poff;
1001		vm_object_t obj;
1002		struct vnode *vp;
1003
1004		vp = bp->b_vp;
1005
1006		/*
1007		 * Get the base offset and length of the buffer.  Note that
1008		 * for block sizes that are less then PAGE_SIZE, the b_data
1009		 * base of the buffer does not represent exactly b_offset and
1010		 * neither b_offset nor b_size are necessarily page aligned.
1011		 * Instead, the starting position of b_offset is:
1012		 *
1013		 * 	b_data + (b_offset & PAGE_MASK)
1014		 *
1015		 * block sizes less then DEV_BSIZE (usually 512) are not
1016		 * supported due to the page granularity bits (m->valid,
1017		 * m->dirty, etc...).
1018		 *
1019		 * See man buf(9) for more information
1020		 */
1021
1022		resid = bp->b_bufsize;
1023		foff = bp->b_offset;
1024
1025		for (i = 0; i < bp->b_npages; i++) {
1026			m = bp->b_pages[i];
1027			vm_page_flag_clear(m, PG_ZERO);
1028			if (m == bogus_page) {
1029
1030				obj = (vm_object_t) vp->v_object;
1031				poff = OFF_TO_IDX(bp->b_offset);
1032
1033				for (j = i; j < bp->b_npages; j++) {
1034					m = bp->b_pages[j];
1035					if (m == bogus_page) {
1036						m = vm_page_lookup(obj, poff + j);
1037						if (!m) {
1038							panic("brelse: page missing\n");
1039						}
1040						bp->b_pages[j] = m;
1041					}
1042				}
1043
1044				if ((bp->b_flags & B_INVAL) == 0) {
1045					pmap_qenter(trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages);
1046				}
1047			}
1048			if ((bp->b_flags & B_NOCACHE) || (bp->b_ioflags & BIO_ERROR)) {
1049				int poffset = foff & PAGE_MASK;
1050				int presid = resid > (PAGE_SIZE - poffset) ?
1051					(PAGE_SIZE - poffset) : resid;
1052
1053				KASSERT(presid >= 0, ("brelse: extra page"));
1054				vm_page_set_invalid(m, poffset, presid);
1055			}
1056			resid -= PAGE_SIZE - (foff & PAGE_MASK);
1057			foff = (foff + PAGE_SIZE) & ~PAGE_MASK;
1058		}
1059
1060		if (bp->b_flags & (B_INVAL | B_RELBUF))
1061			vfs_vmio_release(bp);
1062
1063	} else if (bp->b_flags & B_VMIO) {
1064
1065		if (bp->b_flags & (B_INVAL | B_RELBUF))
1066			vfs_vmio_release(bp);
1067
1068	}
1069
1070	if (bp->b_qindex != QUEUE_NONE)
1071		panic("brelse: free buffer onto another queue???");
1072	if (BUF_REFCNT(bp) > 1) {
1073		/* do not release to free list */
1074		BUF_UNLOCK(bp);
1075		splx(s);
1076		return;
1077	}
1078
1079	/* enqueue */
1080
1081	/* buffers with no memory */
1082	if (bp->b_bufsize == 0) {
1083		bp->b_flags |= B_INVAL;
1084		bp->b_xflags &= ~BX_BKGRDWRITE;
1085		if (bp->b_xflags & BX_BKGRDINPROG)
1086			panic("losing buffer 1");
1087		if (bp->b_kvasize) {
1088			bp->b_qindex = QUEUE_EMPTYKVA;
1089		} else {
1090			bp->b_qindex = QUEUE_EMPTY;
1091		}
1092		TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1093		LIST_REMOVE(bp, b_hash);
1094		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
1095		bp->b_dev = NODEV;
1096	/* buffers with junk contents */
1097	} else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) || (bp->b_ioflags & BIO_ERROR)) {
1098		bp->b_flags |= B_INVAL;
1099		bp->b_xflags &= ~BX_BKGRDWRITE;
1100		if (bp->b_xflags & BX_BKGRDINPROG)
1101			panic("losing buffer 2");
1102		bp->b_qindex = QUEUE_CLEAN;
1103		TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1104		LIST_REMOVE(bp, b_hash);
1105		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
1106		bp->b_dev = NODEV;
1107
1108	/* buffers that are locked */
1109	} else if (bp->b_flags & B_LOCKED) {
1110		bp->b_qindex = QUEUE_LOCKED;
1111		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
1112
1113	/* remaining buffers */
1114	} else {
1115		switch(bp->b_flags & (B_DELWRI|B_AGE)) {
1116		case B_DELWRI | B_AGE:
1117		    bp->b_qindex = QUEUE_DIRTY;
1118		    TAILQ_INSERT_HEAD(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
1119		    break;
1120		case B_DELWRI:
1121		    bp->b_qindex = QUEUE_DIRTY;
1122		    TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
1123		    break;
1124		case B_AGE:
1125		    bp->b_qindex = QUEUE_CLEAN;
1126		    TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1127		    break;
1128		default:
1129		    bp->b_qindex = QUEUE_CLEAN;
1130		    TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1131		    break;
1132		}
1133	}
1134
1135	/*
1136	 * If B_INVAL, clear B_DELWRI.  We've already placed the buffer
1137	 * on the correct queue.
1138	 */
1139	if ((bp->b_flags & (B_INVAL|B_DELWRI)) == (B_INVAL|B_DELWRI)) {
1140		bp->b_flags &= ~B_DELWRI;
1141		--numdirtybuffers;
1142		numdirtywakeup();
1143	}
1144
1145	runningbufspace -= bp->b_bufsize;
1146
1147	/*
1148	 * Fixup numfreebuffers count.  The bp is on an appropriate queue
1149	 * unless locked.  We then bump numfreebuffers if it is not B_DELWRI.
1150	 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1151	 * if B_INVAL is set ).
1152	 */
1153
1154	if ((bp->b_flags & B_LOCKED) == 0 && !(bp->b_flags & B_DELWRI))
1155		bufcountwakeup();
1156
1157	/*
1158	 * Something we can maybe free.
1159	 */
1160
1161	if (bp->b_bufsize || bp->b_kvasize)
1162		bufspacewakeup();
1163
1164	/* unlock */
1165	BUF_UNLOCK(bp);
1166	bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1167	bp->b_ioflags &= ~BIO_ORDERED;
1168	if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1169		panic("brelse: not dirty");
1170	splx(s);
1171}
1172
1173/*
1174 * Release a buffer back to the appropriate queue but do not try to free
1175 * it.
1176 *
1177 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1178 * biodone() to requeue an async I/O on completion.  It is also used when
1179 * known good buffers need to be requeued but we think we may need the data
1180 * again soon.
1181 */
1182void
1183bqrelse(struct buf * bp)
1184{
1185	int s;
1186
1187	s = splbio();
1188
1189	KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)), ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1190
1191	if (bp->b_qindex != QUEUE_NONE)
1192		panic("bqrelse: free buffer onto another queue???");
1193	if (BUF_REFCNT(bp) > 1) {
1194		/* do not release to free list */
1195		BUF_UNLOCK(bp);
1196		splx(s);
1197		return;
1198	}
1199	if (bp->b_flags & B_LOCKED) {
1200		bp->b_ioflags &= ~BIO_ERROR;
1201		bp->b_qindex = QUEUE_LOCKED;
1202		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
1203		/* buffers with stale but valid contents */
1204	} else if (bp->b_flags & B_DELWRI) {
1205		bp->b_qindex = QUEUE_DIRTY;
1206		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY], bp, b_freelist);
1207	} else {
1208		bp->b_qindex = QUEUE_CLEAN;
1209		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1210	}
1211
1212	runningbufspace -= bp->b_bufsize;
1213
1214	if ((bp->b_flags & B_LOCKED) == 0 &&
1215	    ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI))) {
1216		bufcountwakeup();
1217	}
1218
1219	/*
1220	 * Something we can maybe wakeup
1221	 */
1222	if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1223		bufspacewakeup();
1224
1225	/* unlock */
1226	BUF_UNLOCK(bp);
1227	bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1228	bp->b_ioflags &= ~BIO_ORDERED;
1229	if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1230		panic("bqrelse: not dirty");
1231	splx(s);
1232}
1233
1234static void
1235vfs_vmio_release(bp)
1236	struct buf *bp;
1237{
1238	int i, s;
1239	vm_page_t m;
1240
1241	s = splvm();
1242	for (i = 0; i < bp->b_npages; i++) {
1243		m = bp->b_pages[i];
1244		bp->b_pages[i] = NULL;
1245		/*
1246		 * In order to keep page LRU ordering consistent, put
1247		 * everything on the inactive queue.
1248		 */
1249		vm_page_unwire(m, 0);
1250		/*
1251		 * We don't mess with busy pages, it is
1252		 * the responsibility of the process that
1253		 * busied the pages to deal with them.
1254		 */
1255		if ((m->flags & PG_BUSY) || (m->busy != 0))
1256			continue;
1257
1258		if (m->wire_count == 0) {
1259			vm_page_flag_clear(m, PG_ZERO);
1260			/*
1261			 * Might as well free the page if we can and it has
1262			 * no valid data.
1263			 */
1264			if ((bp->b_flags & B_ASYNC) == 0 && !m->valid && m->hold_count == 0) {
1265				vm_page_busy(m);
1266				vm_page_protect(m, VM_PROT_NONE);
1267				vm_page_free(m);
1268			}
1269		}
1270	}
1271	runningbufspace -= bp->b_bufsize;
1272	splx(s);
1273	pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
1274	if (bp->b_bufsize)
1275		bufspacewakeup();
1276	bp->b_npages = 0;
1277	bp->b_bufsize = 0;
1278	bp->b_flags &= ~B_VMIO;
1279	if (bp->b_vp)
1280		brelvp(bp);
1281}
1282
1283/*
1284 * Check to see if a block is currently memory resident.
1285 */
1286struct buf *
1287gbincore(struct vnode * vp, daddr_t blkno)
1288{
1289	struct buf *bp;
1290	struct bufhashhdr *bh;
1291
1292	bh = bufhash(vp, blkno);
1293
1294	/* Search hash chain */
1295	LIST_FOREACH(bp, bh, b_hash) {
1296		/* hit */
1297		if (bp->b_vp == vp && bp->b_lblkno == blkno &&
1298		    (bp->b_flags & B_INVAL) == 0) {
1299			break;
1300		}
1301	}
1302	return (bp);
1303}
1304
1305/*
1306 *	vfs_bio_awrite:
1307 *
1308 *	Implement clustered async writes for clearing out B_DELWRI buffers.
1309 *	This is much better then the old way of writing only one buffer at
1310 *	a time.  Note that we may not be presented with the buffers in the
1311 *	correct order, so we search for the cluster in both directions.
1312 */
1313int
1314vfs_bio_awrite(struct buf * bp)
1315{
1316	int i;
1317	int j;
1318	daddr_t lblkno = bp->b_lblkno;
1319	struct vnode *vp = bp->b_vp;
1320	int s;
1321	int ncl;
1322	struct buf *bpa;
1323	int nwritten;
1324	int size;
1325	int maxcl;
1326
1327	s = splbio();
1328	/*
1329	 * right now we support clustered writing only to regular files.  If
1330	 * we find a clusterable block we could be in the middle of a cluster
1331	 * rather then at the beginning.
1332	 */
1333	if ((vp->v_type == VREG) &&
1334	    (vp->v_mount != 0) && /* Only on nodes that have the size info */
1335	    (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1336
1337		size = vp->v_mount->mnt_stat.f_iosize;
1338		maxcl = MAXPHYS / size;
1339
1340		for (i = 1; i < maxcl; i++) {
1341			if ((bpa = gbincore(vp, lblkno + i)) &&
1342			    BUF_REFCNT(bpa) == 0 &&
1343			    ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) ==
1344			    (B_DELWRI | B_CLUSTEROK)) &&
1345			    (bpa->b_bufsize == size)) {
1346				if ((bpa->b_blkno == bpa->b_lblkno) ||
1347				    (bpa->b_blkno !=
1348				     bp->b_blkno + ((i * size) >> DEV_BSHIFT)))
1349					break;
1350			} else {
1351				break;
1352			}
1353		}
1354		for (j = 1; i + j <= maxcl && j <= lblkno; j++) {
1355			if ((bpa = gbincore(vp, lblkno - j)) &&
1356			    BUF_REFCNT(bpa) == 0 &&
1357			    ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) ==
1358			    (B_DELWRI | B_CLUSTEROK)) &&
1359			    (bpa->b_bufsize == size)) {
1360				if ((bpa->b_blkno == bpa->b_lblkno) ||
1361				    (bpa->b_blkno !=
1362				     bp->b_blkno - ((j * size) >> DEV_BSHIFT)))
1363					break;
1364			} else {
1365				break;
1366			}
1367		}
1368		--j;
1369		ncl = i + j;
1370		/*
1371		 * this is a possible cluster write
1372		 */
1373		if (ncl != 1) {
1374			nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1375			splx(s);
1376			return nwritten;
1377		}
1378	}
1379
1380	BUF_LOCK(bp, LK_EXCLUSIVE);
1381	bremfree(bp);
1382	bp->b_flags |= B_ASYNC;
1383
1384	splx(s);
1385	/*
1386	 * default (old) behavior, writing out only one block
1387	 *
1388	 * XXX returns b_bufsize instead of b_bcount for nwritten?
1389	 */
1390	nwritten = bp->b_bufsize;
1391	(void) BUF_WRITE(bp);
1392
1393	return nwritten;
1394}
1395
1396/*
1397 *	getnewbuf:
1398 *
1399 *	Find and initialize a new buffer header, freeing up existing buffers
1400 *	in the bufqueues as necessary.  The new buffer is returned locked.
1401 *
1402 *	Important:  B_INVAL is not set.  If the caller wishes to throw the
1403 *	buffer away, the caller must set B_INVAL prior to calling brelse().
1404 *
1405 *	We block if:
1406 *		We have insufficient buffer headers
1407 *		We have insufficient buffer space
1408 *		buffer_map is too fragmented ( space reservation fails )
1409 *		If we have to flush dirty buffers ( but we try to avoid this )
1410 *
1411 *	To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1412 *	Instead we ask the buf daemon to do it for us.  We attempt to
1413 *	avoid piecemeal wakeups of the pageout daemon.
1414 */
1415
1416static struct buf *
1417getnewbuf(int slpflag, int slptimeo, int size, int maxsize)
1418{
1419	struct buf *bp;
1420	struct buf *nbp;
1421	int defrag = 0;
1422	int nqindex;
1423	int isspecial;
1424	static int flushingbufs;
1425
1426	if (curproc != idleproc &&
1427	    (curproc->p_flag & (P_COWINPROGRESS|P_BUFEXHAUST)) == 0)
1428		isspecial = 0;
1429	else
1430		isspecial = 1;
1431
1432	++getnewbufcalls;
1433	--getnewbufrestarts;
1434restart:
1435	++getnewbufrestarts;
1436
1437	/*
1438	 * Setup for scan.  If we do not have enough free buffers,
1439	 * we setup a degenerate case that immediately fails.  Note
1440	 * that if we are specially marked process, we are allowed to
1441	 * dip into our reserves.
1442	 *
1443	 * The scanning sequence is nominally:  EMPTY->EMPTYKVA->CLEAN
1444	 *
1445	 * We start with EMPTYKVA.  If the list is empty we backup to EMPTY.
1446	 * However, there are a number of cases (defragging, reusing, ...)
1447	 * where we cannot backup.
1448	 */
1449
1450	if (isspecial == 0 && numfreebuffers < lofreebuffers) {
1451		/*
1452		 * This will cause an immediate failure
1453		 */
1454		nqindex = QUEUE_CLEAN;
1455		nbp = NULL;
1456	} else {
1457		/*
1458		 * Locate a buffer which already has KVA assigned.  First
1459		 * try EMPTYKVA buffers.
1460		 */
1461		nqindex = QUEUE_EMPTYKVA;
1462		nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1463
1464		if (nbp == NULL) {
1465			/*
1466			 * If no EMPTYKVA buffers and we are either
1467			 * defragging or reusing, locate a CLEAN buffer
1468			 * to free or reuse.  If bufspace useage is low
1469			 * skip this step so we can allocate a new buffer.
1470			 */
1471			if (defrag || bufspace >= lobufspace) {
1472				nqindex = QUEUE_CLEAN;
1473				nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1474			}
1475
1476			/*
1477			 * Nada.  If we are allowed to allocate an EMPTY
1478			 * buffer, go get one.
1479			 */
1480			if (nbp == NULL && defrag == 0 &&
1481			    (isspecial || bufspace < hibufspace)) {
1482				nqindex = QUEUE_EMPTY;
1483				nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1484			}
1485		}
1486	}
1487
1488	/*
1489	 * Run scan, possibly freeing data and/or kva mappings on the fly
1490	 * depending.
1491	 */
1492
1493	while ((bp = nbp) != NULL) {
1494		int qindex = nqindex;
1495
1496		/*
1497		 * Calculate next bp ( we can only use it if we do not block
1498		 * or do other fancy things ).
1499		 */
1500		if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1501			switch(qindex) {
1502			case QUEUE_EMPTY:
1503				nqindex = QUEUE_EMPTYKVA;
1504				if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1505					break;
1506				/* fall through */
1507			case QUEUE_EMPTYKVA:
1508				nqindex = QUEUE_CLEAN;
1509				if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1510					break;
1511				/* fall through */
1512			case QUEUE_CLEAN:
1513				/*
1514				 * nbp is NULL.
1515				 */
1516				break;
1517			}
1518		}
1519
1520		/*
1521		 * Sanity Checks
1522		 */
1523		KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1524
1525		/*
1526		 * Note: we no longer distinguish between VMIO and non-VMIO
1527		 * buffers.
1528		 */
1529
1530		KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1531
1532		/*
1533		 * If we are defragging then we need a buffer with
1534		 * b_kvasize != 0.  XXX this situation should no longer
1535		 * occur, if defrag is non-zero the buffer's b_kvasize
1536		 * should also be non-zero at this point.  XXX
1537		 */
1538		if (defrag && bp->b_kvasize == 0) {
1539			printf("Warning: defrag empty buffer %p\n", bp);
1540			continue;
1541		}
1542
1543		/*
1544		 * Start freeing the bp.  This is somewhat involved.  nbp
1545		 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1546		 */
1547
1548		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0)
1549			panic("getnewbuf: locked buf");
1550		bremfree(bp);
1551
1552		if (qindex == QUEUE_CLEAN) {
1553			if (bp->b_flags & B_VMIO) {
1554				bp->b_flags &= ~B_ASYNC;
1555				vfs_vmio_release(bp);
1556			}
1557			if (bp->b_vp)
1558				brelvp(bp);
1559		}
1560
1561		/*
1562		 * NOTE:  nbp is now entirely invalid.  We can only restart
1563		 * the scan from this point on.
1564		 *
1565		 * Get the rest of the buffer freed up.  b_kva* is still
1566		 * valid after this operation.
1567		 */
1568
1569		if (bp->b_rcred != NOCRED) {
1570			crfree(bp->b_rcred);
1571			bp->b_rcred = NOCRED;
1572		}
1573		if (bp->b_wcred != NOCRED) {
1574			crfree(bp->b_wcred);
1575			bp->b_wcred = NOCRED;
1576		}
1577		if (LIST_FIRST(&bp->b_dep) != NULL)
1578			buf_deallocate(bp);
1579		if (bp->b_xflags & BX_BKGRDINPROG)
1580			panic("losing buffer 3");
1581		LIST_REMOVE(bp, b_hash);
1582		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
1583
1584		if (bp->b_bufsize)
1585			allocbuf(bp, 0);
1586
1587		bp->b_flags = 0;
1588		bp->b_ioflags = 0;
1589		bp->b_xflags = 0;
1590		bp->b_dev = NODEV;
1591		bp->b_vp = NULL;
1592		bp->b_blkno = bp->b_lblkno = 0;
1593		bp->b_offset = NOOFFSET;
1594		bp->b_iodone = 0;
1595		bp->b_error = 0;
1596		bp->b_resid = 0;
1597		bp->b_bcount = 0;
1598		bp->b_npages = 0;
1599		bp->b_dirtyoff = bp->b_dirtyend = 0;
1600
1601		LIST_INIT(&bp->b_dep);
1602
1603		/*
1604		 * If we are defragging then free the buffer.
1605		 */
1606		if (defrag) {
1607			bp->b_flags |= B_INVAL;
1608			bfreekva(bp);
1609			brelse(bp);
1610			defrag = 0;
1611			goto restart;
1612		}
1613
1614		/*
1615		 * If we are a normal process then deal with bufspace
1616		 * hysteresis.  A normal process tries to keep bufspace
1617		 * between lobufspace and hibufspace.  Note: if we encounter
1618		 * a buffer with b_kvasize == 0 then it means we started
1619		 * our scan on the EMPTY list and should allocate a new
1620		 * buffer.
1621		 */
1622		if (isspecial == 0) {
1623			if (bufspace > hibufspace)
1624				flushingbufs = 1;
1625			if (flushingbufs && bp->b_kvasize != 0) {
1626				bp->b_flags |= B_INVAL;
1627				bfreekva(bp);
1628				brelse(bp);
1629				goto restart;
1630			}
1631			if (bufspace < lobufspace)
1632				flushingbufs = 0;
1633		}
1634		break;
1635	}
1636
1637	/*
1638	 * If we exhausted our list, sleep as appropriate.  We may have to
1639	 * wakeup various daemons and write out some dirty buffers.
1640	 *
1641	 * Generally we are sleeping due to insufficient buffer space.
1642	 */
1643
1644	if (bp == NULL) {
1645		int flags;
1646		char *waitmsg;
1647
1648		if (defrag) {
1649			flags = VFS_BIO_NEED_BUFSPACE;
1650			waitmsg = "nbufkv";
1651		} else if (bufspace >= hibufspace) {
1652			waitmsg = "nbufbs";
1653			flags = VFS_BIO_NEED_BUFSPACE;
1654		} else {
1655			waitmsg = "newbuf";
1656			flags = VFS_BIO_NEED_ANY;
1657		}
1658
1659		bd_speedup();	/* heeeelp */
1660
1661		needsbuffer |= flags;
1662		while (needsbuffer & flags) {
1663			if (tsleep(&needsbuffer, (PRIBIO + 4) | slpflag,
1664			    waitmsg, slptimeo))
1665				return (NULL);
1666		}
1667	} else {
1668		/*
1669		 * We finally have a valid bp.  We aren't quite out of the
1670		 * woods, we still have to reserve kva space.  In order
1671		 * to keep fragmentation sane we only allocate kva in
1672		 * BKVASIZE chunks.
1673		 */
1674		maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
1675
1676		if (maxsize != bp->b_kvasize) {
1677			vm_offset_t addr = 0;
1678
1679			bfreekva(bp);
1680
1681			if (vm_map_findspace(buffer_map,
1682				vm_map_min(buffer_map), maxsize, &addr)) {
1683				/*
1684				 * Uh oh.  Buffer map is to fragmented.  We
1685				 * must defragment the map.
1686				 */
1687				++bufdefragcnt;
1688				defrag = 1;
1689				bp->b_flags |= B_INVAL;
1690				brelse(bp);
1691				goto restart;
1692			}
1693			if (addr) {
1694				vm_map_insert(buffer_map, NULL, 0,
1695					addr, addr + maxsize,
1696					VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
1697
1698				bp->b_kvabase = (caddr_t) addr;
1699				bp->b_kvasize = maxsize;
1700				bufspace += bp->b_kvasize;
1701				++bufreusecnt;
1702			}
1703		}
1704		bp->b_data = bp->b_kvabase;
1705	}
1706	return(bp);
1707}
1708
1709/*
1710 *	waitfreebuffers:
1711 *
1712 *	Wait for sufficient free buffers.  Only called from normal processes.
1713 */
1714
1715static void
1716waitfreebuffers(int slpflag, int slptimeo)
1717{
1718	while (numfreebuffers < hifreebuffers) {
1719		if (numfreebuffers >= hifreebuffers)
1720			break;
1721		needsbuffer |= VFS_BIO_NEED_FREE;
1722		if (tsleep(&needsbuffer, (PRIBIO + 4)|slpflag, "biofre", slptimeo))
1723			break;
1724	}
1725}
1726
1727/*
1728 *	buf_daemon:
1729 *
1730 *	buffer flushing daemon.  Buffers are normally flushed by the
1731 *	update daemon but if it cannot keep up this process starts to
1732 *	take the load in an attempt to prevent getnewbuf() from blocking.
1733 */
1734
1735static struct proc *bufdaemonproc;
1736static int bd_interval;
1737static int bd_flushto;
1738static int bd_flushinc;
1739
1740static struct kproc_desc buf_kp = {
1741	"bufdaemon",
1742	buf_daemon,
1743	&bufdaemonproc
1744};
1745SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp)
1746
1747static void
1748buf_daemon()
1749{
1750	int s;
1751
1752	mtx_enter(&Giant, MTX_DEF);
1753
1754	/*
1755	 * This process needs to be suspended prior to shutdown sync.
1756	 */
1757	EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, bufdaemonproc,
1758	    SHUTDOWN_PRI_LAST);
1759
1760	/*
1761	 * This process is allowed to take the buffer cache to the limit
1762	 */
1763	curproc->p_flag |= P_BUFEXHAUST;
1764	s = splbio();
1765
1766	bd_interval = 5 * hz;	/* dynamically adjusted */
1767	bd_flushto = hidirtybuffers;	/* dynamically adjusted */
1768	bd_flushinc = 1;
1769
1770	for (;;) {
1771		kproc_suspend_loop(bufdaemonproc);
1772
1773		bd_request = 0;
1774
1775		/*
1776		 * Do the flush.  Limit the number of buffers we flush in one
1777		 * go.  The failure condition occurs when processes are writing
1778		 * buffers faster then we can dispose of them.  In this case
1779		 * we may be flushing so often that the previous set of flushes
1780		 * have not had time to complete, causing us to run out of
1781		 * physical buffers and block.
1782		 */
1783		{
1784			int runcount = maxbdrun;
1785
1786			while (numdirtybuffers > bd_flushto && runcount) {
1787				--runcount;
1788				if (flushbufqueues() == 0)
1789					break;
1790			}
1791		}
1792
1793		if (bd_request ||
1794		    tsleep(&bd_request, PVM, "psleep", bd_interval) == 0) {
1795			/*
1796			 * Another request is pending or we were woken up
1797			 * without timing out.  Flush more.
1798			 */
1799			--bd_flushto;
1800			if (bd_flushto >= numdirtybuffers - 5) {
1801				bd_flushto = numdirtybuffers - 10;
1802				bd_flushinc = 1;
1803			}
1804			if (bd_flushto < 2)
1805				bd_flushto = 2;
1806		} else {
1807			/*
1808			 * We slept and timed out, we can slow down.
1809			 */
1810			bd_flushto += bd_flushinc;
1811			if (bd_flushto > hidirtybuffers)
1812				bd_flushto = hidirtybuffers;
1813			++bd_flushinc;
1814			if (bd_flushinc > hidirtybuffers / 20 + 1)
1815				bd_flushinc = hidirtybuffers / 20 + 1;
1816		}
1817
1818		/*
1819		 * Set the interval on a linear scale based on hidirtybuffers
1820		 * with a maximum frequency of 1/10 second.
1821		 */
1822		bd_interval = bd_flushto * 5 * hz / hidirtybuffers;
1823		if (bd_interval < hz / 10)
1824			bd_interval = hz / 10;
1825	}
1826}
1827
1828/*
1829 *	flushbufqueues:
1830 *
1831 *	Try to flush a buffer in the dirty queue.  We must be careful to
1832 *	free up B_INVAL buffers instead of write them, which NFS is
1833 *	particularly sensitive to.
1834 */
1835
1836static int
1837flushbufqueues(void)
1838{
1839	struct buf *bp;
1840	int r = 0;
1841
1842	bp = TAILQ_FIRST(&bufqueues[QUEUE_DIRTY]);
1843
1844	while (bp) {
1845		KASSERT((bp->b_flags & B_DELWRI), ("unexpected clean buffer %p", bp));
1846		if ((bp->b_flags & B_DELWRI) != 0 &&
1847		    (bp->b_xflags & BX_BKGRDINPROG) == 0) {
1848			if (bp->b_flags & B_INVAL) {
1849				if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT) != 0)
1850					panic("flushbufqueues: locked buf");
1851				bremfree(bp);
1852				brelse(bp);
1853				++r;
1854				break;
1855			}
1856			if (LIST_FIRST(&bp->b_dep) != NULL &&
1857			    (bp->b_flags & B_DEFERRED) == 0 &&
1858			    buf_countdeps(bp, 0)) {
1859				TAILQ_REMOVE(&bufqueues[QUEUE_DIRTY],
1860				    bp, b_freelist);
1861				TAILQ_INSERT_TAIL(&bufqueues[QUEUE_DIRTY],
1862				    bp, b_freelist);
1863				bp->b_flags |= B_DEFERRED;
1864				bp = TAILQ_FIRST(&bufqueues[QUEUE_DIRTY]);
1865				continue;
1866			}
1867			vfs_bio_awrite(bp);
1868			++r;
1869			break;
1870		}
1871		bp = TAILQ_NEXT(bp, b_freelist);
1872	}
1873	return (r);
1874}
1875
1876/*
1877 * Check to see if a block is currently memory resident.
1878 */
1879struct buf *
1880incore(struct vnode * vp, daddr_t blkno)
1881{
1882	struct buf *bp;
1883
1884	int s = splbio();
1885	bp = gbincore(vp, blkno);
1886	splx(s);
1887	return (bp);
1888}
1889
1890/*
1891 * Returns true if no I/O is needed to access the
1892 * associated VM object.  This is like incore except
1893 * it also hunts around in the VM system for the data.
1894 */
1895
1896int
1897inmem(struct vnode * vp, daddr_t blkno)
1898{
1899	vm_object_t obj;
1900	vm_offset_t toff, tinc, size;
1901	vm_page_t m;
1902	vm_ooffset_t off;
1903
1904	if (incore(vp, blkno))
1905		return 1;
1906	if (vp->v_mount == NULL)
1907		return 0;
1908	if ((vp->v_object == NULL) || (vp->v_flag & VOBJBUF) == 0)
1909		return 0;
1910
1911	obj = vp->v_object;
1912	size = PAGE_SIZE;
1913	if (size > vp->v_mount->mnt_stat.f_iosize)
1914		size = vp->v_mount->mnt_stat.f_iosize;
1915	off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
1916
1917	for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
1918		m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
1919		if (!m)
1920			return 0;
1921		tinc = size;
1922		if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
1923			tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
1924		if (vm_page_is_valid(m,
1925		    (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
1926			return 0;
1927	}
1928	return 1;
1929}
1930
1931/*
1932 *	vfs_setdirty:
1933 *
1934 *	Sets the dirty range for a buffer based on the status of the dirty
1935 *	bits in the pages comprising the buffer.
1936 *
1937 *	The range is limited to the size of the buffer.
1938 *
1939 *	This routine is primarily used by NFS, but is generalized for the
1940 *	B_VMIO case.
1941 */
1942static void
1943vfs_setdirty(struct buf *bp)
1944{
1945	int i;
1946	vm_object_t object;
1947
1948	/*
1949	 * Degenerate case - empty buffer
1950	 */
1951
1952	if (bp->b_bufsize == 0)
1953		return;
1954
1955	/*
1956	 * We qualify the scan for modified pages on whether the
1957	 * object has been flushed yet.  The OBJ_WRITEABLE flag
1958	 * is not cleared simply by protecting pages off.
1959	 */
1960
1961	if ((bp->b_flags & B_VMIO) == 0)
1962		return;
1963
1964	object = bp->b_pages[0]->object;
1965
1966	if ((object->flags & OBJ_WRITEABLE) && !(object->flags & OBJ_MIGHTBEDIRTY))
1967		printf("Warning: object %p writeable but not mightbedirty\n", object);
1968	if (!(object->flags & OBJ_WRITEABLE) && (object->flags & OBJ_MIGHTBEDIRTY))
1969		printf("Warning: object %p mightbedirty but not writeable\n", object);
1970
1971	if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) {
1972		vm_offset_t boffset;
1973		vm_offset_t eoffset;
1974
1975		/*
1976		 * test the pages to see if they have been modified directly
1977		 * by users through the VM system.
1978		 */
1979		for (i = 0; i < bp->b_npages; i++) {
1980			vm_page_flag_clear(bp->b_pages[i], PG_ZERO);
1981			vm_page_test_dirty(bp->b_pages[i]);
1982		}
1983
1984		/*
1985		 * Calculate the encompassing dirty range, boffset and eoffset,
1986		 * (eoffset - boffset) bytes.
1987		 */
1988
1989		for (i = 0; i < bp->b_npages; i++) {
1990			if (bp->b_pages[i]->dirty)
1991				break;
1992		}
1993		boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
1994
1995		for (i = bp->b_npages - 1; i >= 0; --i) {
1996			if (bp->b_pages[i]->dirty) {
1997				break;
1998			}
1999		}
2000		eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2001
2002		/*
2003		 * Fit it to the buffer.
2004		 */
2005
2006		if (eoffset > bp->b_bcount)
2007			eoffset = bp->b_bcount;
2008
2009		/*
2010		 * If we have a good dirty range, merge with the existing
2011		 * dirty range.
2012		 */
2013
2014		if (boffset < eoffset) {
2015			if (bp->b_dirtyoff > boffset)
2016				bp->b_dirtyoff = boffset;
2017			if (bp->b_dirtyend < eoffset)
2018				bp->b_dirtyend = eoffset;
2019		}
2020	}
2021}
2022
2023/*
2024 *	getblk:
2025 *
2026 *	Get a block given a specified block and offset into a file/device.
2027 *	The buffers B_DONE bit will be cleared on return, making it almost
2028 * 	ready for an I/O initiation.  B_INVAL may or may not be set on
2029 *	return.  The caller should clear B_INVAL prior to initiating a
2030 *	READ.
2031 *
2032 *	For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2033 *	an existing buffer.
2034 *
2035 *	For a VMIO buffer, B_CACHE is modified according to the backing VM.
2036 *	If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2037 *	and then cleared based on the backing VM.  If the previous buffer is
2038 *	non-0-sized but invalid, B_CACHE will be cleared.
2039 *
2040 *	If getblk() must create a new buffer, the new buffer is returned with
2041 *	both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2042 *	case it is returned with B_INVAL clear and B_CACHE set based on the
2043 *	backing VM.
2044 *
2045 *	getblk() also forces a VOP_BWRITE() for any B_DELWRI buffer whos
2046 *	B_CACHE bit is clear.
2047 *
2048 *	What this means, basically, is that the caller should use B_CACHE to
2049 *	determine whether the buffer is fully valid or not and should clear
2050 *	B_INVAL prior to issuing a read.  If the caller intends to validate
2051 *	the buffer by loading its data area with something, the caller needs
2052 *	to clear B_INVAL.  If the caller does this without issuing an I/O,
2053 *	the caller should set B_CACHE ( as an optimization ), else the caller
2054 *	should issue the I/O and biodone() will set B_CACHE if the I/O was
2055 *	a write attempt or if it was a successfull read.  If the caller
2056 *	intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2057 *	prior to issuing the READ.  biodone() will *not* clear B_INVAL.
2058 */
2059struct buf *
2060getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo)
2061{
2062	struct buf *bp;
2063	int s;
2064	struct bufhashhdr *bh;
2065
2066	if (size > MAXBSIZE)
2067		panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2068
2069	s = splbio();
2070loop:
2071	/*
2072	 * Block if we are low on buffers.   Certain processes are allowed
2073	 * to completely exhaust the buffer cache.
2074         *
2075         * If this check ever becomes a bottleneck it may be better to
2076         * move it into the else, when gbincore() fails.  At the moment
2077         * it isn't a problem.
2078         */
2079	if (curproc == idleproc || (curproc->p_flag & P_BUFEXHAUST)) {
2080		if (numfreebuffers == 0) {
2081			if (curproc == idleproc)
2082				return NULL;
2083			needsbuffer |= VFS_BIO_NEED_ANY;
2084			tsleep(&needsbuffer, (PRIBIO + 4) | slpflag, "newbuf",
2085			    slptimeo);
2086		}
2087	} else if (numfreebuffers < lofreebuffers) {
2088		waitfreebuffers(slpflag, slptimeo);
2089	}
2090
2091	if ((bp = gbincore(vp, blkno))) {
2092		/*
2093		 * Buffer is in-core.  If the buffer is not busy, it must
2094		 * be on a queue.
2095		 */
2096
2097		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
2098			if (BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL,
2099			    "getblk", slpflag, slptimeo) == ENOLCK)
2100				goto loop;
2101			splx(s);
2102			return (struct buf *) NULL;
2103		}
2104
2105		/*
2106		 * The buffer is locked.  B_CACHE is cleared if the buffer is
2107		 * invalid.  Ohterwise, for a non-VMIO buffer, B_CACHE is set
2108		 * and for a VMIO buffer B_CACHE is adjusted according to the
2109		 * backing VM cache.
2110		 */
2111		if (bp->b_flags & B_INVAL)
2112			bp->b_flags &= ~B_CACHE;
2113		else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2114			bp->b_flags |= B_CACHE;
2115		bremfree(bp);
2116
2117		/*
2118		 * check for size inconsistancies for non-VMIO case.
2119		 */
2120
2121		if (bp->b_bcount != size) {
2122			if ((bp->b_flags & B_VMIO) == 0 ||
2123			    (size > bp->b_kvasize)) {
2124				if (bp->b_flags & B_DELWRI) {
2125					bp->b_flags |= B_NOCACHE;
2126					BUF_WRITE(bp);
2127				} else {
2128					if ((bp->b_flags & B_VMIO) &&
2129					   (LIST_FIRST(&bp->b_dep) == NULL)) {
2130						bp->b_flags |= B_RELBUF;
2131						brelse(bp);
2132					} else {
2133						bp->b_flags |= B_NOCACHE;
2134						BUF_WRITE(bp);
2135					}
2136				}
2137				goto loop;
2138			}
2139		}
2140
2141		/*
2142		 * If the size is inconsistant in the VMIO case, we can resize
2143		 * the buffer.  This might lead to B_CACHE getting set or
2144		 * cleared.  If the size has not changed, B_CACHE remains
2145		 * unchanged from its previous state.
2146		 */
2147
2148		if (bp->b_bcount != size)
2149			allocbuf(bp, size);
2150
2151		KASSERT(bp->b_offset != NOOFFSET,
2152		    ("getblk: no buffer offset"));
2153
2154		/*
2155		 * A buffer with B_DELWRI set and B_CACHE clear must
2156		 * be committed before we can return the buffer in
2157		 * order to prevent the caller from issuing a read
2158		 * ( due to B_CACHE not being set ) and overwriting
2159		 * it.
2160		 *
2161		 * Most callers, including NFS and FFS, need this to
2162		 * operate properly either because they assume they
2163		 * can issue a read if B_CACHE is not set, or because
2164		 * ( for example ) an uncached B_DELWRI might loop due
2165		 * to softupdates re-dirtying the buffer.  In the latter
2166		 * case, B_CACHE is set after the first write completes,
2167		 * preventing further loops.
2168		 */
2169
2170		if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2171			BUF_WRITE(bp);
2172			goto loop;
2173		}
2174
2175		splx(s);
2176		bp->b_flags &= ~B_DONE;
2177	} else {
2178		/*
2179		 * Buffer is not in-core, create new buffer.  The buffer
2180		 * returned by getnewbuf() is locked.  Note that the returned
2181		 * buffer is also considered valid (not marked B_INVAL).
2182		 */
2183		int bsize, maxsize, vmio;
2184		off_t offset;
2185
2186		if (vn_isdisk(vp, NULL))
2187			bsize = DEV_BSIZE;
2188		else if (vp->v_mountedhere)
2189			bsize = vp->v_mountedhere->mnt_stat.f_iosize;
2190		else if (vp->v_mount)
2191			bsize = vp->v_mount->mnt_stat.f_iosize;
2192		else
2193			bsize = size;
2194
2195		offset = (off_t)blkno * bsize;
2196		vmio = (vp->v_object != 0) && (vp->v_flag & VOBJBUF);
2197		maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2198		maxsize = imax(maxsize, bsize);
2199
2200		if ((bp = getnewbuf(slpflag, slptimeo, size, maxsize)) == NULL) {
2201			if (slpflag || slptimeo) {
2202				splx(s);
2203				return NULL;
2204			}
2205			goto loop;
2206		}
2207
2208		/*
2209		 * This code is used to make sure that a buffer is not
2210		 * created while the getnewbuf routine is blocked.
2211		 * This can be a problem whether the vnode is locked or not.
2212		 * If the buffer is created out from under us, we have to
2213		 * throw away the one we just created.  There is now window
2214		 * race because we are safely running at splbio() from the
2215		 * point of the duplicate buffer creation through to here,
2216		 * and we've locked the buffer.
2217		 */
2218		if (gbincore(vp, blkno)) {
2219			bp->b_flags |= B_INVAL;
2220			brelse(bp);
2221			goto loop;
2222		}
2223
2224		/*
2225		 * Insert the buffer into the hash, so that it can
2226		 * be found by incore.
2227		 */
2228		bp->b_blkno = bp->b_lblkno = blkno;
2229		bp->b_offset = offset;
2230
2231		bgetvp(vp, bp);
2232		LIST_REMOVE(bp, b_hash);
2233		bh = bufhash(vp, blkno);
2234		LIST_INSERT_HEAD(bh, bp, b_hash);
2235
2236		/*
2237		 * set B_VMIO bit.  allocbuf() the buffer bigger.  Since the
2238		 * buffer size starts out as 0, B_CACHE will be set by
2239		 * allocbuf() for the VMIO case prior to it testing the
2240		 * backing store for validity.
2241		 */
2242
2243		if (vmio) {
2244			bp->b_flags |= B_VMIO;
2245#if defined(VFS_BIO_DEBUG)
2246			if (vp->v_type != VREG && vp->v_type != VBLK)
2247				printf("getblk: vmioing file type %d???\n", vp->v_type);
2248#endif
2249		} else {
2250			bp->b_flags &= ~B_VMIO;
2251		}
2252
2253		allocbuf(bp, size);
2254
2255		splx(s);
2256		bp->b_flags &= ~B_DONE;
2257	}
2258	return (bp);
2259}
2260
2261/*
2262 * Get an empty, disassociated buffer of given size.  The buffer is initially
2263 * set to B_INVAL.
2264 */
2265struct buf *
2266geteblk(int size)
2267{
2268	struct buf *bp;
2269	int s;
2270	int maxsize;
2271
2272	maxsize = (size + BKVAMASK) & ~BKVAMASK;
2273
2274	s = splbio();
2275	while ((bp = getnewbuf(0, 0, size, maxsize)) == 0);
2276	splx(s);
2277	allocbuf(bp, size);
2278	bp->b_flags |= B_INVAL;	/* b_dep cleared by getnewbuf() */
2279	return (bp);
2280}
2281
2282
2283/*
2284 * This code constitutes the buffer memory from either anonymous system
2285 * memory (in the case of non-VMIO operations) or from an associated
2286 * VM object (in the case of VMIO operations).  This code is able to
2287 * resize a buffer up or down.
2288 *
2289 * Note that this code is tricky, and has many complications to resolve
2290 * deadlock or inconsistant data situations.  Tread lightly!!!
2291 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2292 * the caller.  Calling this code willy nilly can result in the loss of data.
2293 *
2294 * allocbuf() only adjusts B_CACHE for VMIO buffers.  getblk() deals with
2295 * B_CACHE for the non-VMIO case.
2296 */
2297
2298int
2299allocbuf(struct buf *bp, int size)
2300{
2301	int newbsize, mbsize;
2302	int i;
2303
2304	if (BUF_REFCNT(bp) == 0)
2305		panic("allocbuf: buffer not busy");
2306
2307	if (bp->b_kvasize < size)
2308		panic("allocbuf: buffer too small");
2309
2310	if ((bp->b_flags & B_VMIO) == 0) {
2311		caddr_t origbuf;
2312		int origbufsize;
2313		/*
2314		 * Just get anonymous memory from the kernel.  Don't
2315		 * mess with B_CACHE.
2316		 */
2317		mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2318#if !defined(NO_B_MALLOC)
2319		if (bp->b_flags & B_MALLOC)
2320			newbsize = mbsize;
2321		else
2322#endif
2323			newbsize = round_page(size);
2324
2325		if (newbsize < bp->b_bufsize) {
2326#if !defined(NO_B_MALLOC)
2327			/*
2328			 * malloced buffers are not shrunk
2329			 */
2330			if (bp->b_flags & B_MALLOC) {
2331				if (newbsize) {
2332					bp->b_bcount = size;
2333				} else {
2334					free(bp->b_data, M_BIOBUF);
2335					bufmallocspace -= bp->b_bufsize;
2336					runningbufspace -= bp->b_bufsize;
2337					if (bp->b_bufsize)
2338						bufspacewakeup();
2339					bp->b_data = bp->b_kvabase;
2340					bp->b_bufsize = 0;
2341					bp->b_bcount = 0;
2342					bp->b_flags &= ~B_MALLOC;
2343				}
2344				return 1;
2345			}
2346#endif
2347			vm_hold_free_pages(
2348			    bp,
2349			    (vm_offset_t) bp->b_data + newbsize,
2350			    (vm_offset_t) bp->b_data + bp->b_bufsize);
2351		} else if (newbsize > bp->b_bufsize) {
2352#if !defined(NO_B_MALLOC)
2353			/*
2354			 * We only use malloced memory on the first allocation.
2355			 * and revert to page-allocated memory when the buffer
2356			 * grows.
2357			 */
2358			if ( (bufmallocspace < maxbufmallocspace) &&
2359				(bp->b_bufsize == 0) &&
2360				(mbsize <= PAGE_SIZE/2)) {
2361
2362				bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2363				bp->b_bufsize = mbsize;
2364				bp->b_bcount = size;
2365				bp->b_flags |= B_MALLOC;
2366				bufmallocspace += mbsize;
2367				runningbufspace += bp->b_bufsize;
2368				return 1;
2369			}
2370#endif
2371			origbuf = NULL;
2372			origbufsize = 0;
2373#if !defined(NO_B_MALLOC)
2374			/*
2375			 * If the buffer is growing on its other-than-first allocation,
2376			 * then we revert to the page-allocation scheme.
2377			 */
2378			if (bp->b_flags & B_MALLOC) {
2379				origbuf = bp->b_data;
2380				origbufsize = bp->b_bufsize;
2381				bp->b_data = bp->b_kvabase;
2382				bufmallocspace -= bp->b_bufsize;
2383				runningbufspace -= bp->b_bufsize;
2384				if (bp->b_bufsize)
2385					bufspacewakeup();
2386				bp->b_bufsize = 0;
2387				bp->b_flags &= ~B_MALLOC;
2388				newbsize = round_page(newbsize);
2389			}
2390#endif
2391			vm_hold_load_pages(
2392			    bp,
2393			    (vm_offset_t) bp->b_data + bp->b_bufsize,
2394			    (vm_offset_t) bp->b_data + newbsize);
2395#if !defined(NO_B_MALLOC)
2396			if (origbuf) {
2397				bcopy(origbuf, bp->b_data, origbufsize);
2398				free(origbuf, M_BIOBUF);
2399			}
2400#endif
2401		}
2402	} else {
2403		vm_page_t m;
2404		int desiredpages;
2405
2406		newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2407		desiredpages = (size == 0) ? 0 :
2408			num_pages((bp->b_offset & PAGE_MASK) + newbsize);
2409
2410#if !defined(NO_B_MALLOC)
2411		if (bp->b_flags & B_MALLOC)
2412			panic("allocbuf: VMIO buffer can't be malloced");
2413#endif
2414		/*
2415		 * Set B_CACHE initially if buffer is 0 length or will become
2416		 * 0-length.
2417		 */
2418		if (size == 0 || bp->b_bufsize == 0)
2419			bp->b_flags |= B_CACHE;
2420
2421		if (newbsize < bp->b_bufsize) {
2422			/*
2423			 * DEV_BSIZE aligned new buffer size is less then the
2424			 * DEV_BSIZE aligned existing buffer size.  Figure out
2425			 * if we have to remove any pages.
2426			 */
2427			if (desiredpages < bp->b_npages) {
2428				for (i = desiredpages; i < bp->b_npages; i++) {
2429					/*
2430					 * the page is not freed here -- it
2431					 * is the responsibility of
2432					 * vnode_pager_setsize
2433					 */
2434					m = bp->b_pages[i];
2435					KASSERT(m != bogus_page,
2436					    ("allocbuf: bogus page found"));
2437					while (vm_page_sleep_busy(m, TRUE, "biodep"))
2438						;
2439
2440					bp->b_pages[i] = NULL;
2441					vm_page_unwire(m, 0);
2442				}
2443				pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) +
2444				    (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages));
2445				bp->b_npages = desiredpages;
2446			}
2447		} else if (size > bp->b_bcount) {
2448			/*
2449			 * We are growing the buffer, possibly in a
2450			 * byte-granular fashion.
2451			 */
2452			struct vnode *vp;
2453			vm_object_t obj;
2454			vm_offset_t toff;
2455			vm_offset_t tinc;
2456
2457			/*
2458			 * Step 1, bring in the VM pages from the object,
2459			 * allocating them if necessary.  We must clear
2460			 * B_CACHE if these pages are not valid for the
2461			 * range covered by the buffer.
2462			 */
2463
2464			vp = bp->b_vp;
2465			obj = vp->v_object;
2466
2467			while (bp->b_npages < desiredpages) {
2468				vm_page_t m;
2469				vm_pindex_t pi;
2470
2471				pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages;
2472				if ((m = vm_page_lookup(obj, pi)) == NULL) {
2473					m = vm_page_alloc(obj, pi, VM_ALLOC_NORMAL);
2474					if (m == NULL) {
2475						VM_WAIT;
2476						vm_pageout_deficit += desiredpages - bp->b_npages;
2477					} else {
2478						vm_page_wire(m);
2479						vm_page_wakeup(m);
2480						bp->b_flags &= ~B_CACHE;
2481						bp->b_pages[bp->b_npages] = m;
2482						++bp->b_npages;
2483					}
2484					continue;
2485				}
2486
2487				/*
2488				 * We found a page.  If we have to sleep on it,
2489				 * retry because it might have gotten freed out
2490				 * from under us.
2491				 *
2492				 * We can only test PG_BUSY here.  Blocking on
2493				 * m->busy might lead to a deadlock:
2494				 *
2495				 *  vm_fault->getpages->cluster_read->allocbuf
2496				 *
2497				 */
2498
2499				if (vm_page_sleep_busy(m, FALSE, "pgtblk"))
2500					continue;
2501
2502				/*
2503				 * We have a good page.  Should we wakeup the
2504				 * page daemon?
2505				 */
2506				if ((curproc != pageproc) &&
2507				    ((m->queue - m->pc) == PQ_CACHE) &&
2508				    ((cnt.v_free_count + cnt.v_cache_count) <
2509					(cnt.v_free_min + cnt.v_cache_min))) {
2510					pagedaemon_wakeup();
2511				}
2512				vm_page_flag_clear(m, PG_ZERO);
2513				vm_page_wire(m);
2514				bp->b_pages[bp->b_npages] = m;
2515				++bp->b_npages;
2516			}
2517
2518			/*
2519			 * Step 2.  We've loaded the pages into the buffer,
2520			 * we have to figure out if we can still have B_CACHE
2521			 * set.  Note that B_CACHE is set according to the
2522			 * byte-granular range ( bcount and size ), new the
2523			 * aligned range ( newbsize ).
2524			 *
2525			 * The VM test is against m->valid, which is DEV_BSIZE
2526			 * aligned.  Needless to say, the validity of the data
2527			 * needs to also be DEV_BSIZE aligned.  Note that this
2528			 * fails with NFS if the server or some other client
2529			 * extends the file's EOF.  If our buffer is resized,
2530			 * B_CACHE may remain set! XXX
2531			 */
2532
2533			toff = bp->b_bcount;
2534			tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
2535
2536			while ((bp->b_flags & B_CACHE) && toff < size) {
2537				vm_pindex_t pi;
2538
2539				if (tinc > (size - toff))
2540					tinc = size - toff;
2541
2542				pi = ((bp->b_offset & PAGE_MASK) + toff) >>
2543				    PAGE_SHIFT;
2544
2545				vfs_buf_test_cache(
2546				    bp,
2547				    bp->b_offset,
2548				    toff,
2549				    tinc,
2550				    bp->b_pages[pi]
2551				);
2552				toff += tinc;
2553				tinc = PAGE_SIZE;
2554			}
2555
2556			/*
2557			 * Step 3, fixup the KVM pmap.  Remember that
2558			 * bp->b_data is relative to bp->b_offset, but
2559			 * bp->b_offset may be offset into the first page.
2560			 */
2561
2562			bp->b_data = (caddr_t)
2563			    trunc_page((vm_offset_t)bp->b_data);
2564			pmap_qenter(
2565			    (vm_offset_t)bp->b_data,
2566			    bp->b_pages,
2567			    bp->b_npages
2568			);
2569			bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
2570			    (vm_offset_t)(bp->b_offset & PAGE_MASK));
2571		}
2572	}
2573	runningbufspace += (newbsize - bp->b_bufsize);
2574	if (newbsize < bp->b_bufsize)
2575		bufspacewakeup();
2576	bp->b_bufsize = newbsize;	/* actual buffer allocation	*/
2577	bp->b_bcount = size;		/* requested buffer size	*/
2578	return 1;
2579}
2580
2581/*
2582 *	bufwait:
2583 *
2584 *	Wait for buffer I/O completion, returning error status.  The buffer
2585 *	is left locked and B_DONE on return.  B_EINTR is converted into a EINTR
2586 *	error and cleared.
2587 */
2588int
2589bufwait(register struct buf * bp)
2590{
2591	int s;
2592
2593	s = splbio();
2594	while ((bp->b_flags & B_DONE) == 0) {
2595		if (bp->b_iocmd == BIO_READ)
2596			tsleep(bp, PRIBIO, "biord", 0);
2597		else
2598			tsleep(bp, PRIBIO, "biowr", 0);
2599	}
2600	splx(s);
2601	if (bp->b_flags & B_EINTR) {
2602		bp->b_flags &= ~B_EINTR;
2603		return (EINTR);
2604	}
2605	if (bp->b_ioflags & BIO_ERROR) {
2606		return (bp->b_error ? bp->b_error : EIO);
2607	} else {
2608		return (0);
2609	}
2610}
2611
2612 /*
2613  * Call back function from struct bio back up to struct buf.
2614  * The corresponding initialization lives in sys/conf.h:DEV_STRATEGY().
2615  */
2616void
2617bufdonebio(struct bio *bp)
2618{
2619	bufdone(bp->bio_caller2);
2620}
2621
2622/*
2623 *	bufdone:
2624 *
2625 *	Finish I/O on a buffer, optionally calling a completion function.
2626 *	This is usually called from an interrupt so process blocking is
2627 *	not allowed.
2628 *
2629 *	biodone is also responsible for setting B_CACHE in a B_VMIO bp.
2630 *	In a non-VMIO bp, B_CACHE will be set on the next getblk()
2631 *	assuming B_INVAL is clear.
2632 *
2633 *	For the VMIO case, we set B_CACHE if the op was a read and no
2634 *	read error occured, or if the op was a write.  B_CACHE is never
2635 *	set if the buffer is invalid or otherwise uncacheable.
2636 *
2637 *	biodone does not mess with B_INVAL, allowing the I/O routine or the
2638 *	initiator to leave B_INVAL set to brelse the buffer out of existance
2639 *	in the biodone routine.
2640 */
2641void
2642bufdone(struct buf *bp)
2643{
2644	int s;
2645	void    (*biodone) __P((struct buf *));
2646
2647	s = splbio();
2648
2649	KASSERT(BUF_REFCNT(bp) > 0, ("biodone: bp %p not busy %d", bp, BUF_REFCNT(bp)));
2650	KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
2651
2652	bp->b_flags |= B_DONE;
2653
2654	if (bp->b_iocmd == BIO_DELETE) {
2655		brelse(bp);
2656		splx(s);
2657		return;
2658	}
2659
2660	if (bp->b_iocmd == BIO_WRITE) {
2661		vwakeup(bp);
2662	}
2663
2664	/* call optional completion function if requested */
2665	if (bp->b_iodone != NULL) {
2666		biodone = bp->b_iodone;
2667		bp->b_iodone = NULL;
2668		(*biodone) (bp);
2669		splx(s);
2670		return;
2671	}
2672	if (LIST_FIRST(&bp->b_dep) != NULL)
2673		buf_complete(bp);
2674
2675	if (bp->b_flags & B_VMIO) {
2676		int i, resid;
2677		vm_ooffset_t foff;
2678		vm_page_t m;
2679		vm_object_t obj;
2680		int iosize;
2681		struct vnode *vp = bp->b_vp;
2682
2683		obj = vp->v_object;
2684
2685#if defined(VFS_BIO_DEBUG)
2686		if (vp->v_usecount == 0) {
2687			panic("biodone: zero vnode ref count");
2688		}
2689
2690		if (vp->v_object == NULL) {
2691			panic("biodone: missing VM object");
2692		}
2693
2694		if ((vp->v_flag & VOBJBUF) == 0) {
2695			panic("biodone: vnode is not setup for merged cache");
2696		}
2697#endif
2698
2699		foff = bp->b_offset;
2700		KASSERT(bp->b_offset != NOOFFSET,
2701		    ("biodone: no buffer offset"));
2702
2703		if (!obj) {
2704			panic("biodone: no object");
2705		}
2706#if defined(VFS_BIO_DEBUG)
2707		if (obj->paging_in_progress < bp->b_npages) {
2708			printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
2709			    obj->paging_in_progress, bp->b_npages);
2710		}
2711#endif
2712
2713		/*
2714		 * Set B_CACHE if the op was a normal read and no error
2715		 * occured.  B_CACHE is set for writes in the b*write()
2716		 * routines.
2717		 */
2718		iosize = bp->b_bcount - bp->b_resid;
2719		if (bp->b_iocmd == BIO_READ &&
2720		    !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
2721		    !(bp->b_ioflags & BIO_ERROR)) {
2722			bp->b_flags |= B_CACHE;
2723		}
2724
2725		for (i = 0; i < bp->b_npages; i++) {
2726			int bogusflag = 0;
2727			m = bp->b_pages[i];
2728			if (m == bogus_page) {
2729				bogusflag = 1;
2730				m = vm_page_lookup(obj, OFF_TO_IDX(foff));
2731				if (!m) {
2732#if defined(VFS_BIO_DEBUG)
2733					printf("biodone: page disappeared\n");
2734#endif
2735					vm_object_pip_subtract(obj, 1);
2736					bp->b_flags &= ~B_CACHE;
2737					continue;
2738				}
2739				bp->b_pages[i] = m;
2740				pmap_qenter(trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages);
2741			}
2742#if defined(VFS_BIO_DEBUG)
2743			if (OFF_TO_IDX(foff) != m->pindex) {
2744				printf(
2745"biodone: foff(%lu)/m->pindex(%d) mismatch\n",
2746				    (unsigned long)foff, m->pindex);
2747			}
2748#endif
2749			resid = IDX_TO_OFF(m->pindex + 1) - foff;
2750			if (resid > iosize)
2751				resid = iosize;
2752
2753			/*
2754			 * In the write case, the valid and clean bits are
2755			 * already changed correctly ( see bdwrite() ), so we
2756			 * only need to do this here in the read case.
2757			 */
2758			if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
2759				vfs_page_set_valid(bp, foff, i, m);
2760			}
2761			vm_page_flag_clear(m, PG_ZERO);
2762
2763			/*
2764			 * when debugging new filesystems or buffer I/O methods, this
2765			 * is the most common error that pops up.  if you see this, you
2766			 * have not set the page busy flag correctly!!!
2767			 */
2768			if (m->busy == 0) {
2769				printf("biodone: page busy < 0, "
2770				    "pindex: %d, foff: 0x(%x,%x), "
2771				    "resid: %d, index: %d\n",
2772				    (int) m->pindex, (int)(foff >> 32),
2773						(int) foff & 0xffffffff, resid, i);
2774				if (!vn_isdisk(vp, NULL))
2775					printf(" iosize: %ld, lblkno: %d, flags: 0x%lx, npages: %d\n",
2776					    bp->b_vp->v_mount->mnt_stat.f_iosize,
2777					    (int) bp->b_lblkno,
2778					    bp->b_flags, bp->b_npages);
2779				else
2780					printf(" VDEV, lblkno: %d, flags: 0x%lx, npages: %d\n",
2781					    (int) bp->b_lblkno,
2782					    bp->b_flags, bp->b_npages);
2783				printf(" valid: 0x%x, dirty: 0x%x, wired: %d\n",
2784				    m->valid, m->dirty, m->wire_count);
2785				panic("biodone: page busy < 0\n");
2786			}
2787			vm_page_io_finish(m);
2788			vm_object_pip_subtract(obj, 1);
2789			foff += resid;
2790			iosize -= resid;
2791		}
2792		if (obj)
2793			vm_object_pip_wakeupn(obj, 0);
2794	}
2795	/*
2796	 * For asynchronous completions, release the buffer now. The brelse
2797	 * will do a wakeup there if necessary - so no need to do a wakeup
2798	 * here in the async case. The sync case always needs to do a wakeup.
2799	 */
2800
2801	if (bp->b_flags & B_ASYNC) {
2802		if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
2803			brelse(bp);
2804		else
2805			bqrelse(bp);
2806	} else {
2807		wakeup(bp);
2808	}
2809	splx(s);
2810}
2811
2812/*
2813 * This routine is called in lieu of iodone in the case of
2814 * incomplete I/O.  This keeps the busy status for pages
2815 * consistant.
2816 */
2817void
2818vfs_unbusy_pages(struct buf * bp)
2819{
2820	int i;
2821
2822	if (bp->b_flags & B_VMIO) {
2823		struct vnode *vp = bp->b_vp;
2824		vm_object_t obj = vp->v_object;
2825
2826		for (i = 0; i < bp->b_npages; i++) {
2827			vm_page_t m = bp->b_pages[i];
2828
2829			if (m == bogus_page) {
2830				m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
2831				if (!m) {
2832					panic("vfs_unbusy_pages: page missing\n");
2833				}
2834				bp->b_pages[i] = m;
2835				pmap_qenter(trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages);
2836			}
2837			vm_object_pip_subtract(obj, 1);
2838			vm_page_flag_clear(m, PG_ZERO);
2839			vm_page_io_finish(m);
2840		}
2841		vm_object_pip_wakeupn(obj, 0);
2842	}
2843}
2844
2845/*
2846 * vfs_page_set_valid:
2847 *
2848 *	Set the valid bits in a page based on the supplied offset.   The
2849 *	range is restricted to the buffer's size.
2850 *
2851 *	This routine is typically called after a read completes.
2852 */
2853static void
2854vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, int pageno, vm_page_t m)
2855{
2856	vm_ooffset_t soff, eoff;
2857
2858	/*
2859	 * Start and end offsets in buffer.  eoff - soff may not cross a
2860	 * page boundry or cross the end of the buffer.  The end of the
2861	 * buffer, in this case, is our file EOF, not the allocation size
2862	 * of the buffer.
2863	 */
2864	soff = off;
2865	eoff = (off + PAGE_SIZE) & ~PAGE_MASK;
2866	if (eoff > bp->b_offset + bp->b_bcount)
2867		eoff = bp->b_offset + bp->b_bcount;
2868
2869	/*
2870	 * Set valid range.  This is typically the entire buffer and thus the
2871	 * entire page.
2872	 */
2873	if (eoff > soff) {
2874		vm_page_set_validclean(
2875		    m,
2876		   (vm_offset_t) (soff & PAGE_MASK),
2877		   (vm_offset_t) (eoff - soff)
2878		);
2879	}
2880}
2881
2882/*
2883 * This routine is called before a device strategy routine.
2884 * It is used to tell the VM system that paging I/O is in
2885 * progress, and treat the pages associated with the buffer
2886 * almost as being PG_BUSY.  Also the object paging_in_progress
2887 * flag is handled to make sure that the object doesn't become
2888 * inconsistant.
2889 *
2890 * Since I/O has not been initiated yet, certain buffer flags
2891 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
2892 * and should be ignored.
2893 */
2894void
2895vfs_busy_pages(struct buf * bp, int clear_modify)
2896{
2897	int i, bogus;
2898
2899	if (bp->b_flags & B_VMIO) {
2900		struct vnode *vp = bp->b_vp;
2901		vm_object_t obj = vp->v_object;
2902		vm_ooffset_t foff;
2903
2904		foff = bp->b_offset;
2905		KASSERT(bp->b_offset != NOOFFSET,
2906		    ("vfs_busy_pages: no buffer offset"));
2907		vfs_setdirty(bp);
2908
2909retry:
2910		for (i = 0; i < bp->b_npages; i++) {
2911			vm_page_t m = bp->b_pages[i];
2912			if (vm_page_sleep_busy(m, FALSE, "vbpage"))
2913				goto retry;
2914		}
2915
2916		bogus = 0;
2917		for (i = 0; i < bp->b_npages; i++) {
2918			vm_page_t m = bp->b_pages[i];
2919
2920			vm_page_flag_clear(m, PG_ZERO);
2921			if ((bp->b_flags & B_CLUSTER) == 0) {
2922				vm_object_pip_add(obj, 1);
2923				vm_page_io_start(m);
2924			}
2925
2926			/*
2927			 * When readying a buffer for a read ( i.e
2928			 * clear_modify == 0 ), it is important to do
2929			 * bogus_page replacement for valid pages in
2930			 * partially instantiated buffers.  Partially
2931			 * instantiated buffers can, in turn, occur when
2932			 * reconstituting a buffer from its VM backing store
2933			 * base.  We only have to do this if B_CACHE is
2934			 * clear ( which causes the I/O to occur in the
2935			 * first place ).  The replacement prevents the read
2936			 * I/O from overwriting potentially dirty VM-backed
2937			 * pages.  XXX bogus page replacement is, uh, bogus.
2938			 * It may not work properly with small-block devices.
2939			 * We need to find a better way.
2940			 */
2941
2942			vm_page_protect(m, VM_PROT_NONE);
2943			if (clear_modify)
2944				vfs_page_set_valid(bp, foff, i, m);
2945			else if (m->valid == VM_PAGE_BITS_ALL &&
2946				(bp->b_flags & B_CACHE) == 0) {
2947				bp->b_pages[i] = bogus_page;
2948				bogus++;
2949			}
2950			foff = (foff + PAGE_SIZE) & ~PAGE_MASK;
2951		}
2952		if (bogus)
2953			pmap_qenter(trunc_page((vm_offset_t)bp->b_data), bp->b_pages, bp->b_npages);
2954	}
2955}
2956
2957/*
2958 * Tell the VM system that the pages associated with this buffer
2959 * are clean.  This is used for delayed writes where the data is
2960 * going to go to disk eventually without additional VM intevention.
2961 *
2962 * Note that while we only really need to clean through to b_bcount, we
2963 * just go ahead and clean through to b_bufsize.
2964 */
2965static void
2966vfs_clean_pages(struct buf * bp)
2967{
2968	int i;
2969
2970	if (bp->b_flags & B_VMIO) {
2971		vm_ooffset_t foff;
2972
2973		foff = bp->b_offset;
2974		KASSERT(bp->b_offset != NOOFFSET,
2975		    ("vfs_clean_pages: no buffer offset"));
2976		for (i = 0; i < bp->b_npages; i++) {
2977			vm_page_t m = bp->b_pages[i];
2978			vm_ooffset_t noff = (foff + PAGE_SIZE) & ~PAGE_MASK;
2979			vm_ooffset_t eoff = noff;
2980
2981			if (eoff > bp->b_offset + bp->b_bufsize)
2982				eoff = bp->b_offset + bp->b_bufsize;
2983			vfs_page_set_valid(bp, foff, i, m);
2984			/* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2985			foff = noff;
2986		}
2987	}
2988}
2989
2990/*
2991 *	vfs_bio_set_validclean:
2992 *
2993 *	Set the range within the buffer to valid and clean.  The range is
2994 *	relative to the beginning of the buffer, b_offset.  Note that b_offset
2995 *	itself may be offset from the beginning of the first page.
2996 */
2997
2998void
2999vfs_bio_set_validclean(struct buf *bp, int base, int size)
3000{
3001	if (bp->b_flags & B_VMIO) {
3002		int i;
3003		int n;
3004
3005		/*
3006		 * Fixup base to be relative to beginning of first page.
3007		 * Set initial n to be the maximum number of bytes in the
3008		 * first page that can be validated.
3009		 */
3010
3011		base += (bp->b_offset & PAGE_MASK);
3012		n = PAGE_SIZE - (base & PAGE_MASK);
3013
3014		for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3015			vm_page_t m = bp->b_pages[i];
3016
3017			if (n > size)
3018				n = size;
3019
3020			vm_page_set_validclean(m, base & PAGE_MASK, n);
3021			base += n;
3022			size -= n;
3023			n = PAGE_SIZE;
3024		}
3025	}
3026}
3027
3028/*
3029 *	vfs_bio_clrbuf:
3030 *
3031 *	clear a buffer.  This routine essentially fakes an I/O, so we need
3032 *	to clear BIO_ERROR and B_INVAL.
3033 *
3034 *	Note that while we only theoretically need to clear through b_bcount,
3035 *	we go ahead and clear through b_bufsize.
3036 */
3037
3038void
3039vfs_bio_clrbuf(struct buf *bp) {
3040	int i, mask = 0;
3041	caddr_t sa, ea;
3042	if ((bp->b_flags & (B_VMIO | B_MALLOC)) == B_VMIO) {
3043		bp->b_flags &= ~B_INVAL;
3044		bp->b_ioflags &= ~BIO_ERROR;
3045		if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3046		    (bp->b_offset & PAGE_MASK) == 0) {
3047			mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3048			if (((bp->b_pages[0]->flags & PG_ZERO) == 0) &&
3049			    ((bp->b_pages[0]->valid & mask) != mask)) {
3050				bzero(bp->b_data, bp->b_bufsize);
3051			}
3052			bp->b_pages[0]->valid |= mask;
3053			bp->b_resid = 0;
3054			return;
3055		}
3056		ea = sa = bp->b_data;
3057		for(i=0;i<bp->b_npages;i++,sa=ea) {
3058			int j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3059			ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3060			ea = (caddr_t)(vm_offset_t)ulmin(
3061			    (u_long)(vm_offset_t)ea,
3062			    (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3063			mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3064			if ((bp->b_pages[i]->valid & mask) == mask)
3065				continue;
3066			if ((bp->b_pages[i]->valid & mask) == 0) {
3067				if ((bp->b_pages[i]->flags & PG_ZERO) == 0) {
3068					bzero(sa, ea - sa);
3069				}
3070			} else {
3071				for (; sa < ea; sa += DEV_BSIZE, j++) {
3072					if (((bp->b_pages[i]->flags & PG_ZERO) == 0) &&
3073						(bp->b_pages[i]->valid & (1<<j)) == 0)
3074						bzero(sa, DEV_BSIZE);
3075				}
3076			}
3077			bp->b_pages[i]->valid |= mask;
3078			vm_page_flag_clear(bp->b_pages[i], PG_ZERO);
3079		}
3080		bp->b_resid = 0;
3081	} else {
3082		clrbuf(bp);
3083	}
3084}
3085
3086/*
3087 * vm_hold_load_pages and vm_hold_unload pages get pages into
3088 * a buffers address space.  The pages are anonymous and are
3089 * not associated with a file object.
3090 */
3091void
3092vm_hold_load_pages(struct buf * bp, vm_offset_t from, vm_offset_t to)
3093{
3094	vm_offset_t pg;
3095	vm_page_t p;
3096	int index;
3097
3098	to = round_page(to);
3099	from = round_page(from);
3100	index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3101
3102	for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3103
3104tryagain:
3105
3106		p = vm_page_alloc(kernel_object,
3107			((pg - VM_MIN_KERNEL_ADDRESS) >> PAGE_SHIFT),
3108		    VM_ALLOC_NORMAL);
3109		if (!p) {
3110			vm_pageout_deficit += (to - from) >> PAGE_SHIFT;
3111			VM_WAIT;
3112			goto tryagain;
3113		}
3114		vm_page_wire(p);
3115		p->valid = VM_PAGE_BITS_ALL;
3116		vm_page_flag_clear(p, PG_ZERO);
3117		pmap_kenter(pg, VM_PAGE_TO_PHYS(p));
3118		bp->b_pages[index] = p;
3119		vm_page_wakeup(p);
3120	}
3121	bp->b_npages = index;
3122}
3123
3124void
3125vm_hold_free_pages(struct buf * bp, vm_offset_t from, vm_offset_t to)
3126{
3127	vm_offset_t pg;
3128	vm_page_t p;
3129	int index, newnpages;
3130
3131	from = round_page(from);
3132	to = round_page(to);
3133	newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3134
3135	for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3136		p = bp->b_pages[index];
3137		if (p && (index < bp->b_npages)) {
3138			if (p->busy) {
3139				printf("vm_hold_free_pages: blkno: %d, lblkno: %d\n",
3140					bp->b_blkno, bp->b_lblkno);
3141			}
3142			bp->b_pages[index] = NULL;
3143			pmap_kremove(pg);
3144			vm_page_busy(p);
3145			vm_page_unwire(p, 0);
3146			vm_page_free(p);
3147		}
3148	}
3149	bp->b_npages = newnpages;
3150}
3151
3152
3153#include "opt_ddb.h"
3154#ifdef DDB
3155#include <ddb/ddb.h>
3156
3157DB_SHOW_COMMAND(buffer, db_show_buffer)
3158{
3159	/* get args */
3160	struct buf *bp = (struct buf *)addr;
3161
3162	if (!have_addr) {
3163		db_printf("usage: show buffer <addr>\n");
3164		return;
3165	}
3166
3167	db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS);
3168	db_printf("b_error = %d, b_bufsize = %ld, b_bcount = %ld, "
3169		  "b_resid = %ld\nb_dev = (%d,%d), b_data = %p, "
3170		  "b_blkno = %d, b_pblkno = %d\n",
3171		  bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
3172		  major(bp->b_dev), minor(bp->b_dev),
3173		  bp->b_data, bp->b_blkno, bp->b_pblkno);
3174	if (bp->b_npages) {
3175		int i;
3176		db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
3177		for (i = 0; i < bp->b_npages; i++) {
3178			vm_page_t m;
3179			m = bp->b_pages[i];
3180			db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
3181			    (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
3182			if ((i + 1) < bp->b_npages)
3183				db_printf(",");
3184		}
3185		db_printf("\n");
3186	}
3187}
3188#endif /* DDB */
3189