summaryrefslogtreecommitdiffstats
path: root/sys/vm/vm_vmio.c
blob: 8450c92bdfe34414ea1bb34ff8e20f442a8bc99d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Copyright (c) 1994 John S. Dyson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice immediately at the beginning of the file, without modification,
 *    this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Absolutely no warranty of function or purpose is made by the author
 *    John S. Dyson.
 * 4. Modifications may be freely made to this file if the above conditions
 *    are met.
 *
 * $Id$
 */
/*
 * THIS IS PRELIMINARY, BUGGY AND NON-WORKING CODE
 * WHEN THIS NOTICE IS REMOVED -- IT WILL WORK... :-).
 *
 * THINGS TO DO:
 *	COMMENTS, THE SYSTEM ALMOST RUNS WITH IT.  THIS IS CURRENTLY MEANT
 *	ONLY AS A PLACEHOLDER!!!
 */
#define VMIO
#include "param.h"
#include "proc.h"
#include "malloc.h"
#include "vm_param.h"
#include "vm.h"
#include "lock.h"
#include "queue.h"
#include "vm_prot.h"
#include "vm_object.h"
#include "vm_page.h"
#include "vnode_pager.h"
#include "vm_map.h"
#include "vm_pageout.h"
#include "vnode.h"
#include "uio.h"
#include "mount.h"

/* #include "buf.h" */
#include "miscfs/specfs/specdev.h"
/* #include "vmio.h" */

int vnode_pager_initialized;
extern vm_map_t pager_map;

struct buf * getpbuf();
void relpbuf(struct buf *bp);

/*
 * map an object into kva
 * return 1 if all are in memory, and 0 if any are fake.
 */
int
vmio_alloc_pages( vm_object_t object, vm_offset_t start,
		vm_offset_t size, vm_page_t *ms) {

	int pagecount;
	vm_page_t m;
	int i,j;
	int s;
	vm_offset_t kva;
	struct buf *bp;
	int ioneeded=0;
	
	pagecount = size / PAGE_SIZE;
	
	for(i=0;i<pagecount;i++) {
		vm_page_t m;
		m = vm_page_lookup( object, start + i * PAGE_SIZE);
		if( m) {
			/*
			 * See if something else has already gotten this page
			 */
			if( m->busy || (m->flags & (PG_VMIO|PG_BUSY))) {
				/*
				 * Something has the page, so we have to
				 * release all the pages we've gotten to
				 * this point, wait for the page to unbusy
				 * and then start over.
				 */
				int j;
				for(j = 0; j < i; j++) {
					vm_page_t n;
					n = ms[j];
					if( n) {
						/*
						 * unbusy the page.
						 */
						PAGE_WAKEUP(n);
						if( n->flags & PG_FAKE)
							vm_page_free(n);
					}
				}
				m->flags |= PG_WANTED;
				tsleep((caddr_t)m, PVM, "vngpwt", 0);
				return -1;
			}
			m->flags |= PG_REFERENCED;
		} else {
			m = (vm_page_t) vm_page_alloc( object,
				start + i * PAGE_SIZE);
			if( !m) {
				VM_WAIT;
				for(j=0;j<i;j++) {
					vm_page_t n;
					n = ms[j];
					if( n) {
						PAGE_WAKEUP(n);
						if( n->flags & PG_FAKE)
							vm_page_free(n);
					}
				}
				return -1;
			}
		}
		ms[i] = m;
	}
	/*
	 * hold the pages and assign them to the mapping
	 */
	for(i=0;i<pagecount;i++) {
		m = ms[i];
		vm_page_hold(m);
		if( m->flags & PG_FAKE)
			++ioneeded;

		vm_page_deactivate(m);
		if( (m->flags & PG_ACTIVE) == 0)
			vm_page_activate(m);
		m->flags |= PG_VMIO;
		m->flags &= ~PG_BUSY;
		++m->act_count;
		pmap_page_protect( VM_PAGE_TO_PHYS(m), VM_PROT_READ);
	}
	return ioneeded;

}

