vm_vfs.c revision 1.32.2.2 1 /* $NetBSD: vm_vfs.c,v 1.32.2.2 2012/10/30 17:22:55 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.2 2012/10/30 17:22:55 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 = NULL;
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
59 if (uobj == NULL) {
60 uobj = pgs[i]->uobject;
61 KASSERT(uobj != NULL);
62 mutex_enter(uobj->vmobjlock);
63 } else {
64 KASSERT(uobj == pgs[i]->uobject);
65 }
66
67 if (pgs[i]->flags & PG_PAGEOUT) {
68 KASSERT((pgs[i]->flags & PG_FAKE) == 0);
69 pageout++;
70 pgs[i]->flags &= ~PG_PAGEOUT;
71 pgs[i]->flags |= PG_RELEASED;
72 }
73 }
74 KASSERT(mutex_owned(uobj->vmobjlock));
75
76 mutex_enter(&uvm_pageqlock);
77 uvm_page_unbusy(pgs, npages);
78 mutex_exit(&uvm_pageqlock);
79 mutex_exit(uobj->vmobjlock);
80
81 uvm_pagermapout((vaddr_t)bp->b_data, npages);
82 uvm_pageout_done(pageout);
83
84 if (BUF_ISWRITE(bp) && (bp->b_cflags & BC_AGE) != 0) {
85 mutex_enter(bp->b_objlock);
86 vwakeup(bp);
87 mutex_exit(bp->b_objlock);
88 }
89
90 putiobuf(bp);
91
92 kmem_free(pgs, npages * sizeof(*pgs));
93 }
94
95 void
96 uvm_aio_biodone(struct buf *bp)
97 {
98
99 uvm_aio_aiodone(bp);
100 }
101
102 /*
103 * UBC
104 */
105
106 #define PAGERFLAGS (PGO_SYNCIO | PGO_NOBLOCKALLOC | PGO_NOTIMESTAMP)
107
108 void
109 ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
110 {
111 struct vm_page **pgs;
112 int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
113 int rv, npages, i;
114
115 if (maxpages == 0)
116 return;
117
118 pgs = kmem_alloc(maxpages * sizeof(pgs), KM_SLEEP);
119 mutex_enter(uobj->vmobjlock);
120 while (len) {
121 npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
122 memset(pgs, 0, npages * sizeof(struct vm_page *));
123 rv = uobj->pgops->pgo_get(uobj, trunc_page(off),
124 pgs, &npages, 0, VM_PROT_READ | VM_PROT_WRITE,
125 0, PAGERFLAGS | PGO_PASTEOF);
126 KASSERT(npages > 0);
127
128 mutex_enter(uobj->vmobjlock);
129 for (i = 0; i < npages; i++) {
130 struct vm_page *pg;
131 uint8_t *start;
132 size_t chunkoff, chunklen;
133
134 pg = pgs[i];
135 if (pg == NULL)
136 break;
137
138 KASSERT(pg->uobject != NULL);
139 KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
140
141 chunkoff = off & PAGE_MASK;
142 chunklen = MIN(PAGE_SIZE - chunkoff, len);
143 start = (uint8_t *)pg->uanon + chunkoff;
144
145 memset(start, 0, chunklen);
146 mutex_enter(pguobj->vmobjlock);
147 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
148 mutex_exit(pguobj->vmobjlock);
149
150 off += chunklen;
151 len -= chunklen;
152 }
153 uvm_page_unbusy(pgs, npages);
154 }
155 mutex_exit(uobj->vmobjlock);
156 kmem_free(pgs, maxpages * sizeof(pgs));
157 }
158
159 #define len2npages(off, len) \
160 ((round_page(off+len) - trunc_page(off)) >> PAGE_SHIFT)
161
162 int
163 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
164 int advice, int flags)
165 {
166 struct vm_page **pgs;
167 int npages = len2npages(uio->uio_offset, todo);
168 size_t pgalloc;
169 int i, rv, pagerflags;
170 vm_prot_t prot;
171
172 pgalloc = npages * sizeof(pgs);
173 pgs = kmem_alloc(pgalloc, KM_SLEEP);
174
175 pagerflags = PAGERFLAGS;
176 if (flags & UBC_WRITE)
177 pagerflags |= PGO_PASTEOF;
178 if (flags & UBC_FAULTBUSY)
179 pagerflags |= PGO_OVERWRITE;
180
181 prot = VM_PROT_READ;
182 if (flags & UBC_WRITE)
183 prot |= VM_PROT_WRITE;
184
185 mutex_enter(uobj->vmobjlock);
186 do {
187 npages = len2npages(uio->uio_offset, todo);
188 memset(pgs, 0, pgalloc);
189 rv = uobj->pgops->pgo_get(uobj, trunc_page(uio->uio_offset),
190 pgs, &npages, 0, prot, 0, pagerflags);
191 if (rv)
192 goto out;
193
194 mutex_enter(uobj->vmobjlock);
195 for (i = 0; i < npages; i++) {
196 struct vm_page *pg;
197 size_t xfersize;
198 off_t pageoff;
199
200 pg = pgs[i];
201 if (pg == NULL)
202 break;
203
204 KASSERT(pg->uobject != NULL);
205 KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
206 pageoff = uio->uio_offset & PAGE_MASK;
207
208 xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
209 KASSERT(xfersize > 0);
210 rv = uiomove((uint8_t *)pg->uanon + pageoff,
211 xfersize, uio);
212 if (rv) {
213 uvm_page_unbusy(pgs, npages);
214 mutex_exit(uobj->vmobjlock);
215 goto out;
216 }
217 if (uio->uio_rw == UIO_WRITE) {
218 mutex_enter(pguobj->vmobjlock);
219 pg->flags &= ~PG_FAKE;
220 uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
221 mutex_exit(pguobj->vmobjlock);
222 }
223 todo -= xfersize;
224 }
225 uvm_page_unbusy(pgs, npages);
226 } while (todo);
227 mutex_exit(uobj->vmobjlock);
228
229 out:
230 kmem_free(pgs, pgalloc);
231 return rv;
232 }
233