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