genfs_vnops.c revision 1.21 1 1.21 chs /* $NetBSD: genfs_vnops.c,v 1.21 2000/11/27 08:39:45 chs 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.21 chs #include <uvm/uvm.h>
54 1.21 chs #include <uvm/uvm_pager.h>
55 1.21 chs
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.20 fvdl off_t offlo;
86 1.20 fvdl off_t offhi;
87 1.1 mycroft struct proc *a_p;
88 1.1 mycroft } */ *ap = v;
89 1.16 augustss 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.18 mycroft 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.19 thorpej 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.15 fvdl * 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.21 chs }
420 1.21 chs
421 1.21 chs /*
422 1.21 chs * generic VM getpages routine.
423 1.21 chs * Return PG_BUSY pages for the given range,
424 1.21 chs * reading from backing store if necessary.
425 1.21 chs */
426 1.21 chs
427 1.21 chs int
428 1.21 chs genfs_getpages(v)
429 1.21 chs void *v;
430 1.21 chs {
431 1.21 chs struct vop_getpages_args /* {
432 1.21 chs struct vnode *a_vp;
433 1.21 chs voff_t a_offset;
434 1.21 chs vm_page_t *a_m;
435 1.21 chs int *a_count;
436 1.21 chs int a_centeridx;
437 1.21 chs vm_prot_t a_access_type;
438 1.21 chs int a_advice;
439 1.21 chs int a_flags;
440 1.21 chs } */ *ap = v;
441 1.21 chs
442 1.21 chs off_t eof, offset, origoffset, startoffset, endoffset, raoffset;
443 1.21 chs daddr_t lbn, blkno;
444 1.21 chs int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
445 1.21 chs int fs_bshift, fs_bsize, dev_bshift, dev_bsize;
446 1.21 chs int flags = ap->a_flags;
447 1.21 chs size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
448 1.21 chs vaddr_t kva;
449 1.21 chs struct buf *bp, *mbp;
450 1.21 chs struct vnode *vp = ap->a_vp;
451 1.21 chs struct uvm_object *uobj = &vp->v_uvm.u_obj;
452 1.21 chs struct vm_page *pgs[16]; /* XXXUBC 16 */
453 1.21 chs struct ucred *cred = curproc->p_ucred; /* XXXUBC curproc */
454 1.21 chs boolean_t async = (flags & PGO_SYNCIO) == 0;
455 1.21 chs boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
456 1.21 chs boolean_t sawhole = FALSE;
457 1.21 chs UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
458 1.21 chs
459 1.21 chs /* XXXUBC temp limit */
460 1.21 chs if (*ap->a_count > 16) {
461 1.21 chs return EINVAL;
462 1.21 chs }
463 1.21 chs
464 1.21 chs error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
465 1.21 chs if (error) {
466 1.21 chs return error;
467 1.21 chs }
468 1.21 chs
469 1.21 chs #ifdef DIAGNOSTIC
470 1.21 chs if (ap->a_centeridx < 0 || ap->a_centeridx > *ap->a_count) {
471 1.21 chs panic("genfs_getpages: centeridx %d out of range",
472 1.21 chs ap->a_centeridx);
473 1.21 chs }
474 1.21 chs if (ap->a_offset & (PAGE_SIZE - 1) || ap->a_offset < 0) {
475 1.21 chs panic("genfs_getpages: offset 0x%x", (int)ap->a_offset);
476 1.21 chs }
477 1.21 chs if (*ap->a_count < 0) {
478 1.21 chs panic("genfs_getpages: count %d < 0", *ap->a_count);
479 1.21 chs }
480 1.21 chs #endif
481 1.21 chs
482 1.21 chs /*
483 1.21 chs * Bounds-check the request.
484 1.21 chs */
485 1.21 chs
486 1.21 chs error = 0;
487 1.21 chs origoffset = ap->a_offset;
488 1.21 chs
489 1.21 chs if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= eof &&
490 1.21 chs (flags & PGO_PASTEOF) == 0) {
491 1.21 chs if ((flags & PGO_LOCKED) == 0) {
492 1.21 chs simple_unlock(&uobj->vmobjlock);
493 1.21 chs }
494 1.21 chs UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
495 1.21 chs origoffset, *ap->a_count, eof,0);
496 1.21 chs return EINVAL;
497 1.21 chs }
498 1.21 chs
499 1.21 chs /*
500 1.21 chs * For PGO_LOCKED requests, just return whatever's in memory.
501 1.21 chs */
502 1.21 chs
503 1.21 chs if (flags & PGO_LOCKED) {
504 1.21 chs uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
505 1.21 chs UFP_NOWAIT|UFP_NOALLOC|UFP_NORDONLY);
506 1.21 chs
507 1.21 chs return ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0;
508 1.21 chs }
509 1.21 chs
510 1.21 chs /* vnode is VOP_LOCKed, uobj is locked */
511 1.21 chs
512 1.21 chs if (write && (vp->v_flag & VONWORKLST) == 0) {
513 1.21 chs vn_syncer_add_to_worklist(vp, filedelay);
514 1.21 chs }
515 1.21 chs
516 1.21 chs /*
517 1.21 chs * find the requested pages and make some simple checks.
518 1.21 chs * leave space in the page array for a whole block.
519 1.21 chs */
520 1.21 chs
521 1.21 chs fs_bshift = vp->v_mount->mnt_fs_bshift;
522 1.21 chs fs_bsize = 1 << fs_bshift;
523 1.21 chs dev_bshift = vp->v_mount->mnt_dev_bshift;
524 1.21 chs dev_bsize = 1 << dev_bshift;
525 1.21 chs KASSERT((eof & (dev_bsize - 1)) == 0);
526 1.21 chs
527 1.21 chs orignpages = min(*ap->a_count,
528 1.21 chs round_page(eof - origoffset) >> PAGE_SHIFT);
529 1.21 chs if (flags & PGO_PASTEOF) {
530 1.21 chs orignpages = *ap->a_count;
531 1.21 chs }
532 1.21 chs npages = orignpages;
533 1.21 chs startoffset = origoffset & ~(fs_bsize - 1);
534 1.21 chs endoffset = round_page((origoffset + (npages << PAGE_SHIFT)
535 1.21 chs + fs_bsize - 1) & ~(fs_bsize - 1));
536 1.21 chs endoffset = min(endoffset, round_page(eof));
537 1.21 chs ridx = (origoffset - startoffset) >> PAGE_SHIFT;
538 1.21 chs
539 1.21 chs memset(pgs, 0, sizeof(pgs));
540 1.21 chs uvn_findpages(uobj, origoffset, &npages, &pgs[ridx], UFP_ALL);
541 1.21 chs
542 1.21 chs /*
543 1.21 chs * if PGO_OVERWRITE is set, don't bother reading the pages.
544 1.21 chs * PGO_OVERWRITE also means that the caller guarantees
545 1.21 chs * that the pages already have backing store allocated.
546 1.21 chs */
547 1.21 chs
548 1.21 chs if (flags & PGO_OVERWRITE) {
549 1.21 chs UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
550 1.21 chs
551 1.21 chs for (i = 0; i < npages; i++) {
552 1.21 chs struct vm_page *pg = pgs[ridx + i];
553 1.21 chs
554 1.21 chs if (pg->flags & PG_FAKE) {
555 1.21 chs uvm_pagezero(pg);
556 1.21 chs pg->flags &= ~(PG_FAKE);
557 1.21 chs }
558 1.21 chs pg->flags &= ~(PG_RDONLY);
559 1.21 chs }
560 1.21 chs goto out;
561 1.21 chs }
562 1.21 chs
563 1.21 chs /*
564 1.21 chs * if the pages are already resident, just return them.
565 1.21 chs */
566 1.21 chs
567 1.21 chs for (i = 0; i < npages; i++) {
568 1.21 chs struct vm_page *pg = pgs[ridx + i];
569 1.21 chs
570 1.21 chs if ((pg->flags & PG_FAKE) ||
571 1.21 chs (write && (pg->flags & PG_RDONLY))) {
572 1.21 chs break;
573 1.21 chs }
574 1.21 chs }
575 1.21 chs if (i == npages) {
576 1.21 chs UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
577 1.21 chs raoffset = origoffset + (orignpages << PAGE_SHIFT);
578 1.21 chs goto raout;
579 1.21 chs }
580 1.21 chs
581 1.21 chs /*
582 1.21 chs * the page wasn't resident and we're not overwriting,
583 1.21 chs * so we're going to have to do some i/o.
584 1.21 chs * find any additional pages needed to cover the expanded range.
585 1.21 chs */
586 1.21 chs
587 1.21 chs if (startoffset != origoffset) {
588 1.21 chs
589 1.21 chs /*
590 1.21 chs * XXXUBC we need to avoid deadlocks caused by locking
591 1.21 chs * additional pages at lower offsets than pages we
592 1.21 chs * already have locked. for now, unlock them all and
593 1.21 chs * start over.
594 1.21 chs */
595 1.21 chs
596 1.21 chs for (i = 0; i < npages; i++) {
597 1.21 chs struct vm_page *pg = pgs[ridx + i];
598 1.21 chs
599 1.21 chs if (pg->flags & PG_FAKE) {
600 1.21 chs pg->flags |= PG_RELEASED;
601 1.21 chs }
602 1.21 chs }
603 1.21 chs uvm_page_unbusy(&pgs[ridx], npages);
604 1.21 chs memset(pgs, 0, sizeof(pgs));
605 1.21 chs
606 1.21 chs UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
607 1.21 chs startoffset, endoffset, 0,0);
608 1.21 chs npages = (endoffset - startoffset) >> PAGE_SHIFT;
609 1.21 chs npgs = npages;
610 1.21 chs uvn_findpages(uobj, startoffset, &npgs, pgs, UFP_ALL);
611 1.21 chs }
612 1.21 chs simple_unlock(&uobj->vmobjlock);
613 1.21 chs
614 1.21 chs /*
615 1.21 chs * read the desired page(s).
616 1.21 chs */
617 1.21 chs
618 1.21 chs totalbytes = npages << PAGE_SHIFT;
619 1.21 chs bytes = min(totalbytes, eof - startoffset);
620 1.21 chs tailbytes = totalbytes - bytes;
621 1.21 chs skipbytes = 0;
622 1.21 chs
623 1.21 chs kva = uvm_pagermapin(pgs, npages, UVMPAGER_MAPIN_WAITOK |
624 1.21 chs UVMPAGER_MAPIN_READ);
625 1.21 chs
626 1.21 chs s = splbio();
627 1.21 chs mbp = pool_get(&bufpool, PR_WAITOK);
628 1.21 chs splx(s);
629 1.21 chs mbp->b_bufsize = totalbytes;
630 1.21 chs mbp->b_data = (void *)kva;
631 1.21 chs mbp->b_resid = mbp->b_bcount = bytes;
632 1.21 chs mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL : 0);
633 1.21 chs mbp->b_iodone = uvm_aio_biodone;
634 1.21 chs mbp->b_vp = vp;
635 1.21 chs LIST_INIT(&mbp->b_dep);
636 1.21 chs
637 1.21 chs /*
638 1.21 chs * if EOF is in the middle of the last page, zero the part past EOF.
639 1.21 chs */
640 1.21 chs
641 1.21 chs if (tailbytes > 0) {
642 1.21 chs memset((void *)(kva + bytes), 0, tailbytes);
643 1.21 chs }
644 1.21 chs
645 1.21 chs /*
646 1.21 chs * now loop over the pages, reading as needed.
647 1.21 chs */
648 1.21 chs
649 1.21 chs if (write) {
650 1.21 chs lockmgr(&vp->v_glock, LK_EXCLUSIVE, NULL);
651 1.21 chs } else {
652 1.21 chs lockmgr(&vp->v_glock, LK_SHARED, NULL);
653 1.21 chs }
654 1.21 chs
655 1.21 chs bp = NULL;
656 1.21 chs for (offset = startoffset;
657 1.21 chs bytes > 0;
658 1.21 chs offset += iobytes, bytes -= iobytes) {
659 1.21 chs
660 1.21 chs /*
661 1.21 chs * skip pages which don't need to be read.
662 1.21 chs */
663 1.21 chs
664 1.21 chs pidx = (offset - startoffset) >> PAGE_SHIFT;
665 1.21 chs while ((pgs[pidx]->flags & PG_FAKE) == 0) {
666 1.21 chs size_t b;
667 1.21 chs
668 1.21 chs #ifdef DEBUG
669 1.21 chs if (offset & (PAGE_SIZE - 1)) {
670 1.21 chs panic("genfs_getpages: skipping from middle "
671 1.21 chs "of page");
672 1.21 chs }
673 1.21 chs #endif
674 1.21 chs
675 1.21 chs b = min(PAGE_SIZE, bytes);
676 1.21 chs offset += b;
677 1.21 chs bytes -= b;
678 1.21 chs skipbytes += b;
679 1.21 chs pidx++;
680 1.21 chs UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
681 1.21 chs offset, 0,0,0);
682 1.21 chs if (bytes == 0) {
683 1.21 chs goto loopdone;
684 1.21 chs }
685 1.21 chs }
686 1.21 chs
687 1.21 chs /*
688 1.21 chs * bmap the file to find out the blkno to read from and
689 1.21 chs * how much we can read in one i/o. if bmap returns an error,
690 1.21 chs * skip the rest of the top-level i/o.
691 1.21 chs */
692 1.21 chs
693 1.21 chs lbn = offset >> fs_bshift;
694 1.21 chs error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
695 1.21 chs if (error) {
696 1.21 chs UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
697 1.21 chs lbn, error,0,0);
698 1.21 chs skipbytes += bytes;
699 1.21 chs goto loopdone;
700 1.21 chs }
701 1.21 chs
702 1.21 chs /*
703 1.21 chs * see how many pages can be read with this i/o.
704 1.21 chs * reduce the i/o size if necessary to avoid
705 1.21 chs * overwriting pages with valid data.
706 1.21 chs */
707 1.21 chs
708 1.21 chs iobytes = min(((lbn + 1 + run) << fs_bshift) - offset, bytes);
709 1.21 chs if (offset + iobytes > round_page(offset)) {
710 1.21 chs pcount = 1;
711 1.21 chs while (pidx + pcount < npages &&
712 1.21 chs pgs[pidx + pcount]->flags & PG_FAKE) {
713 1.21 chs pcount++;
714 1.21 chs }
715 1.21 chs iobytes = min(iobytes, (pcount << PAGE_SHIFT) -
716 1.21 chs (offset - trunc_page(offset)));
717 1.21 chs }
718 1.21 chs
719 1.21 chs /*
720 1.21 chs * if this block isn't allocated, zero it instead of reading it.
721 1.21 chs * if this is a read access, mark the pages we zeroed PG_RDONLY.
722 1.21 chs */
723 1.21 chs
724 1.21 chs if (blkno < 0) {
725 1.21 chs UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
726 1.21 chs
727 1.21 chs sawhole = TRUE;
728 1.21 chs memset((char *)kva + (offset - startoffset), 0,
729 1.21 chs iobytes);
730 1.21 chs skipbytes += iobytes;
731 1.21 chs
732 1.21 chs if (!write) {
733 1.21 chs int holepages =
734 1.21 chs (round_page(offset + iobytes) -
735 1.21 chs trunc_page(offset)) >> PAGE_SHIFT;
736 1.21 chs for (i = 0; i < holepages; i++) {
737 1.21 chs pgs[pidx + i]->flags |= PG_RDONLY;
738 1.21 chs }
739 1.21 chs }
740 1.21 chs continue;
741 1.21 chs }
742 1.21 chs
743 1.21 chs /*
744 1.21 chs * allocate a sub-buf for this piece of the i/o
745 1.21 chs * (or just use mbp if there's only 1 piece),
746 1.21 chs * and start it going.
747 1.21 chs */
748 1.21 chs
749 1.21 chs if (offset == startoffset && iobytes == bytes) {
750 1.21 chs bp = mbp;
751 1.21 chs } else {
752 1.21 chs s = splbio();
753 1.21 chs bp = pool_get(&bufpool, PR_WAITOK);
754 1.21 chs splx(s);
755 1.21 chs bp->b_data = (char *)kva + offset - startoffset;
756 1.21 chs bp->b_resid = bp->b_bcount = iobytes;
757 1.21 chs bp->b_flags = B_BUSY|B_READ|B_CALL;
758 1.21 chs bp->b_iodone = uvm_aio_biodone1;
759 1.21 chs bp->b_vp = vp;
760 1.21 chs LIST_INIT(&bp->b_dep);
761 1.21 chs }
762 1.21 chs bp->b_lblkno = 0;
763 1.21 chs bp->b_private = mbp;
764 1.21 chs
765 1.21 chs /* adjust physical blkno for partial blocks */
766 1.21 chs bp->b_blkno = blkno + ((offset - (lbn << fs_bshift)) >>
767 1.21 chs dev_bshift);
768 1.21 chs
769 1.21 chs UVMHIST_LOG(ubchist, "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
770 1.21 chs bp, offset, iobytes, bp->b_blkno);
771 1.21 chs
772 1.21 chs VOP_STRATEGY(bp);
773 1.21 chs }
774 1.21 chs
775 1.21 chs loopdone:
776 1.21 chs if (skipbytes) {
777 1.21 chs s = splbio();
778 1.21 chs if (error) {
779 1.21 chs mbp->b_flags |= B_ERROR;
780 1.21 chs mbp->b_error = error;
781 1.21 chs }
782 1.21 chs mbp->b_resid -= skipbytes;
783 1.21 chs if (mbp->b_resid == 0) {
784 1.21 chs biodone(mbp);
785 1.21 chs }
786 1.21 chs splx(s);
787 1.21 chs }
788 1.21 chs
789 1.21 chs if (async) {
790 1.21 chs UVMHIST_LOG(ubchist, "returning PEND",0,0,0,0);
791 1.21 chs lockmgr(&vp->v_glock, LK_RELEASE, NULL);
792 1.21 chs return EINPROGRESS;
793 1.21 chs }
794 1.21 chs if (bp != NULL) {
795 1.21 chs error = biowait(mbp);
796 1.21 chs }
797 1.21 chs s = splbio();
798 1.21 chs pool_put(&bufpool, mbp);
799 1.21 chs splx(s);
800 1.21 chs uvm_pagermapout(kva, npages);
801 1.21 chs raoffset = offset;
802 1.21 chs
803 1.21 chs /*
804 1.21 chs * if this we encountered a hole then we have to do a little more work.
805 1.21 chs * for read faults, we marked the page PG_RDONLY so that future
806 1.21 chs * write accesses to the page will fault again.
807 1.21 chs * for write faults, we must make sure that the backing store for
808 1.21 chs * the page is completely allocated while the pages are locked.
809 1.21 chs */
810 1.21 chs
811 1.21 chs if (error == 0 && sawhole && write) {
812 1.21 chs error = VOP_BALLOCN(vp, startoffset, npages << PAGE_SHIFT,
813 1.21 chs cred, 0);
814 1.21 chs if (error) {
815 1.21 chs UVMHIST_LOG(ubchist, "balloc lbn 0x%x -> %d",
816 1.21 chs lbn, error,0,0);
817 1.21 chs lockmgr(&vp->v_glock, LK_RELEASE, NULL);
818 1.21 chs simple_lock(&uobj->vmobjlock);
819 1.21 chs goto out;
820 1.21 chs }
821 1.21 chs }
822 1.21 chs lockmgr(&vp->v_glock, LK_RELEASE, NULL);
823 1.21 chs simple_lock(&uobj->vmobjlock);
824 1.21 chs
825 1.21 chs /*
826 1.21 chs * see if we want to start any readahead.
827 1.21 chs * XXXUBC for now, just read the next 128k on 64k boundaries.
828 1.21 chs * this is pretty nonsensical, but it is 50% faster than reading
829 1.21 chs * just the next 64k.
830 1.21 chs */
831 1.21 chs
832 1.21 chs raout:
833 1.21 chs if (!async && !write && ((int)raoffset & 0xffff) == 0 &&
834 1.21 chs PAGE_SHIFT <= 16) {
835 1.21 chs int racount;
836 1.21 chs
837 1.21 chs racount = 1 << (16 - PAGE_SHIFT);
838 1.21 chs (void) VOP_GETPAGES(vp, raoffset, NULL, &racount, 0,
839 1.21 chs VM_PROT_READ, 0, 0);
840 1.21 chs simple_lock(&uobj->vmobjlock);
841 1.21 chs
842 1.21 chs racount = 1 << (16 - PAGE_SHIFT);
843 1.21 chs (void) VOP_GETPAGES(vp, raoffset + 0x10000, NULL, &racount, 0,
844 1.21 chs VM_PROT_READ, 0, 0);
845 1.21 chs simple_lock(&uobj->vmobjlock);
846 1.21 chs }
847 1.21 chs
848 1.21 chs /*
849 1.21 chs * we're almost done! release the pages...
850 1.21 chs * for errors, we free the pages.
851 1.21 chs * otherwise we activate them and mark them as valid and clean.
852 1.21 chs * also, unbusy pages that were not actually requested.
853 1.21 chs */
854 1.21 chs
855 1.21 chs out:
856 1.21 chs if (error) {
857 1.21 chs uvm_lock_pageq();
858 1.21 chs for (i = 0; i < npages; i++) {
859 1.21 chs if (pgs[i] == NULL) {
860 1.21 chs continue;
861 1.21 chs }
862 1.21 chs UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
863 1.21 chs pgs[i], pgs[i]->flags, 0,0);
864 1.21 chs if ((pgs[i]->flags & PG_FAKE) == 0) {
865 1.21 chs continue;
866 1.21 chs }
867 1.21 chs if (pgs[i]->flags & PG_WANTED) {
868 1.21 chs wakeup(pgs[i]);
869 1.21 chs }
870 1.21 chs uvm_pagefree(pgs[i]);
871 1.21 chs }
872 1.21 chs uvm_unlock_pageq();
873 1.21 chs simple_unlock(&uobj->vmobjlock);
874 1.21 chs UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
875 1.21 chs return error;
876 1.21 chs }
877 1.21 chs
878 1.21 chs UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
879 1.21 chs for (i = 0; i < npages; i++) {
880 1.21 chs if (pgs[i] == NULL) {
881 1.21 chs continue;
882 1.21 chs }
883 1.21 chs UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
884 1.21 chs pgs[i], pgs[i]->flags, 0,0);
885 1.21 chs if (pgs[i]->flags & PG_FAKE) {
886 1.21 chs UVMHIST_LOG(ubchist, "unfaking pg %p offset 0x%x",
887 1.21 chs pgs[i], pgs[i]->offset,0,0);
888 1.21 chs pgs[i]->flags &= ~(PG_FAKE);
889 1.21 chs pmap_clear_modify(pgs[i]);
890 1.21 chs pmap_clear_reference(pgs[i]);
891 1.21 chs }
892 1.21 chs if (write) {
893 1.21 chs pgs[i]->flags &= ~(PG_RDONLY);
894 1.21 chs }
895 1.21 chs if (i < ridx || i >= ridx + orignpages || async) {
896 1.21 chs UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
897 1.21 chs pgs[i], pgs[i]->offset,0,0);
898 1.21 chs if (pgs[i]->flags & PG_WANTED) {
899 1.21 chs wakeup(pgs[i]);
900 1.21 chs }
901 1.21 chs if (pgs[i]->wire_count == 0) {
902 1.21 chs uvm_pageactivate(pgs[i]);
903 1.21 chs }
904 1.21 chs pgs[i]->flags &= ~(PG_WANTED|PG_BUSY);
905 1.21 chs UVM_PAGE_OWN(pgs[i], NULL);
906 1.21 chs }
907 1.21 chs }
908 1.21 chs simple_unlock(&uobj->vmobjlock);
909 1.21 chs if (ap->a_m != NULL) {
910 1.21 chs memcpy(ap->a_m, &pgs[ridx],
911 1.21 chs orignpages * sizeof(struct vm_page *));
912 1.21 chs }
913 1.21 chs return 0;
914 1.21 chs }
915 1.21 chs
916 1.21 chs /*
917 1.21 chs * generic VM putpages routine.
918 1.21 chs * Write the given range of pages to backing store.
919 1.21 chs */
920 1.21 chs
921 1.21 chs int
922 1.21 chs genfs_putpages(v)
923 1.21 chs void *v;
924 1.21 chs {
925 1.21 chs struct vop_putpages_args /* {
926 1.21 chs struct vnode *a_vp;
927 1.21 chs struct vm_page **a_m;
928 1.21 chs int a_count;
929 1.21 chs int a_flags;
930 1.21 chs int *a_rtvals;
931 1.21 chs } */ *ap = v;
932 1.21 chs
933 1.21 chs int s, error, error2, npages, run;
934 1.21 chs int fs_bshift, dev_bshift, dev_bsize;
935 1.21 chs vaddr_t kva;
936 1.21 chs off_t eof, offset, startoffset;
937 1.21 chs size_t bytes, iobytes, skipbytes;
938 1.21 chs daddr_t lbn, blkno;
939 1.21 chs struct vm_page *pg;
940 1.21 chs struct buf *mbp, *bp;
941 1.21 chs struct vnode *vp = ap->a_vp;
942 1.21 chs boolean_t async = (ap->a_flags & PGO_SYNCIO) == 0;
943 1.21 chs UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
944 1.21 chs
945 1.21 chs simple_unlock(&vp->v_uvm.u_obj.vmobjlock);
946 1.21 chs
947 1.21 chs error = VOP_SIZE(vp, vp->v_uvm.u_size, &eof);
948 1.21 chs if (error) {
949 1.21 chs return error;
950 1.21 chs }
951 1.21 chs
952 1.21 chs error = error2 = 0;
953 1.21 chs npages = ap->a_count;
954 1.21 chs fs_bshift = vp->v_mount->mnt_fs_bshift;
955 1.21 chs dev_bshift = vp->v_mount->mnt_dev_bshift;
956 1.21 chs dev_bsize = 1 << dev_bshift;
957 1.21 chs KASSERT((eof & (dev_bsize - 1)) == 0);
958 1.21 chs
959 1.21 chs pg = ap->a_m[0];
960 1.21 chs startoffset = pg->offset;
961 1.21 chs bytes = min(npages << PAGE_SHIFT, eof - startoffset);
962 1.21 chs skipbytes = 0;
963 1.21 chs KASSERT(bytes != 0);
964 1.21 chs
965 1.21 chs kva = uvm_pagermapin(ap->a_m, npages, UVMPAGER_MAPIN_WAITOK);
966 1.21 chs
967 1.21 chs s = splbio();
968 1.21 chs vp->v_numoutput += 2;
969 1.21 chs mbp = pool_get(&bufpool, PR_WAITOK);
970 1.21 chs UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
971 1.21 chs vp, mbp, vp->v_numoutput, bytes);
972 1.21 chs splx(s);
973 1.21 chs mbp->b_bufsize = npages << PAGE_SHIFT;
974 1.21 chs mbp->b_data = (void *)kva;
975 1.21 chs mbp->b_resid = mbp->b_bcount = bytes;
976 1.21 chs mbp->b_flags = B_BUSY|B_WRITE|B_AGE |
977 1.21 chs (async ? B_CALL : 0) |
978 1.21 chs (curproc == uvm.pagedaemon_proc ? B_PDAEMON : 0);
979 1.21 chs mbp->b_iodone = uvm_aio_biodone;
980 1.21 chs mbp->b_vp = vp;
981 1.21 chs LIST_INIT(&mbp->b_dep);
982 1.21 chs
983 1.21 chs bp = NULL;
984 1.21 chs for (offset = startoffset;
985 1.21 chs bytes > 0;
986 1.21 chs offset += iobytes, bytes -= iobytes) {
987 1.21 chs lbn = offset >> fs_bshift;
988 1.21 chs error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
989 1.21 chs if (error) {
990 1.21 chs UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
991 1.21 chs skipbytes += bytes;
992 1.21 chs bytes = 0;
993 1.21 chs break;
994 1.21 chs }
995 1.21 chs
996 1.21 chs iobytes = min(((lbn + 1 + run) << fs_bshift) - offset, bytes);
997 1.21 chs if (blkno == (daddr_t)-1) {
998 1.21 chs skipbytes += iobytes;
999 1.21 chs continue;
1000 1.21 chs }
1001 1.21 chs
1002 1.21 chs /* if it's really one i/o, don't make a second buf */
1003 1.21 chs if (offset == startoffset && iobytes == bytes) {
1004 1.21 chs bp = mbp;
1005 1.21 chs } else {
1006 1.21 chs s = splbio();
1007 1.21 chs vp->v_numoutput++;
1008 1.21 chs bp = pool_get(&bufpool, PR_WAITOK);
1009 1.21 chs UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1010 1.21 chs vp, bp, vp->v_numoutput, 0);
1011 1.21 chs splx(s);
1012 1.21 chs bp->b_data = (char *)kva +
1013 1.21 chs (vaddr_t)(offset - pg->offset);
1014 1.21 chs bp->b_resid = bp->b_bcount = iobytes;
1015 1.21 chs bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1016 1.21 chs bp->b_iodone = uvm_aio_biodone1;
1017 1.21 chs bp->b_vp = vp;
1018 1.21 chs LIST_INIT(&bp->b_dep);
1019 1.21 chs }
1020 1.21 chs bp->b_lblkno = 0;
1021 1.21 chs bp->b_private = mbp;
1022 1.21 chs
1023 1.21 chs /* adjust physical blkno for partial blocks */
1024 1.21 chs bp->b_blkno = blkno + ((offset - (lbn << fs_bshift)) >>
1025 1.21 chs dev_bshift);
1026 1.21 chs UVMHIST_LOG(ubchist, "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1027 1.21 chs vp, offset, bp->b_bcount, bp->b_blkno);
1028 1.21 chs VOP_STRATEGY(bp);
1029 1.21 chs }
1030 1.21 chs if (skipbytes) {
1031 1.21 chs UVMHIST_LOG(ubchist, "skipbytes %d", bytes, 0,0,0);
1032 1.21 chs s = splbio();
1033 1.21 chs mbp->b_resid -= skipbytes;
1034 1.21 chs if (mbp->b_resid == 0) {
1035 1.21 chs biodone(mbp);
1036 1.21 chs }
1037 1.21 chs splx(s);
1038 1.21 chs }
1039 1.21 chs if (async) {
1040 1.21 chs UVMHIST_LOG(ubchist, "returning PEND", 0,0,0,0);
1041 1.21 chs return EINPROGRESS;
1042 1.21 chs }
1043 1.21 chs if (bp != NULL) {
1044 1.21 chs UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1045 1.21 chs error2 = biowait(mbp);
1046 1.21 chs }
1047 1.21 chs {
1048 1.21 chs /* XXXUBC */
1049 1.21 chs void softdep_pageiodone(struct buf *);
1050 1.21 chs softdep_pageiodone(mbp);
1051 1.21 chs }
1052 1.21 chs s = splbio();
1053 1.21 chs vwakeup(mbp);
1054 1.21 chs pool_put(&bufpool, mbp);
1055 1.21 chs splx(s);
1056 1.21 chs uvm_pagermapout(kva, npages);
1057 1.21 chs UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1058 1.21 chs return error ? error : error2;
1059 1.21 chs }
1060 1.21 chs
1061 1.21 chs int
1062 1.21 chs genfs_size(v)
1063 1.21 chs void *v;
1064 1.21 chs {
1065 1.21 chs struct vop_size_args /* {
1066 1.21 chs struct vnode *a_vp;
1067 1.21 chs off_t a_size;
1068 1.21 chs off_t *a_eobp;
1069 1.21 chs } */ *ap = v;
1070 1.21 chs int bsize;
1071 1.21 chs
1072 1.21 chs bsize = 1 << ap->a_vp->v_mount->mnt_fs_bshift;
1073 1.21 chs *ap->a_eobp = (ap->a_size + bsize) & ~(bsize - 1);
1074 1.21 chs return 0;
1075 1.1 mycroft }
1076