xref: /netbsd-src/sys/uvm/uvm_vnode.c (revision a536ee5124e62c9a0051a252f7833dc8f50f44c9)
1 /*	$NetBSD: uvm_vnode.c,v 1.99 2012/07/30 23:56:48 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
5  * Copyright (c) 1991, 1993
6  *      The Regents of the University of California.
7  * Copyright (c) 1990 University of Utah.
8  *
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to Berkeley by
12  * the Systems Programming Group of the University of Utah Computer
13  * Science Department.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *      @(#)vnode_pager.c       8.8 (Berkeley) 2/13/94
40  * from: Id: uvm_vnode.c,v 1.1.2.26 1998/02/02 20:38:07 chuck Exp
41  */
42 
43 /*
44  * uvm_vnode.c: the vnode pager.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: uvm_vnode.c,v 1.99 2012/07/30 23:56:48 matt Exp $");
49 
50 #include "opt_uvmhist.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/vnode.h>
56 #include <sys/disklabel.h>
57 #include <sys/ioctl.h>
58 #include <sys/fcntl.h>
59 #include <sys/conf.h>
60 #include <sys/pool.h>
61 #include <sys/mount.h>
62 
63 #include <miscfs/specfs/specdev.h>
64 
65 #include <uvm/uvm.h>
66 #include <uvm/uvm_readahead.h>
67 
68 #ifdef UVMHIST
69 UVMHIST_DEFINE(ubchist);
70 #endif
71 
72 /*
73  * functions
74  */
75 
76 static void	uvn_detach(struct uvm_object *);
77 static int	uvn_get(struct uvm_object *, voff_t, struct vm_page **, int *,
78 			int, vm_prot_t, int, int);
79 static int	uvn_put(struct uvm_object *, voff_t, voff_t, int);
80 static void	uvn_reference(struct uvm_object *);
81 
82 static int	uvn_findpage(struct uvm_object *, voff_t, struct vm_page **,
83 			     int);
84 
85 /*
86  * master pager structure
87  */
88 
89 const struct uvm_pagerops uvm_vnodeops = {
90 	.pgo_reference = uvn_reference,
91 	.pgo_detach = uvn_detach,
92 	.pgo_get = uvn_get,
93 	.pgo_put = uvn_put,
94 };
95 
96 /*
97  * the ops!
98  */
99 
100 /*
101  * uvn_reference
102  *
103  * duplicate a reference to a VM object.  Note that the reference
104  * count must already be at least one (the passed in reference) so
105  * there is no chance of the uvn being killed or locked out here.
106  *
107  * => caller must call with object unlocked.
108  * => caller must be using the same accessprot as was used at attach time
109  */
110 
111 static void
112 uvn_reference(struct uvm_object *uobj)
113 {
114 	vref((struct vnode *)uobj);
115 }
116 
117 
118 /*
119  * uvn_detach
120  *
121  * remove a reference to a VM object.
122  *
123  * => caller must call with object unlocked and map locked.
124  */
125 
126 static void
127 uvn_detach(struct uvm_object *uobj)
128 {
129 	vrele((struct vnode *)uobj);
130 }
131 
132 /*
133  * uvn_put: flush page data to backing store.
134  *
135  * => object must be locked on entry!   VOP_PUTPAGES must unlock it.
136  * => flags: PGO_SYNCIO -- use sync. I/O
137  * => note: caller must set PG_CLEAN and pmap_clear_modify (if needed)
138  */
139 
140 static int
141 uvn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
142 {
143 	struct vnode *vp = (struct vnode *)uobj;
144 	int error;
145 
146 	KASSERT(mutex_owned(vp->v_interlock));
147 	error = VOP_PUTPAGES(vp, offlo, offhi, flags);
148 
149 	return error;
150 }
151 
152 
153 /*
154  * uvn_get: get pages (synchronously) from backing store
155  *
156  * => prefer map unlocked (not required)
157  * => object must be locked!  we will _unlock_ it before starting any I/O.
158  * => flags: PGO_ALLPAGES: get all of the pages
159  *           PGO_LOCKED: fault data structures are locked
160  * => NOTE: offset is the offset of pps[0], _NOT_ pps[centeridx]
161  * => NOTE: caller must check for released pages!!
162  */
163 
164 static int
165 uvn_get(struct uvm_object *uobj, voff_t offset,
166     struct vm_page **pps /* IN/OUT */,
167     int *npagesp /* IN (OUT if PGO_LOCKED)*/,
168     int centeridx, vm_prot_t access_type, int advice, int flags)
169 {
170 	struct vnode *vp = (struct vnode *)uobj;
171 	int error;
172 
173 	UVMHIST_FUNC("uvn_get"); UVMHIST_CALLED(ubchist);
174 
175 	UVMHIST_LOG(ubchist, "vp %p off 0x%x", vp, (int)offset, 0,0);
176 
177 	if (vp->v_type == VREG && (access_type & VM_PROT_WRITE) == 0
178 	    && (flags & PGO_LOCKED) == 0) {
179 		vn_ra_allocctx(vp);
180 		uvm_ra_request(vp->v_ractx, advice, uobj, offset,
181 		    *npagesp << PAGE_SHIFT);
182 	}
183 
184 	error = VOP_GETPAGES(vp, offset, pps, npagesp, centeridx,
185 			     access_type, advice, flags);
186 
187 	KASSERT(((flags & PGO_LOCKED) != 0 && mutex_owned(vp->v_interlock)) ||
188 	    (flags & PGO_LOCKED) == 0);
189 	return error;
190 }
191 
192 
193 /*
194  * uvn_findpages:
195  * return the page for the uobj and offset requested, allocating if needed.
196  * => uobj must be locked.
197  * => returned pages will be BUSY.
198  */
199 
200 int
201 uvn_findpages(struct uvm_object *uobj, voff_t offset, int *npagesp,
202     struct vm_page **pgs, int flags)
203 {
204 	int i, count, found, npages, rv;
205 
206 	count = found = 0;
207 	npages = *npagesp;
208 	if (flags & UFP_BACKWARD) {
209 		for (i = npages - 1; i >= 0; i--, offset -= PAGE_SIZE) {
210 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
211 			if (rv == 0) {
212 				if (flags & UFP_DIRTYONLY)
213 					break;
214 			} else
215 				found++;
216 			count++;
217 		}
218 	} else {
219 		for (i = 0; i < npages; i++, offset += PAGE_SIZE) {
220 			rv = uvn_findpage(uobj, offset, &pgs[i], flags);
221 			if (rv == 0) {
222 				if (flags & UFP_DIRTYONLY)
223 					break;
224 			} else
225 				found++;
226 			count++;
227 		}
228 	}
229 	*npagesp = count;
230 	return (found);
231 }
232 
233 static int
234 uvn_findpage(struct uvm_object *uobj, voff_t offset, struct vm_page **pgp,
235     int flags)
236 {
237 	struct vm_page *pg;
238 	bool dirty;
239 	UVMHIST_FUNC("uvn_findpage"); UVMHIST_CALLED(ubchist);
240 	UVMHIST_LOG(ubchist, "vp %p off 0x%lx", uobj, offset,0,0);
241 
242 	KASSERT(mutex_owned(uobj->vmobjlock));
243 
244 	if (*pgp != NULL) {
245 		UVMHIST_LOG(ubchist, "dontcare", 0,0,0,0);
246 		return 0;
247 	}
248 	for (;;) {
249 		/* look for an existing page */
250 		pg = uvm_pagelookup(uobj, offset);
251 
252 		/* nope?  allocate one now */
253 		if (pg == NULL) {
254 			if (flags & UFP_NOALLOC) {
255 				UVMHIST_LOG(ubchist, "noalloc", 0,0,0,0);
256 				return 0;
257 			}
258 			pg = uvm_pagealloc(uobj, offset, NULL,
259 			    UVM_FLAG_COLORMATCH);
260 			if (pg == NULL) {
261 				if (flags & UFP_NOWAIT) {
262 					UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
263 					return 0;
264 				}
265 				mutex_exit(uobj->vmobjlock);
266 				uvm_wait("uvn_fp1");
267 				mutex_enter(uobj->vmobjlock);
268 				continue;
269 			}
270 			UVMHIST_LOG(ubchist, "alloced %p (color %u)", pg,
271 			    VM_PGCOLOR_BUCKET(pg), 0,0);
272 			break;
273 		} else if (flags & UFP_NOCACHE) {
274 			UVMHIST_LOG(ubchist, "nocache",0,0,0,0);
275 			return 0;
276 		}
277 
278 		/* page is there, see if we need to wait on it */
279 		if ((pg->flags & PG_BUSY) != 0) {
280 			if (flags & UFP_NOWAIT) {
281 				UVMHIST_LOG(ubchist, "nowait",0,0,0,0);
282 				return 0;
283 			}
284 			pg->flags |= PG_WANTED;
285 			UVMHIST_LOG(ubchist, "wait %p (color %u)", pg,
286 			    VM_PGCOLOR_BUCKET(pg), 0,0);
287 			UVM_UNLOCK_AND_WAIT(pg, uobj->vmobjlock, 0,
288 					    "uvn_fp2", 0);
289 			mutex_enter(uobj->vmobjlock);
290 			continue;
291 		}
292 
293 		/* skip PG_RDONLY pages if requested */
294 		if ((flags & UFP_NORDONLY) && (pg->flags & PG_RDONLY)) {
295 			UVMHIST_LOG(ubchist, "nordonly",0,0,0,0);
296 			return 0;
297 		}
298 
299 		/* stop on clean pages if requested */
300 		if (flags & UFP_DIRTYONLY) {
301 			dirty = pmap_clear_modify(pg) ||
302 				(pg->flags & PG_CLEAN) == 0;
303 			pg->flags |= PG_CLEAN;
304 			if (!dirty) {
305 				UVMHIST_LOG(ubchist, "dirtonly", 0,0,0,0);
306 				return 0;
307 			}
308 		}
309 
310 		/* mark the page BUSY and we're done. */
311 		pg->flags |= PG_BUSY;
312 		UVM_PAGE_OWN(pg, "uvn_findpage");
313 		UVMHIST_LOG(ubchist, "found %p (color %u)",
314 		    pg, VM_PGCOLOR_BUCKET(pg), 0,0);
315 		break;
316 	}
317 	*pgp = pg;
318 	return 1;
319 }
320 
321 /*
322  * uvm_vnp_setsize: grow or shrink a vnode uobj
323  *
324  * grow   => just update size value
325  * shrink => toss un-needed pages
326  *
327  * => we assume that the caller has a reference of some sort to the
328  *	vnode in question so that it will not be yanked out from under
329  *	us.
330  */
331 
332 void
333 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
334 {
335 	struct uvm_object *uobj = &vp->v_uobj;
336 	voff_t pgend = round_page(newsize);
337 	voff_t oldsize;
338 	UVMHIST_FUNC("uvm_vnp_setsize"); UVMHIST_CALLED(ubchist);
339 
340 	mutex_enter(uobj->vmobjlock);
341 	UVMHIST_LOG(ubchist, "vp %p old 0x%x new 0x%x",
342 	    vp, vp->v_size, newsize, 0);
343 
344 	/*
345 	 * now check if the size has changed: if we shrink we had better
346 	 * toss some pages...
347 	 */
348 
349 	KASSERT(newsize != VSIZENOTSET);
350 	KASSERT(vp->v_size <= vp->v_writesize);
351 	KASSERT(vp->v_size == vp->v_writesize ||
352 	    newsize == vp->v_writesize || newsize <= vp->v_size);
353 
354 	oldsize = vp->v_writesize;
355 	KASSERT(oldsize != VSIZENOTSET || pgend > oldsize);
356 
357 	if (oldsize > pgend) {
358 		(void) uvn_put(uobj, pgend, 0, PGO_FREE | PGO_SYNCIO);
359 		mutex_enter(uobj->vmobjlock);
360 	}
361 	vp->v_size = vp->v_writesize = newsize;
362 	mutex_exit(uobj->vmobjlock);
363 }
364 
365 void
366 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
367 {
368 
369 	mutex_enter(vp->v_interlock);
370 	KASSERT(newsize != VSIZENOTSET);
371 	KASSERT(vp->v_size != VSIZENOTSET);
372 	KASSERT(vp->v_writesize != VSIZENOTSET);
373 	KASSERT(vp->v_size <= vp->v_writesize);
374 	KASSERT(vp->v_size <= newsize);
375 	vp->v_writesize = newsize;
376 	mutex_exit(vp->v_interlock);
377 }
378 
379 bool
380 uvn_text_p(struct uvm_object *uobj)
381 {
382 	struct vnode *vp = (struct vnode *)uobj;
383 
384 	return (vp->v_iflag & VI_EXECMAP) != 0;
385 }
386 
387 bool
388 uvn_clean_p(struct uvm_object *uobj)
389 {
390 	struct vnode *vp = (struct vnode *)uobj;
391 
392 	return (vp->v_iflag & VI_ONWORKLST) == 0;
393 }
394 
395 bool
396 uvn_needs_writefault_p(struct uvm_object *uobj)
397 {
398 	struct vnode *vp = (struct vnode *)uobj;
399 
400 	return uvn_clean_p(uobj) ||
401 	    (vp->v_iflag & (VI_WRMAP|VI_WRMAPDIRTY)) == VI_WRMAP;
402 }
403