vm_vfs.c revision 1.32.2.1 1 /* $NetBSD: vm_vfs.c,v 1.32.2.1 2011/11/02 21:53:59 yamt Exp $ */
2
3 /*
4 * Copyright (c) 2008-2011 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: vm_vfs.c,v 1.32.2.1 2011/11/02 21:53:59 yamt Exp $");
30
31 #include <sys/param.h>
32
33 #include <sys/buf.h>
34 #include <sys/vnode.h>
35
36 #include <uvm/uvm.h>
37 #include <uvm/uvm_readahead.h>
38
39 /*
40 * release resources held during async io. this is almost the
41 * same as uvm_aio_aiodone() from uvm_pager.c and only lacks the
42 * call to uvm_aio_aiodone_pages(): unbusies pages directly here.
43 */
44 void
45 uvm_aio_aiodone(struct buf *bp)
46 {
47 struct uvm_object *uobj;
48 int i, npages = bp->b_bufsize >> PAGE_SHIFT;
49 struct vm_page **pgs;
50 vaddr_t va;
51 int pageout = 0;
52
53 KASSERT(npages > 0);
54 pgs = kmem_alloc(npages * sizeof(*pgs), KM_SLEEP);
55 for (i = 0; i < npages; i++) {
56 va = (vaddr_t)bp->b_data + (i << PAGE_SHIFT);
57 pgs[i] = uvm_pageratop(va);
58 if (pgs[i]->flags & PG_PAGEOUT) {
59 KASSERT((pgs[i]->flags & PG_FAKE) == 0);
60 pageout++;
61 pgs[i]->flags &= ~PG_PAGEOUT;
62 pgs[i]->flags |= PG_RELEASED;
63 }
64 }
65
66 uvm_pagermapout((vaddr_t)bp->b_data, npages);
67
68 /* get uobj because we need it after pages might be recycled */
69 uobj = pgs[0]->uobject;
70 KASSERT(uobj);
71
72 mutex_enter(uobj->vmobjlock);
73 mutex_enter(&uvm_pageqlock);
74 uvm_page_unbusy(pgs, npages);
75 mutex_exit(&uvm_pageqlock);
76 mutex_exit(uobj->vmobjlock);
77
78 uvm_pageout_done(pageout);
79
80 if (BUF_ISWRITE(bp) && (bp->b_cflags & BC_AGE) != 0) {
81 mutex_enter(bp->b_objlock);
82 vwakeup(bp);
83 mutex_exit(bp->b_objlock);
84 }
85
86 putiobuf(bp);
87
88 kmem_free(pgs, npages * sizeof(*pgs));
89 }
90
91 void
92 uvm_aio_biodone(struct buf *bp)
93 {
94
95 uvm_aio_aiodone(bp);
96 }
97
98 /*
99 * UBC
100 */
101
102 #define PAGERFLAGS (PGO_SYNCIO | PGO_NOBLOCKALLOC | PGO_NOTIMESTAMP)
103
104 void
105 ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
106 {
107 struct vm_page **pgs;
108 struct uvm_object *pguobj;
109 int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
110 int rv, npages, i;
111
112 if (maxpages == 0)
113 return;
114
115 pgs = kmem_alloc(maxpages * sizeof(pgs), KM_SLEEP);
116 mutex_enter(uobj->vmobjlock);
117 while (len) {
118 npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
119 memset(pgs, 0, npages * sizeof(struct vm_page *));
120 rv = uobj->pgops->pgo_get(uobj, trunc_page(off),
121 pgs, &npages, 0, VM_PROT_READ | VM_PROT_WRITE,
122 0, PAGERFLAGS | PGO_PASTEOF);
123 KASSERT(npages > 0);
124
125 for (i = 0, pguobj = NULL; i < npages; i++) {
126 struct vm_page *pg;
127 uint8_t *start;
128 size_t chunkoff, chunklen;
129
130 pg = pgs[i];
131 if (pg == NULL)
132 break;
133 if (pguobj == NULL)
134 pguobj = pg->uobject;
135 KASSERT(pguobj == pg->uobject);
136
137 chunkoff = off & PAGE_MASK;
138 chunklen = MIN(PAGE_SIZE - chunkoff, len);
139 start = (uint8_t *)pg->uanon + chunkoff;
140
141 memset(start, 0, chunklen);
142 mutex_enter(pguobj->vmobjlock);
143 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
144 mutex_exit(pguobj->vmobjlock);
145
146 off += chunklen;
147 len -= chunklen;
148 }
149 mutex_enter(pguobj->vmobjlock);
150 uvm_page_unbusy(pgs, npages);
151 if (pguobj != uobj) {
152 mutex_exit(pguobj->vmobjlock);
153 mutex_enter(uobj->vmobjlock);
154 }
155 }
156 mutex_exit(uobj->vmobjlock);
157 kmem_free(pgs, maxpages * sizeof(pgs));
158
159 return;
160 }
161
162 #define len2npages(off, len) \
163 ((round_page(off+len) - trunc_page(off)) >> PAGE_SHIFT)
164
165 int
166 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
167 int advice, int flags)
168 {
169 struct vm_page **pgs;
170 struct uvm_object *pguobj;
171 int npages = len2npages(uio->uio_offset, todo);
172 size_t pgalloc;
173 int i, rv, pagerflags;
174 vm_prot_t prot;
175
176 pgalloc = npages * sizeof(pgs);
177 pgs = kmem_alloc(pgalloc, KM_SLEEP);
178
179 pagerflags = PAGERFLAGS;
180 if (flags & UBC_WRITE)
181 pagerflags |= PGO_PASTEOF;
182 if (flags & UBC_FAULTBUSY)
183 pagerflags |= PGO_OVERWRITE;
184
185 prot = VM_PROT_READ;
186 if (flags & UBC_WRITE)
187 prot |= VM_PROT_WRITE;
188
189 mutex_enter(uobj->vmobjlock);
190 do {
191 npages = len2npages(uio->uio_offset, todo);
192 memset(pgs, 0, pgalloc);
193 rv = uobj->pgops->pgo_get(uobj, trunc_page(uio->uio_offset),
194 pgs, &npages, 0, prot, 0, pagerflags);
195 if (rv)
196 goto out;
197
198 for (i = 0, pguobj = NULL; i < npages; i++) {
199 struct vm_page *pg;
200 size_t xfersize;
201 off_t pageoff;
202
203 pg = pgs[i];
204 if (pg == NULL)
205 break;
206 if (pguobj == NULL)
207 pguobj = pg->uobject;
208 KASSERT(pguobj == pg->uobject);
209
210 pageoff = uio->uio_offset & PAGE_MASK;
211 xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
212 KASSERT(xfersize > 0);
213 rv = uiomove((uint8_t *)pg->uanon + pageoff,
214 xfersize, uio);
215 if (rv) {
216 mutex_enter(pguobj->vmobjlock);
217 uvm_page_unbusy(pgs, npages);
218 mutex_exit(pguobj->vmobjlock);
219 goto out;
220 }
221 if (uio->uio_rw == UIO_WRITE) {
222 mutex_enter(pguobj->vmobjlock);
223 pg->flags &= ~PG_FAKE;
224 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
225 mutex_exit(pguobj->vmobjlock);
226 }
227 todo -= xfersize;
228 }
229 mutex_enter(pguobj->vmobjlock);
230 uvm_page_unbusy(pgs, npages);
231 if (pguobj != uobj) {
232 mutex_exit(pguobj->vmobjlock);
233 mutex_enter(uobj->vmobjlock);
234 }
235 } while (todo);
236 mutex_exit(uobj->vmobjlock);
237
238 out:
239 kmem_free(pgs, pgalloc);
240 return rv;
241 }
242