vfs_bio.c revision 11332
1/*
2 * Copyright (c) 1994 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. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
15 *    John S. Dyson.
16 * 4. This work was done expressly for inclusion into FreeBSD.  Other use
17 *    is allowed if this notation is included.
18 * 5. Modifications may be freely made to this file if the above conditions
19 *    are met.
20 *
21 * $Id: vfs_bio.c,v 1.65 1995/10/01 05:50:27 davidg Exp $
22 */
23
24/*
25 * this file contains a new buffer I/O scheme implementing a coherent
26 * VM object and buffer cache scheme.  Pains have been taken to make
27 * sure that the performance degradation associated with schemes such
28 * as this is not realized.
29 *
30 * Author:  John S. Dyson
31 * Significant help during the development and debugging phases
32 * had been provided by David Greenman, also of the FreeBSD core team.
33 */
34
35#define VMIO
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/sysproto.h>
39#include <sys/kernel.h>
40#include <sys/proc.h>
41#include <sys/vnode.h>
42#include <vm/vm.h>
43#include <vm/vm_kern.h>
44#include <vm/vm_pageout.h>
45#include <vm/vm_page.h>
46#include <vm/vm_object.h>
47#include <sys/buf.h>
48#include <sys/mount.h>
49#include <sys/malloc.h>
50#include <sys/resourcevar.h>
51#include <sys/proc.h>
52
53#include <miscfs/specfs/specdev.h>
54
55/*
56 * System initialization
57 */
58
59static void vfs_update __P((void));
60struct	proc *updateproc;
61
62static struct kproc_desc up_kp = {
63	"update",
64	vfs_update,
65	&updateproc
66};
67SYSINIT_KT(update, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
68
69
70struct buf *buf;		/* buffer header pool */
71struct swqueue bswlist;
72
73void vm_hold_free_pages(struct buf * bp, vm_offset_t from, vm_offset_t to);
74void vm_hold_load_pages(struct buf * bp, vm_offset_t from, vm_offset_t to);
75void vfs_clean_pages(struct buf * bp);
76static void vfs_setdirty(struct buf *bp);
77static __inline struct buf * gbincore(struct vnode * vp, daddr_t blkno);
78
79int needsbuffer;
80
81/*
82 * Internal update daemon, process 3
83 *	The variable vfs_update_wakeup allows for internal syncs.
84 */
85int vfs_update_wakeup;
86
87
88/*
89 * buffers base kva
90 */
91caddr_t buffers_kva;
92
93/*
94 * bogus page -- for I/O to/from partially complete buffers
95 * this is a temporary solution to the problem, but it is not
96 * really that bad.  it would be better to split the buffer
97 * for input in the case of buffers partially already in memory,
98 * but the code is intricate enough already.
99 */
100vm_page_t bogus_page;
101vm_offset_t bogus_offset;
102
103int bufspace, maxbufspace;
104
105/*
106 * advisory minimum for size of LRU queue or VMIO queue
107 */
108int minbuf;
109
110struct bufhashhdr bufhashtbl[BUFHSZ], invalhash;
111struct bqueues bufqueues[BUFFER_QUEUES];
112
113/*
114 * Initialize buffer headers and related structures.
115 */
116void
117bufinit()
118{
119	struct buf *bp;
120	int i;
121
122	TAILQ_INIT(&bswlist);
123	LIST_INIT(&invalhash);
124
125	/* first, make a null hash table */
126	for (i = 0; i < BUFHSZ; i++)
127		LIST_INIT(&bufhashtbl[i]);
128
129	/* next, make a null set of free lists */
130	for (i = 0; i < BUFFER_QUEUES; i++)
131		TAILQ_INIT(&bufqueues[i]);
132
133	buffers_kva = (caddr_t) kmem_alloc_pageable(buffer_map, MAXBSIZE * nbuf);
134	/* finally, initialize each buffer header and stick on empty q */
135	for (i = 0; i < nbuf; i++) {
136		bp = &buf[i];
137		bzero(bp, sizeof *bp);
138		bp->b_flags = B_INVAL;	/* we're just an empty header */
139		bp->b_dev = NODEV;
140		bp->b_rcred = NOCRED;
141		bp->b_wcred = NOCRED;
142		bp->b_qindex = QUEUE_EMPTY;
143		bp->b_vnbufs.le_next = NOLIST;
144		bp->b_data = buffers_kva + i * MAXBSIZE;
145		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
146		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
147	}
148/*
149 * maxbufspace is currently calculated to support all filesystem blocks
150 * to be 8K.  If you happen to use a 16K filesystem, the size of the buffer
151 * cache is still the same as it would be for 8K filesystems.  This
152 * keeps the size of the buffer cache "in check" for big block filesystems.
153 */
154	minbuf = nbuf / 3;
155	maxbufspace = 2 * (nbuf + 8) * PAGE_SIZE;
156
157	bogus_offset = kmem_alloc_pageable(kernel_map, PAGE_SIZE);
158	bogus_page = vm_page_alloc(kernel_object,
159			bogus_offset - VM_MIN_KERNEL_ADDRESS, VM_ALLOC_NORMAL);
160
161}
162
163/*
164 * remove the buffer from the appropriate free list
165 */
166void
167bremfree(struct buf * bp)
168{
169	int s = splbio();
170
171	if (bp->b_qindex != QUEUE_NONE) {
172		TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
173		bp->b_qindex = QUEUE_NONE;
174	} else {
175		panic("bremfree: removing a buffer when not on a queue");
176	}
177	splx(s);
178}
179
180/*
181 * Get a buffer with the specified data.  Look in the cache first.
182 */
183int
184bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
185    struct buf ** bpp)
186{
187	struct buf *bp;
188
189	bp = getblk(vp, blkno, size, 0, 0);
190	*bpp = bp;
191
192	/* if not found in cache, do some I/O */
193	if ((bp->b_flags & B_CACHE) == 0) {
194		if (curproc != NULL)
195			curproc->p_stats->p_ru.ru_inblock++;
196		bp->b_flags |= B_READ;
197		bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
198		if (bp->b_rcred == NOCRED) {
199			if (cred != NOCRED)
200				crhold(cred);
201			bp->b_rcred = cred;
202		}
203		vfs_busy_pages(bp, 0);
204		VOP_STRATEGY(bp);
205		return (biowait(bp));
206	}
207	return (0);
208}
209
210/*
211 * Operates like bread, but also starts asynchronous I/O on
212 * read-ahead blocks.
213 */
214int
215breadn(struct vnode * vp, daddr_t blkno, int size,
216    daddr_t * rablkno, int *rabsize,
217    int cnt, struct ucred * cred, struct buf ** bpp)
218{
219	struct buf *bp, *rabp;
220	int i;
221	int rv = 0, readwait = 0;
222
223	*bpp = bp = getblk(vp, blkno, size, 0, 0);
224
225	/* if not found in cache, do some I/O */
226	if ((bp->b_flags & B_CACHE) == 0) {
227		if (curproc != NULL)
228			curproc->p_stats->p_ru.ru_inblock++;
229		bp->b_flags |= B_READ;
230		bp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
231		if (bp->b_rcred == NOCRED) {
232			if (cred != NOCRED)
233				crhold(cred);
234			bp->b_rcred = cred;
235		}
236		vfs_busy_pages(bp, 0);
237		VOP_STRATEGY(bp);
238		++readwait;
239	}
240	for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
241		if (inmem(vp, *rablkno))
242			continue;
243		rabp = getblk(vp, *rablkno, *rabsize, 0, 0);
244
245		if ((rabp->b_flags & B_CACHE) == 0) {
246			if (curproc != NULL)
247				curproc->p_stats->p_ru.ru_inblock++;
248			rabp->b_flags |= B_READ | B_ASYNC;
249			rabp->b_flags &= ~(B_DONE | B_ERROR | B_INVAL);
250			if (rabp->b_rcred == NOCRED) {
251				if (cred != NOCRED)
252					crhold(cred);
253				rabp->b_rcred = cred;
254			}
255			vfs_busy_pages(rabp, 0);
256			VOP_STRATEGY(rabp);
257		} else {
258			brelse(rabp);
259		}
260	}
261
262	if (readwait) {
263		rv = biowait(bp);
264	}
265	return (rv);
266}
267
268/*
269 * Write, release buffer on completion.  (Done by iodone
270 * if async.)
271 */
272int
273bwrite(struct buf * bp)
274{
275	int oldflags = bp->b_flags;
276
277	if (bp->b_flags & B_INVAL) {
278		brelse(bp);
279		return (0);
280	}
281	if (!(bp->b_flags & B_BUSY))
282		panic("bwrite: buffer is not busy???");
283
284	bp->b_flags &= ~(B_READ | B_DONE | B_ERROR | B_DELWRI);
285	bp->b_flags |= B_WRITEINPROG;
286
287	if ((oldflags & (B_ASYNC|B_DELWRI)) == (B_ASYNC|B_DELWRI)) {
288		reassignbuf(bp, bp->b_vp);
289	}
290
291	bp->b_vp->v_numoutput++;
292	vfs_busy_pages(bp, 1);
293	if (curproc != NULL)
294		curproc->p_stats->p_ru.ru_oublock++;
295	VOP_STRATEGY(bp);
296
297	if ((oldflags & B_ASYNC) == 0) {
298		int rtval = biowait(bp);
299
300		if (oldflags & B_DELWRI) {
301			reassignbuf(bp, bp->b_vp);
302		}
303		brelse(bp);
304		return (rtval);
305	}
306	return (0);
307}
308
309int
310vn_bwrite(ap)
311	struct vop_bwrite_args *ap;
312{
313	return (bwrite(ap->a_bp));
314}
315
316/*
317 * Delayed write. (Buffer is marked dirty).
318 */
319void
320bdwrite(struct buf * bp)
321{
322
323	if ((bp->b_flags & B_BUSY) == 0) {
324		panic("bdwrite: buffer is not busy");
325	}
326	if (bp->b_flags & B_INVAL) {
327		brelse(bp);
328		return;
329	}
330	if (bp->b_flags & B_TAPE) {
331		bawrite(bp);
332		return;
333	}
334	bp->b_flags &= ~(B_READ|B_RELBUF);
335	if ((bp->b_flags & B_DELWRI) == 0) {
336		bp->b_flags |= B_DONE | B_DELWRI;
337		reassignbuf(bp, bp->b_vp);
338	}
339
340	/*
341	 * This bmap keeps the system from needing to do the bmap later,
342	 * perhaps when the system is attempting to do a sync.  Since it
343	 * is likely that the indirect block -- or whatever other datastructure
344	 * that the filesystem needs is still in memory now, it is a good
345	 * thing to do this.  Note also, that if the pageout daemon is
346	 * requesting a sync -- there might not be enough memory to do
347	 * the bmap then...  So, this is important to do.
348	 */
349	if( bp->b_lblkno == bp->b_blkno) {
350		VOP_BMAP(bp->b_vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
351	}
352
353	/*
354	 * Set the *dirty* buffer range based upon the VM system dirty pages.
355	 */
356	vfs_setdirty(bp);
357
358	/*
359	 * We need to do this here to satisfy the vnode_pager and the
360	 * pageout daemon, so that it thinks that the pages have been
361	 * "cleaned".  Note that since the pages are in a delayed write
362	 * buffer -- the VFS layer "will" see that the pages get written
363	 * out on the next sync, or perhaps the cluster will be completed.
364	 */
365	vfs_clean_pages(bp);
366	brelse(bp);
367	return;
368}
369
370/*
371 * Asynchronous write.
372 * Start output on a buffer, but do not wait for it to complete.
373 * The buffer is released when the output completes.
374 */
375void
376bawrite(struct buf * bp)
377{
378	bp->b_flags |= B_ASYNC;
379	(void) VOP_BWRITE(bp);
380}
381
382/*
383 * Release a buffer.
384 */
385void
386brelse(struct buf * bp)
387{
388	int s;
389
390	if (bp->b_flags & B_CLUSTER) {
391		relpbuf(bp);
392		return;
393	}
394	/* anyone need a "free" block? */
395	s = splbio();
396
397	if (needsbuffer) {
398		needsbuffer = 0;
399		wakeup(&needsbuffer);
400	}
401
402	/* anyone need this block? */
403	if (bp->b_flags & B_WANTED) {
404		bp->b_flags &= ~(B_WANTED | B_AGE);
405		wakeup(bp);
406	} else if (bp->b_flags & B_VMIO) {
407		bp->b_flags &= ~B_WANTED;
408		wakeup(bp);
409	}
410	if (bp->b_flags & B_LOCKED)
411		bp->b_flags &= ~B_ERROR;
412
413	if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_ERROR)) ||
414	    (bp->b_bufsize <= 0)) {
415		bp->b_flags |= B_INVAL;
416		bp->b_flags &= ~(B_DELWRI | B_CACHE);
417		if (((bp->b_flags & B_VMIO) == 0) && bp->b_vp)
418			brelvp(bp);
419	}
420
421	/*
422	 * VMIO buffer rundown.  It is not very necessary to keep a VMIO buffer
423	 * constituted, so the B_INVAL flag is used to *invalidate* the buffer,
424	 * but the VM object is kept around.  The B_NOCACHE flag is used to
425	 * invalidate the pages in the VM object.
426	 */
427	if (bp->b_flags & B_VMIO) {
428		vm_offset_t foff;
429		vm_object_t obj;
430		int i, resid;
431		vm_page_t m;
432		struct vnode *vp;
433		int iototal = bp->b_bufsize;
434
435		vp = bp->b_vp;
436		if (!vp)
437			panic("brelse: missing vp");
438		if (!vp->v_mount)
439			panic("brelse: missing mount info");
440
441		if (bp->b_npages) {
442			obj = (vm_object_t) vp->v_object;
443			foff = trunc_page(vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno);
444			for (i = 0; i < bp->b_npages; i++) {
445				m = bp->b_pages[i];
446				if (m == bogus_page) {
447					m = vm_page_lookup(obj, foff);
448					if (!m) {
449						panic("brelse: page missing\n");
450					}
451					bp->b_pages[i] = m;
452					pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
453				}
454				resid = (m->offset + PAGE_SIZE) - foff;
455				if (resid > iototal)
456					resid = iototal;
457				if (resid > 0) {
458					/*
459					 * Don't invalidate the page if the local machine has already
460					 * modified it.  This is the lesser of two evils, and should
461					 * be fixed.
462					 */
463					if (bp->b_flags & (B_NOCACHE | B_ERROR)) {
464						vm_page_test_dirty(m);
465						if (m->dirty == 0) {
466							vm_page_set_invalid(m, foff, resid);
467							if (m->valid == 0)
468								vm_page_protect(m, VM_PROT_NONE);
469						}
470					}
471				}
472				foff += resid;
473				iototal -= resid;
474			}
475		}
476
477		if (bp->b_flags & (B_INVAL | B_RELBUF)) {
478			for(i = 0; i < bp->b_npages; i++) {
479				m = bp->b_pages[i];
480				--m->bmapped;
481				if (m->bmapped == 0) {
482					if (m->flags & PG_WANTED) {
483						wakeup(m);
484						m->flags &= ~PG_WANTED;
485					}
486					if ((m->busy == 0) && ((m->flags & PG_BUSY) == 0)) {
487						vm_page_test_dirty(m);
488						/*
489						 * if page isn't valid, no sense in keeping it around
490						 */
491						if (m->valid == 0) {
492							vm_page_protect(m, VM_PROT_NONE);
493							vm_page_free(m);
494						/*
495						 * if page isn't dirty and hasn't been referenced by
496						 * a process, then cache it
497						 */
498						} else if ((m->dirty & m->valid) == 0 &&
499						    (m->flags & PG_REFERENCED) == 0 &&
500						    !pmap_is_referenced(VM_PAGE_TO_PHYS(m))) {
501							vm_page_cache(m);
502						/*
503						 * otherwise activate it
504						 */
505						} else if ((m->flags & PG_ACTIVE) == 0) {
506							vm_page_activate(m);
507							m->act_count = 0;
508						}
509					}
510				}
511			}
512			bufspace -= bp->b_bufsize;
513			pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
514			bp->b_npages = 0;
515			bp->b_bufsize = 0;
516			bp->b_flags &= ~B_VMIO;
517			if (bp->b_vp)
518				brelvp(bp);
519		}
520	}
521	if (bp->b_qindex != QUEUE_NONE)
522		panic("brelse: free buffer onto another queue???");
523
524	/* enqueue */
525	/* buffers with no memory */
526	if (bp->b_bufsize == 0) {
527		bp->b_qindex = QUEUE_EMPTY;
528		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
529		LIST_REMOVE(bp, b_hash);
530		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
531		bp->b_dev = NODEV;
532		/* buffers with junk contents */
533	} else if (bp->b_flags & (B_ERROR | B_INVAL | B_NOCACHE | B_RELBUF)) {
534		bp->b_qindex = QUEUE_AGE;
535		TAILQ_INSERT_HEAD(&bufqueues[QUEUE_AGE], bp, b_freelist);
536		LIST_REMOVE(bp, b_hash);
537		LIST_INSERT_HEAD(&invalhash, bp, b_hash);
538		bp->b_dev = NODEV;
539		/* buffers that are locked */
540	} else if (bp->b_flags & B_LOCKED) {
541		bp->b_qindex = QUEUE_LOCKED;
542		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LOCKED], bp, b_freelist);
543		/* buffers with stale but valid contents */
544	} else if (bp->b_flags & B_AGE) {
545		bp->b_qindex = QUEUE_AGE;
546		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_AGE], bp, b_freelist);
547		/* buffers with valid and quite potentially reuseable contents */
548	} else {
549		bp->b_qindex = QUEUE_LRU;
550		TAILQ_INSERT_TAIL(&bufqueues[QUEUE_LRU], bp, b_freelist);
551	}
552
553	/* unlock */
554	bp->b_flags &= ~(B_WANTED | B_BUSY | B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
555	splx(s);
556}
557
558/*
559 * Check to see if a block is currently memory resident.
560 */
561static __inline struct buf *
562gbincore(struct vnode * vp, daddr_t blkno)
563{
564	struct buf *bp;
565	struct bufhashhdr *bh;
566
567	bh = BUFHASH(vp, blkno);
568	bp = bh->lh_first;
569
570	/* Search hash chain */
571	while (bp != NULL) {
572		/* hit */
573		if (bp->b_vp == vp && bp->b_lblkno == blkno) {
574			break;
575		}
576		bp = bp->b_hash.le_next;
577	}
578	return (bp);
579}
580
581/*
582 * this routine implements clustered async writes for
583 * clearing out B_DELWRI buffers...  This is much better
584 * than the old way of writing only one buffer at a time.
585 */
586void
587vfs_bio_awrite(struct buf * bp)
588{
589	int i;
590	daddr_t lblkno = bp->b_lblkno;
591	struct vnode *vp = bp->b_vp;
592	int s;
593	int ncl;
594	struct buf *bpa;
595
596	s = splbio();
597	if (vp->v_mount && (vp->v_flag & VVMIO) &&
598	    (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
599		int size = vp->v_mount->mnt_stat.f_iosize;
600		int maxcl = MAXPHYS / size;
601
602		for (i = 1; i < maxcl; i++) {
603			if ((bpa = gbincore(vp, lblkno + i)) &&
604			    ((bpa->b_flags & (B_BUSY | B_DELWRI | B_CLUSTEROK | B_INVAL)) ==
605			    (B_DELWRI | B_CLUSTEROK)) &&
606			    (bpa->b_bufsize == size)) {
607				if ((bpa->b_blkno == bpa->b_lblkno) ||
608				    (bpa->b_blkno != bp->b_blkno + (i * size) / DEV_BSIZE))
609					break;
610			} else {
611				break;
612			}
613		}
614		ncl = i;
615		/*
616		 * this is a possible cluster write
617		 */
618		if (ncl != 1) {
619			bremfree(bp);
620			cluster_wbuild(vp, bp, size, lblkno, ncl, -1);
621			splx(s);
622			return;
623		}
624	}
625	/*
626	 * default (old) behavior, writing out only one block
627	 */
628	bremfree(bp);
629	bp->b_flags |= B_BUSY | B_ASYNC;
630	(void) VOP_BWRITE(bp);
631	splx(s);
632}
633
634
635/*
636 * Find a buffer header which is available for use.
637 */
638static struct buf *
639getnewbuf(int slpflag, int slptimeo, int doingvmio)
640{
641	struct buf *bp;
642	int s;
643	int firstbp = 1;
644
645	s = splbio();
646start:
647	if (bufspace >= maxbufspace)
648		goto trytofreespace;
649
650	/* can we constitute a new buffer? */
651	if ((bp = bufqueues[QUEUE_EMPTY].tqh_first)) {
652		if (bp->b_qindex != QUEUE_EMPTY)
653			panic("getnewbuf: inconsistent EMPTY queue");
654		bremfree(bp);
655		goto fillbuf;
656	}
657trytofreespace:
658	/*
659	 * We keep the file I/O from hogging metadata I/O
660	 * This is desirable because file data is cached in the
661	 * VM/Buffer cache even if a buffer is freed.
662	 */
663	if ((bp = bufqueues[QUEUE_AGE].tqh_first)) {
664		if (bp->b_qindex != QUEUE_AGE)
665			panic("getnewbuf: inconsistent AGE queue");
666	} else if ((bp = bufqueues[QUEUE_LRU].tqh_first)) {
667		if (bp->b_qindex != QUEUE_LRU)
668			panic("getnewbuf: inconsistent LRU queue");
669	}
670	if (!bp) {
671		/* wait for a free buffer of any kind */
672		needsbuffer = 1;
673		tsleep(&needsbuffer, PRIBIO | slpflag, "newbuf", slptimeo);
674		splx(s);
675		return (0);
676	}
677
678	/* if we are a delayed write, convert to an async write */
679	if ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) {
680		vfs_bio_awrite(bp);
681		if (!slpflag && !slptimeo) {
682			splx(s);
683			return (0);
684		}
685		goto start;
686	}
687
688	if (bp->b_flags & B_WANTED) {
689		bp->b_flags &= ~B_WANTED;
690		wakeup(bp);
691	}
692	bremfree(bp);
693
694	if (bp->b_flags & B_VMIO) {
695		bp->b_flags |= B_RELBUF | B_BUSY | B_DONE;
696		brelse(bp);
697		bremfree(bp);
698	}
699
700	if (bp->b_vp)
701		brelvp(bp);
702
703	/* we are not free, nor do we contain interesting data */
704	if (bp->b_rcred != NOCRED)
705		crfree(bp->b_rcred);
706	if (bp->b_wcred != NOCRED)
707		crfree(bp->b_wcred);
708fillbuf:
709	bp->b_flags |= B_BUSY;
710	LIST_REMOVE(bp, b_hash);
711	LIST_INSERT_HEAD(&invalhash, bp, b_hash);
712	splx(s);
713	if (bp->b_bufsize) {
714		allocbuf(bp, 0);
715	}
716	bp->b_flags = B_BUSY;
717	bp->b_dev = NODEV;
718	bp->b_vp = NULL;
719	bp->b_blkno = bp->b_lblkno = 0;
720	bp->b_iodone = 0;
721	bp->b_error = 0;
722	bp->b_resid = 0;
723	bp->b_bcount = 0;
724	bp->b_npages = 0;
725	bp->b_wcred = bp->b_rcred = NOCRED;
726	bp->b_data = buffers_kva + (bp - buf) * MAXBSIZE;
727	bp->b_dirtyoff = bp->b_dirtyend = 0;
728	bp->b_validoff = bp->b_validend = 0;
729	if (bufspace >= maxbufspace) {
730		s = splbio();
731		bp->b_flags |= B_INVAL;
732		brelse(bp);
733		goto trytofreespace;
734	}
735	return (bp);
736}
737
738/*
739 * Check to see if a block is currently memory resident.
740 */
741struct buf *
742incore(struct vnode * vp, daddr_t blkno)
743{
744	struct buf *bp;
745	struct bufhashhdr *bh;
746
747	int s = splbio();
748
749	bh = BUFHASH(vp, blkno);
750	bp = bh->lh_first;
751
752	/* Search hash chain */
753	while (bp != NULL) {
754		/* hit */
755		if (bp->b_vp == vp && bp->b_lblkno == blkno &&
756		    (bp->b_flags & B_INVAL) == 0) {
757			break;
758		}
759		bp = bp->b_hash.le_next;
760	}
761	splx(s);
762	return (bp);
763}
764
765/*
766 * Returns true if no I/O is needed to access the
767 * associated VM object.  This is like incore except
768 * it also hunts around in the VM system for the data.
769 */
770
771int
772inmem(struct vnode * vp, daddr_t blkno)
773{
774	vm_object_t obj;
775	vm_offset_t off, toff, tinc;
776	vm_page_t m;
777
778	if (incore(vp, blkno))
779		return 1;
780	if (vp->v_mount == NULL)
781		return 0;
782	if ((vp->v_object == NULL) || (vp->v_flag & VVMIO) == 0)
783		return 0;
784
785	obj = vp->v_object;
786	tinc = PAGE_SIZE;
787	if (tinc > vp->v_mount->mnt_stat.f_iosize)
788		tinc = vp->v_mount->mnt_stat.f_iosize;
789	off = blkno * vp->v_mount->mnt_stat.f_iosize;
790
791	for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
792		int mask;
793
794		m = vm_page_lookup(obj, trunc_page(toff + off));
795		if (!m)
796			return 0;
797		if (vm_page_is_valid(m, toff + off, tinc) == 0)
798			return 0;
799	}
800	return 1;
801}
802
803/*
804 * now we set the dirty range for the buffer --
805 * for NFS -- if the file is mapped and pages have
806 * been written to, let it know.  We want the
807 * entire range of the buffer to be marked dirty if
808 * any of the pages have been written to for consistancy
809 * with the b_validoff, b_validend set in the nfs write
810 * code, and used by the nfs read code.
811 */
812static void
813vfs_setdirty(struct buf *bp) {
814	int i;
815	vm_object_t object;
816	vm_offset_t boffset, offset;
817	/*
818	 * We qualify the scan for modified pages on whether the
819	 * object has been flushed yet.  The OBJ_WRITEABLE flag
820	 * is not cleared simply by protecting pages off.
821	 */
822	if ((bp->b_flags & B_VMIO) &&
823		((object = bp->b_pages[0]->object)->flags & OBJ_WRITEABLE)) {
824		/*
825		 * test the pages to see if they have been modified directly
826		 * by users through the VM system.
827		 */
828		for (i = 0; i < bp->b_npages; i++)
829			vm_page_test_dirty(bp->b_pages[i]);
830
831		/*
832		 * scan forwards for the first page modified
833		 */
834		for (i = 0; i < bp->b_npages; i++) {
835			if (bp->b_pages[i]->dirty) {
836				break;
837			}
838		}
839		boffset = i * PAGE_SIZE;
840		if (boffset < bp->b_dirtyoff) {
841			bp->b_dirtyoff = boffset;
842		}
843
844		/*
845		 * scan backwards for the last page modified
846		 */
847		for (i = bp->b_npages - 1; i >= 0; --i) {
848			if (bp->b_pages[i]->dirty) {
849				break;
850			}
851		}
852		boffset = (i + 1) * PAGE_SIZE;
853		offset = boffset + bp->b_pages[0]->offset;
854		if (offset >= object->size) {
855			boffset = object->size - bp->b_pages[0]->offset;
856		}
857		if (bp->b_dirtyend < boffset) {
858			bp->b_dirtyend = boffset;
859		}
860	}
861}
862
863/*
864 * Get a block given a specified block and offset into a file/device.
865 */
866struct buf *
867getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo)
868{
869	struct buf *bp;
870	int s;
871	struct bufhashhdr *bh;
872	vm_offset_t off;
873	int nleft;
874
875	s = splbio();
876loop:
877	if (bp = gbincore(vp, blkno)) {
878		if (bp->b_flags & (B_BUSY|B_INVAL)) {
879			bp->b_flags |= B_WANTED;
880			if (!tsleep(bp, PRIBIO | slpflag, "getblk", slptimeo))
881				goto loop;
882
883			splx(s);
884			return (struct buf *) NULL;
885		}
886		bp->b_flags |= B_BUSY | B_CACHE;
887		bremfree(bp);
888
889		/*
890		 * check for size inconsistancies (note that they shouldn't happen
891		 * but do when filesystems don't handle the size changes correctly.)
892		 * We are conservative on metadata and don't just extend the buffer
893		 * but write and re-constitute it.
894		 */
895		if (bp->b_bcount != size) {
896			if (bp->b_flags & B_VMIO) {
897				allocbuf(bp, size);
898			} else {
899				bp->b_flags |= B_NOCACHE;
900				VOP_BWRITE(bp);
901				goto loop;
902			}
903		}
904		/*
905		 * make sure that all pages in the buffer are valid, if they
906		 * aren't, clear the cache flag.
907		 * ASSUMPTION:
908		 *  if the buffer is greater than 1 page in size, it is assumed
909		 *  that the buffer address starts on a page boundary...
910		 */
911		if (bp->b_flags & B_VMIO) {
912			int szleft, i;
913			szleft = size;
914			for (i=0;i<bp->b_npages;i++) {
915				if (szleft > PAGE_SIZE) {
916					if ((bp->b_pages[i]->valid & VM_PAGE_BITS_ALL) !=
917						VM_PAGE_BITS_ALL) {
918						bp->b_flags &= ~B_CACHE;
919						break;
920					}
921					szleft -= PAGE_SIZE;
922				} else {
923					if (!vm_page_is_valid(bp->b_pages[i],
924						(((vm_offset_t) bp->b_data) & PAGE_MASK),
925						szleft)) {
926						bp->b_flags &= ~B_CACHE;
927						break;
928					}
929					szleft = 0;
930				}
931			}
932		}
933		splx(s);
934		return (bp);
935	} else {
936		vm_object_t obj;
937		int doingvmio;
938
939		if ((obj = vp->v_object) && (vp->v_flag & VVMIO)) {
940			doingvmio = 1;
941		} else {
942			doingvmio = 0;
943		}
944		if ((bp = getnewbuf(slpflag, slptimeo, doingvmio)) == 0) {
945			if (slpflag || slptimeo)
946				return NULL;
947			goto loop;
948		}
949
950		/*
951		 * This code is used to make sure that a buffer is not
952		 * created while the getnewbuf routine is blocked.
953		 * Normally the vnode is locked so this isn't a problem.
954		 * VBLK type I/O requests, however, don't lock the vnode.
955		 */
956		if (!VOP_ISLOCKED(vp) && gbincore(vp, blkno)) {
957			bp->b_flags |= B_INVAL;
958			brelse(bp);
959			goto loop;
960		}
961
962		/*
963		 * Insert the buffer into the hash, so that it can
964		 * be found by incore.
965		 */
966		bp->b_blkno = bp->b_lblkno = blkno;
967		bgetvp(vp, bp);
968		LIST_REMOVE(bp, b_hash);
969		bh = BUFHASH(vp, blkno);
970		LIST_INSERT_HEAD(bh, bp, b_hash);
971
972		if (doingvmio) {
973			bp->b_flags |= (B_VMIO | B_CACHE);
974#if defined(VFS_BIO_DEBUG)
975			if (vp->v_type != VREG)
976				printf("getblk: vmioing file type %d???\n", vp->v_type);
977#endif
978		} else {
979			bp->b_flags &= ~B_VMIO;
980		}
981		splx(s);
982
983		allocbuf(bp, size);
984		return (bp);
985	}
986}
987
988/*
989 * Get an empty, disassociated buffer of given size.
990 */
991struct buf *
992geteblk(int size)
993{
994	struct buf *bp;
995
996	while ((bp = getnewbuf(0, 0, 0)) == 0);
997	allocbuf(bp, size);
998	bp->b_flags |= B_INVAL;
999	return (bp);
1000}
1001
1002/*
1003 * This code constitutes the buffer memory from either anonymous system
1004 * memory (in the case of non-VMIO operations) or from an associated
1005 * VM object (in the case of VMIO operations).
1006 *
1007 * Note that this code is tricky, and has many complications to resolve
1008 * deadlock or inconsistant data situations.  Tread lightly!!!
1009 *
1010 * Modify the length of a buffer's underlying buffer storage without
1011 * destroying information (unless, of course the buffer is shrinking).
1012 */
1013int
1014allocbuf(struct buf * bp, int size)
1015{
1016
1017	int s;
1018	int newbsize, mbsize;
1019	int i;
1020
1021	if (!(bp->b_flags & B_BUSY))
1022		panic("allocbuf: buffer not busy");
1023
1024	if ((bp->b_flags & B_VMIO) == 0) {
1025		/*
1026		 * Just get anonymous memory from the kernel
1027		 */
1028		mbsize = ((size + DEV_BSIZE - 1) / DEV_BSIZE) * DEV_BSIZE;
1029		newbsize = round_page(size);
1030
1031		if (newbsize < bp->b_bufsize) {
1032			vm_hold_free_pages(
1033			    bp,
1034			    (vm_offset_t) bp->b_data + newbsize,
1035			    (vm_offset_t) bp->b_data + bp->b_bufsize);
1036		} else if (newbsize > bp->b_bufsize) {
1037			vm_hold_load_pages(
1038			    bp,
1039			    (vm_offset_t) bp->b_data + bp->b_bufsize,
1040			    (vm_offset_t) bp->b_data + newbsize);
1041		}
1042	} else {
1043		vm_page_t m;
1044		int desiredpages;
1045
1046		newbsize = ((size + DEV_BSIZE - 1) / DEV_BSIZE) * DEV_BSIZE;
1047		desiredpages = round_page(newbsize) / PAGE_SIZE;
1048
1049		if (newbsize < bp->b_bufsize) {
1050			if (desiredpages < bp->b_npages) {
1051				pmap_qremove((vm_offset_t) trunc_page(bp->b_data) +
1052				    desiredpages * PAGE_SIZE, (bp->b_npages - desiredpages));
1053				for (i = desiredpages; i < bp->b_npages; i++) {
1054					m = bp->b_pages[i];
1055					s = splhigh();
1056					while ((m->flags & PG_BUSY) || (m->busy != 0)) {
1057						m->flags |= PG_WANTED;
1058						tsleep(m, PVM, "biodep", 0);
1059					}
1060					splx(s);
1061
1062					if (m->bmapped == 0) {
1063						printf("allocbuf: bmapped is zero for page %d\n", i);
1064						panic("allocbuf: error");
1065					}
1066					--m->bmapped;
1067					if (m->bmapped == 0) {
1068						vm_page_protect(m, VM_PROT_NONE);
1069						vm_page_free(m);
1070					}
1071					bp->b_pages[i] = NULL;
1072				}
1073				bp->b_npages = desiredpages;
1074			}
1075		} else if (newbsize > bp->b_bufsize) {
1076			vm_object_t obj;
1077			vm_offset_t tinc, off, toff, objoff;
1078			int pageindex, curbpnpages;
1079			struct vnode *vp;
1080			int bsize;
1081
1082			vp = bp->b_vp;
1083			bsize = vp->v_mount->mnt_stat.f_iosize;
1084
1085			if (bp->b_npages < desiredpages) {
1086				obj = vp->v_object;
1087				tinc = PAGE_SIZE;
1088				if (tinc > bsize)
1089					tinc = bsize;
1090				off = bp->b_lblkno * bsize;
1091		doretry:
1092				curbpnpages = bp->b_npages;
1093				bp->b_flags |= B_CACHE;
1094				for (toff = 0; toff < newbsize; toff += tinc) {
1095					int mask;
1096					int bytesinpage;
1097
1098					pageindex = toff / PAGE_SIZE;
1099					objoff = trunc_page(toff + off);
1100					if (pageindex < curbpnpages) {
1101						int pb;
1102
1103						m = bp->b_pages[pageindex];
1104						if (m->offset != objoff)
1105							panic("allocbuf: page changed offset??!!!?");
1106						bytesinpage = tinc;
1107						if (tinc > (newbsize - toff))
1108							bytesinpage = newbsize - toff;
1109						if (!vm_page_is_valid(m, toff + off, bytesinpage)) {
1110							bp->b_flags &= ~B_CACHE;
1111						}
1112						if ((m->flags & PG_ACTIVE) == 0) {
1113							vm_page_activate(m);
1114							m->act_count = 0;
1115						}
1116						continue;
1117					}
1118					m = vm_page_lookup(obj, objoff);
1119					if (!m) {
1120						m = vm_page_alloc(obj, objoff, VM_ALLOC_NORMAL);
1121						if (!m) {
1122							int j;
1123
1124							for (j = bp->b_npages; j < pageindex; j++) {
1125								PAGE_WAKEUP(bp->b_pages[j]);
1126							}
1127							VM_WAIT;
1128							goto doretry;
1129						}
1130						vm_page_activate(m);
1131						m->act_count = 0;
1132						m->valid = 0;
1133						bp->b_flags &= ~B_CACHE;
1134					} else if (m->flags & PG_BUSY) {
1135						int j;
1136
1137						for (j = bp->b_npages; j < pageindex; j++) {
1138							PAGE_WAKEUP(bp->b_pages[j]);
1139						}
1140
1141						s = splbio();
1142						m->flags |= PG_WANTED;
1143						tsleep(m, PRIBIO, "pgtblk", 0);
1144						splx(s);
1145
1146						goto doretry;
1147					} else {
1148						int pb;
1149						if ((curproc != pageproc) &&
1150							(m->flags & PG_CACHE) &&
1151						    (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) {
1152							pagedaemon_wakeup();
1153						}
1154						bytesinpage = tinc;
1155						if (tinc > (newbsize - toff))
1156							bytesinpage = newbsize - toff;
1157						if (!vm_page_is_valid(m, toff + off, bytesinpage)) {
1158							bp->b_flags &= ~B_CACHE;
1159						}
1160						if ((m->flags & PG_ACTIVE) == 0) {
1161							vm_page_activate(m);
1162							m->act_count = 0;
1163						}
1164						m->flags |= PG_BUSY;
1165					}
1166					bp->b_pages[pageindex] = m;
1167					curbpnpages = pageindex + 1;
1168				}
1169				for (i = bp->b_npages; i < curbpnpages; i++) {
1170					m = bp->b_pages[i];
1171					m->bmapped++;
1172					PAGE_WAKEUP(m);
1173				}
1174				bp->b_npages = curbpnpages;
1175				bp->b_data = buffers_kva + (bp - buf) * MAXBSIZE;
1176				pmap_qenter((vm_offset_t) bp->b_data, bp->b_pages, bp->b_npages);
1177				bp->b_data += off % PAGE_SIZE;
1178			}
1179		}
1180	}
1181	bufspace += (newbsize - bp->b_bufsize);
1182	bp->b_bufsize = newbsize;
1183	bp->b_bcount = size;
1184	return 1;
1185}
1186
1187/*
1188 * Wait for buffer I/O completion, returning error status.
1189 */
1190int
1191biowait(register struct buf * bp)
1192{
1193	int s;
1194
1195	s = splbio();
1196	while ((bp->b_flags & B_DONE) == 0)
1197		tsleep(bp, PRIBIO, "biowait", 0);
1198	splx(s);
1199	if (bp->b_flags & B_EINTR) {
1200		bp->b_flags &= ~B_EINTR;
1201		return (EINTR);
1202	}
1203	if (bp->b_flags & B_ERROR) {
1204		return (bp->b_error ? bp->b_error : EIO);
1205	} else {
1206		return (0);
1207	}
1208}
1209
1210/*
1211 * Finish I/O on a buffer, calling an optional function.
1212 * This is usually called from interrupt level, so process blocking
1213 * is not *a good idea*.
1214 */
1215void
1216biodone(register struct buf * bp)
1217{
1218	int s;
1219
1220	s = splbio();
1221	if (!(bp->b_flags & B_BUSY))
1222		panic("biodone: buffer not busy");
1223
1224	if (bp->b_flags & B_DONE) {
1225		splx(s);
1226		printf("biodone: buffer already done\n");
1227		return;
1228	}
1229	bp->b_flags |= B_DONE;
1230
1231	if ((bp->b_flags & B_READ) == 0) {
1232		struct vnode *vp = bp->b_vp;
1233		vwakeup(bp);
1234	}
1235#ifdef BOUNCE_BUFFERS
1236	if (bp->b_flags & B_BOUNCE)
1237		vm_bounce_free(bp);
1238#endif
1239
1240	/* call optional completion function if requested */
1241	if (bp->b_flags & B_CALL) {
1242		bp->b_flags &= ~B_CALL;
1243		(*bp->b_iodone) (bp);
1244		splx(s);
1245		return;
1246	}
1247	if (bp->b_flags & B_VMIO) {
1248		int i, resid;
1249		vm_offset_t foff;
1250		vm_page_t m;
1251		vm_object_t obj;
1252		int iosize;
1253		struct vnode *vp = bp->b_vp;
1254
1255		foff = vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
1256		obj = vp->v_object;
1257		if (!obj) {
1258			panic("biodone: no object");
1259		}
1260#if defined(VFS_BIO_DEBUG)
1261		if (obj->paging_in_progress < bp->b_npages) {
1262			printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
1263			    obj->paging_in_progress, bp->b_npages);
1264		}
1265#endif
1266		iosize = bp->b_bufsize;
1267		for (i = 0; i < bp->b_npages; i++) {
1268			int bogusflag = 0;
1269			m = bp->b_pages[i];
1270			if (m == bogus_page) {
1271				bogusflag = 1;
1272				m = vm_page_lookup(obj, foff);
1273				if (!m) {
1274#if defined(VFS_BIO_DEBUG)
1275					printf("biodone: page disappeared\n");
1276#endif
1277					--obj->paging_in_progress;
1278					continue;
1279				}
1280				bp->b_pages[i] = m;
1281				pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
1282			}
1283#if defined(VFS_BIO_DEBUG)
1284			if (trunc_page(foff) != m->offset) {
1285				printf("biodone: foff(%d)/m->offset(%d) mismatch\n", foff, m->offset);
1286			}
1287#endif
1288			resid = (m->offset + PAGE_SIZE) - foff;
1289			if (resid > iosize)
1290				resid = iosize;
1291			/*
1292			 * In the write case, the valid and clean bits are
1293			 * already changed correctly, so we only need to do this
1294			 * here in the read case.
1295			 */
1296			if ((bp->b_flags & B_READ) && !bogusflag && resid > 0) {
1297				vm_page_set_validclean(m, foff & (PAGE_SIZE-1), resid);
1298			}
1299
1300			/*
1301			 * when debugging new filesystems or buffer I/O methods, this
1302			 * is the most common error that pops up.  if you see this, you
1303			 * have not set the page busy flag correctly!!!
1304			 */
1305			if (m->busy == 0) {
1306				printf("biodone: page busy < 0, "
1307				    "off: %ld, foff: %ld, "
1308				    "resid: %d, index: %d\n",
1309				    m->offset, foff, resid, i);
1310				printf(" iosize: %ld, lblkno: %ld, flags: 0x%x, npages: %d\n",
1311				    bp->b_vp->v_mount->mnt_stat.f_iosize,
1312				    bp->b_lblkno, bp->b_flags, bp->b_npages);
1313				printf(" valid: 0x%x, dirty: 0x%x, mapped: %d\n",
1314				    m->valid, m->dirty, m->bmapped);
1315				panic("biodone: page busy < 0\n");
1316			}
1317			--m->busy;
1318			if ((m->busy == 0) && (m->flags & PG_WANTED)) {
1319				m->flags &= ~PG_WANTED;
1320				wakeup(m);
1321			}
1322			--obj->paging_in_progress;
1323			foff += resid;
1324			iosize -= resid;
1325		}
1326		if (obj && obj->paging_in_progress == 0 &&
1327		    (obj->flags & OBJ_PIPWNT)) {
1328			obj->flags &= ~OBJ_PIPWNT;
1329			wakeup(obj);
1330		}
1331	}
1332	/*
1333	 * For asynchronous completions, release the buffer now. The brelse
1334	 * checks for B_WANTED and will do the wakeup there if necessary - so
1335	 * no need to do a wakeup here in the async case.
1336	 */
1337
1338	if (bp->b_flags & B_ASYNC) {
1339		brelse(bp);
1340	} else {
1341		bp->b_flags &= ~B_WANTED;
1342		wakeup(bp);
1343	}
1344	splx(s);
1345}
1346
1347int
1348count_lock_queue()
1349{
1350	int count;
1351	struct buf *bp;
1352
1353	count = 0;
1354	for (bp = bufqueues[QUEUE_LOCKED].tqh_first;
1355	    bp != NULL;
1356	    bp = bp->b_freelist.tqe_next)
1357		count++;
1358	return (count);
1359}
1360
1361int vfs_update_interval = 30;
1362
1363void
1364vfs_update()
1365{
1366	(void) spl0();
1367	while (1) {
1368		tsleep(&vfs_update_wakeup, PRIBIO, "update",
1369		    hz * vfs_update_interval);
1370		vfs_update_wakeup = 0;
1371		sync(curproc, NULL, NULL);
1372	}
1373}
1374
1375/*
1376 * This routine is called in lieu of iodone in the case of
1377 * incomplete I/O.  This keeps the busy status for pages
1378 * consistant.
1379 */
1380void
1381vfs_unbusy_pages(struct buf * bp)
1382{
1383	int i;
1384
1385	if (bp->b_flags & B_VMIO) {
1386		struct vnode *vp = bp->b_vp;
1387		vm_object_t obj = vp->v_object;
1388		vm_offset_t foff;
1389
1390		foff = trunc_page(vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno);
1391
1392		for (i = 0; i < bp->b_npages; i++) {
1393			vm_page_t m = bp->b_pages[i];
1394
1395			if (m == bogus_page) {
1396				m = vm_page_lookup(obj, foff + i * PAGE_SIZE);
1397				if (!m) {
1398					panic("vfs_unbusy_pages: page missing\n");
1399				}
1400				bp->b_pages[i] = m;
1401				pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
1402			}
1403			--obj->paging_in_progress;
1404			--m->busy;
1405			if ((m->busy == 0) && (m->flags & PG_WANTED)) {
1406				m->flags &= ~PG_WANTED;
1407				wakeup(m);
1408			}
1409		}
1410		if (obj->paging_in_progress == 0 &&
1411		    (obj->flags & OBJ_PIPWNT)) {
1412			obj->flags &= ~OBJ_PIPWNT;
1413			wakeup(obj);
1414		}
1415	}
1416}
1417
1418/*
1419 * This routine is called before a device strategy routine.
1420 * It is used to tell the VM system that paging I/O is in
1421 * progress, and treat the pages associated with the buffer
1422 * almost as being PG_BUSY.  Also the object paging_in_progress
1423 * flag is handled to make sure that the object doesn't become
1424 * inconsistant.
1425 */
1426void
1427vfs_busy_pages(struct buf * bp, int clear_modify)
1428{
1429	int i;
1430
1431	if (bp->b_flags & B_VMIO) {
1432		vm_object_t obj = bp->b_vp->v_object;
1433		vm_offset_t foff = bp->b_vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
1434		int iocount = bp->b_bufsize;
1435
1436		vfs_setdirty(bp);
1437		for (i = 0; i < bp->b_npages; i++) {
1438			vm_page_t m = bp->b_pages[i];
1439			int resid = (m->offset + PAGE_SIZE) - foff;
1440
1441			if (resid > iocount)
1442				resid = iocount;
1443			if ((bp->b_flags & B_CLUSTER) == 0) {
1444				obj->paging_in_progress++;
1445				m->busy++;
1446			}
1447			if (clear_modify) {
1448				vm_page_protect(m, VM_PROT_READ);
1449				vm_page_set_validclean(m,
1450					foff & (PAGE_SIZE-1), resid);
1451			} else if (bp->b_bcount >= PAGE_SIZE) {
1452				if (m->valid && (bp->b_flags & B_CACHE) == 0) {
1453					bp->b_pages[i] = bogus_page;
1454					pmap_qenter(trunc_page(bp->b_data), bp->b_pages, bp->b_npages);
1455				}
1456			}
1457			foff += resid;
1458			iocount -= resid;
1459		}
1460	}
1461}
1462
1463/*
1464 * Tell the VM system that the pages associated with this buffer
1465 * are clean.  This is used for delayed writes where the data is
1466 * going to go to disk eventually without additional VM intevention.
1467 */
1468void
1469vfs_clean_pages(struct buf * bp)
1470{
1471	int i;
1472
1473	if (bp->b_flags & B_VMIO) {
1474		vm_offset_t foff =
1475			bp->b_vp->v_mount->mnt_stat.f_iosize * bp->b_lblkno;
1476		int iocount = bp->b_bufsize;
1477
1478		for (i = 0; i < bp->b_npages; i++) {
1479			vm_page_t m = bp->b_pages[i];
1480			int resid = (m->offset + PAGE_SIZE) - foff;
1481
1482			if (resid > iocount)
1483				resid = iocount;
1484			if (resid > 0) {
1485				vm_page_set_validclean(m,
1486					foff & (PAGE_SIZE-1), resid);
1487			}
1488			foff += resid;
1489			iocount -= resid;
1490		}
1491	}
1492}
1493
1494void
1495vfs_bio_clrbuf(struct buf *bp) {
1496	int i;
1497	if( bp->b_flags & B_VMIO) {
1498		if( (bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE)) {
1499			int j;
1500			if( bp->b_pages[0]->valid != VM_PAGE_BITS_ALL) {
1501				for(j=0; j < bp->b_bufsize / DEV_BSIZE;j++) {
1502					bzero(bp->b_data + j * DEV_BSIZE, DEV_BSIZE);
1503				}
1504			}
1505			bp->b_resid = 0;
1506			return;
1507		}
1508		for(i=0;i<bp->b_npages;i++) {
1509			if( bp->b_pages[i]->valid == VM_PAGE_BITS_ALL)
1510				continue;
1511			if( bp->b_pages[i]->valid == 0) {
1512				bzero(bp->b_data + i * PAGE_SIZE, PAGE_SIZE);
1513			} else {
1514				int j;
1515				for(j=0;j<PAGE_SIZE/DEV_BSIZE;j++) {
1516					if( (bp->b_pages[i]->valid & (1<<j)) == 0)
1517						bzero(bp->b_data + i * PAGE_SIZE + j * DEV_BSIZE, DEV_BSIZE);
1518				}
1519			}
1520			bp->b_pages[i]->valid = VM_PAGE_BITS_ALL;
1521		}
1522		bp->b_resid = 0;
1523	} else {
1524		clrbuf(bp);
1525	}
1526}
1527
1528/*
1529 * vm_hold_load_pages and vm_hold_unload pages get pages into
1530 * a buffers address space.  The pages are anonymous and are
1531 * not associated with a file object.
1532 */
1533void
1534vm_hold_load_pages(struct buf * bp, vm_offset_t froma, vm_offset_t toa)
1535{
1536	vm_offset_t pg;
1537	vm_page_t p;
1538	vm_offset_t from = round_page(froma);
1539	vm_offset_t to = round_page(toa);
1540
1541	for (pg = from; pg < to; pg += PAGE_SIZE) {
1542
1543tryagain:
1544
1545		p = vm_page_alloc(kernel_object, pg - VM_MIN_KERNEL_ADDRESS,
1546		    VM_ALLOC_NORMAL);
1547		if (!p) {
1548			VM_WAIT;
1549			goto tryagain;
1550		}
1551		vm_page_wire(p);
1552		pmap_kenter(pg, VM_PAGE_TO_PHYS(p));
1553		bp->b_pages[((caddr_t) pg - bp->b_data) / PAGE_SIZE] = p;
1554		PAGE_WAKEUP(p);
1555		bp->b_npages++;
1556	}
1557}
1558
1559void
1560vm_hold_free_pages(struct buf * bp, vm_offset_t froma, vm_offset_t toa)
1561{
1562	vm_offset_t pg;
1563	vm_page_t p;
1564	vm_offset_t from = round_page(froma);
1565	vm_offset_t to = round_page(toa);
1566
1567	for (pg = from; pg < to; pg += PAGE_SIZE) {
1568		p = bp->b_pages[((caddr_t) pg - bp->b_data) / PAGE_SIZE];
1569		bp->b_pages[((caddr_t) pg - bp->b_data) / PAGE_SIZE] = 0;
1570		pmap_kremove(pg);
1571		vm_page_free(p);
1572		--bp->b_npages;
1573	}
1574}
1575