vm_vfs.c revision 1.3 1 /* $NetBSD: vm_vfs.c,v 1.3 2008/12/18 00:24:13 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.3 2008/12/18 00:24:13 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 void
89 uvm_aio_aiodone(struct buf *bp)
90 {
91
92 if (((bp->b_flags|bp->b_cflags) & (B_READ|BC_NOCACHE)) == 0 && bioopsp)
93 bioopsp->io_pageiodone(bp);
94 }
95
96 struct uvm_ractx *
97 uvm_ra_allocctx()
98 {
99
100 return NULL;
101 }
102
103 void
104 uvm_ra_freectx(struct uvm_ractx *ra)
105 {
106
107 return;
108 }
109
110 bool
111 uvn_clean_p(struct uvm_object *uobj)
112 {
113 struct vnode *vp = (void *)uobj;
114
115 return (vp->v_iflag & VI_ONWORKLST) == 0;
116 }
117
118 void
119 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
120 {
121
122 mutex_enter(&vp->v_interlock);
123 vp->v_size = vp->v_writesize = newsize;
124 mutex_exit(&vp->v_interlock);
125 }
126
127 void
128 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
129 {
130
131 mutex_enter(&vp->v_interlock);
132 vp->v_writesize = newsize;
133 mutex_exit(&vp->v_interlock);
134 }
135
136 void
137 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
138 {
139 struct uvm_object *uobj = &vp->v_uobj;
140 struct vm_page **pgs;
141 int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
142 int rv, npages, i;
143
144 pgs = kmem_zalloc(maxpages * sizeof(pgs), KM_SLEEP);
145 while (len) {
146 npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
147 memset(pgs, 0, npages * sizeof(struct vm_page *));
148 mutex_enter(&uobj->vmobjlock);
149 rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
150 KASSERT(npages > 0);
151
152 for (i = 0; i < npages; i++) {
153 uint8_t *start;
154 size_t chunkoff, chunklen;
155
156 chunkoff = off & PAGE_MASK;
157 chunklen = MIN(PAGE_SIZE - chunkoff, len);
158 start = (uint8_t *)pgs[i]->uanon + chunkoff;
159
160 memset(start, 0, chunklen);
161 pgs[i]->flags &= ~PG_CLEAN;
162
163 off += chunklen;
164 len -= chunklen;
165 }
166 uvm_page_unbusy(pgs, npages);
167 }
168 kmem_free(pgs, maxpages * sizeof(pgs));
169
170 return;
171 }
172
173 /*
174 * UBC
175 */
176
177 /* dumdidumdum */
178 #define len2npages(off, len) \
179 (((((len) + PAGE_MASK) & ~(PAGE_MASK)) >> PAGE_SHIFT) \
180 + (((off & PAGE_MASK) + (len & PAGE_MASK)) > PAGE_SIZE))
181
182 int
183 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
184 int advice, int flags)
185 {
186 struct vm_page **pgs;
187 int npages = len2npages(uio->uio_offset, todo);
188 size_t pgalloc;
189 int i, rv;
190
191 pgalloc = npages * sizeof(pgs);
192 pgs = kmem_zalloc(pgalloc, KM_SLEEP);
193
194 do {
195 mutex_enter(&uobj->vmobjlock);
196 rv = uobj->pgops->pgo_get(uobj, uio->uio_offset, pgs, &npages,
197 0, 0, 0, 0);
198 if (rv)
199 goto out;
200
201 for (i = 0; i < npages; i++) {
202 size_t xfersize;
203 off_t pageoff;
204
205 pageoff = uio->uio_offset & PAGE_MASK;
206 xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
207 uiomove((uint8_t *)pgs[i]->uanon + pageoff,
208 xfersize, uio);
209 if (uio->uio_rw == UIO_WRITE)
210 pgs[i]->flags &= ~PG_CLEAN;
211 todo -= xfersize;
212 }
213 uvm_page_unbusy(pgs, npages);
214 } while (todo);
215
216 out:
217 kmem_free(pgs, pgalloc);
218 return rv;
219 }
220