genfs_vnops.c revision 1.13.2.3 1 1.13.2.2 bouyer /* $NetBSD: genfs_vnops.c,v 1.13.2.3 2000/12/13 15:50:26 bouyer Exp $ */
2 1.6 fvdl
3 1.6 fvdl /*
4 1.6 fvdl * Copyright (c) 1982, 1986, 1989, 1993
5 1.6 fvdl * The Regents of the University of California. All rights reserved.
6 1.6 fvdl *
7 1.6 fvdl * Redistribution and use in source and binary forms, with or without
8 1.6 fvdl * modification, are permitted provided that the following conditions
9 1.6 fvdl * are met:
10 1.6 fvdl * 1. Redistributions of source code must retain the above copyright
11 1.6 fvdl * notice, this list of conditions and the following disclaimer.
12 1.6 fvdl * 2. Redistributions in binary form must reproduce the above copyright
13 1.6 fvdl * notice, this list of conditions and the following disclaimer in the
14 1.6 fvdl * documentation and/or other materials provided with the distribution.
15 1.6 fvdl * 3. All advertising materials mentioning features or use of this software
16 1.6 fvdl * must display the following acknowledgement:
17 1.6 fvdl * This product includes software developed by the University of
18 1.6 fvdl * California, Berkeley and its contributors.
19 1.6 fvdl * 4. Neither the name of the University nor the names of its contributors
20 1.6 fvdl * may be used to endorse or promote products derived from this software
21 1.6 fvdl * without specific prior written permission.
22 1.6 fvdl *
23 1.6 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.6 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.6 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.6 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.6 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.6 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.6 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.6 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.6 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.6 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.6 fvdl * SUCH DAMAGE.
34 1.6 fvdl *
35 1.6 fvdl */
36 1.5 perry
37 1.8 thorpej #include "opt_nfsserver.h"
38 1.8 thorpej
39 1.1 mycroft #include <sys/param.h>
40 1.1 mycroft #include <sys/systm.h>
41 1.6 fvdl #include <sys/proc.h>
42 1.1 mycroft #include <sys/kernel.h>
43 1.1 mycroft #include <sys/mount.h>
44 1.1 mycroft #include <sys/namei.h>
45 1.1 mycroft #include <sys/vnode.h>
46 1.13 wrstuden #include <sys/fcntl.h>
47 1.1 mycroft #include <sys/malloc.h>
48 1.3 mycroft #include <sys/poll.h>
49 1.1 mycroft
50 1.1 mycroft #include <miscfs/genfs/genfs.h>
51 1.6 fvdl #include <miscfs/specfs/specdev.h>
52 1.1 mycroft
53 1.13.2.2 bouyer #include <uvm/uvm.h>
54 1.13.2.2 bouyer #include <uvm/uvm_pager.h>
55 1.13.2.2 bouyer
56 1.8 thorpej #ifdef NFSSERVER
57 1.8 thorpej #include <nfs/rpcv2.h>
58 1.8 thorpej #include <nfs/nfsproto.h>
59 1.8 thorpej #include <nfs/nfs.h>
60 1.8 thorpej #include <nfs/nqnfs.h>
61 1.8 thorpej #include <nfs/nfs_var.h>
62 1.8 thorpej #endif
63 1.8 thorpej
64 1.1 mycroft int
65 1.3 mycroft genfs_poll(v)
66 1.1 mycroft void *v;
67 1.1 mycroft {
68 1.3 mycroft struct vop_poll_args /* {
69 1.1 mycroft struct vnode *a_vp;
70 1.3 mycroft int a_events;
71 1.1 mycroft struct proc *a_p;
72 1.1 mycroft } */ *ap = v;
73 1.1 mycroft
74 1.3 mycroft return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
75 1.1 mycroft }
76 1.1 mycroft
77 1.1 mycroft int
78 1.1 mycroft genfs_fsync(v)
79 1.1 mycroft void *v;
80 1.1 mycroft {
81 1.1 mycroft struct vop_fsync_args /* {
82 1.1 mycroft struct vnode *a_vp;
83 1.1 mycroft struct ucred *a_cred;
84 1.7 kleink int a_flags;
85 1.13.2.1 bouyer off_t offlo;
86 1.13.2.1 bouyer off_t offhi;
87 1.1 mycroft struct proc *a_p;
88 1.1 mycroft } */ *ap = v;
89 1.13.2.1 bouyer struct vnode *vp = ap->a_vp;
90 1.11 mycroft int wait;
91 1.1 mycroft
92 1.11 mycroft wait = (ap->a_flags & FSYNC_WAIT) != 0;
93 1.11 mycroft vflushbuf(vp, wait);
94 1.11 mycroft if ((ap->a_flags & FSYNC_DATAONLY) != 0)
95 1.7 kleink return (0);
96 1.11 mycroft else
97 1.13.2.1 bouyer return (VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0));
98 1.1 mycroft }
99 1.1 mycroft
100 1.1 mycroft int
101 1.4 kleink genfs_seek(v)
102 1.4 kleink void *v;
103 1.4 kleink {
104 1.4 kleink struct vop_seek_args /* {
105 1.4 kleink struct vnode *a_vp;
106 1.4 kleink off_t a_oldoff;
107 1.4 kleink off_t a_newoff;
108 1.4 kleink struct ucred *a_ucred;
109 1.4 kleink } */ *ap = v;
110 1.4 kleink
111 1.4 kleink if (ap->a_newoff < 0)
112 1.4 kleink return (EINVAL);
113 1.4 kleink
114 1.4 kleink return (0);
115 1.4 kleink }
116 1.4 kleink
117 1.4 kleink int
118 1.1 mycroft genfs_abortop(v)
119 1.1 mycroft void *v;
120 1.1 mycroft {
121 1.1 mycroft struct vop_abortop_args /* {
122 1.1 mycroft struct vnode *a_dvp;
123 1.1 mycroft struct componentname *a_cnp;
124 1.1 mycroft } */ *ap = v;
125 1.1 mycroft
126 1.1 mycroft if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
127 1.13.2.1 bouyer PNBUF_PUT(ap->a_cnp->cn_pnbuf);
128 1.1 mycroft return (0);
129 1.13 wrstuden }
130 1.13 wrstuden
131 1.13 wrstuden int
132 1.13 wrstuden genfs_fcntl(v)
133 1.13 wrstuden void *v;
134 1.13 wrstuden {
135 1.13 wrstuden struct vop_fcntl_args /* {
136 1.13 wrstuden struct vnode *a_vp;
137 1.13 wrstuden u_int a_command;
138 1.13 wrstuden caddr_t a_data;
139 1.13 wrstuden int a_fflag;
140 1.13 wrstuden struct ucred *a_cred;
141 1.13 wrstuden struct proc *a_p;
142 1.13 wrstuden } */ *ap = v;
143 1.13 wrstuden
144 1.13 wrstuden if (ap->a_command == F_SETFL)
145 1.13 wrstuden return (0);
146 1.13 wrstuden else
147 1.13 wrstuden return (EOPNOTSUPP);
148 1.1 mycroft }
149 1.1 mycroft
150 1.1 mycroft /*ARGSUSED*/
151 1.1 mycroft int
152 1.1 mycroft genfs_badop(v)
153 1.1 mycroft void *v;
154 1.1 mycroft {
155 1.1 mycroft
156 1.1 mycroft panic("genfs: bad op");
157 1.1 mycroft }
158 1.1 mycroft
159 1.1 mycroft /*ARGSUSED*/
160 1.1 mycroft int
161 1.1 mycroft genfs_nullop(v)
162 1.1 mycroft void *v;
163 1.1 mycroft {
164 1.1 mycroft
165 1.1 mycroft return (0);
166 1.10 kleink }
167 1.10 kleink
168 1.10 kleink /*ARGSUSED*/
169 1.10 kleink int
170 1.10 kleink genfs_einval(v)
171 1.10 kleink void *v;
172 1.10 kleink {
173 1.10 kleink
174 1.10 kleink return (EINVAL);
175 1.1 mycroft }
176 1.1 mycroft
177 1.1 mycroft /*ARGSUSED*/
178 1.1 mycroft int
179 1.1 mycroft genfs_eopnotsupp(v)
180 1.1 mycroft void *v;
181 1.1 mycroft {
182 1.1 mycroft
183 1.1 mycroft return (EOPNOTSUPP);
184 1.1 mycroft }
185 1.1 mycroft
186 1.12 wrstuden /*
187 1.12 wrstuden * Called when an fs doesn't support a particular vop but the vop needs to
188 1.12 wrstuden * vrele, vput, or vunlock passed in vnodes.
189 1.12 wrstuden */
190 1.12 wrstuden int
191 1.12 wrstuden genfs_eopnotsupp_rele(v)
192 1.12 wrstuden void *v;
193 1.12 wrstuden {
194 1.12 wrstuden struct vop_generic_args /*
195 1.12 wrstuden struct vnodeop_desc *a_desc;
196 1.12 wrstuden / * other random data follows, presumably * /
197 1.12 wrstuden } */ *ap = v;
198 1.12 wrstuden struct vnodeop_desc *desc = ap->a_desc;
199 1.12 wrstuden struct vnode *vp;
200 1.12 wrstuden int flags, i, j, offset;
201 1.12 wrstuden
202 1.12 wrstuden flags = desc->vdesc_flags;
203 1.12 wrstuden for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
204 1.12 wrstuden if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
205 1.12 wrstuden break; /* stop at end of list */
206 1.12 wrstuden if ((j = flags & VDESC_VP0_WILLPUT)) {
207 1.12 wrstuden vp = *VOPARG_OFFSETTO(struct vnode**,offset,ap);
208 1.12 wrstuden switch (j) {
209 1.12 wrstuden case VDESC_VP0_WILLPUT:
210 1.12 wrstuden vput(vp);
211 1.12 wrstuden break;
212 1.12 wrstuden case VDESC_VP0_WILLUNLOCK:
213 1.12 wrstuden VOP_UNLOCK(vp, 0);
214 1.12 wrstuden break;
215 1.12 wrstuden case VDESC_VP0_WILLRELE:
216 1.12 wrstuden vrele(vp);
217 1.12 wrstuden break;
218 1.12 wrstuden }
219 1.12 wrstuden }
220 1.12 wrstuden }
221 1.12 wrstuden
222 1.12 wrstuden return (EOPNOTSUPP);
223 1.12 wrstuden }
224 1.12 wrstuden
225 1.1 mycroft /*ARGSUSED*/
226 1.1 mycroft int
227 1.1 mycroft genfs_ebadf(v)
228 1.1 mycroft void *v;
229 1.1 mycroft {
230 1.1 mycroft
231 1.1 mycroft return (EBADF);
232 1.9 matthias }
233 1.9 matthias
234 1.9 matthias /* ARGSUSED */
235 1.9 matthias int
236 1.9 matthias genfs_enoioctl(v)
237 1.9 matthias void *v;
238 1.9 matthias {
239 1.9 matthias
240 1.9 matthias return (ENOTTY);
241 1.6 fvdl }
242 1.6 fvdl
243 1.6 fvdl
244 1.6 fvdl /*
245 1.13.2.1 bouyer * Eliminate all activity associated with the requested vnode
246 1.6 fvdl * and with all vnodes aliased to the requested vnode.
247 1.6 fvdl */
248 1.6 fvdl int
249 1.6 fvdl genfs_revoke(v)
250 1.6 fvdl void *v;
251 1.6 fvdl {
252 1.6 fvdl struct vop_revoke_args /* {
253 1.6 fvdl struct vnode *a_vp;
254 1.6 fvdl int a_flags;
255 1.6 fvdl } */ *ap = v;
256 1.6 fvdl struct vnode *vp, *vq;
257 1.6 fvdl struct proc *p = curproc; /* XXX */
258 1.6 fvdl
259 1.6 fvdl #ifdef DIAGNOSTIC
260 1.6 fvdl if ((ap->a_flags & REVOKEALL) == 0)
261 1.6 fvdl panic("genfs_revoke: not revokeall");
262 1.6 fvdl #endif
263 1.6 fvdl
264 1.6 fvdl vp = ap->a_vp;
265 1.6 fvdl simple_lock(&vp->v_interlock);
266 1.6 fvdl
267 1.6 fvdl if (vp->v_flag & VALIASED) {
268 1.6 fvdl /*
269 1.6 fvdl * If a vgone (or vclean) is already in progress,
270 1.6 fvdl * wait until it is done and return.
271 1.6 fvdl */
272 1.6 fvdl if (vp->v_flag & VXLOCK) {
273 1.6 fvdl vp->v_flag |= VXWANT;
274 1.6 fvdl simple_unlock(&vp->v_interlock);
275 1.6 fvdl tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
276 1.6 fvdl return (0);
277 1.6 fvdl }
278 1.6 fvdl /*
279 1.6 fvdl * Ensure that vp will not be vgone'd while we
280 1.6 fvdl * are eliminating its aliases.
281 1.6 fvdl */
282 1.6 fvdl vp->v_flag |= VXLOCK;
283 1.6 fvdl simple_unlock(&vp->v_interlock);
284 1.6 fvdl while (vp->v_flag & VALIASED) {
285 1.6 fvdl simple_lock(&spechash_slock);
286 1.6 fvdl for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
287 1.6 fvdl if (vq->v_rdev != vp->v_rdev ||
288 1.6 fvdl vq->v_type != vp->v_type || vp == vq)
289 1.6 fvdl continue;
290 1.6 fvdl simple_unlock(&spechash_slock);
291 1.6 fvdl vgone(vq);
292 1.6 fvdl break;
293 1.6 fvdl }
294 1.6 fvdl if (vq == NULLVP)
295 1.6 fvdl simple_unlock(&spechash_slock);
296 1.6 fvdl }
297 1.6 fvdl /*
298 1.6 fvdl * Remove the lock so that vgone below will
299 1.6 fvdl * really eliminate the vnode after which time
300 1.6 fvdl * vgone will awaken any sleepers.
301 1.6 fvdl */
302 1.6 fvdl simple_lock(&vp->v_interlock);
303 1.6 fvdl vp->v_flag &= ~VXLOCK;
304 1.6 fvdl }
305 1.6 fvdl vgonel(vp, p);
306 1.6 fvdl return (0);
307 1.6 fvdl }
308 1.6 fvdl
309 1.6 fvdl /*
310 1.12 wrstuden * Lock the node.
311 1.6 fvdl */
312 1.6 fvdl int
313 1.12 wrstuden genfs_lock(v)
314 1.6 fvdl void *v;
315 1.6 fvdl {
316 1.6 fvdl struct vop_lock_args /* {
317 1.6 fvdl struct vnode *a_vp;
318 1.6 fvdl int a_flags;
319 1.6 fvdl } */ *ap = v;
320 1.6 fvdl struct vnode *vp = ap->a_vp;
321 1.6 fvdl
322 1.12 wrstuden return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
323 1.6 fvdl }
324 1.6 fvdl
325 1.6 fvdl /*
326 1.12 wrstuden * Unlock the node.
327 1.6 fvdl */
328 1.6 fvdl int
329 1.12 wrstuden genfs_unlock(v)
330 1.6 fvdl void *v;
331 1.6 fvdl {
332 1.6 fvdl struct vop_unlock_args /* {
333 1.6 fvdl struct vnode *a_vp;
334 1.6 fvdl int a_flags;
335 1.6 fvdl } */ *ap = v;
336 1.6 fvdl struct vnode *vp = ap->a_vp;
337 1.6 fvdl
338 1.12 wrstuden return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
339 1.12 wrstuden &vp->v_interlock));
340 1.6 fvdl }
341 1.6 fvdl
342 1.6 fvdl /*
343 1.12 wrstuden * Return whether or not the node is locked.
344 1.6 fvdl */
345 1.6 fvdl int
346 1.12 wrstuden genfs_islocked(v)
347 1.6 fvdl void *v;
348 1.6 fvdl {
349 1.6 fvdl struct vop_islocked_args /* {
350 1.6 fvdl struct vnode *a_vp;
351 1.6 fvdl } */ *ap = v;
352 1.6 fvdl struct vnode *vp = ap->a_vp;
353 1.6 fvdl
354 1.12 wrstuden return (lockstatus(&vp->v_lock));
355 1.12 wrstuden }
356 1.12 wrstuden
357 1.12 wrstuden /*
358 1.12 wrstuden * Stubs to use when there is no locking to be done on the underlying object.
359 1.12 wrstuden */
360 1.12 wrstuden int
361 1.12 wrstuden genfs_nolock(v)
362 1.12 wrstuden void *v;
363 1.12 wrstuden {
364 1.12 wrstuden struct vop_lock_args /* {
365 1.12 wrstuden struct vnode *a_vp;
366 1.12 wrstuden int a_flags;
367 1.12 wrstuden struct proc *a_p;
368 1.12 wrstuden } */ *ap = v;
369 1.12 wrstuden
370 1.12 wrstuden /*
371 1.12 wrstuden * Since we are not using the lock manager, we must clear
372 1.12 wrstuden * the interlock here.
373 1.12 wrstuden */
374 1.12 wrstuden if (ap->a_flags & LK_INTERLOCK)
375 1.12 wrstuden simple_unlock(&ap->a_vp->v_interlock);
376 1.12 wrstuden return (0);
377 1.12 wrstuden }
378 1.12 wrstuden
379 1.12 wrstuden int
380 1.12 wrstuden genfs_nounlock(v)
381 1.12 wrstuden void *v;
382 1.12 wrstuden {
383 1.12 wrstuden return (0);
384 1.12 wrstuden }
385 1.12 wrstuden
386 1.12 wrstuden int
387 1.12 wrstuden genfs_noislocked(v)
388 1.12 wrstuden void *v;
389 1.12 wrstuden {
390 1.12 wrstuden return (0);
391 1.8 thorpej }
392 1.8 thorpej
393 1.8 thorpej /*
394 1.8 thorpej * Local lease check for NFS servers. Just set up args and let
395 1.8 thorpej * nqsrv_getlease() do the rest. If NFSSERVER is not in the kernel,
396 1.8 thorpej * this is a null operation.
397 1.8 thorpej */
398 1.8 thorpej int
399 1.8 thorpej genfs_lease_check(v)
400 1.8 thorpej void *v;
401 1.8 thorpej {
402 1.8 thorpej #ifdef NFSSERVER
403 1.8 thorpej struct vop_lease_args /* {
404 1.8 thorpej struct vnode *a_vp;
405 1.8 thorpej struct proc *a_p;
406 1.8 thorpej struct ucred *a_cred;
407 1.8 thorpej int a_flag;
408 1.8 thorpej } */ *ap = v;
409 1.8 thorpej u_int32_t duration = 0;
410 1.8 thorpej int cache;
411 1.8 thorpej u_quad_t frev;
412 1.8 thorpej
413 1.8 thorpej (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
414 1.8 thorpej NQLOCALSLP, ap->a_p, (struct mbuf *)0, &cache, &frev, ap->a_cred);
415 1.8 thorpej return (0);
416 1.8 thorpej #else
417 1.8 thorpej return (0);
418 1.8 thorpej #endif /* NFSSERVER */
419 1.13.2.2 bouyer }
420 1.13.2.2 bouyer
421 1.13.2.2 bouyer /*
422 1.13.2.2 bouyer * generic VM getpages routine.
423 1.13.2.2 bouyer * Return PG_BUSY pages for the given range,
424 1.13.2.2 bouyer * reading from backing store if necessary.
425 1.13.2.2 bouyer */
426 1.13.2.2 bouyer
427 1.13.2.2 bouyer int
428 1.13.2.2 bouyer genfs_getpages(v)
429 1.13.2.2 bouyer void *v;
430 1.13.2.2 bouyer {
431 1.13.2.2 bouyer struct vop_getpages_args /* {
432 1.13.2.2 bouyer struct vnode *a_vp;
433 1.13.2.2 bouyer voff_t a_offset;
434 1.13.2.2 bouyer vm_page_t *a_m;
435 1.13.2.2 bouyer int *a_count;
436 1.13.2.2 bouyer int a_centeridx;
437 1.13.2.2 bouyer vm_prot_t a_access_type;
438 1.13.2.2 bouyer int a_advice;
439 1.13.2.2 bouyer int a_flags;
440 1.13.2.2 bouyer } */ *ap = v;
441 1.13.2.2 bouyer
442 1.13.2.2 bouyer off_t eof, offset, origoffset, startoffset, endoffset, raoffset;
443 1.13.2.2 bouyer daddr_t lbn, blkno;
444 1.13.2.2 bouyer int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
445 1.13.2.2 bouyer int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
446 1.13.2.2 bouyer int flags = ap->a_flags;
447 1.13.2.2 bouyer size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
448 1.13.2.2 bouyer vaddr_t kva;
449 1.13.2.2 bouyer struct buf *bp, *mbp;
450 1.13.2.2 bouyer struct vnode *vp = ap->a_vp;
451 1.13.2.2 bouyer struct uvm_object *uobj = &vp->v_uvm.u_obj;
452 1.13.2.2 bouyer struct vm_page *pgs[16]; /* XXXUBC 16 */
453 1.13.2.2 bouyer struct ucred *cred = curproc->p_ucred; /* XXXUBC curproc */
454 1.13.2.2 bouyer boolean_t async = (flags & PGO_SYNCIO) == 0;
455 1.13.2.2 bouyer boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
456 1.13.2.2 bouyer boolean_t sawhole = FALSE;
457 1.13.2.2 bouyer UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
458 1.13.2.2 bouyer
459 1.13.2.2 bouyer /* XXXUBC temp limit */
460 1.13.2.2 bouyer if (*ap->a_count > 16) {
461 1.13.2.2 bouyer return EINVAL;
462 1.13.2.2 bouyer }
463 1.13.2.2 bouyer
464 1.13.2.2 bouyer error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
465 1.13.2.2 bouyer if (error) {
466 1.13.2.2 bouyer return error;
467 1.13.2.2 bouyer }
468 1.13.2.2 bouyer
469 1.13.2.2 bouyer #ifdef DIAGNOSTIC
470 1.13.2.2 bouyer if (ap->a_centeridx < 0 || ap->a_centeridx > *ap->a_count) {
471 1.13.2.2 bouyer panic("genfs_getpages: centeridx %d out of range",
472 1.13.2.2 bouyer ap->a_centeridx);
473 1.13.2.2 bouyer }
474 1.13.2.2 bouyer if (ap->a_offset & (PAGE_SIZE - 1) || ap->a_offset < 0) {
475 1.13.2.2 bouyer panic("genfs_getpages: offset 0x%x", (int)ap->a_offset);
476 1.13.2.2 bouyer }
477 1.13.2.2 bouyer if (*ap->a_count < 0) {
478 1.13.2.2 bouyer panic("genfs_getpages: count %d < 0", *ap->a_count);
479 1.13.2.2 bouyer }
480 1.13.2.2 bouyer #endif
481 1.13.2.2 bouyer
482 1.13.2.2 bouyer /*
483 1.13.2.2 bouyer * Bounds-check the request.
484 1.13.2.2 bouyer */
485 1.13.2.2 bouyer
486 1.13.2.2 bouyer error = 0;
487 1.13.2.2 bouyer origoffset = ap->a_offset;
488 1.13.2.2 bouyer
489 1.13.2.2 bouyer if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= eof &&
490 1.13.2.2 bouyer (flags & PGO_PASTEOF) == 0) {
491 1.13.2.2 bouyer if ((flags & PGO_LOCKED) == 0) {
492 1.13.2.2 bouyer simple_unlock(&uobj->vmobjlock);
493 1.13.2.2 bouyer }
494 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
495 1.13.2.2 bouyer origoffset, *ap->a_count, eof,0);
496 1.13.2.2 bouyer return EINVAL;
497 1.13.2.2 bouyer }
498 1.13.2.2 bouyer
499 1.13.2.2 bouyer /*
500 1.13.2.2 bouyer * For PGO_LOCKED requests, just return whatever's in memory.
501 1.13.2.2 bouyer */
502 1.13.2.2 bouyer
503 1.13.2.2 bouyer if (flags & PGO_LOCKED) {
504 1.13.2.2 bouyer uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
505 1.13.2.2 bouyer UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
506 1.13.2.2 bouyer
507 1.13.2.2 bouyer return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
508 1.13.2.2 bouyer }
509 1.13.2.2 bouyer
510 1.13.2.2 bouyer /* vnode is VOP_LOCKed, uobj is locked */
511 1.13.2.2 bouyer
512 1.13.2.2 bouyer if (write && (vp->v_flag & VONWORKLST) == 0) {
513 1.13.2.2 bouyer vn_syncer_add_to_worklist(vp, filedelay);
514 1.13.2.2 bouyer }
515 1.13.2.2 bouyer
516 1.13.2.2 bouyer /*
517 1.13.2.2 bouyer * find the requested pages and make some simple checks.
518 1.13.2.2 bouyer * leave space in the page array for a whole block.
519 1.13.2.2 bouyer */
520 1.13.2.2 bouyer
521 1.13.2.2 bouyer fs_bshift = vp->v_mount->mnt_fs_bshift;
522 1.13.2.2 bouyer fs_bsize = 1 << fs_bshift;
523 1.13.2.2 bouyer dev_bshift = vp->v_mount->mnt_dev_bshift;
524 1.13.2.2 bouyer dev_bsize = 1 << dev_bshift;
525 1.13.2.2 bouyer KASSERT((eof & (dev_bsize - 1)) == 0);
526 1.13.2.2 bouyer
527 1.13.2.2 bouyer orignpages = min(*ap->a_count,
528 1.13.2.2 bouyer round_page(eof - origoffset) >> PAGE_SHIFT);
529 1.13.2.2 bouyer if (flags & PGO_PASTEOF) {
530 1.13.2.2 bouyer orignpages = *ap->a_count;
531 1.13.2.2 bouyer }
532 1.13.2.2 bouyer npages = orignpages;
533 1.13.2.2 bouyer startoffset = origoffset & ~(fs_bsize - 1);
534 1.13.2.2 bouyer endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
535 1.13.2.2 bouyer + fs_bsize - 1) & ~(fs_bsize - 1));
536 1.13.2.2 bouyer endoffset = min(endoffset, round_page(eof));
537 1.13.2.2 bouyer ridx = (origoffset - startoffset) >> PAGE_SHIFT;
538 1.13.2.2 bouyer
539 1.13.2.2 bouyer memset(pgs, 0, sizeof(pgs));
540 1.13.2.2 bouyer uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
541 1.13.2.2 bouyer
542 1.13.2.2 bouyer /*
543 1.13.2.2 bouyer * if PGO_OVERWRITE is set, don't bother reading the pages.
544 1.13.2.2 bouyer * PGO_OVERWRITE also means that the caller guarantees
545 1.13.2.2 bouyer * that the pages already have backing store allocated.
546 1.13.2.2 bouyer */
547 1.13.2.2 bouyer
548 1.13.2.2 bouyer if (flags & PGO_OVERWRITE) {
549 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
550 1.13.2.2 bouyer
551 1.13.2.2 bouyer for (i = 0; i < npages; i++) {
552 1.13.2.2 bouyer struct vm_page *pg = pgs[ridx + i];
553 1.13.2.2 bouyer
554 1.13.2.2 bouyer if (pg->flags & PG_FAKE) {
555 1.13.2.2 bouyer uvm_pagezero(pg);
556 1.13.2.2 bouyer pg->flags &= ~(PG_FAKE);
557 1.13.2.2 bouyer }
558 1.13.2.2 bouyer pg->flags &= ~(PG_RDONLY);
559 1.13.2.2 bouyer }
560 1.13.2.2 bouyer goto out;
561 1.13.2.2 bouyer }
562 1.13.2.2 bouyer
563 1.13.2.2 bouyer /*
564 1.13.2.2 bouyer * if the pages are already resident, just return them.
565 1.13.2.2 bouyer */
566 1.13.2.2 bouyer
567 1.13.2.2 bouyer for (i = 0; i < npages; i++) {
568 1.13.2.2 bouyer struct vm_page *pg = pgs[ridx + i];
569 1.13.2.2 bouyer
570 1.13.2.2 bouyer if ((pg->flags & PG_FAKE) ||
571 1.13.2.2 bouyer (write && (pg->flags & PG_RDONLY))) {
572 1.13.2.2 bouyer break;
573 1.13.2.2 bouyer }
574 1.13.2.2 bouyer }
575 1.13.2.2 bouyer if (i == npages) {
576 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
577 1.13.2.2 bouyer raoffset = origoffset + (orignpages << PAGE_SHIFT);
578 1.13.2.2 bouyer goto raout;
579 1.13.2.2 bouyer }
580 1.13.2.2 bouyer
581 1.13.2.2 bouyer /*
582 1.13.2.2 bouyer * the page wasn't resident and we're not overwriting,
583 1.13.2.2 bouyer * so we're going to have to do some i/o.
584 1.13.2.2 bouyer * find any additional pages needed to cover the expanded range.
585 1.13.2.2 bouyer */
586 1.13.2.2 bouyer
587 1.13.2.2 bouyer if (startoffset != origoffset) {
588 1.13.2.2 bouyer
589 1.13.2.2 bouyer /*
590 1.13.2.2 bouyer * XXXUBC we need to avoid deadlocks caused by locking
591 1.13.2.2 bouyer * additional pages at lower offsets than pages we
592 1.13.2.2 bouyer * already have locked. for now, unlock them all and
593 1.13.2.2 bouyer * start over.
594 1.13.2.2 bouyer */
595 1.13.2.2 bouyer
596 1.13.2.2 bouyer for (i = 0; i < npages; i++) {
597 1.13.2.2 bouyer struct vm_page *pg = pgs[ridx + i];
598 1.13.2.2 bouyer
599 1.13.2.2 bouyer if (pg->flags & PG_FAKE) {
600 1.13.2.2 bouyer pg->flags |= PG_RELEASED;
601 1.13.2.2 bouyer }
602 1.13.2.2 bouyer }
603 1.13.2.2 bouyer uvm_page_unbusy(&pgs[ridx], npages);
604 1.13.2.2 bouyer memset(pgs, 0, sizeof(pgs));
605 1.13.2.2 bouyer
606 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
607 1.13.2.2 bouyer startoffset, endoffset, 0,0);
608 1.13.2.2 bouyer npages = (endoffset - startoffset) >> PAGE_SHIFT;
609 1.13.2.2 bouyer npgs = npages;
610 1.13.2.2 bouyer uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
611 1.13.2.2 bouyer }
612 1.13.2.2 bouyer simple_unlock(&uobj->vmobjlock);
613 1.13.2.2 bouyer
614 1.13.2.2 bouyer /*
615 1.13.2.2 bouyer * read the desired page(s).
616 1.13.2.2 bouyer */
617 1.13.2.2 bouyer
618 1.13.2.2 bouyer totalbytes = npages << PAGE_SHIFT;
619 1.13.2.2 bouyer bytes = min(totalbytes, eof - startoffset);
620 1.13.2.2 bouyer tailbytes = totalbytes - bytes;
621 1.13.2.2 bouyer skipbytes = 0;
622 1.13.2.2 bouyer
623 1.13.2.2 bouyer kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
624 1.13.2.2 bouyer UVMPAGER_MAPIN_READ);
625 1.13.2.2 bouyer
626 1.13.2.2 bouyer s = splbio();
627 1.13.2.2 bouyer mbp = pool_get(&bufpool, PR_WAITOK);
628 1.13.2.2 bouyer splx(s);
629 1.13.2.2 bouyer mbp->b_bufsize = totalbytes;
630 1.13.2.2 bouyer mbp->b_data = (void *)kva;
631 1.13.2.2 bouyer mbp->b_resid = mbp->b_bcount = bytes;
632 1.13.2.2 bouyer mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL : 0);
633 1.13.2.2 bouyer mbp->b_iodone = uvm_aio_biodone;
634 1.13.2.2 bouyer mbp->b_vp = vp;
635 1.13.2.2 bouyer LIST_INIT(&mbp->b_dep);
636 1.13.2.2 bouyer
637 1.13.2.2 bouyer /*
638 1.13.2.2 bouyer * if EOF is in the middle of the last page, zero the part past EOF.
639 1.13.2.2 bouyer */
640 1.13.2.2 bouyer
641 1.13.2.3 bouyer if (tailbytes > 0 && (pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE)) {
642 1.13.2.2 bouyer memset((void *)(kva + bytes), 0, tailbytes);
643 1.13.2.2 bouyer }
644 1.13.2.2 bouyer
645 1.13.2.2 bouyer /*
646 1.13.2.2 bouyer * now loop over the pages, reading as needed.
647 1.13.2.2 bouyer */
648 1.13.2.2 bouyer
649 1.13.2.2 bouyer if (write) {
650 1.13.2.2 bouyer lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
651 1.13.2.2 bouyer } else {
652 1.13.2.2 bouyer lockmgr(&vp->v_glock, LK_SHARED, NULL);
653 1.13.2.2 bouyer }
654 1.13.2.2 bouyer
655 1.13.2.2 bouyer bp = NULL;
656 1.13.2.2 bouyer for (offset = startoffset;
657 1.13.2.2 bouyer bytes > 0;
658 1.13.2.2 bouyer offset += iobytes, bytes -= iobytes) {
659 1.13.2.2 bouyer
660 1.13.2.2 bouyer /*
661 1.13.2.2 bouyer * skip pages which don't need to be read.
662 1.13.2.2 bouyer */
663 1.13.2.2 bouyer
664 1.13.2.2 bouyer pidx = (offset - startoffset) >> PAGE_SHIFT;
665 1.13.2.2 bouyer while ((pgs[pidx]->flags & PG_FAKE) == 0) {
666 1.13.2.2 bouyer size_t b;
667 1.13.2.2 bouyer
668 1.13.2.2 bouyer #ifdef DEBUG
669 1.13.2.2 bouyer if (offset & (PAGE_SIZE - 1)) {
670 1.13.2.2 bouyer panic("genfs_getpages: skipping from middle "
671 1.13.2.2 bouyer "of page");
672 1.13.2.2 bouyer }
673 1.13.2.2 bouyer #endif
674 1.13.2.2 bouyer
675 1.13.2.2 bouyer b = min(PAGE_SIZE, bytes);
676 1.13.2.2 bouyer offset += b;
677 1.13.2.2 bouyer bytes -= b;
678 1.13.2.2 bouyer skipbytes += b;
679 1.13.2.2 bouyer pidx++;
680 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
681 1.13.2.2 bouyer offset, 0,0,0);
682 1.13.2.2 bouyer if (bytes == 0) {
683 1.13.2.2 bouyer goto loopdone;
684 1.13.2.2 bouyer }
685 1.13.2.2 bouyer }
686 1.13.2.2 bouyer
687 1.13.2.2 bouyer /*
688 1.13.2.2 bouyer * bmap the file to find out the blkno to read from and
689 1.13.2.2 bouyer * how much we can read in one i/o. if bmap returns an error,
690 1.13.2.2 bouyer * skip the rest of the top-level i/o.
691 1.13.2.2 bouyer */
692 1.13.2.2 bouyer
693 1.13.2.2 bouyer lbn = offset >> fs_bshift;
694 1.13.2.2 bouyer error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
695 1.13.2.2 bouyer if (error) {
696 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
697 1.13.2.2 bouyer lbn, error,0,0);
698 1.13.2.2 bouyer skipbytes += bytes;
699 1.13.2.2 bouyer goto loopdone;
700 1.13.2.2 bouyer }
701 1.13.2.2 bouyer
702 1.13.2.2 bouyer /*
703 1.13.2.2 bouyer * see how many pages can be read with this i/o.
704 1.13.2.2 bouyer * reduce the i/o size if necessary to avoid
705 1.13.2.2 bouyer * overwriting pages with valid data.
706 1.13.2.2 bouyer */
707 1.13.2.2 bouyer
708 1.13.2.2 bouyer iobytes = min(((lbn + 1 + run) << fs_bshift) - offset, bytes);
709 1.13.2.2 bouyer if (offset + iobytes > round_page(offset)) {
710 1.13.2.2 bouyer pcount = 1;
711 1.13.2.2 bouyer while (pidx + pcount < npages &&
712 1.13.2.2 bouyer pgs[pidx + pcount]->flags & PG_FAKE) {
713 1.13.2.2 bouyer pcount++;
714 1.13.2.2 bouyer }
715 1.13.2.2 bouyer iobytes = min(iobytes, (pcount << PAGE_SHIFT) -
716 1.13.2.2 bouyer (offset - trunc_page(offset)));
717 1.13.2.2 bouyer }
718 1.13.2.2 bouyer
719 1.13.2.2 bouyer /*
720 1.13.2.2 bouyer * if this block isn't allocated, zero it instead of reading it.
721 1.13.2.2 bouyer * if this is a read access, mark the pages we zeroed PG_RDONLY.
722 1.13.2.2 bouyer */
723 1.13.2.2 bouyer
724 1.13.2.2 bouyer if (blkno < 0) {
725 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
726 1.13.2.2 bouyer
727 1.13.2.2 bouyer sawhole = TRUE;
728 1.13.2.2 bouyer memset((char *)kva + (offset - startoffset), 0,
729 1.13.2.2 bouyer iobytes);
730 1.13.2.2 bouyer skipbytes += iobytes;
731 1.13.2.2 bouyer
732 1.13.2.2 bouyer if (!write) {
733 1.13.2.2 bouyer int holepages =
734 1.13.2.2 bouyer (round_page(offset + iobytes) -
735 1.13.2.2 bouyer trunc_page(offset)) >> PAGE_SHIFT;
736 1.13.2.2 bouyer for (i = 0; i < holepages; i++) {
737 1.13.2.2 bouyer pgs[pidx + i]->flags |= PG_RDONLY;
738 1.13.2.2 bouyer }
739 1.13.2.2 bouyer }
740 1.13.2.2 bouyer continue;
741 1.13.2.2 bouyer }
742 1.13.2.2 bouyer
743 1.13.2.2 bouyer /*
744 1.13.2.2 bouyer * allocate a sub-buf for this piece of the i/o
745 1.13.2.2 bouyer * (or just use mbp if there's only 1 piece),
746 1.13.2.2 bouyer * and start it going.
747 1.13.2.2 bouyer */
748 1.13.2.2 bouyer
749 1.13.2.2 bouyer if (offset == startoffset && iobytes == bytes) {
750 1.13.2.2 bouyer bp = mbp;
751 1.13.2.2 bouyer } else {
752 1.13.2.2 bouyer s = splbio();
753 1.13.2.2 bouyer bp = pool_get(&bufpool, PR_WAITOK);
754 1.13.2.2 bouyer splx(s);
755 1.13.2.2 bouyer bp->b_data = (char *)kva + offset - startoffset;
756 1.13.2.2 bouyer bp->b_resid = bp->b_bcount = iobytes;
757 1.13.2.2 bouyer bp->b_flags = B_BUSY|B_READ|B_CALL;
758 1.13.2.2 bouyer bp->b_iodone = uvm_aio_biodone1;
759 1.13.2.2 bouyer bp->b_vp = vp;
760 1.13.2.2 bouyer LIST_INIT(&bp->b_dep);
761 1.13.2.2 bouyer }
762 1.13.2.2 bouyer bp->b_lblkno = 0;
763 1.13.2.2 bouyer bp->b_private = mbp;
764 1.13.2.2 bouyer
765 1.13.2.2 bouyer /* adjust physical blkno for partial blocks */
766 1.13.2.2 bouyer bp->b_blkno = blkno + ((offset - (lbn << fs_bshift)) >>
767 1.13.2.2 bouyer dev_bshift);
768 1.13.2.2 bouyer
769 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
770 1.13.2.2 bouyer bp, offset, iobytes, bp->b_blkno);
771 1.13.2.2 bouyer
772 1.13.2.2 bouyer VOP_STRATEGY(bp);
773 1.13.2.2 bouyer }
774 1.13.2.2 bouyer
775 1.13.2.2 bouyer loopdone:
776 1.13.2.2 bouyer if (skipbytes) {
777 1.13.2.2 bouyer s = splbio();
778 1.13.2.2 bouyer if (error) {
779 1.13.2.2 bouyer mbp->b_flags |= B_ERROR;
780 1.13.2.2 bouyer mbp->b_error = error;
781 1.13.2.2 bouyer }
782 1.13.2.2 bouyer mbp->b_resid -= skipbytes;
783 1.13.2.2 bouyer if (mbp->b_resid == 0) {
784 1.13.2.2 bouyer biodone(mbp);
785 1.13.2.2 bouyer }
786 1.13.2.2 bouyer splx(s);
787 1.13.2.2 bouyer }
788 1.13.2.2 bouyer
789 1.13.2.2 bouyer if (async) {
790 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "returning PEND",0,0,0,0);
791 1.13.2.2 bouyer lockmgr(&vp->v_glock, LK_RELEASE, NULL);
792 1.13.2.2 bouyer return EINPROGRESS;
793 1.13.2.2 bouyer }
794 1.13.2.2 bouyer if (bp != NULL) {
795 1.13.2.2 bouyer error = biowait(mbp);
796 1.13.2.2 bouyer }
797 1.13.2.2 bouyer s = splbio();
798 1.13.2.2 bouyer pool_put(&bufpool, mbp);
799 1.13.2.2 bouyer splx(s);
800 1.13.2.2 bouyer uvm_pagermapout(kva, npages);
801 1.13.2.2 bouyer raoffset = offset;
802 1.13.2.2 bouyer
803 1.13.2.2 bouyer /*
804 1.13.2.2 bouyer * if this we encountered a hole then we have to do a little more work.
805 1.13.2.2 bouyer * for read faults, we marked the page PG_RDONLY so that future
806 1.13.2.2 bouyer * write accesses to the page will fault again.
807 1.13.2.2 bouyer * for write faults, we must make sure that the backing store for
808 1.13.2.2 bouyer * the page is completely allocated while the pages are locked.
809 1.13.2.2 bouyer */
810 1.13.2.2 bouyer
811 1.13.2.2 bouyer if (error == 0 && sawhole && write) {
812 1.13.2.2 bouyer error = VOP_BALLOCN(vp, startoffset, npages << PAGE_SHIFT,
813 1.13.2.2 bouyer cred, 0);
814 1.13.2.2 bouyer if (error) {
815 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
816 1.13.2.2 bouyer lbn, error,0,0);
817 1.13.2.2 bouyer lockmgr(&vp->v_glock, LK_RELEASE, NULL);
818 1.13.2.2 bouyer simple_lock(&uobj->vmobjlock);
819 1.13.2.2 bouyer goto out;
820 1.13.2.2 bouyer }
821 1.13.2.2 bouyer }
822 1.13.2.2 bouyer lockmgr(&vp->v_glock, LK_RELEASE, NULL);
823 1.13.2.2 bouyer simple_lock(&uobj->vmobjlock);
824 1.13.2.2 bouyer
825 1.13.2.2 bouyer /*
826 1.13.2.2 bouyer * see if we want to start any readahead.
827 1.13.2.2 bouyer * XXXUBC for now, just read the next 128k on 64k boundaries.
828 1.13.2.2 bouyer * this is pretty nonsensical, but it is 50% faster than reading
829 1.13.2.2 bouyer * just the next 64k.
830 1.13.2.2 bouyer */
831 1.13.2.2 bouyer
832 1.13.2.2 bouyer raout:
833 1.13.2.2 bouyer if (!async && !write && ((int)raoffset & 0xffff) == 0 &&
834 1.13.2.2 bouyer PAGE_SHIFT <= 16) {
835 1.13.2.2 bouyer int racount;
836 1.13.2.2 bouyer
837 1.13.2.2 bouyer racount = 1 << (16 - PAGE_SHIFT);
838 1.13.2.2 bouyer (void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
839 1.13.2.2 bouyer VM_PROT_READ, 0, 0);
840 1.13.2.2 bouyer simple_lock(&uobj->vmobjlock);
841 1.13.2.2 bouyer
842 1.13.2.2 bouyer racount = 1 << (16 - PAGE_SHIFT);
843 1.13.2.2 bouyer (void) VOP_GETPAGES(vp, raoffset + 0x10000, NULL, &racount, 0,
844 1.13.2.2 bouyer VM_PROT_READ, 0, 0);
845 1.13.2.2 bouyer simple_lock(&uobj->vmobjlock);
846 1.13.2.2 bouyer }
847 1.13.2.2 bouyer
848 1.13.2.2 bouyer /*
849 1.13.2.2 bouyer * we're almost done! release the pages...
850 1.13.2.2 bouyer * for errors, we free the pages.
851 1.13.2.2 bouyer * otherwise we activate them and mark them as valid and clean.
852 1.13.2.2 bouyer * also, unbusy pages that were not actually requested.
853 1.13.2.2 bouyer */
854 1.13.2.2 bouyer
855 1.13.2.2 bouyer out:
856 1.13.2.2 bouyer if (error) {
857 1.13.2.2 bouyer uvm_lock_pageq();
858 1.13.2.2 bouyer for (i = 0; i < npages; i++) {
859 1.13.2.2 bouyer if (pgs[i] == NULL) {
860 1.13.2.2 bouyer continue;
861 1.13.2.2 bouyer }
862 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
863 1.13.2.2 bouyer pgs[i], pgs[i]->flags, 0,0);
864 1.13.2.2 bouyer if ((pgs[i]->flags & PG_FAKE) == 0) {
865 1.13.2.2 bouyer continue;
866 1.13.2.2 bouyer }
867 1.13.2.2 bouyer if (pgs[i]->flags & PG_WANTED) {
868 1.13.2.2 bouyer wakeup(pgs[i]);
869 1.13.2.2 bouyer }
870 1.13.2.2 bouyer uvm_pagefree(pgs[i]);
871 1.13.2.2 bouyer }
872 1.13.2.2 bouyer uvm_unlock_pageq();
873 1.13.2.2 bouyer simple_unlock(&uobj->vmobjlock);
874 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
875 1.13.2.2 bouyer return error;
876 1.13.2.2 bouyer }
877 1.13.2.2 bouyer
878 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
879 1.13.2.2 bouyer for (i = 0; i < npages; i++) {
880 1.13.2.2 bouyer if (pgs[i] == NULL) {
881 1.13.2.2 bouyer continue;
882 1.13.2.2 bouyer }
883 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
884 1.13.2.2 bouyer pgs[i], pgs[i]->flags, 0,0);
885 1.13.2.2 bouyer if (pgs[i]->flags & PG_FAKE) {
886 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
887 1.13.2.2 bouyer pgs[i], pgs[i]->offset,0,0);
888 1.13.2.2 bouyer pgs[i]->flags &= ~(PG_FAKE);
889 1.13.2.2 bouyer pmap_clear_modify(pgs[i]);
890 1.13.2.2 bouyer pmap_clear_reference(pgs[i]);
891 1.13.2.2 bouyer }
892 1.13.2.2 bouyer if (write) {
893 1.13.2.2 bouyer pgs[i]->flags &= ~(PG_RDONLY);
894 1.13.2.2 bouyer }
895 1.13.2.2 bouyer if (i < ridx || i >= ridx + orignpages || async) {
896 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
897 1.13.2.2 bouyer pgs[i], pgs[i]->offset,0,0);
898 1.13.2.2 bouyer if (pgs[i]->flags & PG_WANTED) {
899 1.13.2.2 bouyer wakeup(pgs[i]);
900 1.13.2.2 bouyer }
901 1.13.2.2 bouyer if (pgs[i]->wire_count == 0) {
902 1.13.2.2 bouyer uvm_pageactivate(pgs[i]);
903 1.13.2.2 bouyer }
904 1.13.2.2 bouyer pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
905 1.13.2.2 bouyer UVM_PAGE_OWN(pgs[i], NULL);
906 1.13.2.2 bouyer }
907 1.13.2.2 bouyer }
908 1.13.2.2 bouyer simple_unlock(&uobj->vmobjlock);
909 1.13.2.2 bouyer if (ap->a_m != NULL) {
910 1.13.2.2 bouyer memcpy(ap->a_m, &pgs[ridx],
911 1.13.2.2 bouyer orignpages * sizeof(struct vm_page *));
912 1.13.2.2 bouyer }
913 1.13.2.2 bouyer return 0;
914 1.13.2.2 bouyer }
915 1.13.2.2 bouyer
916 1.13.2.2 bouyer /*
917 1.13.2.2 bouyer * generic VM putpages routine.
918 1.13.2.2 bouyer * Write the given range of pages to backing store.
919 1.13.2.2 bouyer */
920 1.13.2.2 bouyer
921 1.13.2.2 bouyer int
922 1.13.2.2 bouyer genfs_putpages(v)
923 1.13.2.2 bouyer void *v;
924 1.13.2.2 bouyer {
925 1.13.2.2 bouyer struct vop_putpages_args /* {
926 1.13.2.2 bouyer struct vnode *a_vp;
927 1.13.2.2 bouyer struct vm_page **a_m;
928 1.13.2.2 bouyer int a_count;
929 1.13.2.2 bouyer int a_flags;
930 1.13.2.2 bouyer int *a_rtvals;
931 1.13.2.2 bouyer } */ *ap = v;
932 1.13.2.2 bouyer
933 1.13.2.2 bouyer int s, error, error2, npages, run;
934 1.13.2.2 bouyer int fs_bshift, dev_bshift, dev_bsize;
935 1.13.2.2 bouyer vaddr_t kva;
936 1.13.2.2 bouyer off_t eof, offset, startoffset;
937 1.13.2.2 bouyer size_t bytes, iobytes, skipbytes;
938 1.13.2.2 bouyer daddr_t lbn, blkno;
939 1.13.2.2 bouyer struct vm_page *pg;
940 1.13.2.2 bouyer struct buf *mbp, *bp;
941 1.13.2.2 bouyer struct vnode *vp = ap->a_vp;
942 1.13.2.2 bouyer boolean_t async = (ap->a_flags & PGO_SYNCIO) == 0;
943 1.13.2.2 bouyer UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
944 1.13.2.2 bouyer
945 1.13.2.2 bouyer simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
946 1.13.2.2 bouyer
947 1.13.2.2 bouyer error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
948 1.13.2.2 bouyer if (error) {
949 1.13.2.2 bouyer return error;
950 1.13.2.2 bouyer }
951 1.13.2.2 bouyer
952 1.13.2.2 bouyer error = error2 = 0;
953 1.13.2.2 bouyer npages = ap->a_count;
954 1.13.2.2 bouyer fs_bshift = vp->v_mount->mnt_fs_bshift;
955 1.13.2.2 bouyer dev_bshift = vp->v_mount->mnt_dev_bshift;
956 1.13.2.2 bouyer dev_bsize = 1 << dev_bshift;
957 1.13.2.2 bouyer KASSERT((eof & (dev_bsize - 1)) == 0);
958 1.13.2.2 bouyer
959 1.13.2.2 bouyer pg = ap->a_m[0];
960 1.13.2.2 bouyer startoffset = pg->offset;
961 1.13.2.2 bouyer bytes = min(npages << PAGE_SHIFT, eof - startoffset);
962 1.13.2.2 bouyer skipbytes = 0;
963 1.13.2.2 bouyer KASSERT(bytes != 0);
964 1.13.2.2 bouyer
965 1.13.2.2 bouyer kva = uvm_pagermapin(ap->a_m, npages, UVMPAGER_MAPIN_WAITOK);
966 1.13.2.2 bouyer
967 1.13.2.2 bouyer s = splbio();
968 1.13.2.2 bouyer vp->v_numoutput += 2;
969 1.13.2.2 bouyer mbp = pool_get(&bufpool, PR_WAITOK);
970 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
971 1.13.2.2 bouyer vp, mbp, vp->v_numoutput, bytes);
972 1.13.2.2 bouyer splx(s);
973 1.13.2.2 bouyer mbp->b_bufsize = npages << PAGE_SHIFT;
974 1.13.2.2 bouyer mbp->b_data = (void *)kva;
975 1.13.2.2 bouyer mbp->b_resid = mbp->b_bcount = bytes;
976 1.13.2.2 bouyer mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
977 1.13.2.2 bouyer (async ? B_CALL : 0) |
978 1.13.2.2 bouyer (curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
979 1.13.2.2 bouyer mbp->b_iodone = uvm_aio_biodone;
980 1.13.2.2 bouyer mbp->b_vp = vp;
981 1.13.2.2 bouyer LIST_INIT(&mbp->b_dep);
982 1.13.2.2 bouyer
983 1.13.2.2 bouyer bp = NULL;
984 1.13.2.2 bouyer for (offset = startoffset;
985 1.13.2.2 bouyer bytes > 0;
986 1.13.2.2 bouyer offset += iobytes, bytes -= iobytes) {
987 1.13.2.2 bouyer lbn = offset >> fs_bshift;
988 1.13.2.2 bouyer error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
989 1.13.2.2 bouyer if (error) {
990 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
991 1.13.2.2 bouyer skipbytes += bytes;
992 1.13.2.2 bouyer bytes = 0;
993 1.13.2.2 bouyer break;
994 1.13.2.2 bouyer }
995 1.13.2.2 bouyer
996 1.13.2.2 bouyer iobytes = min(((lbn + 1 + run) << fs_bshift) - offset, bytes);
997 1.13.2.2 bouyer if (blkno == (daddr_t)-1) {
998 1.13.2.2 bouyer skipbytes += iobytes;
999 1.13.2.2 bouyer continue;
1000 1.13.2.2 bouyer }
1001 1.13.2.2 bouyer
1002 1.13.2.2 bouyer /* if it's really one i/o, don't make a second buf */
1003 1.13.2.2 bouyer if (offset == startoffset && iobytes == bytes) {
1004 1.13.2.2 bouyer bp = mbp;
1005 1.13.2.2 bouyer } else {
1006 1.13.2.2 bouyer s = splbio();
1007 1.13.2.2 bouyer vp->v_numoutput++;
1008 1.13.2.2 bouyer bp = pool_get(&bufpool, PR_WAITOK);
1009 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1010 1.13.2.2 bouyer vp, bp, vp->v_numoutput, 0);
1011 1.13.2.2 bouyer splx(s);
1012 1.13.2.2 bouyer bp->b_data = (char *)kva +
1013 1.13.2.2 bouyer (vaddr_t)(offset - pg->offset);
1014 1.13.2.2 bouyer bp->b_resid = bp->b_bcount = iobytes;
1015 1.13.2.2 bouyer bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1016 1.13.2.2 bouyer bp->b_iodone = uvm_aio_biodone1;
1017 1.13.2.2 bouyer bp->b_vp = vp;
1018 1.13.2.2 bouyer LIST_INIT(&bp->b_dep);
1019 1.13.2.2 bouyer }
1020 1.13.2.2 bouyer bp->b_lblkno = 0;
1021 1.13.2.2 bouyer bp->b_private = mbp;
1022 1.13.2.2 bouyer
1023 1.13.2.2 bouyer /* adjust physical blkno for partial blocks */
1024 1.13.2.2 bouyer bp->b_blkno = blkno + ((offset - (lbn << fs_bshift)) >>
1025 1.13.2.2 bouyer dev_bshift);
1026 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1027 1.13.2.2 bouyer vp, offset, bp->b_bcount, bp->b_blkno);
1028 1.13.2.2 bouyer VOP_STRATEGY(bp);
1029 1.13.2.2 bouyer }
1030 1.13.2.2 bouyer if (skipbytes) {
1031 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
1032 1.13.2.2 bouyer s = splbio();
1033 1.13.2.2 bouyer mbp->b_resid -= skipbytes;
1034 1.13.2.2 bouyer if (mbp->b_resid == 0) {
1035 1.13.2.2 bouyer biodone(mbp);
1036 1.13.2.2 bouyer }
1037 1.13.2.2 bouyer splx(s);
1038 1.13.2.2 bouyer }
1039 1.13.2.2 bouyer if (async) {
1040 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "returning PEND", 0,0,0,0);
1041 1.13.2.2 bouyer return EINPROGRESS;
1042 1.13.2.2 bouyer }
1043 1.13.2.2 bouyer if (bp != NULL) {
1044 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1045 1.13.2.2 bouyer error2 = biowait(mbp);
1046 1.13.2.2 bouyer }
1047 1.13.2.2 bouyer if (bioops.io_pageiodone) {
1048 1.13.2.2 bouyer (*bioops.io_pageiodone)(mbp);
1049 1.13.2.2 bouyer }
1050 1.13.2.2 bouyer s = splbio();
1051 1.13.2.2 bouyer vwakeup(mbp);
1052 1.13.2.2 bouyer pool_put(&bufpool, mbp);
1053 1.13.2.2 bouyer splx(s);
1054 1.13.2.2 bouyer uvm_pagermapout(kva, npages);
1055 1.13.2.2 bouyer UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1056 1.13.2.2 bouyer return error ? error : error2;
1057 1.13.2.2 bouyer }
1058 1.13.2.2 bouyer
1059 1.13.2.2 bouyer int
1060 1.13.2.2 bouyer genfs_size(v)
1061 1.13.2.2 bouyer void *v;
1062 1.13.2.2 bouyer {
1063 1.13.2.2 bouyer struct vop_size_args /* {
1064 1.13.2.2 bouyer struct vnode *a_vp;
1065 1.13.2.2 bouyer off_t a_size;
1066 1.13.2.2 bouyer off_t *a_eobp;
1067 1.13.2.2 bouyer } */ *ap = v;
1068 1.13.2.2 bouyer int bsize;
1069 1.13.2.2 bouyer
1070 1.13.2.2 bouyer bsize = 1 << ap->a_vp->v_mount->mnt_fs_bshift;
1071 1.13.2.2 bouyer *ap->a_eobp = (ap->a_size + bsize) & ~(bsize - 1);
1072 1.13.2.2 bouyer return 0;
1073 1.1 mycroft }
1074