vm_vfs.c revision 1.29 1 /* $NetBSD: vm_vfs.c,v 1.29 2011/06/12 03:35:59 rmind 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.29 2011/06/12 03:35:59 rmind 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 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
106 {
107 struct uvm_object *uobj = &vp->v_uobj;
108 struct vm_page **pgs;
109 struct uvm_object *pguobj;
110 int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
111 int rv, npages, i;
112
113 if (maxpages == 0)
114 return;
115
116 pgs = kmem_alloc(maxpages * sizeof(pgs), KM_SLEEP);
117 mutex_enter(uobj->vmobjlock);
118 while (len) {
119 npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
120 memset(pgs, 0, npages * sizeof(struct vm_page *));
121 rv = uobj->pgops->pgo_get(uobj, trunc_page(off),
122 pgs, &npages, 0, VM_PROT_READ | VM_PROT_WRITE,
123 0, PAGERFLAGS | PGO_PASTEOF);
124 KASSERT(npages > 0);
125
126 for (i = 0, pguobj = NULL; i < npages; i++) {
127 struct vm_page *pg;
128 uint8_t *start;
129 size_t chunkoff, chunklen;
130
131 pg = pgs[i];
132 if (pg == NULL)
133 break;
134 if (pguobj == NULL)
135 pguobj = pg->uobject;
136 KASSERT(pguobj == pg->uobject);
137
138 chunkoff = off & PAGE_MASK;
139 chunklen = MIN(PAGE_SIZE - chunkoff, len);
140 start = (uint8_t *)pg->uanon + chunkoff;
141
142 memset(start, 0, chunklen);
143 pg->flags &= ~PG_CLEAN;
144
145 off += chunklen;
146 len -= chunklen;
147 }
148 mutex_enter(pguobj->vmobjlock);
149 uvm_page_unbusy(pgs, npages);
150 if (pguobj != uobj) {
151 mutex_exit(pguobj->vmobjlock);
152 mutex_enter(uobj->vmobjlock);
153 }
154 }
155 mutex_exit(uobj->vmobjlock);
156 kmem_free(pgs, maxpages * sizeof(pgs));
157
158 return;
159 }
160
161 #define len2npages(off, len) \
162 ((round_page(off+len) - trunc_page(off)) >> PAGE_SHIFT)
163
164 int
165 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
166 int advice, int flags)
167 {
168 struct vm_page **pgs;
169 struct uvm_object *pguobj;
170 int npages = len2npages(uio->uio_offset, todo);
171 size_t pgalloc;
172 int i, rv, pagerflags;
173 vm_prot_t prot;
174
175 pgalloc = npages * sizeof(pgs);
176 pgs = kmem_alloc(pgalloc, KM_SLEEP);
177
178 pagerflags = PAGERFLAGS;
179 if (flags & UBC_WRITE)
180 pagerflags |= PGO_PASTEOF;
181 if (flags & UBC_FAULTBUSY)
182 pagerflags |= PGO_OVERWRITE;
183
184 prot = VM_PROT_READ;
185 if (flags & UBC_WRITE)
186 prot |= VM_PROT_WRITE;
187
188 mutex_enter(uobj->vmobjlock);
189 do {
190 npages = len2npages(uio->uio_offset, todo);
191 memset(pgs, 0, pgalloc);
192 rv = uobj->pgops->pgo_get(uobj, trunc_page(uio->uio_offset),
193 pgs, &npages, 0, prot, 0, pagerflags);
194 if (rv)
195 goto out;
196
197 for (i = 0, pguobj = NULL; i < npages; i++) {
198 struct vm_page *pg;
199 size_t xfersize;
200 off_t pageoff;
201
202 pg = pgs[i];
203 if (pg == NULL)
204 break;
205 if (pguobj == NULL)
206 pguobj = pg->uobject;
207 KASSERT(pguobj == pg->uobject);
208
209 pageoff = uio->uio_offset & PAGE_MASK;
210 xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
211 KASSERT(xfersize > 0);
212 rv = uiomove((uint8_t *)pg->uanon + pageoff,
213 xfersize, uio);
214 if (rv) {
215 mutex_enter(pguobj->vmobjlock);
216 uvm_page_unbusy(pgs, npages);
217 mutex_exit(pguobj->vmobjlock);
218 goto out;
219 }
220 if (uio->uio_rw == UIO_WRITE)
221 pg->flags &= ~(PG_CLEAN | PG_FAKE);
222 todo -= xfersize;
223 }
224 mutex_enter(pguobj->vmobjlock);
225 uvm_page_unbusy(pgs, npages);
226 if (pguobj != uobj) {
227 mutex_exit(pguobj->vmobjlock);
228 mutex_enter(uobj->vmobjlock);
229 }
230 } while (todo);
231 mutex_exit(uobj->vmobjlock);
232
233 out:
234 kmem_free(pgs, pgalloc);
235 return rv;
236 }
237