void
vmio_rawiodone( struct buf *bp) {
	int s;
	int i;
	vm_object_t object;
	vm_page_t m;

	if( bp->b_bufsize != bp->b_bcount)
		bzero( bp->b_data + bp->b_bcount, bp->b_bufsize - bp->b_bcount);
	printf("rawdone: (blk: %d, count: %d)\n",
		bp->b_blkno, bp->b_bcount);
	s = splbio();
	object = bp->b_pages[0]->object;
	for( i = 0; i < bp->b_npages; i++) {
		m = bp->b_pages[i];
		if( m) {
			--m->busy;
			if( m->busy == 0) {
				m->flags |= PG_CLEAN;
				m->flags &= ~(PG_LAUNDRY|PG_FAKE);
				PAGE_WAKEUP(m);
			}
		} else {
			panic("vmio_rawiodone: page is gone!!!");
		}
	}
	HOLDRELE(bp->b_vp);
	relpbuf(bp);
	--object->paging_in_progress;
	if( object->paging_in_progress == 0)
		wakeup((caddr_t)object);
	splx(s);
	return;
}

void
vmio_get_pager(struct vnode *vp) {
	if( vp->v_type == VREG) {
		vm_object_t object;
		vm_pager_t pager;
		if((vp->v_vmdata == NULL) || (vp->v_flag & VVMIO) == 0) {
			pager = (vm_pager_t) vnode_pager_alloc(vp, 0, 0, 0);
			object = (vm_object_t) vp->v_vmdata;
			if( object->pager != pager)
				panic("vmio_get_pager: pager/object mismatch");
			(void) vm_object_lookup( pager);
			pager_cache( object, TRUE);
			vp->v_flag |= VVMIO;
		} else {
			object = (vm_object_t) vp->v_vmdata;
			pager = object->pager;
			if( pager == NULL) {
				panic("vmio_get_pager: pager missing");
			}
			(void) vm_object_lookup( pager);
		}
	}
}

void
vmio_free_pager( struct vnode *vp) {
	if( vp->v_vmdata == NULL)
		panic("vmio_free_pager: object missing");
	vm_object_deallocate( (vm_object_t) vp->v_vmdata);
}

