genfs_vnops.c revision 1.157 1 1.157 ad /* $NetBSD: genfs_vnops.c,v 1.157 2007/10/10 20:42:28 ad 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.81 agc * 3. Neither the name of the University nor the names of its contributors
16 1.6 fvdl * may be used to endorse or promote products derived from this software
17 1.6 fvdl * without specific prior written permission.
18 1.6 fvdl *
19 1.6 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.6 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.6 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.6 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.6 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.6 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.6 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.6 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.6 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.6 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.6 fvdl * SUCH DAMAGE.
30 1.6 fvdl *
31 1.6 fvdl */
32 1.40 lukem
33 1.40 lukem #include <sys/cdefs.h>
34 1.157 ad __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.157 2007/10/10 20:42:28 ad Exp $");
35 1.8 thorpej
36 1.1 mycroft #include <sys/param.h>
37 1.1 mycroft #include <sys/systm.h>
38 1.6 fvdl #include <sys/proc.h>
39 1.1 mycroft #include <sys/kernel.h>
40 1.1 mycroft #include <sys/mount.h>
41 1.1 mycroft #include <sys/namei.h>
42 1.1 mycroft #include <sys/vnode.h>
43 1.13 wrstuden #include <sys/fcntl.h>
44 1.135 yamt #include <sys/kmem.h>
45 1.3 mycroft #include <sys/poll.h>
46 1.37 chs #include <sys/mman.h>
47 1.66 jdolecek #include <sys/file.h>
48 1.125 elad #include <sys/kauth.h>
49 1.143 hannken #include <sys/fstrans.h>
50 1.1 mycroft
51 1.1 mycroft #include <miscfs/genfs/genfs.h>
52 1.37 chs #include <miscfs/genfs/genfs_node.h>
53 1.6 fvdl #include <miscfs/specfs/specdev.h>
54 1.1 mycroft
55 1.21 chs #include <uvm/uvm.h>
56 1.21 chs #include <uvm/uvm_pager.h>
57 1.21 chs
58 1.130 chs static int genfs_do_directio(struct vmspace *, vaddr_t, size_t, struct vnode *,
59 1.130 chs off_t, enum uio_rw);
60 1.130 chs static void genfs_dio_iodone(struct buf *);
61 1.130 chs
62 1.130 chs static int genfs_do_io(struct vnode *, off_t, vaddr_t, size_t, int, enum uio_rw,
63 1.130 chs void (*)(struct buf *));
64 1.118 perry static inline void genfs_rel_pages(struct vm_page **, int);
65 1.70 christos static void filt_genfsdetach(struct knote *);
66 1.70 christos static int filt_genfsread(struct knote *, long);
67 1.70 christos static int filt_genfsvnode(struct knote *, long);
68 1.70 christos
69 1.110 yamt #define MAX_READ_PAGES 16 /* XXXUBC 16 */
70 1.41 christos
71 1.130 chs int genfs_maxdio = MAXPHYS;
72 1.130 chs
73 1.1 mycroft int
74 1.53 enami genfs_poll(void *v)
75 1.1 mycroft {
76 1.3 mycroft struct vop_poll_args /* {
77 1.1 mycroft struct vnode *a_vp;
78 1.3 mycroft int a_events;
79 1.116 christos struct lwp *a_l;
80 1.1 mycroft } */ *ap = v;
81 1.1 mycroft
82 1.3 mycroft return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
83 1.1 mycroft }
84 1.1 mycroft
85 1.1 mycroft int
86 1.53 enami genfs_seek(void *v)
87 1.4 kleink {
88 1.4 kleink struct vop_seek_args /* {
89 1.4 kleink struct vnode *a_vp;
90 1.4 kleink off_t a_oldoff;
91 1.4 kleink off_t a_newoff;
92 1.125 elad kauth_cred_t cred;
93 1.4 kleink } */ *ap = v;
94 1.4 kleink
95 1.4 kleink if (ap->a_newoff < 0)
96 1.4 kleink return (EINVAL);
97 1.4 kleink
98 1.4 kleink return (0);
99 1.4 kleink }
100 1.4 kleink
101 1.4 kleink int
102 1.53 enami genfs_abortop(void *v)
103 1.1 mycroft {
104 1.1 mycroft struct vop_abortop_args /* {
105 1.1 mycroft struct vnode *a_dvp;
106 1.1 mycroft struct componentname *a_cnp;
107 1.1 mycroft } */ *ap = v;
108 1.53 enami
109 1.1 mycroft if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
110 1.19 thorpej PNBUF_PUT(ap->a_cnp->cn_pnbuf);
111 1.1 mycroft return (0);
112 1.13 wrstuden }
113 1.13 wrstuden
114 1.13 wrstuden int
115 1.53 enami genfs_fcntl(void *v)
116 1.13 wrstuden {
117 1.13 wrstuden struct vop_fcntl_args /* {
118 1.13 wrstuden struct vnode *a_vp;
119 1.13 wrstuden u_int a_command;
120 1.150 christos void *a_data;
121 1.13 wrstuden int a_fflag;
122 1.125 elad kauth_cred_t a_cred;
123 1.116 christos struct lwp *a_l;
124 1.13 wrstuden } */ *ap = v;
125 1.13 wrstuden
126 1.13 wrstuden if (ap->a_command == F_SETFL)
127 1.13 wrstuden return (0);
128 1.13 wrstuden else
129 1.13 wrstuden return (EOPNOTSUPP);
130 1.1 mycroft }
131 1.1 mycroft
132 1.1 mycroft /*ARGSUSED*/
133 1.1 mycroft int
134 1.138 christos genfs_badop(void *v)
135 1.1 mycroft {
136 1.1 mycroft
137 1.1 mycroft panic("genfs: bad op");
138 1.1 mycroft }
139 1.1 mycroft
140 1.1 mycroft /*ARGSUSED*/
141 1.1 mycroft int
142 1.138 christos genfs_nullop(void *v)
143 1.1 mycroft {
144 1.1 mycroft
145 1.1 mycroft return (0);
146 1.10 kleink }
147 1.10 kleink
148 1.10 kleink /*ARGSUSED*/
149 1.10 kleink int
150 1.138 christos genfs_einval(void *v)
151 1.10 kleink {
152 1.10 kleink
153 1.10 kleink return (EINVAL);
154 1.1 mycroft }
155 1.1 mycroft
156 1.12 wrstuden /*
157 1.74 jdolecek * Called when an fs doesn't support a particular vop.
158 1.74 jdolecek * This takes care to vrele, vput, or vunlock passed in vnodes.
159 1.12 wrstuden */
160 1.12 wrstuden int
161 1.75 jdolecek genfs_eopnotsupp(void *v)
162 1.12 wrstuden {
163 1.12 wrstuden struct vop_generic_args /*
164 1.12 wrstuden struct vnodeop_desc *a_desc;
165 1.53 enami / * other random data follows, presumably * /
166 1.12 wrstuden } */ *ap = v;
167 1.12 wrstuden struct vnodeop_desc *desc = ap->a_desc;
168 1.74 jdolecek struct vnode *vp, *vp_last = NULL;
169 1.12 wrstuden int flags, i, j, offset;
170 1.12 wrstuden
171 1.12 wrstuden flags = desc->vdesc_flags;
172 1.12 wrstuden for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
173 1.12 wrstuden if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
174 1.12 wrstuden break; /* stop at end of list */
175 1.12 wrstuden if ((j = flags & VDESC_VP0_WILLPUT)) {
176 1.53 enami vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
177 1.74 jdolecek
178 1.74 jdolecek /* Skip if NULL */
179 1.74 jdolecek if (!vp)
180 1.74 jdolecek continue;
181 1.74 jdolecek
182 1.12 wrstuden switch (j) {
183 1.12 wrstuden case VDESC_VP0_WILLPUT:
184 1.74 jdolecek /* Check for dvp == vp cases */
185 1.74 jdolecek if (vp == vp_last)
186 1.74 jdolecek vrele(vp);
187 1.74 jdolecek else {
188 1.74 jdolecek vput(vp);
189 1.74 jdolecek vp_last = vp;
190 1.74 jdolecek }
191 1.12 wrstuden break;
192 1.12 wrstuden case VDESC_VP0_WILLUNLOCK:
193 1.12 wrstuden VOP_UNLOCK(vp, 0);
194 1.12 wrstuden break;
195 1.12 wrstuden case VDESC_VP0_WILLRELE:
196 1.12 wrstuden vrele(vp);
197 1.12 wrstuden break;
198 1.12 wrstuden }
199 1.12 wrstuden }
200 1.12 wrstuden }
201 1.12 wrstuden
202 1.12 wrstuden return (EOPNOTSUPP);
203 1.12 wrstuden }
204 1.12 wrstuden
205 1.1 mycroft /*ARGSUSED*/
206 1.1 mycroft int
207 1.138 christos genfs_ebadf(void *v)
208 1.1 mycroft {
209 1.1 mycroft
210 1.1 mycroft return (EBADF);
211 1.9 matthias }
212 1.9 matthias
213 1.9 matthias /* ARGSUSED */
214 1.9 matthias int
215 1.138 christos genfs_enoioctl(void *v)
216 1.9 matthias {
217 1.9 matthias
218 1.51 atatat return (EPASSTHROUGH);
219 1.6 fvdl }
220 1.6 fvdl
221 1.6 fvdl
222 1.6 fvdl /*
223 1.15 fvdl * Eliminate all activity associated with the requested vnode
224 1.6 fvdl * and with all vnodes aliased to the requested vnode.
225 1.6 fvdl */
226 1.6 fvdl int
227 1.53 enami genfs_revoke(void *v)
228 1.6 fvdl {
229 1.6 fvdl struct vop_revoke_args /* {
230 1.6 fvdl struct vnode *a_vp;
231 1.6 fvdl int a_flags;
232 1.6 fvdl } */ *ap = v;
233 1.6 fvdl struct vnode *vp, *vq;
234 1.116 christos struct lwp *l = curlwp; /* XXX */
235 1.6 fvdl
236 1.6 fvdl #ifdef DIAGNOSTIC
237 1.6 fvdl if ((ap->a_flags & REVOKEALL) == 0)
238 1.6 fvdl panic("genfs_revoke: not revokeall");
239 1.6 fvdl #endif
240 1.6 fvdl
241 1.6 fvdl vp = ap->a_vp;
242 1.6 fvdl simple_lock(&vp->v_interlock);
243 1.6 fvdl
244 1.157 ad if (vp->v_iflag & VI_ALIASED) {
245 1.6 fvdl /*
246 1.6 fvdl * If a vgone (or vclean) is already in progress,
247 1.6 fvdl * wait until it is done and return.
248 1.6 fvdl */
249 1.157 ad if (vp->v_iflag & VI_XLOCK) {
250 1.157 ad vp->v_iflag |= VI_XWANT;
251 1.83 pk ltsleep(vp, PINOD|PNORELOCK, "vop_revokeall", 0,
252 1.83 pk &vp->v_interlock);
253 1.6 fvdl return (0);
254 1.6 fvdl }
255 1.6 fvdl /*
256 1.6 fvdl * Ensure that vp will not be vgone'd while we
257 1.6 fvdl * are eliminating its aliases.
258 1.6 fvdl */
259 1.157 ad vp->v_iflag |= VI_XLOCK;
260 1.6 fvdl simple_unlock(&vp->v_interlock);
261 1.157 ad while (vp->v_iflag & VI_ALIASED) {
262 1.6 fvdl simple_lock(&spechash_slock);
263 1.6 fvdl for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
264 1.6 fvdl if (vq->v_rdev != vp->v_rdev ||
265 1.6 fvdl vq->v_type != vp->v_type || vp == vq)
266 1.6 fvdl continue;
267 1.6 fvdl simple_unlock(&spechash_slock);
268 1.6 fvdl vgone(vq);
269 1.6 fvdl break;
270 1.6 fvdl }
271 1.6 fvdl if (vq == NULLVP)
272 1.6 fvdl simple_unlock(&spechash_slock);
273 1.6 fvdl }
274 1.6 fvdl /*
275 1.6 fvdl * Remove the lock so that vgone below will
276 1.6 fvdl * really eliminate the vnode after which time
277 1.6 fvdl * vgone will awaken any sleepers.
278 1.6 fvdl */
279 1.6 fvdl simple_lock(&vp->v_interlock);
280 1.157 ad vp->v_iflag &= ~VI_XLOCK;
281 1.6 fvdl }
282 1.116 christos vgonel(vp, l);
283 1.6 fvdl return (0);
284 1.6 fvdl }
285 1.6 fvdl
286 1.6 fvdl /*
287 1.12 wrstuden * Lock the node.
288 1.6 fvdl */
289 1.6 fvdl int
290 1.53 enami genfs_lock(void *v)
291 1.6 fvdl {
292 1.6 fvdl struct vop_lock_args /* {
293 1.6 fvdl struct vnode *a_vp;
294 1.6 fvdl int a_flags;
295 1.6 fvdl } */ *ap = v;
296 1.6 fvdl struct vnode *vp = ap->a_vp;
297 1.6 fvdl
298 1.86 hannken return (lockmgr(vp->v_vnlock, ap->a_flags, &vp->v_interlock));
299 1.6 fvdl }
300 1.6 fvdl
301 1.6 fvdl /*
302 1.12 wrstuden * Unlock the node.
303 1.6 fvdl */
304 1.6 fvdl int
305 1.53 enami genfs_unlock(void *v)
306 1.6 fvdl {
307 1.6 fvdl struct vop_unlock_args /* {
308 1.6 fvdl struct vnode *a_vp;
309 1.6 fvdl int a_flags;
310 1.6 fvdl } */ *ap = v;
311 1.6 fvdl struct vnode *vp = ap->a_vp;
312 1.6 fvdl
313 1.86 hannken return (lockmgr(vp->v_vnlock, ap->a_flags | LK_RELEASE,
314 1.53 enami &vp->v_interlock));
315 1.6 fvdl }
316 1.6 fvdl
317 1.6 fvdl /*
318 1.12 wrstuden * Return whether or not the node is locked.
319 1.6 fvdl */
320 1.6 fvdl int
321 1.53 enami genfs_islocked(void *v)
322 1.6 fvdl {
323 1.6 fvdl struct vop_islocked_args /* {
324 1.6 fvdl struct vnode *a_vp;
325 1.6 fvdl } */ *ap = v;
326 1.6 fvdl struct vnode *vp = ap->a_vp;
327 1.6 fvdl
328 1.86 hannken return (lockstatus(vp->v_vnlock));
329 1.12 wrstuden }
330 1.12 wrstuden
331 1.12 wrstuden /*
332 1.12 wrstuden * Stubs to use when there is no locking to be done on the underlying object.
333 1.12 wrstuden */
334 1.12 wrstuden int
335 1.53 enami genfs_nolock(void *v)
336 1.12 wrstuden {
337 1.12 wrstuden struct vop_lock_args /* {
338 1.12 wrstuden struct vnode *a_vp;
339 1.12 wrstuden int a_flags;
340 1.116 christos struct lwp *a_l;
341 1.12 wrstuden } */ *ap = v;
342 1.12 wrstuden
343 1.12 wrstuden /*
344 1.12 wrstuden * Since we are not using the lock manager, we must clear
345 1.12 wrstuden * the interlock here.
346 1.12 wrstuden */
347 1.12 wrstuden if (ap->a_flags & LK_INTERLOCK)
348 1.12 wrstuden simple_unlock(&ap->a_vp->v_interlock);
349 1.12 wrstuden return (0);
350 1.12 wrstuden }
351 1.12 wrstuden
352 1.12 wrstuden int
353 1.138 christos genfs_nounlock(void *v)
354 1.12 wrstuden {
355 1.53 enami
356 1.12 wrstuden return (0);
357 1.12 wrstuden }
358 1.12 wrstuden
359 1.12 wrstuden int
360 1.138 christos genfs_noislocked(void *v)
361 1.12 wrstuden {
362 1.53 enami
363 1.12 wrstuden return (0);
364 1.8 thorpej }
365 1.8 thorpej
366 1.8 thorpej /*
367 1.142 yamt * Local lease check.
368 1.8 thorpej */
369 1.8 thorpej int
370 1.53 enami genfs_lease_check(void *v)
371 1.8 thorpej {
372 1.8 thorpej
373 1.8 thorpej return (0);
374 1.34 chs }
375 1.34 chs
376 1.34 chs int
377 1.138 christos genfs_mmap(void *v)
378 1.34 chs {
379 1.53 enami
380 1.53 enami return (0);
381 1.21 chs }
382 1.21 chs
383 1.118 perry static inline void
384 1.63 enami genfs_rel_pages(struct vm_page **pgs, int npages)
385 1.63 enami {
386 1.63 enami int i;
387 1.63 enami
388 1.63 enami for (i = 0; i < npages; i++) {
389 1.63 enami struct vm_page *pg = pgs[i];
390 1.63 enami
391 1.127 yamt if (pg == NULL || pg == PGO_DONTCARE)
392 1.63 enami continue;
393 1.63 enami if (pg->flags & PG_FAKE) {
394 1.63 enami pg->flags |= PG_RELEASED;
395 1.63 enami }
396 1.63 enami }
397 1.64 enami uvm_lock_pageq();
398 1.63 enami uvm_page_unbusy(pgs, npages);
399 1.64 enami uvm_unlock_pageq();
400 1.63 enami }
401 1.63 enami
402 1.21 chs /*
403 1.21 chs * generic VM getpages routine.
404 1.21 chs * Return PG_BUSY pages for the given range,
405 1.21 chs * reading from backing store if necessary.
406 1.21 chs */
407 1.21 chs
408 1.21 chs int
409 1.53 enami genfs_getpages(void *v)
410 1.21 chs {
411 1.21 chs struct vop_getpages_args /* {
412 1.21 chs struct vnode *a_vp;
413 1.21 chs voff_t a_offset;
414 1.33 chs struct vm_page **a_m;
415 1.21 chs int *a_count;
416 1.21 chs int a_centeridx;
417 1.21 chs vm_prot_t a_access_type;
418 1.21 chs int a_advice;
419 1.21 chs int a_flags;
420 1.21 chs } */ *ap = v;
421 1.21 chs
422 1.30 chs off_t newsize, diskeof, memeof;
423 1.124 yamt off_t offset, origoffset, startoffset, endoffset;
424 1.21 chs daddr_t lbn, blkno;
425 1.120 yamt int i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
426 1.37 chs int fs_bshift, fs_bsize, dev_bshift;
427 1.21 chs int flags = ap->a_flags;
428 1.154 yamt size_t bytes, iobytes, tailstart, tailbytes, totalbytes, skipbytes;
429 1.21 chs vaddr_t kva;
430 1.21 chs struct buf *bp, *mbp;
431 1.21 chs struct vnode *vp = ap->a_vp;
432 1.36 chs struct vnode *devvp;
433 1.37 chs struct genfs_node *gp = VTOG(vp);
434 1.37 chs struct uvm_object *uobj = &vp->v_uobj;
435 1.110 yamt struct vm_page *pg, **pgs, *pgs_onstack[MAX_READ_PAGES];
436 1.77 yamt int pgs_size;
437 1.128 ad kauth_cred_t cred = curlwp->l_cred; /* XXXUBC curlwp */
438 1.148 thorpej bool async = (flags & PGO_SYNCIO) == 0;
439 1.148 thorpej bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
440 1.149 thorpej bool sawhole = false;
441 1.149 thorpej bool has_trans = false;
442 1.148 thorpej bool overwrite = (flags & PGO_OVERWRITE) != 0;
443 1.148 thorpej bool blockalloc = write && (flags & PGO_NOBLOCKALLOC) == 0;
444 1.126 yamt voff_t origvsize;
445 1.21 chs UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
446 1.21 chs
447 1.30 chs UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
448 1.53 enami vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
449 1.30 chs
450 1.121 reinoud KASSERT(vp->v_type == VREG || vp->v_type == VDIR ||
451 1.121 reinoud vp->v_type == VLNK || vp->v_type == VBLK);
452 1.109 yamt
453 1.21 chs /* XXXUBC temp limit */
454 1.110 yamt if (*ap->a_count > MAX_READ_PAGES) {
455 1.37 chs panic("genfs_getpages: too many pages");
456 1.21 chs }
457 1.21 chs
458 1.143 hannken pgs = pgs_onstack;
459 1.143 hannken pgs_size = sizeof(pgs_onstack);
460 1.143 hannken
461 1.126 yamt startover:
462 1.26 chs error = 0;
463 1.126 yamt origvsize = vp->v_size;
464 1.26 chs origoffset = ap->a_offset;
465 1.26 chs orignpages = *ap->a_count;
466 1.152 yamt GOP_SIZE(vp, origvsize, &diskeof, 0);
467 1.26 chs if (flags & PGO_PASTEOF) {
468 1.154 yamt #if defined(DIAGNOSTIC)
469 1.154 yamt off_t writeeof;
470 1.154 yamt #endif /* defined(DIAGNOSTIC) */
471 1.154 yamt
472 1.152 yamt newsize = MAX(origvsize,
473 1.53 enami origoffset + (orignpages << PAGE_SHIFT));
474 1.123 yamt GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_MEM);
475 1.154 yamt #if defined(DIAGNOSTIC)
476 1.154 yamt GOP_SIZE(vp, vp->v_writesize, &writeeof, GOP_SIZE_MEM);
477 1.154 yamt if (newsize > round_page(writeeof)) {
478 1.154 yamt panic("%s: past eof", __func__);
479 1.154 yamt }
480 1.154 yamt #endif /* defined(DIAGNOSTIC) */
481 1.26 chs } else {
482 1.152 yamt GOP_SIZE(vp, origvsize, &memeof, GOP_SIZE_MEM);
483 1.21 chs }
484 1.30 chs KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
485 1.30 chs KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
486 1.30 chs KASSERT(orignpages > 0);
487 1.95 chs
488 1.95 chs /*
489 1.95 chs * Bounds-check the request.
490 1.95 chs */
491 1.95 chs
492 1.95 chs if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
493 1.95 chs if ((flags & PGO_LOCKED) == 0) {
494 1.95 chs simple_unlock(&uobj->vmobjlock);
495 1.95 chs }
496 1.95 chs UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
497 1.95 chs origoffset, *ap->a_count, memeof,0);
498 1.143 hannken error = EINVAL;
499 1.143 hannken goto out_err;
500 1.95 chs }
501 1.21 chs
502 1.99 yamt /* uobj is locked */
503 1.99 yamt
504 1.103 yamt if ((flags & PGO_NOTIMESTAMP) == 0 &&
505 1.121 reinoud (vp->v_type != VBLK ||
506 1.103 yamt (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
507 1.103 yamt int updflags = 0;
508 1.103 yamt
509 1.103 yamt if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
510 1.103 yamt updflags = GOP_UPDATE_ACCESSED;
511 1.103 yamt }
512 1.103 yamt if (write) {
513 1.103 yamt updflags |= GOP_UPDATE_MODIFIED;
514 1.103 yamt }
515 1.103 yamt if (updflags != 0) {
516 1.103 yamt GOP_MARKUPDATE(vp, updflags);
517 1.103 yamt }
518 1.103 yamt }
519 1.103 yamt
520 1.101 yamt if (write) {
521 1.101 yamt gp->g_dirtygen++;
522 1.157 ad if ((vp->v_iflag & VI_ONWORKLST) == 0) {
523 1.101 yamt vn_syncer_add_to_worklist(vp, filedelay);
524 1.101 yamt }
525 1.157 ad if ((vp->v_iflag & (VI_WRMAP|VI_WRMAPDIRTY)) == VI_WRMAP) {
526 1.157 ad vp->v_iflag |= VI_WRMAPDIRTY;
527 1.103 yamt }
528 1.99 yamt }
529 1.99 yamt
530 1.21 chs /*
531 1.21 chs * For PGO_LOCKED requests, just return whatever's in memory.
532 1.21 chs */
533 1.21 chs
534 1.21 chs if (flags & PGO_LOCKED) {
535 1.127 yamt int nfound;
536 1.127 yamt
537 1.127 yamt npages = *ap->a_count;
538 1.127 yamt #if defined(DEBUG)
539 1.127 yamt for (i = 0; i < npages; i++) {
540 1.127 yamt pg = ap->a_m[i];
541 1.127 yamt KASSERT(pg == NULL || pg == PGO_DONTCARE);
542 1.127 yamt }
543 1.127 yamt #endif /* defined(DEBUG) */
544 1.127 yamt nfound = uvn_findpages(uobj, origoffset, &npages,
545 1.127 yamt ap->a_m, UFP_NOWAIT|UFP_NOALLOC|(write ? UFP_NORDONLY : 0));
546 1.127 yamt KASSERT(npages == *ap->a_count);
547 1.127 yamt if (nfound == 0) {
548 1.143 hannken error = EBUSY;
549 1.143 hannken goto out_err;
550 1.127 yamt }
551 1.146 ad if (!rw_tryenter(&gp->g_glock, RW_READER)) {
552 1.127 yamt genfs_rel_pages(ap->a_m, npages);
553 1.127 yamt
554 1.127 yamt /*
555 1.127 yamt * restore the array.
556 1.127 yamt */
557 1.127 yamt
558 1.127 yamt for (i = 0; i < npages; i++) {
559 1.127 yamt pg = ap->a_m[i];
560 1.21 chs
561 1.127 yamt if (pg != NULL || pg != PGO_DONTCARE) {
562 1.127 yamt ap->a_m[i] = NULL;
563 1.127 yamt }
564 1.127 yamt }
565 1.127 yamt } else {
566 1.146 ad rw_exit(&gp->g_glock);
567 1.127 yamt }
568 1.143 hannken error = (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
569 1.143 hannken goto out_err;
570 1.21 chs }
571 1.126 yamt simple_unlock(&uobj->vmobjlock);
572 1.21 chs
573 1.21 chs /*
574 1.21 chs * find the requested pages and make some simple checks.
575 1.21 chs * leave space in the page array for a whole block.
576 1.21 chs */
577 1.21 chs
578 1.121 reinoud if (vp->v_type != VBLK) {
579 1.36 chs fs_bshift = vp->v_mount->mnt_fs_bshift;
580 1.36 chs dev_bshift = vp->v_mount->mnt_dev_bshift;
581 1.36 chs } else {
582 1.36 chs fs_bshift = DEV_BSHIFT;
583 1.36 chs dev_bshift = DEV_BSHIFT;
584 1.36 chs }
585 1.21 chs fs_bsize = 1 << fs_bshift;
586 1.21 chs
587 1.30 chs orignpages = MIN(orignpages,
588 1.30 chs round_page(memeof - origoffset) >> PAGE_SHIFT);
589 1.21 chs npages = orignpages;
590 1.21 chs startoffset = origoffset & ~(fs_bsize - 1);
591 1.53 enami endoffset = round_page((origoffset + (npages << PAGE_SHIFT) +
592 1.53 enami fs_bsize - 1) & ~(fs_bsize - 1));
593 1.30 chs endoffset = MIN(endoffset, round_page(memeof));
594 1.21 chs ridx = (origoffset - startoffset) >> PAGE_SHIFT;
595 1.21 chs
596 1.77 yamt pgs_size = sizeof(struct vm_page *) *
597 1.77 yamt ((endoffset - startoffset) >> PAGE_SHIFT);
598 1.77 yamt if (pgs_size > sizeof(pgs_onstack)) {
599 1.135 yamt pgs = kmem_zalloc(pgs_size, async ? KM_NOSLEEP : KM_SLEEP);
600 1.78 simonb if (pgs == NULL) {
601 1.143 hannken pgs = pgs_onstack;
602 1.143 hannken error = ENOMEM;
603 1.143 hannken goto out_err;
604 1.78 simonb }
605 1.77 yamt } else {
606 1.143 hannken /* pgs == pgs_onstack */
607 1.77 yamt memset(pgs, 0, pgs_size);
608 1.77 yamt }
609 1.63 enami UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld",
610 1.63 enami ridx, npages, startoffset, endoffset);
611 1.126 yamt
612 1.153 hannken if (!has_trans) {
613 1.153 hannken fstrans_start(vp->v_mount, FSTRANS_SHARED);
614 1.153 hannken has_trans = true;
615 1.143 hannken }
616 1.143 hannken
617 1.126 yamt /*
618 1.126 yamt * hold g_glock to prevent a race with truncate.
619 1.126 yamt *
620 1.126 yamt * check if our idea of v_size is still valid.
621 1.126 yamt */
622 1.126 yamt
623 1.126 yamt if (blockalloc) {
624 1.146 ad rw_enter(&gp->g_glock, RW_WRITER);
625 1.126 yamt } else {
626 1.146 ad rw_enter(&gp->g_glock, RW_READER);
627 1.126 yamt }
628 1.126 yamt simple_lock(&uobj->vmobjlock);
629 1.126 yamt if (vp->v_size < origvsize) {
630 1.146 ad rw_exit(&gp->g_glock);
631 1.126 yamt if (pgs != pgs_onstack)
632 1.135 yamt kmem_free(pgs, pgs_size);
633 1.126 yamt goto startover;
634 1.126 yamt }
635 1.126 yamt
636 1.63 enami if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
637 1.63 enami async ? UFP_NOWAIT : UFP_ALL) != orignpages) {
638 1.146 ad rw_exit(&gp->g_glock);
639 1.63 enami KASSERT(async != 0);
640 1.63 enami genfs_rel_pages(&pgs[ridx], orignpages);
641 1.63 enami simple_unlock(&uobj->vmobjlock);
642 1.143 hannken error = EBUSY;
643 1.143 hannken goto out_err;
644 1.63 enami }
645 1.21 chs
646 1.21 chs /*
647 1.21 chs * if the pages are already resident, just return them.
648 1.21 chs */
649 1.21 chs
650 1.21 chs for (i = 0; i < npages; i++) {
651 1.97 christos struct vm_page *pg1 = pgs[ridx + i];
652 1.21 chs
653 1.97 christos if ((pg1->flags & PG_FAKE) ||
654 1.100 yamt (blockalloc && (pg1->flags & PG_RDONLY))) {
655 1.21 chs break;
656 1.21 chs }
657 1.21 chs }
658 1.21 chs if (i == npages) {
659 1.146 ad rw_exit(&gp->g_glock);
660 1.21 chs UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
661 1.26 chs npages += ridx;
662 1.110 yamt goto out;
663 1.21 chs }
664 1.21 chs
665 1.21 chs /*
666 1.37 chs * if PGO_OVERWRITE is set, don't bother reading the pages.
667 1.37 chs */
668 1.37 chs
669 1.124 yamt if (overwrite) {
670 1.146 ad rw_exit(&gp->g_glock);
671 1.37 chs UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
672 1.37 chs
673 1.37 chs for (i = 0; i < npages; i++) {
674 1.97 christos struct vm_page *pg1 = pgs[ridx + i];
675 1.37 chs
676 1.97 christos pg1->flags &= ~(PG_RDONLY|PG_CLEAN);
677 1.37 chs }
678 1.37 chs npages += ridx;
679 1.37 chs goto out;
680 1.37 chs }
681 1.37 chs
682 1.37 chs /*
683 1.21 chs * the page wasn't resident and we're not overwriting,
684 1.21 chs * so we're going to have to do some i/o.
685 1.21 chs * find any additional pages needed to cover the expanded range.
686 1.21 chs */
687 1.21 chs
688 1.35 chs npages = (endoffset - startoffset) >> PAGE_SHIFT;
689 1.35 chs if (startoffset != origoffset || npages != orignpages) {
690 1.21 chs
691 1.21 chs /*
692 1.37 chs * we need to avoid deadlocks caused by locking
693 1.21 chs * additional pages at lower offsets than pages we
694 1.37 chs * already have locked. unlock them all and start over.
695 1.21 chs */
696 1.21 chs
697 1.63 enami genfs_rel_pages(&pgs[ridx], orignpages);
698 1.77 yamt memset(pgs, 0, pgs_size);
699 1.21 chs
700 1.21 chs UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
701 1.53 enami startoffset, endoffset, 0,0);
702 1.21 chs npgs = npages;
703 1.63 enami if (uvn_findpages(uobj, startoffset, &npgs, pgs,
704 1.63 enami async ? UFP_NOWAIT : UFP_ALL) != npages) {
705 1.146 ad rw_exit(&gp->g_glock);
706 1.63 enami KASSERT(async != 0);
707 1.63 enami genfs_rel_pages(pgs, npages);
708 1.63 enami simple_unlock(&uobj->vmobjlock);
709 1.143 hannken error = EBUSY;
710 1.143 hannken goto out_err;
711 1.63 enami }
712 1.21 chs }
713 1.21 chs simple_unlock(&uobj->vmobjlock);
714 1.21 chs
715 1.21 chs /*
716 1.21 chs * read the desired page(s).
717 1.21 chs */
718 1.21 chs
719 1.21 chs totalbytes = npages << PAGE_SHIFT;
720 1.30 chs bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
721 1.21 chs tailbytes = totalbytes - bytes;
722 1.21 chs skipbytes = 0;
723 1.21 chs
724 1.53 enami kva = uvm_pagermapin(pgs, npages,
725 1.53 enami UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
726 1.21 chs
727 1.119 yamt mbp = getiobuf();
728 1.21 chs mbp->b_bufsize = totalbytes;
729 1.21 chs mbp->b_data = (void *)kva;
730 1.21 chs mbp->b_resid = mbp->b_bcount = bytes;
731 1.65 fvdl mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
732 1.37 chs mbp->b_iodone = (async ? uvm_aio_biodone : 0);
733 1.21 chs mbp->b_vp = vp;
734 1.120 yamt if (async)
735 1.120 yamt BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
736 1.120 yamt else
737 1.120 yamt BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
738 1.21 chs
739 1.21 chs /*
740 1.31 chs * if EOF is in the middle of the range, zero the part past EOF.
741 1.154 yamt * skip over pages which are not PG_FAKE since in that case they have
742 1.154 yamt * valid data that we need to preserve.
743 1.21 chs */
744 1.21 chs
745 1.154 yamt tailstart = bytes;
746 1.154 yamt while (tailbytes > 0) {
747 1.154 yamt const int len = PAGE_SIZE - (tailstart & PAGE_MASK);
748 1.154 yamt
749 1.154 yamt KASSERT(len <= tailbytes);
750 1.154 yamt if ((pgs[tailstart >> PAGE_SHIFT]->flags & PG_FAKE) != 0) {
751 1.154 yamt memset((void *)(kva + tailstart), 0, len);
752 1.154 yamt UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
753 1.154 yamt kva, tailstart, len, 0);
754 1.38 chs }
755 1.154 yamt tailstart += len;
756 1.154 yamt tailbytes -= len;
757 1.21 chs }
758 1.21 chs
759 1.21 chs /*
760 1.21 chs * now loop over the pages, reading as needed.
761 1.21 chs */
762 1.21 chs
763 1.21 chs bp = NULL;
764 1.21 chs for (offset = startoffset;
765 1.53 enami bytes > 0;
766 1.53 enami offset += iobytes, bytes -= iobytes) {
767 1.21 chs
768 1.21 chs /*
769 1.21 chs * skip pages which don't need to be read.
770 1.21 chs */
771 1.21 chs
772 1.21 chs pidx = (offset - startoffset) >> PAGE_SHIFT;
773 1.100 yamt while ((pgs[pidx]->flags & PG_FAKE) == 0) {
774 1.21 chs size_t b;
775 1.21 chs
776 1.24 chs KASSERT((offset & (PAGE_SIZE - 1)) == 0);
777 1.100 yamt if ((pgs[pidx]->flags & PG_RDONLY)) {
778 1.149 thorpej sawhole = true;
779 1.100 yamt }
780 1.26 chs b = MIN(PAGE_SIZE, bytes);
781 1.21 chs offset += b;
782 1.21 chs bytes -= b;
783 1.21 chs skipbytes += b;
784 1.21 chs pidx++;
785 1.21 chs UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
786 1.53 enami offset, 0,0,0);
787 1.21 chs if (bytes == 0) {
788 1.21 chs goto loopdone;
789 1.21 chs }
790 1.21 chs }
791 1.21 chs
792 1.21 chs /*
793 1.21 chs * bmap the file to find out the blkno to read from and
794 1.21 chs * how much we can read in one i/o. if bmap returns an error,
795 1.21 chs * skip the rest of the top-level i/o.
796 1.21 chs */
797 1.21 chs
798 1.21 chs lbn = offset >> fs_bshift;
799 1.36 chs error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
800 1.21 chs if (error) {
801 1.21 chs UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
802 1.53 enami lbn, error,0,0);
803 1.21 chs skipbytes += bytes;
804 1.21 chs goto loopdone;
805 1.21 chs }
806 1.21 chs
807 1.21 chs /*
808 1.21 chs * see how many pages can be read with this i/o.
809 1.21 chs * reduce the i/o size if necessary to avoid
810 1.21 chs * overwriting pages with valid data.
811 1.21 chs */
812 1.21 chs
813 1.26 chs iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
814 1.26 chs bytes);
815 1.21 chs if (offset + iobytes > round_page(offset)) {
816 1.21 chs pcount = 1;
817 1.21 chs while (pidx + pcount < npages &&
818 1.53 enami pgs[pidx + pcount]->flags & PG_FAKE) {
819 1.21 chs pcount++;
820 1.21 chs }
821 1.26 chs iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
822 1.53 enami (offset - trunc_page(offset)));
823 1.21 chs }
824 1.21 chs
825 1.21 chs /*
826 1.53 enami * if this block isn't allocated, zero it instead of
827 1.100 yamt * reading it. unless we are going to allocate blocks,
828 1.100 yamt * mark the pages we zeroed PG_RDONLY.
829 1.21 chs */
830 1.21 chs
831 1.21 chs if (blkno < 0) {
832 1.53 enami int holepages = (round_page(offset + iobytes) -
833 1.53 enami trunc_page(offset)) >> PAGE_SHIFT;
834 1.21 chs UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
835 1.21 chs
836 1.149 thorpej sawhole = true;
837 1.21 chs memset((char *)kva + (offset - startoffset), 0,
838 1.53 enami iobytes);
839 1.21 chs skipbytes += iobytes;
840 1.21 chs
841 1.35 chs for (i = 0; i < holepages; i++) {
842 1.35 chs if (write) {
843 1.35 chs pgs[pidx + i]->flags &= ~PG_CLEAN;
844 1.100 yamt }
845 1.100 yamt if (!blockalloc) {
846 1.21 chs pgs[pidx + i]->flags |= PG_RDONLY;
847 1.21 chs }
848 1.21 chs }
849 1.21 chs continue;
850 1.21 chs }
851 1.21 chs
852 1.21 chs /*
853 1.21 chs * allocate a sub-buf for this piece of the i/o
854 1.21 chs * (or just use mbp if there's only 1 piece),
855 1.21 chs * and start it going.
856 1.21 chs */
857 1.21 chs
858 1.21 chs if (offset == startoffset && iobytes == bytes) {
859 1.21 chs bp = mbp;
860 1.21 chs } else {
861 1.119 yamt bp = getiobuf();
862 1.120 yamt nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
863 1.21 chs }
864 1.112 yamt bp->b_lblkno = 0;
865 1.21 chs
866 1.21 chs /* adjust physical blkno for partial blocks */
867 1.25 fvdl bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
868 1.53 enami dev_bshift);
869 1.21 chs
870 1.53 enami UVMHIST_LOG(ubchist,
871 1.53 enami "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
872 1.53 enami bp, offset, iobytes, bp->b_blkno);
873 1.21 chs
874 1.109 yamt VOP_STRATEGY(devvp, bp);
875 1.21 chs }
876 1.21 chs
877 1.21 chs loopdone:
878 1.120 yamt nestiobuf_done(mbp, skipbytes, error);
879 1.21 chs if (async) {
880 1.32 chs UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
881 1.146 ad rw_exit(&gp->g_glock);
882 1.143 hannken error = 0;
883 1.143 hannken goto out_err;
884 1.21 chs }
885 1.21 chs if (bp != NULL) {
886 1.21 chs error = biowait(mbp);
887 1.21 chs }
888 1.119 yamt putiobuf(mbp);
889 1.21 chs uvm_pagermapout(kva, npages);
890 1.21 chs
891 1.21 chs /*
892 1.21 chs * if this we encountered a hole then we have to do a little more work.
893 1.21 chs * for read faults, we marked the page PG_RDONLY so that future
894 1.21 chs * write accesses to the page will fault again.
895 1.21 chs * for write faults, we must make sure that the backing store for
896 1.21 chs * the page is completely allocated while the pages are locked.
897 1.21 chs */
898 1.21 chs
899 1.100 yamt if (!error && sawhole && blockalloc) {
900 1.37 chs error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
901 1.53 enami cred);
902 1.37 chs UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
903 1.37 chs startoffset, npages << PAGE_SHIFT, error,0);
904 1.100 yamt if (!error) {
905 1.100 yamt for (i = 0; i < npages; i++) {
906 1.100 yamt if (pgs[i] == NULL) {
907 1.100 yamt continue;
908 1.100 yamt }
909 1.100 yamt pgs[i]->flags &= ~(PG_CLEAN|PG_RDONLY);
910 1.100 yamt UVMHIST_LOG(ubchist, "mark dirty pg %p",
911 1.100 yamt pgs[i],0,0,0);
912 1.100 yamt }
913 1.100 yamt }
914 1.21 chs }
915 1.146 ad rw_exit(&gp->g_glock);
916 1.21 chs simple_lock(&uobj->vmobjlock);
917 1.21 chs
918 1.21 chs /*
919 1.21 chs * we're almost done! release the pages...
920 1.21 chs * for errors, we free the pages.
921 1.21 chs * otherwise we activate them and mark them as valid and clean.
922 1.21 chs * also, unbusy pages that were not actually requested.
923 1.21 chs */
924 1.21 chs
925 1.21 chs if (error) {
926 1.21 chs for (i = 0; i < npages; i++) {
927 1.21 chs if (pgs[i] == NULL) {
928 1.21 chs continue;
929 1.21 chs }
930 1.21 chs UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
931 1.53 enami pgs[i], pgs[i]->flags, 0,0);
932 1.26 chs if (pgs[i]->flags & PG_FAKE) {
933 1.37 chs pgs[i]->flags |= PG_RELEASED;
934 1.21 chs }
935 1.21 chs }
936 1.37 chs uvm_lock_pageq();
937 1.37 chs uvm_page_unbusy(pgs, npages);
938 1.21 chs uvm_unlock_pageq();
939 1.21 chs simple_unlock(&uobj->vmobjlock);
940 1.21 chs UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
941 1.143 hannken goto out_err;
942 1.21 chs }
943 1.21 chs
944 1.37 chs out:
945 1.21 chs UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
946 1.143 hannken error = 0;
947 1.26 chs uvm_lock_pageq();
948 1.21 chs for (i = 0; i < npages; i++) {
949 1.37 chs pg = pgs[i];
950 1.37 chs if (pg == NULL) {
951 1.21 chs continue;
952 1.21 chs }
953 1.21 chs UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
954 1.53 enami pg, pg->flags, 0,0);
955 1.37 chs if (pg->flags & PG_FAKE && !overwrite) {
956 1.37 chs pg->flags &= ~(PG_FAKE);
957 1.21 chs pmap_clear_modify(pgs[i]);
958 1.21 chs }
959 1.100 yamt KASSERT(!write || !blockalloc || (pg->flags & PG_RDONLY) == 0);
960 1.21 chs if (i < ridx || i >= ridx + orignpages || async) {
961 1.21 chs UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
962 1.53 enami pg, pg->offset,0,0);
963 1.37 chs if (pg->flags & PG_WANTED) {
964 1.37 chs wakeup(pg);
965 1.37 chs }
966 1.37 chs if (pg->flags & PG_FAKE) {
967 1.37 chs KASSERT(overwrite);
968 1.37 chs uvm_pagezero(pg);
969 1.37 chs }
970 1.37 chs if (pg->flags & PG_RELEASED) {
971 1.37 chs uvm_pagefree(pg);
972 1.26 chs continue;
973 1.21 chs }
974 1.129 yamt uvm_pageenqueue(pg);
975 1.37 chs pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
976 1.37 chs UVM_PAGE_OWN(pg, NULL);
977 1.21 chs }
978 1.21 chs }
979 1.26 chs uvm_unlock_pageq();
980 1.21 chs simple_unlock(&uobj->vmobjlock);
981 1.21 chs if (ap->a_m != NULL) {
982 1.21 chs memcpy(ap->a_m, &pgs[ridx],
983 1.53 enami orignpages * sizeof(struct vm_page *));
984 1.21 chs }
985 1.143 hannken
986 1.143 hannken out_err:
987 1.77 yamt if (pgs != pgs_onstack)
988 1.135 yamt kmem_free(pgs, pgs_size);
989 1.143 hannken if (has_trans)
990 1.143 hannken fstrans_done(vp->v_mount);
991 1.143 hannken return (error);
992 1.21 chs }
993 1.21 chs
994 1.21 chs /*
995 1.21 chs * generic VM putpages routine.
996 1.21 chs * Write the given range of pages to backing store.
997 1.37 chs *
998 1.37 chs * => "offhi == 0" means flush all pages at or after "offlo".
999 1.140 pooka * => object should be locked by caller. we return with the
1000 1.140 pooka * object unlocked.
1001 1.37 chs * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
1002 1.37 chs * thus, a caller might want to unlock higher level resources
1003 1.37 chs * (e.g. vm_map) before calling flush.
1004 1.140 pooka * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, we will not block
1005 1.37 chs * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
1006 1.37 chs * => NOTE: we rely on the fact that the object's memq is a TAILQ and
1007 1.37 chs * that new pages are inserted on the tail end of the list. thus,
1008 1.37 chs * we can make a complete pass through the object in one go by starting
1009 1.37 chs * at the head and working towards the tail (new pages are put in
1010 1.37 chs * front of us).
1011 1.37 chs * => NOTE: we are allowed to lock the page queues, so the caller
1012 1.37 chs * must not be holding the page queue lock.
1013 1.37 chs *
1014 1.37 chs * note on "cleaning" object and PG_BUSY pages:
1015 1.37 chs * this routine is holding the lock on the object. the only time
1016 1.37 chs * that it can run into a PG_BUSY page that it does not own is if
1017 1.37 chs * some other process has started I/O on the page (e.g. either
1018 1.37 chs * a pagein, or a pageout). if the PG_BUSY page is being paged
1019 1.37 chs * in, then it can not be dirty (!PG_CLEAN) because no one has
1020 1.37 chs * had a chance to modify it yet. if the PG_BUSY page is being
1021 1.37 chs * paged out then it means that someone else has already started
1022 1.53 enami * cleaning the page for us (how nice!). in this case, if we
1023 1.37 chs * have syncio specified, then after we make our pass through the
1024 1.53 enami * object we need to wait for the other PG_BUSY pages to clear
1025 1.37 chs * off (i.e. we need to do an iosync). also note that once a
1026 1.37 chs * page is PG_BUSY it must stay in its object until it is un-busyed.
1027 1.37 chs *
1028 1.37 chs * note on page traversal:
1029 1.37 chs * we can traverse the pages in an object either by going down the
1030 1.37 chs * linked list in "uobj->memq", or we can go over the address range
1031 1.37 chs * by page doing hash table lookups for each address. depending
1032 1.53 enami * on how many pages are in the object it may be cheaper to do one
1033 1.37 chs * or the other. we set "by_list" to true if we are using memq.
1034 1.37 chs * if the cost of a hash lookup was equal to the cost of the list
1035 1.37 chs * traversal we could compare the number of pages in the start->stop
1036 1.37 chs * range to the total number of pages in the object. however, it
1037 1.37 chs * seems that a hash table lookup is more expensive than the linked
1038 1.53 enami * list traversal, so we multiply the number of pages in the
1039 1.37 chs * range by an estimate of the relatively higher cost of the hash lookup.
1040 1.21 chs */
1041 1.21 chs
1042 1.21 chs int
1043 1.53 enami genfs_putpages(void *v)
1044 1.21 chs {
1045 1.21 chs struct vop_putpages_args /* {
1046 1.21 chs struct vnode *a_vp;
1047 1.37 chs voff_t a_offlo;
1048 1.37 chs voff_t a_offhi;
1049 1.21 chs int a_flags;
1050 1.21 chs } */ *ap = v;
1051 1.151 perseant
1052 1.151 perseant return genfs_do_putpages(ap->a_vp, ap->a_offlo, ap->a_offhi,
1053 1.151 perseant ap->a_flags, NULL);
1054 1.151 perseant }
1055 1.151 perseant
1056 1.151 perseant int
1057 1.151 perseant genfs_do_putpages(struct vnode *vp, off_t startoff, off_t endoff, int flags,
1058 1.151 perseant struct vm_page **busypg)
1059 1.151 perseant {
1060 1.37 chs struct uvm_object *uobj = &vp->v_uobj;
1061 1.46 chs struct simplelock *slock = &uobj->vmobjlock;
1062 1.37 chs off_t off;
1063 1.76 tls /* Even for strange MAXPHYS, the shift rounds down to a page */
1064 1.139 christos #define maxpages (MAXPHYS >> PAGE_SHIFT)
1065 1.37 chs int i, s, error, npages, nback;
1066 1.37 chs int freeflag;
1067 1.60 enami struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
1068 1.148 thorpej bool wasclean, by_list, needs_clean, yld;
1069 1.148 thorpej bool async = (flags & PGO_SYNCIO) == 0;
1070 1.155 ad bool pagedaemon = curlwp == uvm.pagedaemon_lwp;
1071 1.70 christos struct lwp *l = curlwp ? curlwp : &lwp0;
1072 1.101 yamt struct genfs_node *gp = VTOG(vp);
1073 1.101 yamt int dirtygen;
1074 1.149 thorpej bool modified = false;
1075 1.149 thorpej bool has_trans = false;
1076 1.148 thorpej bool cleanall;
1077 1.70 christos
1078 1.37 chs UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
1079 1.37 chs
1080 1.37 chs KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
1081 1.37 chs KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
1082 1.37 chs KASSERT(startoff < endoff || endoff == 0);
1083 1.37 chs
1084 1.37 chs UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
1085 1.37 chs vp, uobj->uo_npages, startoff, endoff - startoff);
1086 1.103 yamt
1087 1.157 ad KASSERT((vp->v_iflag & VI_ONWORKLST) != 0 ||
1088 1.157 ad (vp->v_iflag & VI_WRMAPDIRTY) == 0);
1089 1.37 chs if (uobj->uo_npages == 0) {
1090 1.62 perseant s = splbio();
1091 1.157 ad if (vp->v_iflag & VI_ONWORKLST) {
1092 1.157 ad vp->v_iflag &= ~VI_WRMAPDIRTY;
1093 1.137 reinoud if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
1094 1.137 reinoud vn_syncer_remove_from_worklist(vp);
1095 1.37 chs }
1096 1.62 perseant splx(s);
1097 1.46 chs simple_unlock(slock);
1098 1.53 enami return (0);
1099 1.37 chs }
1100 1.37 chs
1101 1.37 chs /*
1102 1.37 chs * the vnode has pages, set up to process the request.
1103 1.37 chs */
1104 1.37 chs
1105 1.143 hannken if ((flags & PGO_CLEANIT) != 0) {
1106 1.143 hannken simple_unlock(slock);
1107 1.153 hannken if (pagedaemon) {
1108 1.144 hannken error = fstrans_start_nowait(vp->v_mount, FSTRANS_LAZY);
1109 1.153 hannken if (error)
1110 1.153 hannken return error;
1111 1.153 hannken } else
1112 1.153 hannken fstrans_start(vp->v_mount, FSTRANS_LAZY);
1113 1.149 thorpej has_trans = true;
1114 1.143 hannken simple_lock(slock);
1115 1.143 hannken }
1116 1.143 hannken
1117 1.37 chs error = 0;
1118 1.44 chs s = splbio();
1119 1.71 pk simple_lock(&global_v_numoutput_slock);
1120 1.44 chs wasclean = (vp->v_numoutput == 0);
1121 1.71 pk simple_unlock(&global_v_numoutput_slock);
1122 1.44 chs splx(s);
1123 1.37 chs off = startoff;
1124 1.37 chs if (endoff == 0 || flags & PGO_ALLPAGES) {
1125 1.37 chs endoff = trunc_page(LLONG_MAX);
1126 1.37 chs }
1127 1.37 chs by_list = (uobj->uo_npages <=
1128 1.37 chs ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
1129 1.37 chs
1130 1.102 yamt #if !defined(DEBUG)
1131 1.102 yamt /*
1132 1.102 yamt * if this vnode is known not to have dirty pages,
1133 1.102 yamt * don't bother to clean it out.
1134 1.102 yamt */
1135 1.102 yamt
1136 1.157 ad if ((vp->v_iflag & VI_ONWORKLST) == 0) {
1137 1.102 yamt if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
1138 1.102 yamt goto skip_scan;
1139 1.102 yamt }
1140 1.102 yamt flags &= ~PGO_CLEANIT;
1141 1.102 yamt }
1142 1.102 yamt #endif /* !defined(DEBUG) */
1143 1.102 yamt
1144 1.37 chs /*
1145 1.37 chs * start the loop. when scanning by list, hold the last page
1146 1.37 chs * in the list before we start. pages allocated after we start
1147 1.37 chs * will be added to the end of the list, so we can stop at the
1148 1.37 chs * current last page.
1149 1.37 chs */
1150 1.37 chs
1151 1.104 yamt cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
1152 1.104 yamt startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
1153 1.157 ad (vp->v_iflag & VI_ONWORKLST) != 0;
1154 1.101 yamt dirtygen = gp->g_dirtygen;
1155 1.56 enami freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
1156 1.37 chs if (by_list) {
1157 1.113 yamt curmp.uobject = uobj;
1158 1.113 yamt curmp.offset = (voff_t)-1;
1159 1.113 yamt curmp.flags = PG_BUSY;
1160 1.113 yamt endmp.uobject = uobj;
1161 1.113 yamt endmp.offset = (voff_t)-1;
1162 1.113 yamt endmp.flags = PG_BUSY;
1163 1.37 chs pg = TAILQ_FIRST(&uobj->memq);
1164 1.37 chs TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
1165 1.155 ad uvm_lwp_hold(l);
1166 1.37 chs } else {
1167 1.37 chs pg = uvm_pagelookup(uobj, off);
1168 1.37 chs }
1169 1.37 chs nextpg = NULL;
1170 1.37 chs while (by_list || off < endoff) {
1171 1.37 chs
1172 1.37 chs /*
1173 1.37 chs * if the current page is not interesting, move on to the next.
1174 1.37 chs */
1175 1.37 chs
1176 1.37 chs KASSERT(pg == NULL || pg->uobject == uobj);
1177 1.37 chs KASSERT(pg == NULL ||
1178 1.53 enami (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1179 1.53 enami (pg->flags & PG_BUSY) != 0);
1180 1.37 chs if (by_list) {
1181 1.37 chs if (pg == &endmp) {
1182 1.37 chs break;
1183 1.37 chs }
1184 1.37 chs if (pg->offset < startoff || pg->offset >= endoff ||
1185 1.37 chs pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1186 1.101 yamt if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1187 1.149 thorpej wasclean = false;
1188 1.101 yamt }
1189 1.37 chs pg = TAILQ_NEXT(pg, listq);
1190 1.37 chs continue;
1191 1.37 chs }
1192 1.37 chs off = pg->offset;
1193 1.101 yamt } else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1194 1.101 yamt if (pg != NULL) {
1195 1.149 thorpej wasclean = false;
1196 1.101 yamt }
1197 1.37 chs off += PAGE_SIZE;
1198 1.37 chs if (off < endoff) {
1199 1.37 chs pg = uvm_pagelookup(uobj, off);
1200 1.37 chs }
1201 1.37 chs continue;
1202 1.37 chs }
1203 1.21 chs
1204 1.37 chs /*
1205 1.37 chs * if the current page needs to be cleaned and it's busy,
1206 1.37 chs * wait for it to become unbusy.
1207 1.37 chs */
1208 1.37 chs
1209 1.97 christos yld = (l->l_cpu->ci_schedstate.spc_flags &
1210 1.56 enami SPCF_SHOULDYIELD) && !pagedaemon;
1211 1.97 christos if (pg->flags & PG_BUSY || yld) {
1212 1.72 perseant UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
1213 1.72 perseant if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
1214 1.72 perseant UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
1215 1.72 perseant error = EDEADLK;
1216 1.151 perseant if (busypg != NULL)
1217 1.151 perseant *busypg = pg;
1218 1.72 perseant break;
1219 1.72 perseant }
1220 1.157 ad if (pagedaemon) {
1221 1.157 ad /*
1222 1.157 ad * someone has taken the page while we
1223 1.157 ad * dropped the lock for fstrans_start.
1224 1.157 ad */
1225 1.157 ad break;
1226 1.157 ad }
1227 1.37 chs if (by_list) {
1228 1.37 chs TAILQ_INSERT_BEFORE(pg, &curmp, listq);
1229 1.37 chs UVMHIST_LOG(ubchist, "curmp next %p",
1230 1.53 enami TAILQ_NEXT(&curmp, listq), 0,0,0);
1231 1.37 chs }
1232 1.97 christos if (yld) {
1233 1.49 chs simple_unlock(slock);
1234 1.145 ad preempt();
1235 1.49 chs simple_lock(slock);
1236 1.49 chs } else {
1237 1.49 chs pg->flags |= PG_WANTED;
1238 1.49 chs UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
1239 1.49 chs simple_lock(slock);
1240 1.49 chs }
1241 1.37 chs if (by_list) {
1242 1.37 chs UVMHIST_LOG(ubchist, "after next %p",
1243 1.53 enami TAILQ_NEXT(&curmp, listq), 0,0,0);
1244 1.37 chs pg = TAILQ_NEXT(&curmp, listq);
1245 1.37 chs TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1246 1.37 chs } else {
1247 1.37 chs pg = uvm_pagelookup(uobj, off);
1248 1.37 chs }
1249 1.37 chs continue;
1250 1.49 chs }
1251 1.49 chs
1252 1.49 chs /*
1253 1.49 chs * if we're freeing, remove all mappings of the page now.
1254 1.49 chs * if we're cleaning, check if the page is needs to be cleaned.
1255 1.49 chs */
1256 1.49 chs
1257 1.49 chs if (flags & PGO_FREE) {
1258 1.49 chs pmap_page_protect(pg, VM_PROT_NONE);
1259 1.101 yamt } else if (flags & PGO_CLEANIT) {
1260 1.101 yamt
1261 1.101 yamt /*
1262 1.101 yamt * if we still have some hope to pull this vnode off
1263 1.101 yamt * from the syncer queue, write-protect the page.
1264 1.101 yamt */
1265 1.101 yamt
1266 1.104 yamt if (cleanall && wasclean &&
1267 1.104 yamt gp->g_dirtygen == dirtygen) {
1268 1.104 yamt
1269 1.104 yamt /*
1270 1.104 yamt * uobj pages get wired only by uvm_fault
1271 1.104 yamt * where uobj is locked.
1272 1.104 yamt */
1273 1.104 yamt
1274 1.104 yamt if (pg->wire_count == 0) {
1275 1.104 yamt pmap_page_protect(pg,
1276 1.104 yamt VM_PROT_READ|VM_PROT_EXECUTE);
1277 1.104 yamt } else {
1278 1.149 thorpej cleanall = false;
1279 1.104 yamt }
1280 1.101 yamt }
1281 1.49 chs }
1282 1.101 yamt
1283 1.49 chs if (flags & PGO_CLEANIT) {
1284 1.49 chs needs_clean = pmap_clear_modify(pg) ||
1285 1.53 enami (pg->flags & PG_CLEAN) == 0;
1286 1.49 chs pg->flags |= PG_CLEAN;
1287 1.49 chs } else {
1288 1.149 thorpej needs_clean = false;
1289 1.37 chs }
1290 1.37 chs
1291 1.37 chs /*
1292 1.37 chs * if we're cleaning, build a cluster.
1293 1.37 chs * the cluster will consist of pages which are currently dirty,
1294 1.37 chs * but they will be returned to us marked clean.
1295 1.37 chs * if not cleaning, just operate on the one page.
1296 1.37 chs */
1297 1.37 chs
1298 1.37 chs if (needs_clean) {
1299 1.157 ad KDASSERT((vp->v_iflag & VI_ONWORKLST));
1300 1.149 thorpej wasclean = false;
1301 1.37 chs memset(pgs, 0, sizeof(pgs));
1302 1.37 chs pg->flags |= PG_BUSY;
1303 1.37 chs UVM_PAGE_OWN(pg, "genfs_putpages");
1304 1.37 chs
1305 1.37 chs /*
1306 1.37 chs * first look backward.
1307 1.37 chs */
1308 1.37 chs
1309 1.60 enami npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1310 1.37 chs nback = npages;
1311 1.37 chs uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1312 1.37 chs UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1313 1.37 chs if (nback) {
1314 1.37 chs memmove(&pgs[0], &pgs[npages - nback],
1315 1.37 chs nback * sizeof(pgs[0]));
1316 1.47 enami if (npages - nback < nback)
1317 1.47 enami memset(&pgs[nback], 0,
1318 1.47 enami (npages - nback) * sizeof(pgs[0]));
1319 1.47 enami else
1320 1.47 enami memset(&pgs[npages - nback], 0,
1321 1.47 enami nback * sizeof(pgs[0]));
1322 1.37 chs }
1323 1.37 chs
1324 1.37 chs /*
1325 1.37 chs * then plug in our page of interest.
1326 1.37 chs */
1327 1.37 chs
1328 1.37 chs pgs[nback] = pg;
1329 1.37 chs
1330 1.37 chs /*
1331 1.37 chs * then look forward to fill in the remaining space in
1332 1.37 chs * the array of pages.
1333 1.37 chs */
1334 1.37 chs
1335 1.60 enami npages = maxpages - nback - 1;
1336 1.37 chs uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1337 1.37 chs &pgs[nback + 1],
1338 1.37 chs UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1339 1.37 chs npages += nback + 1;
1340 1.37 chs } else {
1341 1.37 chs pgs[0] = pg;
1342 1.37 chs npages = 1;
1343 1.61 enami nback = 0;
1344 1.37 chs }
1345 1.37 chs
1346 1.37 chs /*
1347 1.37 chs * apply FREE or DEACTIVATE options if requested.
1348 1.37 chs */
1349 1.37 chs
1350 1.37 chs if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1351 1.37 chs uvm_lock_pageq();
1352 1.37 chs }
1353 1.37 chs for (i = 0; i < npages; i++) {
1354 1.37 chs tpg = pgs[i];
1355 1.37 chs KASSERT(tpg->uobject == uobj);
1356 1.59 enami if (by_list && tpg == TAILQ_NEXT(pg, listq))
1357 1.59 enami pg = tpg;
1358 1.91 enami if (tpg->offset < startoff || tpg->offset >= endoff)
1359 1.91 enami continue;
1360 1.141 yamt if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
1361 1.37 chs (void) pmap_clear_reference(tpg);
1362 1.37 chs uvm_pagedeactivate(tpg);
1363 1.37 chs } else if (flags & PGO_FREE) {
1364 1.37 chs pmap_page_protect(tpg, VM_PROT_NONE);
1365 1.37 chs if (tpg->flags & PG_BUSY) {
1366 1.37 chs tpg->flags |= freeflag;
1367 1.56 enami if (pagedaemon) {
1368 1.37 chs uvmexp.paging++;
1369 1.37 chs uvm_pagedequeue(tpg);
1370 1.37 chs }
1371 1.37 chs } else {
1372 1.59 enami
1373 1.59 enami /*
1374 1.59 enami * ``page is not busy''
1375 1.59 enami * implies that npages is 1
1376 1.59 enami * and needs_clean is false.
1377 1.59 enami */
1378 1.59 enami
1379 1.37 chs nextpg = TAILQ_NEXT(tpg, listq);
1380 1.37 chs uvm_pagefree(tpg);
1381 1.89 enami if (pagedaemon)
1382 1.89 enami uvmexp.pdfreed++;
1383 1.37 chs }
1384 1.37 chs }
1385 1.37 chs }
1386 1.37 chs if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1387 1.37 chs uvm_unlock_pageq();
1388 1.37 chs }
1389 1.37 chs if (needs_clean) {
1390 1.149 thorpej modified = true;
1391 1.37 chs
1392 1.37 chs /*
1393 1.37 chs * start the i/o. if we're traversing by list,
1394 1.37 chs * keep our place in the list with a marker page.
1395 1.37 chs */
1396 1.37 chs
1397 1.37 chs if (by_list) {
1398 1.37 chs TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1399 1.37 chs listq);
1400 1.37 chs }
1401 1.46 chs simple_unlock(slock);
1402 1.37 chs error = GOP_WRITE(vp, pgs, npages, flags);
1403 1.46 chs simple_lock(slock);
1404 1.37 chs if (by_list) {
1405 1.37 chs pg = TAILQ_NEXT(&curmp, listq);
1406 1.37 chs TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1407 1.37 chs }
1408 1.37 chs if (error) {
1409 1.37 chs break;
1410 1.37 chs }
1411 1.37 chs if (by_list) {
1412 1.37 chs continue;
1413 1.37 chs }
1414 1.37 chs }
1415 1.37 chs
1416 1.37 chs /*
1417 1.37 chs * find the next page and continue if there was no error.
1418 1.37 chs */
1419 1.37 chs
1420 1.37 chs if (by_list) {
1421 1.37 chs if (nextpg) {
1422 1.37 chs pg = nextpg;
1423 1.37 chs nextpg = NULL;
1424 1.37 chs } else {
1425 1.37 chs pg = TAILQ_NEXT(pg, listq);
1426 1.37 chs }
1427 1.37 chs } else {
1428 1.61 enami off += (npages - nback) << PAGE_SHIFT;
1429 1.37 chs if (off < endoff) {
1430 1.37 chs pg = uvm_pagelookup(uobj, off);
1431 1.37 chs }
1432 1.37 chs }
1433 1.37 chs }
1434 1.37 chs if (by_list) {
1435 1.37 chs TAILQ_REMOVE(&uobj->memq, &endmp, listq);
1436 1.155 ad uvm_lwp_rele(l);
1437 1.37 chs }
1438 1.37 chs
1439 1.157 ad if (modified && (vp->v_iflag & VI_WRMAPDIRTY) != 0 &&
1440 1.121 reinoud (vp->v_type != VBLK ||
1441 1.103 yamt (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
1442 1.103 yamt GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
1443 1.103 yamt }
1444 1.103 yamt
1445 1.37 chs /*
1446 1.37 chs * if we're cleaning and there was nothing to clean,
1447 1.37 chs * take us off the syncer list. if we started any i/o
1448 1.37 chs * and we're doing sync i/o, wait for all writes to finish.
1449 1.37 chs */
1450 1.37 chs
1451 1.62 perseant s = splbio();
1452 1.104 yamt if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
1453 1.157 ad (vp->v_iflag & VI_ONWORKLST) != 0) {
1454 1.157 ad vp->v_iflag &= ~VI_WRMAPDIRTY;
1455 1.137 reinoud if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
1456 1.137 reinoud vn_syncer_remove_from_worklist(vp);
1457 1.37 chs }
1458 1.62 perseant splx(s);
1459 1.102 yamt
1460 1.102 yamt #if !defined(DEBUG)
1461 1.102 yamt skip_scan:
1462 1.102 yamt #endif /* !defined(DEBUG) */
1463 1.37 chs if (!wasclean && !async) {
1464 1.37 chs s = splbio();
1465 1.71 pk /*
1466 1.71 pk * XXX - we want simple_unlock(&global_v_numoutput_slock);
1467 1.71 pk * but the slot in ltsleep() is taken!
1468 1.71 pk * XXX - try to recover from missed wakeups with a timeout..
1469 1.71 pk * must think of something better.
1470 1.71 pk */
1471 1.37 chs while (vp->v_numoutput != 0) {
1472 1.157 ad vp->v_iflag |= VI_BWAIT;
1473 1.149 thorpej UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, false,
1474 1.71 pk "genput2", hz);
1475 1.46 chs simple_lock(slock);
1476 1.37 chs }
1477 1.37 chs splx(s);
1478 1.37 chs }
1479 1.140 pooka simple_unlock(slock);
1480 1.143 hannken
1481 1.143 hannken if (has_trans)
1482 1.143 hannken fstrans_done(vp->v_mount);
1483 1.143 hannken
1484 1.53 enami return (error);
1485 1.37 chs }
1486 1.37 chs
1487 1.37 chs int
1488 1.37 chs genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1489 1.37 chs {
1490 1.130 chs off_t off;
1491 1.130 chs vaddr_t kva;
1492 1.130 chs size_t len;
1493 1.130 chs int error;
1494 1.130 chs UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1495 1.130 chs
1496 1.130 chs UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1497 1.130 chs vp, pgs, npages, flags);
1498 1.130 chs
1499 1.130 chs off = pgs[0]->offset;
1500 1.130 chs kva = uvm_pagermapin(pgs, npages,
1501 1.130 chs UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1502 1.130 chs len = npages << PAGE_SHIFT;
1503 1.130 chs
1504 1.130 chs error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1505 1.130 chs uvm_aio_biodone);
1506 1.130 chs
1507 1.130 chs return error;
1508 1.130 chs }
1509 1.130 chs
1510 1.130 chs /*
1511 1.130 chs * Backend routine for doing I/O to vnode pages. Pages are already locked
1512 1.130 chs * and mapped into kernel memory. Here we just look up the underlying
1513 1.130 chs * device block addresses and call the strategy routine.
1514 1.130 chs */
1515 1.130 chs
1516 1.130 chs static int
1517 1.130 chs genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
1518 1.130 chs enum uio_rw rw, void (*iodone)(struct buf *))
1519 1.130 chs {
1520 1.37 chs int s, error, run;
1521 1.37 chs int fs_bshift, dev_bshift;
1522 1.21 chs off_t eof, offset, startoffset;
1523 1.21 chs size_t bytes, iobytes, skipbytes;
1524 1.21 chs daddr_t lbn, blkno;
1525 1.21 chs struct buf *mbp, *bp;
1526 1.36 chs struct vnode *devvp;
1527 1.148 thorpej bool async = (flags & PGO_SYNCIO) == 0;
1528 1.148 thorpej bool write = rw == UIO_WRITE;
1529 1.130 chs int brw = write ? B_WRITE : B_READ;
1530 1.130 chs UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1531 1.21 chs
1532 1.130 chs UVMHIST_LOG(ubchist, "vp %p kva %p len 0x%x flags 0x%x",
1533 1.130 chs vp, kva, len, flags);
1534 1.21 chs
1535 1.154 yamt KASSERT(vp->v_size <= vp->v_writesize);
1536 1.154 yamt GOP_SIZE(vp, vp->v_writesize, &eof, 0);
1537 1.121 reinoud if (vp->v_type != VBLK) {
1538 1.36 chs fs_bshift = vp->v_mount->mnt_fs_bshift;
1539 1.36 chs dev_bshift = vp->v_mount->mnt_dev_bshift;
1540 1.36 chs } else {
1541 1.36 chs fs_bshift = DEV_BSHIFT;
1542 1.36 chs dev_bshift = DEV_BSHIFT;
1543 1.36 chs }
1544 1.37 chs error = 0;
1545 1.130 chs startoffset = off;
1546 1.130 chs bytes = MIN(len, eof - startoffset);
1547 1.21 chs skipbytes = 0;
1548 1.21 chs KASSERT(bytes != 0);
1549 1.21 chs
1550 1.130 chs if (write) {
1551 1.130 chs s = splbio();
1552 1.130 chs simple_lock(&global_v_numoutput_slock);
1553 1.130 chs vp->v_numoutput += 2;
1554 1.130 chs simple_unlock(&global_v_numoutput_slock);
1555 1.130 chs splx(s);
1556 1.130 chs }
1557 1.119 yamt mbp = getiobuf();
1558 1.21 chs UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1559 1.53 enami vp, mbp, vp->v_numoutput, bytes);
1560 1.130 chs mbp->b_bufsize = len;
1561 1.21 chs mbp->b_data = (void *)kva;
1562 1.21 chs mbp->b_resid = mbp->b_bcount = bytes;
1563 1.130 chs mbp->b_flags = B_BUSY | brw | B_AGE | (async ? (B_CALL | B_ASYNC) : 0);
1564 1.130 chs mbp->b_iodone = iodone;
1565 1.21 chs mbp->b_vp = vp;
1566 1.155 ad if (curlwp == uvm.pagedaemon_lwp)
1567 1.120 yamt BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
1568 1.120 yamt else if (async)
1569 1.120 yamt BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
1570 1.120 yamt else
1571 1.120 yamt BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
1572 1.21 chs
1573 1.21 chs bp = NULL;
1574 1.21 chs for (offset = startoffset;
1575 1.53 enami bytes > 0;
1576 1.53 enami offset += iobytes, bytes -= iobytes) {
1577 1.21 chs lbn = offset >> fs_bshift;
1578 1.36 chs error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1579 1.21 chs if (error) {
1580 1.21 chs UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1581 1.21 chs skipbytes += bytes;
1582 1.21 chs bytes = 0;
1583 1.21 chs break;
1584 1.21 chs }
1585 1.21 chs
1586 1.26 chs iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1587 1.26 chs bytes);
1588 1.21 chs if (blkno == (daddr_t)-1) {
1589 1.130 chs if (!write) {
1590 1.130 chs memset((char *)kva + (offset - startoffset), 0,
1591 1.130 chs iobytes);
1592 1.130 chs }
1593 1.21 chs skipbytes += iobytes;
1594 1.21 chs continue;
1595 1.21 chs }
1596 1.21 chs
1597 1.21 chs /* if it's really one i/o, don't make a second buf */
1598 1.21 chs if (offset == startoffset && iobytes == bytes) {
1599 1.21 chs bp = mbp;
1600 1.21 chs } else {
1601 1.21 chs UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1602 1.53 enami vp, bp, vp->v_numoutput, 0);
1603 1.120 yamt bp = getiobuf();
1604 1.130 chs nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
1605 1.21 chs }
1606 1.21 chs bp->b_lblkno = 0;
1607 1.21 chs
1608 1.21 chs /* adjust physical blkno for partial blocks */
1609 1.25 fvdl bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1610 1.53 enami dev_bshift);
1611 1.53 enami UVMHIST_LOG(ubchist,
1612 1.53 enami "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1613 1.53 enami vp, offset, bp->b_bcount, bp->b_blkno);
1614 1.114 yamt
1615 1.114 yamt VOP_STRATEGY(devvp, bp);
1616 1.21 chs }
1617 1.21 chs if (skipbytes) {
1618 1.29 chs UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1619 1.21 chs }
1620 1.120 yamt nestiobuf_done(mbp, skipbytes, error);
1621 1.21 chs if (async) {
1622 1.32 chs UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1623 1.53 enami return (0);
1624 1.21 chs }
1625 1.37 chs UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1626 1.37 chs error = biowait(mbp);
1627 1.134 yamt s = splbio();
1628 1.130 chs (*iodone)(mbp);
1629 1.134 yamt splx(s);
1630 1.21 chs UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1631 1.53 enami return (error);
1632 1.42 chs }
1633 1.42 chs
1634 1.42 chs /*
1635 1.42 chs * VOP_PUTPAGES() for vnodes which never have pages.
1636 1.42 chs */
1637 1.42 chs
1638 1.42 chs int
1639 1.42 chs genfs_null_putpages(void *v)
1640 1.42 chs {
1641 1.42 chs struct vop_putpages_args /* {
1642 1.42 chs struct vnode *a_vp;
1643 1.42 chs voff_t a_offlo;
1644 1.42 chs voff_t a_offhi;
1645 1.42 chs int a_flags;
1646 1.42 chs } */ *ap = v;
1647 1.42 chs struct vnode *vp = ap->a_vp;
1648 1.42 chs
1649 1.42 chs KASSERT(vp->v_uobj.uo_npages == 0);
1650 1.42 chs simple_unlock(&vp->v_interlock);
1651 1.42 chs return (0);
1652 1.21 chs }
1653 1.21 chs
1654 1.37 chs void
1655 1.98 yamt genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
1656 1.37 chs {
1657 1.37 chs struct genfs_node *gp = VTOG(vp);
1658 1.37 chs
1659 1.146 ad rw_init(&gp->g_glock);
1660 1.37 chs gp->g_op = ops;
1661 1.37 chs }
1662 1.37 chs
1663 1.37 chs void
1664 1.147 ad genfs_node_destroy(struct vnode *vp)
1665 1.147 ad {
1666 1.147 ad struct genfs_node *gp = VTOG(vp);
1667 1.147 ad
1668 1.147 ad rw_destroy(&gp->g_glock);
1669 1.147 ad }
1670 1.147 ad
1671 1.147 ad void
1672 1.138 christos genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
1673 1.21 chs {
1674 1.21 chs int bsize;
1675 1.21 chs
1676 1.37 chs bsize = 1 << vp->v_mount->mnt_fs_bshift;
1677 1.37 chs *eobp = (size + bsize - 1) & ~(bsize - 1);
1678 1.43 chs }
1679 1.43 chs
1680 1.43 chs int
1681 1.43 chs genfs_compat_getpages(void *v)
1682 1.43 chs {
1683 1.43 chs struct vop_getpages_args /* {
1684 1.43 chs struct vnode *a_vp;
1685 1.43 chs voff_t a_offset;
1686 1.43 chs struct vm_page **a_m;
1687 1.43 chs int *a_count;
1688 1.43 chs int a_centeridx;
1689 1.43 chs vm_prot_t a_access_type;
1690 1.43 chs int a_advice;
1691 1.43 chs int a_flags;
1692 1.43 chs } */ *ap = v;
1693 1.43 chs
1694 1.43 chs off_t origoffset;
1695 1.43 chs struct vnode *vp = ap->a_vp;
1696 1.43 chs struct uvm_object *uobj = &vp->v_uobj;
1697 1.43 chs struct vm_page *pg, **pgs;
1698 1.43 chs vaddr_t kva;
1699 1.43 chs int i, error, orignpages, npages;
1700 1.43 chs struct iovec iov;
1701 1.43 chs struct uio uio;
1702 1.128 ad kauth_cred_t cred = curlwp->l_cred;
1703 1.148 thorpej bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1704 1.43 chs
1705 1.43 chs error = 0;
1706 1.43 chs origoffset = ap->a_offset;
1707 1.43 chs orignpages = *ap->a_count;
1708 1.43 chs pgs = ap->a_m;
1709 1.43 chs
1710 1.157 ad if (write && (vp->v_iflag & VI_ONWORKLST) == 0) {
1711 1.43 chs vn_syncer_add_to_worklist(vp, filedelay);
1712 1.43 chs }
1713 1.43 chs if (ap->a_flags & PGO_LOCKED) {
1714 1.43 chs uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1715 1.54 enami UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
1716 1.43 chs
1717 1.53 enami return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1718 1.43 chs }
1719 1.43 chs if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1720 1.43 chs simple_unlock(&uobj->vmobjlock);
1721 1.53 enami return (EINVAL);
1722 1.43 chs }
1723 1.115 yamt if ((ap->a_flags & PGO_SYNCIO) == 0) {
1724 1.117 yamt simple_unlock(&uobj->vmobjlock);
1725 1.115 yamt return 0;
1726 1.115 yamt }
1727 1.43 chs npages = orignpages;
1728 1.43 chs uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1729 1.43 chs simple_unlock(&uobj->vmobjlock);
1730 1.53 enami kva = uvm_pagermapin(pgs, npages,
1731 1.53 enami UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1732 1.43 chs for (i = 0; i < npages; i++) {
1733 1.43 chs pg = pgs[i];
1734 1.43 chs if ((pg->flags & PG_FAKE) == 0) {
1735 1.43 chs continue;
1736 1.43 chs }
1737 1.43 chs iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1738 1.43 chs iov.iov_len = PAGE_SIZE;
1739 1.43 chs uio.uio_iov = &iov;
1740 1.43 chs uio.uio_iovcnt = 1;
1741 1.43 chs uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1742 1.43 chs uio.uio_rw = UIO_READ;
1743 1.43 chs uio.uio_resid = PAGE_SIZE;
1744 1.122 yamt UIO_SETUP_SYSSPACE(&uio);
1745 1.87 yamt /* XXX vn_lock */
1746 1.43 chs error = VOP_READ(vp, &uio, 0, cred);
1747 1.43 chs if (error) {
1748 1.43 chs break;
1749 1.52 chs }
1750 1.52 chs if (uio.uio_resid) {
1751 1.52 chs memset(iov.iov_base, 0, uio.uio_resid);
1752 1.43 chs }
1753 1.43 chs }
1754 1.43 chs uvm_pagermapout(kva, npages);
1755 1.43 chs simple_lock(&uobj->vmobjlock);
1756 1.43 chs uvm_lock_pageq();
1757 1.43 chs for (i = 0; i < npages; i++) {
1758 1.43 chs pg = pgs[i];
1759 1.43 chs if (error && (pg->flags & PG_FAKE) != 0) {
1760 1.43 chs pg->flags |= PG_RELEASED;
1761 1.43 chs } else {
1762 1.43 chs pmap_clear_modify(pg);
1763 1.43 chs uvm_pageactivate(pg);
1764 1.43 chs }
1765 1.43 chs }
1766 1.43 chs if (error) {
1767 1.43 chs uvm_page_unbusy(pgs, npages);
1768 1.43 chs }
1769 1.43 chs uvm_unlock_pageq();
1770 1.43 chs simple_unlock(&uobj->vmobjlock);
1771 1.53 enami return (error);
1772 1.43 chs }
1773 1.43 chs
1774 1.43 chs int
1775 1.43 chs genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1776 1.138 christos int flags)
1777 1.43 chs {
1778 1.43 chs off_t offset;
1779 1.43 chs struct iovec iov;
1780 1.43 chs struct uio uio;
1781 1.128 ad kauth_cred_t cred = curlwp->l_cred;
1782 1.43 chs struct buf *bp;
1783 1.43 chs vaddr_t kva;
1784 1.43 chs int s, error;
1785 1.43 chs
1786 1.43 chs offset = pgs[0]->offset;
1787 1.53 enami kva = uvm_pagermapin(pgs, npages,
1788 1.53 enami UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1789 1.43 chs
1790 1.43 chs iov.iov_base = (void *)kva;
1791 1.43 chs iov.iov_len = npages << PAGE_SHIFT;
1792 1.43 chs uio.uio_iov = &iov;
1793 1.68 yamt uio.uio_iovcnt = 1;
1794 1.43 chs uio.uio_offset = offset;
1795 1.43 chs uio.uio_rw = UIO_WRITE;
1796 1.43 chs uio.uio_resid = npages << PAGE_SHIFT;
1797 1.122 yamt UIO_SETUP_SYSSPACE(&uio);
1798 1.87 yamt /* XXX vn_lock */
1799 1.43 chs error = VOP_WRITE(vp, &uio, 0, cred);
1800 1.43 chs
1801 1.43 chs s = splbio();
1802 1.71 pk V_INCR_NUMOUTPUT(vp);
1803 1.43 chs splx(s);
1804 1.43 chs
1805 1.119 yamt bp = getiobuf();
1806 1.43 chs bp->b_flags = B_BUSY | B_WRITE | B_AGE;
1807 1.43 chs bp->b_vp = vp;
1808 1.43 chs bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1809 1.43 chs bp->b_data = (char *)kva;
1810 1.43 chs bp->b_bcount = npages << PAGE_SHIFT;
1811 1.43 chs bp->b_bufsize = npages << PAGE_SHIFT;
1812 1.43 chs bp->b_resid = 0;
1813 1.156 ad bp->b_error = error;
1814 1.43 chs uvm_aio_aiodone(bp);
1815 1.53 enami return (error);
1816 1.66 jdolecek }
1817 1.66 jdolecek
1818 1.130 chs /*
1819 1.130 chs * Process a uio using direct I/O. If we reach a part of the request
1820 1.130 chs * which cannot be processed in this fashion for some reason, just return.
1821 1.130 chs * The caller must handle some additional part of the request using
1822 1.130 chs * buffered I/O before trying direct I/O again.
1823 1.130 chs */
1824 1.130 chs
1825 1.130 chs void
1826 1.138 christos genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
1827 1.130 chs {
1828 1.130 chs struct vmspace *vs;
1829 1.130 chs struct iovec *iov;
1830 1.130 chs vaddr_t va;
1831 1.130 chs size_t len;
1832 1.130 chs const int mask = DEV_BSIZE - 1;
1833 1.130 chs int error;
1834 1.130 chs
1835 1.130 chs /*
1836 1.130 chs * We only support direct I/O to user space for now.
1837 1.130 chs */
1838 1.130 chs
1839 1.130 chs if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
1840 1.130 chs return;
1841 1.130 chs }
1842 1.130 chs
1843 1.130 chs /*
1844 1.130 chs * If the vnode is mapped, we would need to get the getpages lock
1845 1.130 chs * to stabilize the bmap, but then we would get into trouble whil e
1846 1.130 chs * locking the pages if the pages belong to this same vnode (or a
1847 1.130 chs * multi-vnode cascade to the same effect). Just fall back to
1848 1.130 chs * buffered I/O if the vnode is mapped to avoid this mess.
1849 1.130 chs */
1850 1.130 chs
1851 1.157 ad if (vp->v_vflag & VV_MAPPED) {
1852 1.130 chs return;
1853 1.130 chs }
1854 1.130 chs
1855 1.130 chs /*
1856 1.130 chs * Do as much of the uio as possible with direct I/O.
1857 1.130 chs */
1858 1.130 chs
1859 1.130 chs vs = uio->uio_vmspace;
1860 1.130 chs while (uio->uio_resid) {
1861 1.130 chs iov = uio->uio_iov;
1862 1.130 chs if (iov->iov_len == 0) {
1863 1.130 chs uio->uio_iov++;
1864 1.130 chs uio->uio_iovcnt--;
1865 1.130 chs continue;
1866 1.130 chs }
1867 1.130 chs va = (vaddr_t)iov->iov_base;
1868 1.130 chs len = MIN(iov->iov_len, genfs_maxdio);
1869 1.130 chs len &= ~mask;
1870 1.130 chs
1871 1.130 chs /*
1872 1.130 chs * If the next chunk is smaller than DEV_BSIZE or extends past
1873 1.130 chs * the current EOF, then fall back to buffered I/O.
1874 1.130 chs */
1875 1.130 chs
1876 1.130 chs if (len == 0 || uio->uio_offset + len > vp->v_size) {
1877 1.130 chs return;
1878 1.130 chs }
1879 1.130 chs
1880 1.130 chs /*
1881 1.130 chs * Check alignment. The file offset must be at least
1882 1.130 chs * sector-aligned. The exact constraint on memory alignment
1883 1.130 chs * is very hardware-dependent, but requiring sector-aligned
1884 1.130 chs * addresses there too is safe.
1885 1.130 chs */
1886 1.130 chs
1887 1.130 chs if (uio->uio_offset & mask || va & mask) {
1888 1.130 chs return;
1889 1.130 chs }
1890 1.130 chs error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
1891 1.130 chs uio->uio_rw);
1892 1.130 chs if (error) {
1893 1.130 chs break;
1894 1.130 chs }
1895 1.150 christos iov->iov_base = (char *)iov->iov_base + len;
1896 1.130 chs iov->iov_len -= len;
1897 1.130 chs uio->uio_offset += len;
1898 1.130 chs uio->uio_resid -= len;
1899 1.130 chs }
1900 1.130 chs }
1901 1.130 chs
1902 1.130 chs /*
1903 1.130 chs * Iodone routine for direct I/O. We don't do much here since the request is
1904 1.130 chs * always synchronous, so the caller will do most of the work after biowait().
1905 1.130 chs */
1906 1.130 chs
1907 1.130 chs static void
1908 1.130 chs genfs_dio_iodone(struct buf *bp)
1909 1.130 chs {
1910 1.130 chs int s;
1911 1.130 chs
1912 1.130 chs KASSERT((bp->b_flags & B_ASYNC) == 0);
1913 1.130 chs s = splbio();
1914 1.130 chs if ((bp->b_flags & (B_READ | B_AGE)) == B_AGE) {
1915 1.130 chs vwakeup(bp);
1916 1.130 chs }
1917 1.130 chs putiobuf(bp);
1918 1.130 chs splx(s);
1919 1.130 chs }
1920 1.130 chs
1921 1.130 chs /*
1922 1.130 chs * Process one chunk of a direct I/O request.
1923 1.130 chs */
1924 1.130 chs
1925 1.130 chs static int
1926 1.130 chs genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
1927 1.130 chs off_t off, enum uio_rw rw)
1928 1.130 chs {
1929 1.130 chs struct vm_map *map;
1930 1.130 chs struct pmap *upm, *kpm;
1931 1.130 chs size_t klen = round_page(uva + len) - trunc_page(uva);
1932 1.130 chs off_t spoff, epoff;
1933 1.130 chs vaddr_t kva, puva;
1934 1.130 chs paddr_t pa;
1935 1.130 chs vm_prot_t prot;
1936 1.130 chs int error, rv, poff, koff;
1937 1.130 chs const int pgoflags = PGO_CLEANIT | PGO_SYNCIO |
1938 1.130 chs (rw == UIO_WRITE ? PGO_FREE : 0);
1939 1.130 chs
1940 1.130 chs /*
1941 1.130 chs * For writes, verify that this range of the file already has fully
1942 1.130 chs * allocated backing store. If there are any holes, just punt and
1943 1.130 chs * make the caller take the buffered write path.
1944 1.130 chs */
1945 1.130 chs
1946 1.130 chs if (rw == UIO_WRITE) {
1947 1.130 chs daddr_t lbn, elbn, blkno;
1948 1.130 chs int bsize, bshift, run;
1949 1.130 chs
1950 1.130 chs bshift = vp->v_mount->mnt_fs_bshift;
1951 1.130 chs bsize = 1 << bshift;
1952 1.130 chs lbn = off >> bshift;
1953 1.130 chs elbn = (off + len + bsize - 1) >> bshift;
1954 1.130 chs while (lbn < elbn) {
1955 1.130 chs error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
1956 1.130 chs if (error) {
1957 1.130 chs return error;
1958 1.130 chs }
1959 1.130 chs if (blkno == (daddr_t)-1) {
1960 1.130 chs return ENOSPC;
1961 1.130 chs }
1962 1.130 chs lbn += 1 + run;
1963 1.130 chs }
1964 1.130 chs }
1965 1.130 chs
1966 1.130 chs /*
1967 1.130 chs * Flush any cached pages for parts of the file that we're about to
1968 1.130 chs * access. If we're writing, invalidate pages as well.
1969 1.130 chs */
1970 1.130 chs
1971 1.130 chs spoff = trunc_page(off);
1972 1.130 chs epoff = round_page(off + len);
1973 1.130 chs simple_lock(&vp->v_interlock);
1974 1.130 chs error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
1975 1.130 chs if (error) {
1976 1.130 chs return error;
1977 1.130 chs }
1978 1.130 chs
1979 1.130 chs /*
1980 1.130 chs * Wire the user pages and remap them into kernel memory.
1981 1.130 chs */
1982 1.130 chs
1983 1.130 chs prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
1984 1.130 chs error = uvm_vslock(vs, (void *)uva, len, prot);
1985 1.130 chs if (error) {
1986 1.130 chs return error;
1987 1.130 chs }
1988 1.130 chs
1989 1.130 chs map = &vs->vm_map;
1990 1.130 chs upm = vm_map_pmap(map);
1991 1.130 chs kpm = vm_map_pmap(kernel_map);
1992 1.130 chs kva = uvm_km_alloc(kernel_map, klen, 0,
1993 1.130 chs UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1994 1.130 chs puva = trunc_page(uva);
1995 1.130 chs for (poff = 0; poff < klen; poff += PAGE_SIZE) {
1996 1.130 chs rv = pmap_extract(upm, puva + poff, &pa);
1997 1.130 chs KASSERT(rv);
1998 1.130 chs pmap_enter(kpm, kva + poff, pa, prot, prot | PMAP_WIRED);
1999 1.130 chs }
2000 1.130 chs pmap_update(kpm);
2001 1.130 chs
2002 1.130 chs /*
2003 1.130 chs * Do the I/O.
2004 1.130 chs */
2005 1.130 chs
2006 1.130 chs koff = uva - trunc_page(uva);
2007 1.130 chs error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
2008 1.130 chs genfs_dio_iodone);
2009 1.130 chs
2010 1.130 chs /*
2011 1.130 chs * Tear down the kernel mapping.
2012 1.130 chs */
2013 1.130 chs
2014 1.130 chs pmap_remove(kpm, kva, kva + klen);
2015 1.130 chs pmap_update(kpm);
2016 1.130 chs uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
2017 1.130 chs
2018 1.130 chs /*
2019 1.130 chs * Unwire the user pages.
2020 1.130 chs */
2021 1.130 chs
2022 1.130 chs uvm_vsunlock(vs, (void *)uva, len);
2023 1.130 chs return error;
2024 1.130 chs }
2025 1.130 chs
2026 1.130 chs
2027 1.66 jdolecek static void
2028 1.66 jdolecek filt_genfsdetach(struct knote *kn)
2029 1.66 jdolecek {
2030 1.66 jdolecek struct vnode *vp = (struct vnode *)kn->kn_hook;
2031 1.66 jdolecek
2032 1.66 jdolecek /* XXXLUKEM lock the struct? */
2033 1.66 jdolecek SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
2034 1.66 jdolecek }
2035 1.66 jdolecek
2036 1.66 jdolecek static int
2037 1.66 jdolecek filt_genfsread(struct knote *kn, long hint)
2038 1.66 jdolecek {
2039 1.66 jdolecek struct vnode *vp = (struct vnode *)kn->kn_hook;
2040 1.66 jdolecek
2041 1.66 jdolecek /*
2042 1.66 jdolecek * filesystem is gone, so set the EOF flag and schedule
2043 1.66 jdolecek * the knote for deletion.
2044 1.66 jdolecek */
2045 1.66 jdolecek if (hint == NOTE_REVOKE) {
2046 1.66 jdolecek kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2047 1.66 jdolecek return (1);
2048 1.66 jdolecek }
2049 1.66 jdolecek
2050 1.66 jdolecek /* XXXLUKEM lock the struct? */
2051 1.66 jdolecek kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
2052 1.66 jdolecek return (kn->kn_data != 0);
2053 1.66 jdolecek }
2054 1.66 jdolecek
2055 1.66 jdolecek static int
2056 1.66 jdolecek filt_genfsvnode(struct knote *kn, long hint)
2057 1.66 jdolecek {
2058 1.66 jdolecek
2059 1.66 jdolecek if (kn->kn_sfflags & hint)
2060 1.66 jdolecek kn->kn_fflags |= hint;
2061 1.66 jdolecek if (hint == NOTE_REVOKE) {
2062 1.66 jdolecek kn->kn_flags |= EV_EOF;
2063 1.66 jdolecek return (1);
2064 1.66 jdolecek }
2065 1.66 jdolecek return (kn->kn_fflags != 0);
2066 1.66 jdolecek }
2067 1.66 jdolecek
2068 1.96 perry static const struct filterops genfsread_filtops =
2069 1.66 jdolecek { 1, NULL, filt_genfsdetach, filt_genfsread };
2070 1.96 perry static const struct filterops genfsvnode_filtops =
2071 1.66 jdolecek { 1, NULL, filt_genfsdetach, filt_genfsvnode };
2072 1.66 jdolecek
2073 1.66 jdolecek int
2074 1.66 jdolecek genfs_kqfilter(void *v)
2075 1.66 jdolecek {
2076 1.66 jdolecek struct vop_kqfilter_args /* {
2077 1.66 jdolecek struct vnode *a_vp;
2078 1.66 jdolecek struct knote *a_kn;
2079 1.66 jdolecek } */ *ap = v;
2080 1.66 jdolecek struct vnode *vp;
2081 1.66 jdolecek struct knote *kn;
2082 1.66 jdolecek
2083 1.66 jdolecek vp = ap->a_vp;
2084 1.66 jdolecek kn = ap->a_kn;
2085 1.66 jdolecek switch (kn->kn_filter) {
2086 1.66 jdolecek case EVFILT_READ:
2087 1.66 jdolecek kn->kn_fop = &genfsread_filtops;
2088 1.66 jdolecek break;
2089 1.66 jdolecek case EVFILT_VNODE:
2090 1.66 jdolecek kn->kn_fop = &genfsvnode_filtops;
2091 1.66 jdolecek break;
2092 1.66 jdolecek default:
2093 1.66 jdolecek return (1);
2094 1.66 jdolecek }
2095 1.66 jdolecek
2096 1.66 jdolecek kn->kn_hook = vp;
2097 1.66 jdolecek
2098 1.66 jdolecek /* XXXLUKEM lock the struct? */
2099 1.66 jdolecek SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
2100 1.66 jdolecek
2101 1.66 jdolecek return (0);
2102 1.1 mycroft }
2103 1.136 yamt
2104 1.136 yamt void
2105 1.136 yamt genfs_node_wrlock(struct vnode *vp)
2106 1.136 yamt {
2107 1.136 yamt struct genfs_node *gp = VTOG(vp);
2108 1.136 yamt
2109 1.146 ad rw_enter(&gp->g_glock, RW_WRITER);
2110 1.136 yamt }
2111 1.136 yamt
2112 1.136 yamt void
2113 1.136 yamt genfs_node_rdlock(struct vnode *vp)
2114 1.136 yamt {
2115 1.136 yamt struct genfs_node *gp = VTOG(vp);
2116 1.136 yamt
2117 1.146 ad rw_enter(&gp->g_glock, RW_READER);
2118 1.136 yamt }
2119 1.136 yamt
2120 1.136 yamt void
2121 1.136 yamt genfs_node_unlock(struct vnode *vp)
2122 1.136 yamt {
2123 1.136 yamt struct genfs_node *gp = VTOG(vp);
2124 1.136 yamt
2125 1.146 ad rw_exit(&gp->g_glock);
2126 1.136 yamt }
2127