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