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