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