void
vmio_aread( struct buf *lbp) {
	struct vnode *vp, *dp;
	vm_object_t object;
	vm_offset_t offset;
	vm_offset_t size;
	vm_offset_t off;
	int forcemore;
	int runp;
	int s;

	s = splbio();
	vp = lbp->b_vp;
	object = (vm_object_t) vp->v_vmdata;
	offset = trunc_page(lbp->b_lblkno * vp->v_mount->mnt_stat.f_iosize);
	size = round_page(lbp->b_bcount);
	forcemore = 0;
	printf("queueing read: iosize: %d, b_lblkno: %d, offset: %d, size: %d:",
		vp->v_mount->mnt_stat.f_iosize,
		lbp->b_lblkno, offset, size);

	for(off = 0; off < size; ) {
		vm_offset_t curoff;
		int pgidx, pgcnt, i;
		struct buf *bp;
		pgidx = off / PAGE_SIZE;
		if( !forcemore) {
			while( (off < size) &&
				(lbp->b_pages[pgidx]->flags & PG_FAKE) == 0) {
				off = trunc_page(off) + PAGE_SIZE;
				pgidx += 1;
			}
		}
		if( off >= size)
			break;
		bp = getpbuf();

		++object->paging_in_progress;

		curoff = offset + off;
		VOP_BMAP( vp, curoff / vp->v_mount->mnt_stat.f_iosize,
			&dp, &bp->b_blkno, &runp);
		
		bp->b_bcount = (runp + 1) * vp->v_mount->mnt_stat.f_iosize;
		if( off + bp->b_bcount > size)
			bp->b_bcount = size - off;
		bp->b_bufsize = round_page(bp->b_bcount);
			
		pgcnt = bp->b_bufsize / PAGE_SIZE;
		for(i=0;i<pgcnt;i++) {
			++lbp->b_pages[pgidx+i]->busy;
			bp->b_pages[i] = lbp->b_pages[pgidx+i];
			bp->b_pages[i]->flags |= PG_BUSY;
		}
		bp->b_npages = pgcnt;
		pmap_qenter((vm_offset_t) bp->b_data, &lbp->b_pages[pgidx], pgcnt);
		bp->b_data += curoff - trunc_page(curoff);
		off += bp->b_bcount;
		if( off & (PAGE_SIZE - 1))
			forcemore = 1;
		else
			forcemore = 0;
		/*
		 * round up physical size for real devices
		 */
		if( dp->v_type == VBLK || dp->v_type == VCHR) {
			bp->b_bcount = (bp->b_bcount + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
		}
		/*
		 * and map the pages to be read into the kva
		 */
		VHOLD(vp);
		/* build a minimal buffer header */
		bp->b_flags = B_BUSY | B_READ | B_CALL | B_ASYNC;
		bp->b_iodone = vmio_rawiodone;
		/* B_PHYS is not set, but it is nice to fill this in */
		bp->b_proc = curproc;
		bp->b_rcred = bp->b_wcred = bp->b_proc->p_ucred;
		if( bp->b_rcred != NOCRED)
			crhold(bp->b_rcred);
		if( bp->b_wcred != NOCRED)
			crhold(bp->b_wcred);
		bp->b_lblkno = bp->b_blkno;
/*		bp->b_vp = dp; */
		bgetvp( dp, bp);
		printf(" p: (blk: %d, coff: %d, off: %d, count: %d)",
			bp->b_blkno, curoff, off, bp->b_bcount);

		/* Should be a BLOCK or character DEVICE if we get here */
		bp->b_dev = dp->v_rdev;

		/* do the input */
		VOP_STRATEGY(bp);
	}
	printf("\n");
	splx(s);
}

int
vmio_getblk( struct buf *bp) {
	struct vnode *vp;
	vm_object_t object;
	vm_offset_t offset;
	vm_offset_t size;
	vm_page_t ms[(MAXBSIZE + PAGE_SIZE - 1)/PAGE_SIZE];
	int rtval;

	vp = bp->b_vp;
	if( (object = (vm_object_t) vp->v_vmdata) == NULL)
		panic("vmio_build_bp: object missing");

	offset = trunc_page(bp->b_lblkno * vp->v_mount->mnt_stat.f_iosize);
	size = round_page(bp->b_bcount);
	printf("vmio_getblk: off:%d(0x%x) blk: %d, size: %d",
		offset, offset, bp->b_lblkno, size);
		
	bp->b_flags &= ~B_CACHE;
	if( (rtval = vmio_alloc_pages( object, offset, size, ms)) == 0) {
		bp->b_flags |= B_CACHE;
		printf("(cached)\n");
	} else {
		printf("(ioneeded:%d)\n", rtval);
	}

	if( rtval < 0) {
		printf("vmio_getblk: vmio_alloc_pages busy\n");
		return 0;
	}

	bp->b_npages = size / PAGE_SIZE;
	pmap_qenter((vm_offset_t) bp->b_data, ms, bp->b_npages);
	bp->b_flags |= B_VMIO;
	bp->b_bufsize = size;
	bcopy( ms, bp->b_pages, bp->b_npages * sizeof(vm_page_t));
	return 1;
}

int
vmio_biowait( struct buf *lbp) {
	int i;
	int s;
	printf("vmio_biowait -->");
restart:
	s = splbio();
	for(i=lbp->b_npages-1;i >= 0; --i) {
		if( lbp->b_pages[i]->busy || (lbp->b_pages[i]->flags & PG_BUSY)) {
			lbp->b_pages[i]->flags |= PG_WANTED;
			printf(" waiting on page %d of xfer at %d, flags: 0x%x\n",
				i, lbp->b_blkno, lbp->b_pages[i]->flags);
			tsleep( (caddr_t) lbp->b_pages[i], PVM, "vbiowt", 0);
			splx(s);
			goto restart;
		}
	}
	splx(s);
	printf("vmio_biowait: finished\n");
	return 1;
}

void
vmio_brelse( struct buf *lbp) {
	int i;
	int nfullpages = lbp->b_bcount / PAGE_SIZE;
	pmap_qremove((vm_offset_t) lbp->b_data, lbp->b_npages);
	for(i=0;i<lbp->b_npages;i++) {
		if( (i < nfullpages) && ((lbp->b_flags & B_DELWRI) == 0))
			lbp->b_pages[i]->flags |= PG_CLEAN;
		if( lbp->b_flags & B_DELWRI)
			lbp->b_pages[i]->flags &= ~PG_CLEAN;
		lbp->b_pages[i]->flags &= ~PG_VMIO;
		vm_page_unhold(lbp->b_pages[i]);
		PAGE_WAKEUP(lbp->b_pages[i]);
	}
	lbp->b_flags &= ~B_VMIO;
	lbp->b_flags |= B_INVAL;
	lbp->b_bufsize = 0;
}
OpenPOWER on IntegriCloud