genfs_vnops.c revision 1.156 1 /* $NetBSD: genfs_vnops.c,v 1.156 2007/07/29 12:15:46 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.156 2007/07/29 12:15:46 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_flag & VALIASED) {
245 /*
246 * If a vgone (or vclean) is already in progress,
247 * wait until it is done and return.
248 */
249 if (vp->v_flag & VXLOCK) {
250 vp->v_flag |= VXWANT;
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_flag |= VXLOCK;
260 simple_unlock(&vp->v_interlock);
261 while (vp->v_flag & VALIASED) {
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_flag &= ~VXLOCK;
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_flag & VONWORKLST) == 0) {
523 vn_syncer_add_to_worklist(vp, filedelay);
524 }
525 if ((vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP) {
526 vp->v_flag |= VWRITEMAPDIRTY;
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_flag & VONWORKLST) != 0 ||
1088 (vp->v_flag & VWRITEMAPDIRTY) == 0);
1089 if (uobj->uo_npages == 0) {
1090 s = splbio();
1091 if (vp->v_flag & VONWORKLST) {
1092 vp->v_flag &= ~VWRITEMAPDIRTY;
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_flag & VONWORKLST) == 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_flag & VONWORKLST) != 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 KASSERT(!pagedaemon);
1221 if (by_list) {
1222 TAILQ_INSERT_BEFORE(pg, &curmp, listq);
1223 UVMHIST_LOG(ubchist, "curmp next %p",
1224 TAILQ_NEXT(&curmp, listq), 0,0,0);
1225 }
1226 if (yld) {
1227 simple_unlock(slock);
1228 preempt();
1229 simple_lock(slock);
1230 } else {
1231 pg->flags |= PG_WANTED;
1232 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
1233 simple_lock(slock);
1234 }
1235 if (by_list) {
1236 UVMHIST_LOG(ubchist, "after next %p",
1237 TAILQ_NEXT(&curmp, listq), 0,0,0);
1238 pg = TAILQ_NEXT(&curmp, listq);
1239 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1240 } else {
1241 pg = uvm_pagelookup(uobj, off);
1242 }
1243 continue;
1244 }
1245
1246 /*
1247 * if we're freeing, remove all mappings of the page now.
1248 * if we're cleaning, check if the page is needs to be cleaned.
1249 */
1250
1251 if (flags & PGO_FREE) {
1252 pmap_page_protect(pg, VM_PROT_NONE);
1253 } else if (flags & PGO_CLEANIT) {
1254
1255 /*
1256 * if we still have some hope to pull this vnode off
1257 * from the syncer queue, write-protect the page.
1258 */
1259
1260 if (cleanall && wasclean &&
1261 gp->g_dirtygen == dirtygen) {
1262
1263 /*
1264 * uobj pages get wired only by uvm_fault
1265 * where uobj is locked.
1266 */
1267
1268 if (pg->wire_count == 0) {
1269 pmap_page_protect(pg,
1270 VM_PROT_READ|VM_PROT_EXECUTE);
1271 } else {
1272 cleanall = false;
1273 }
1274 }
1275 }
1276
1277 if (flags & PGO_CLEANIT) {
1278 needs_clean = pmap_clear_modify(pg) ||
1279 (pg->flags & PG_CLEAN) == 0;
1280 pg->flags |= PG_CLEAN;
1281 } else {
1282 needs_clean = false;
1283 }
1284
1285 /*
1286 * if we're cleaning, build a cluster.
1287 * the cluster will consist of pages which are currently dirty,
1288 * but they will be returned to us marked clean.
1289 * if not cleaning, just operate on the one page.
1290 */
1291
1292 if (needs_clean) {
1293 KDASSERT((vp->v_flag & VONWORKLST));
1294 wasclean = false;
1295 memset(pgs, 0, sizeof(pgs));
1296 pg->flags |= PG_BUSY;
1297 UVM_PAGE_OWN(pg, "genfs_putpages");
1298
1299 /*
1300 * first look backward.
1301 */
1302
1303 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1304 nback = npages;
1305 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1306 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1307 if (nback) {
1308 memmove(&pgs[0], &pgs[npages - nback],
1309 nback * sizeof(pgs[0]));
1310 if (npages - nback < nback)
1311 memset(&pgs[nback], 0,
1312 (npages - nback) * sizeof(pgs[0]));
1313 else
1314 memset(&pgs[npages - nback], 0,
1315 nback * sizeof(pgs[0]));
1316 }
1317
1318 /*
1319 * then plug in our page of interest.
1320 */
1321
1322 pgs[nback] = pg;
1323
1324 /*
1325 * then look forward to fill in the remaining space in
1326 * the array of pages.
1327 */
1328
1329 npages = maxpages - nback - 1;
1330 uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1331 &pgs[nback + 1],
1332 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1333 npages += nback + 1;
1334 } else {
1335 pgs[0] = pg;
1336 npages = 1;
1337 nback = 0;
1338 }
1339
1340 /*
1341 * apply FREE or DEACTIVATE options if requested.
1342 */
1343
1344 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1345 uvm_lock_pageq();
1346 }
1347 for (i = 0; i < npages; i++) {
1348 tpg = pgs[i];
1349 KASSERT(tpg->uobject == uobj);
1350 if (by_list && tpg == TAILQ_NEXT(pg, listq))
1351 pg = tpg;
1352 if (tpg->offset < startoff || tpg->offset >= endoff)
1353 continue;
1354 if (flags & PGO_DEACTIVATE && tpg->wire_count == 0) {
1355 (void) pmap_clear_reference(tpg);
1356 uvm_pagedeactivate(tpg);
1357 } else if (flags & PGO_FREE) {
1358 pmap_page_protect(tpg, VM_PROT_NONE);
1359 if (tpg->flags & PG_BUSY) {
1360 tpg->flags |= freeflag;
1361 if (pagedaemon) {
1362 uvmexp.paging++;
1363 uvm_pagedequeue(tpg);
1364 }
1365 } else {
1366
1367 /*
1368 * ``page is not busy''
1369 * implies that npages is 1
1370 * and needs_clean is false.
1371 */
1372
1373 nextpg = TAILQ_NEXT(tpg, listq);
1374 uvm_pagefree(tpg);
1375 if (pagedaemon)
1376 uvmexp.pdfreed++;
1377 }
1378 }
1379 }
1380 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1381 uvm_unlock_pageq();
1382 }
1383 if (needs_clean) {
1384 modified = true;
1385
1386 /*
1387 * start the i/o. if we're traversing by list,
1388 * keep our place in the list with a marker page.
1389 */
1390
1391 if (by_list) {
1392 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1393 listq);
1394 }
1395 simple_unlock(slock);
1396 error = GOP_WRITE(vp, pgs, npages, flags);
1397 simple_lock(slock);
1398 if (by_list) {
1399 pg = TAILQ_NEXT(&curmp, listq);
1400 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1401 }
1402 if (error) {
1403 break;
1404 }
1405 if (by_list) {
1406 continue;
1407 }
1408 }
1409
1410 /*
1411 * find the next page and continue if there was no error.
1412 */
1413
1414 if (by_list) {
1415 if (nextpg) {
1416 pg = nextpg;
1417 nextpg = NULL;
1418 } else {
1419 pg = TAILQ_NEXT(pg, listq);
1420 }
1421 } else {
1422 off += (npages - nback) << PAGE_SHIFT;
1423 if (off < endoff) {
1424 pg = uvm_pagelookup(uobj, off);
1425 }
1426 }
1427 }
1428 if (by_list) {
1429 TAILQ_REMOVE(&uobj->memq, &endmp, listq);
1430 uvm_lwp_rele(l);
1431 }
1432
1433 if (modified && (vp->v_flag & VWRITEMAPDIRTY) != 0 &&
1434 (vp->v_type != VBLK ||
1435 (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
1436 GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
1437 }
1438
1439 /*
1440 * if we're cleaning and there was nothing to clean,
1441 * take us off the syncer list. if we started any i/o
1442 * and we're doing sync i/o, wait for all writes to finish.
1443 */
1444
1445 s = splbio();
1446 if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
1447 (vp->v_flag & VONWORKLST) != 0) {
1448 vp->v_flag &= ~VWRITEMAPDIRTY;
1449 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)
1450 vn_syncer_remove_from_worklist(vp);
1451 }
1452 splx(s);
1453
1454 #if !defined(DEBUG)
1455 skip_scan:
1456 #endif /* !defined(DEBUG) */
1457 if (!wasclean && !async) {
1458 s = splbio();
1459 /*
1460 * XXX - we want simple_unlock(&global_v_numoutput_slock);
1461 * but the slot in ltsleep() is taken!
1462 * XXX - try to recover from missed wakeups with a timeout..
1463 * must think of something better.
1464 */
1465 while (vp->v_numoutput != 0) {
1466 vp->v_flag |= VBWAIT;
1467 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, false,
1468 "genput2", hz);
1469 simple_lock(slock);
1470 }
1471 splx(s);
1472 }
1473 simple_unlock(slock);
1474
1475 if (has_trans)
1476 fstrans_done(vp->v_mount);
1477
1478 return (error);
1479 }
1480
1481 int
1482 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1483 {
1484 off_t off;
1485 vaddr_t kva;
1486 size_t len;
1487 int error;
1488 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1489
1490 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1491 vp, pgs, npages, flags);
1492
1493 off = pgs[0]->offset;
1494 kva = uvm_pagermapin(pgs, npages,
1495 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1496 len = npages << PAGE_SHIFT;
1497
1498 error = genfs_do_io(vp, off, kva, len, flags, UIO_WRITE,
1499 uvm_aio_biodone);
1500
1501 return error;
1502 }
1503
1504 /*
1505 * Backend routine for doing I/O to vnode pages. Pages are already locked
1506 * and mapped into kernel memory. Here we just look up the underlying
1507 * device block addresses and call the strategy routine.
1508 */
1509
1510 static int
1511 genfs_do_io(struct vnode *vp, off_t off, vaddr_t kva, size_t len, int flags,
1512 enum uio_rw rw, void (*iodone)(struct buf *))
1513 {
1514 int s, error, run;
1515 int fs_bshift, dev_bshift;
1516 off_t eof, offset, startoffset;
1517 size_t bytes, iobytes, skipbytes;
1518 daddr_t lbn, blkno;
1519 struct buf *mbp, *bp;
1520 struct vnode *devvp;
1521 bool async = (flags & PGO_SYNCIO) == 0;
1522 bool write = rw == UIO_WRITE;
1523 int brw = write ? B_WRITE : B_READ;
1524 UVMHIST_FUNC(__func__); UVMHIST_CALLED(ubchist);
1525
1526 UVMHIST_LOG(ubchist, "vp %p kva %p len 0x%x flags 0x%x",
1527 vp, kva, len, flags);
1528
1529 KASSERT(vp->v_size <= vp->v_writesize);
1530 GOP_SIZE(vp, vp->v_writesize, &eof, 0);
1531 if (vp->v_type != VBLK) {
1532 fs_bshift = vp->v_mount->mnt_fs_bshift;
1533 dev_bshift = vp->v_mount->mnt_dev_bshift;
1534 } else {
1535 fs_bshift = DEV_BSHIFT;
1536 dev_bshift = DEV_BSHIFT;
1537 }
1538 error = 0;
1539 startoffset = off;
1540 bytes = MIN(len, eof - startoffset);
1541 skipbytes = 0;
1542 KASSERT(bytes != 0);
1543
1544 if (write) {
1545 s = splbio();
1546 simple_lock(&global_v_numoutput_slock);
1547 vp->v_numoutput += 2;
1548 simple_unlock(&global_v_numoutput_slock);
1549 splx(s);
1550 }
1551 mbp = getiobuf();
1552 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1553 vp, mbp, vp->v_numoutput, bytes);
1554 mbp->b_bufsize = len;
1555 mbp->b_data = (void *)kva;
1556 mbp->b_resid = mbp->b_bcount = bytes;
1557 mbp->b_flags = B_BUSY | brw | B_AGE | (async ? (B_CALL | B_ASYNC) : 0);
1558 mbp->b_iodone = iodone;
1559 mbp->b_vp = vp;
1560 if (curlwp == uvm.pagedaemon_lwp)
1561 BIO_SETPRIO(mbp, BPRIO_TIMELIMITED);
1562 else if (async)
1563 BIO_SETPRIO(mbp, BPRIO_TIMENONCRITICAL);
1564 else
1565 BIO_SETPRIO(mbp, BPRIO_TIMECRITICAL);
1566
1567 bp = NULL;
1568 for (offset = startoffset;
1569 bytes > 0;
1570 offset += iobytes, bytes -= iobytes) {
1571 lbn = offset >> fs_bshift;
1572 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1573 if (error) {
1574 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1575 skipbytes += bytes;
1576 bytes = 0;
1577 break;
1578 }
1579
1580 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1581 bytes);
1582 if (blkno == (daddr_t)-1) {
1583 if (!write) {
1584 memset((char *)kva + (offset - startoffset), 0,
1585 iobytes);
1586 }
1587 skipbytes += iobytes;
1588 continue;
1589 }
1590
1591 /* if it's really one i/o, don't make a second buf */
1592 if (offset == startoffset && iobytes == bytes) {
1593 bp = mbp;
1594 } else {
1595 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1596 vp, bp, vp->v_numoutput, 0);
1597 bp = getiobuf();
1598 nestiobuf_setup(mbp, bp, offset - startoffset, iobytes);
1599 }
1600 bp->b_lblkno = 0;
1601
1602 /* adjust physical blkno for partial blocks */
1603 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1604 dev_bshift);
1605 UVMHIST_LOG(ubchist,
1606 "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1607 vp, offset, bp->b_bcount, bp->b_blkno);
1608
1609 VOP_STRATEGY(devvp, bp);
1610 }
1611 if (skipbytes) {
1612 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1613 }
1614 nestiobuf_done(mbp, skipbytes, error);
1615 if (async) {
1616 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1617 return (0);
1618 }
1619 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1620 error = biowait(mbp);
1621 s = splbio();
1622 (*iodone)(mbp);
1623 splx(s);
1624 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1625 return (error);
1626 }
1627
1628 /*
1629 * VOP_PUTPAGES() for vnodes which never have pages.
1630 */
1631
1632 int
1633 genfs_null_putpages(void *v)
1634 {
1635 struct vop_putpages_args /* {
1636 struct vnode *a_vp;
1637 voff_t a_offlo;
1638 voff_t a_offhi;
1639 int a_flags;
1640 } */ *ap = v;
1641 struct vnode *vp = ap->a_vp;
1642
1643 KASSERT(vp->v_uobj.uo_npages == 0);
1644 simple_unlock(&vp->v_interlock);
1645 return (0);
1646 }
1647
1648 void
1649 genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
1650 {
1651 struct genfs_node *gp = VTOG(vp);
1652
1653 rw_init(&gp->g_glock);
1654 gp->g_op = ops;
1655 }
1656
1657 void
1658 genfs_node_destroy(struct vnode *vp)
1659 {
1660 struct genfs_node *gp = VTOG(vp);
1661
1662 rw_destroy(&gp->g_glock);
1663 }
1664
1665 void
1666 genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
1667 {
1668 int bsize;
1669
1670 bsize = 1 << vp->v_mount->mnt_fs_bshift;
1671 *eobp = (size + bsize - 1) & ~(bsize - 1);
1672 }
1673
1674 int
1675 genfs_compat_getpages(void *v)
1676 {
1677 struct vop_getpages_args /* {
1678 struct vnode *a_vp;
1679 voff_t a_offset;
1680 struct vm_page **a_m;
1681 int *a_count;
1682 int a_centeridx;
1683 vm_prot_t a_access_type;
1684 int a_advice;
1685 int a_flags;
1686 } */ *ap = v;
1687
1688 off_t origoffset;
1689 struct vnode *vp = ap->a_vp;
1690 struct uvm_object *uobj = &vp->v_uobj;
1691 struct vm_page *pg, **pgs;
1692 vaddr_t kva;
1693 int i, error, orignpages, npages;
1694 struct iovec iov;
1695 struct uio uio;
1696 kauth_cred_t cred = curlwp->l_cred;
1697 bool write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1698
1699 error = 0;
1700 origoffset = ap->a_offset;
1701 orignpages = *ap->a_count;
1702 pgs = ap->a_m;
1703
1704 if (write && (vp->v_flag & VONWORKLST) == 0) {
1705 vn_syncer_add_to_worklist(vp, filedelay);
1706 }
1707 if (ap->a_flags & PGO_LOCKED) {
1708 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1709 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
1710
1711 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1712 }
1713 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1714 simple_unlock(&uobj->vmobjlock);
1715 return (EINVAL);
1716 }
1717 if ((ap->a_flags & PGO_SYNCIO) == 0) {
1718 simple_unlock(&uobj->vmobjlock);
1719 return 0;
1720 }
1721 npages = orignpages;
1722 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1723 simple_unlock(&uobj->vmobjlock);
1724 kva = uvm_pagermapin(pgs, npages,
1725 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1726 for (i = 0; i < npages; i++) {
1727 pg = pgs[i];
1728 if ((pg->flags & PG_FAKE) == 0) {
1729 continue;
1730 }
1731 iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1732 iov.iov_len = PAGE_SIZE;
1733 uio.uio_iov = &iov;
1734 uio.uio_iovcnt = 1;
1735 uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1736 uio.uio_rw = UIO_READ;
1737 uio.uio_resid = PAGE_SIZE;
1738 UIO_SETUP_SYSSPACE(&uio);
1739 /* XXX vn_lock */
1740 error = VOP_READ(vp, &uio, 0, cred);
1741 if (error) {
1742 break;
1743 }
1744 if (uio.uio_resid) {
1745 memset(iov.iov_base, 0, uio.uio_resid);
1746 }
1747 }
1748 uvm_pagermapout(kva, npages);
1749 simple_lock(&uobj->vmobjlock);
1750 uvm_lock_pageq();
1751 for (i = 0; i < npages; i++) {
1752 pg = pgs[i];
1753 if (error && (pg->flags & PG_FAKE) != 0) {
1754 pg->flags |= PG_RELEASED;
1755 } else {
1756 pmap_clear_modify(pg);
1757 uvm_pageactivate(pg);
1758 }
1759 }
1760 if (error) {
1761 uvm_page_unbusy(pgs, npages);
1762 }
1763 uvm_unlock_pageq();
1764 simple_unlock(&uobj->vmobjlock);
1765 return (error);
1766 }
1767
1768 int
1769 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1770 int flags)
1771 {
1772 off_t offset;
1773 struct iovec iov;
1774 struct uio uio;
1775 kauth_cred_t cred = curlwp->l_cred;
1776 struct buf *bp;
1777 vaddr_t kva;
1778 int s, error;
1779
1780 offset = pgs[0]->offset;
1781 kva = uvm_pagermapin(pgs, npages,
1782 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1783
1784 iov.iov_base = (void *)kva;
1785 iov.iov_len = npages << PAGE_SHIFT;
1786 uio.uio_iov = &iov;
1787 uio.uio_iovcnt = 1;
1788 uio.uio_offset = offset;
1789 uio.uio_rw = UIO_WRITE;
1790 uio.uio_resid = npages << PAGE_SHIFT;
1791 UIO_SETUP_SYSSPACE(&uio);
1792 /* XXX vn_lock */
1793 error = VOP_WRITE(vp, &uio, 0, cred);
1794
1795 s = splbio();
1796 V_INCR_NUMOUTPUT(vp);
1797 splx(s);
1798
1799 bp = getiobuf();
1800 bp->b_flags = B_BUSY | B_WRITE | B_AGE;
1801 bp->b_vp = vp;
1802 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1803 bp->b_data = (char *)kva;
1804 bp->b_bcount = npages << PAGE_SHIFT;
1805 bp->b_bufsize = npages << PAGE_SHIFT;
1806 bp->b_resid = 0;
1807 bp->b_error = error;
1808 uvm_aio_aiodone(bp);
1809 return (error);
1810 }
1811
1812 /*
1813 * Process a uio using direct I/O. If we reach a part of the request
1814 * which cannot be processed in this fashion for some reason, just return.
1815 * The caller must handle some additional part of the request using
1816 * buffered I/O before trying direct I/O again.
1817 */
1818
1819 void
1820 genfs_directio(struct vnode *vp, struct uio *uio, int ioflag)
1821 {
1822 struct vmspace *vs;
1823 struct iovec *iov;
1824 vaddr_t va;
1825 size_t len;
1826 const int mask = DEV_BSIZE - 1;
1827 int error;
1828
1829 /*
1830 * We only support direct I/O to user space for now.
1831 */
1832
1833 if (VMSPACE_IS_KERNEL_P(uio->uio_vmspace)) {
1834 return;
1835 }
1836
1837 /*
1838 * If the vnode is mapped, we would need to get the getpages lock
1839 * to stabilize the bmap, but then we would get into trouble whil e
1840 * locking the pages if the pages belong to this same vnode (or a
1841 * multi-vnode cascade to the same effect). Just fall back to
1842 * buffered I/O if the vnode is mapped to avoid this mess.
1843 */
1844
1845 if (vp->v_flag & VMAPPED) {
1846 return;
1847 }
1848
1849 /*
1850 * Do as much of the uio as possible with direct I/O.
1851 */
1852
1853 vs = uio->uio_vmspace;
1854 while (uio->uio_resid) {
1855 iov = uio->uio_iov;
1856 if (iov->iov_len == 0) {
1857 uio->uio_iov++;
1858 uio->uio_iovcnt--;
1859 continue;
1860 }
1861 va = (vaddr_t)iov->iov_base;
1862 len = MIN(iov->iov_len, genfs_maxdio);
1863 len &= ~mask;
1864
1865 /*
1866 * If the next chunk is smaller than DEV_BSIZE or extends past
1867 * the current EOF, then fall back to buffered I/O.
1868 */
1869
1870 if (len == 0 || uio->uio_offset + len > vp->v_size) {
1871 return;
1872 }
1873
1874 /*
1875 * Check alignment. The file offset must be at least
1876 * sector-aligned. The exact constraint on memory alignment
1877 * is very hardware-dependent, but requiring sector-aligned
1878 * addresses there too is safe.
1879 */
1880
1881 if (uio->uio_offset & mask || va & mask) {
1882 return;
1883 }
1884 error = genfs_do_directio(vs, va, len, vp, uio->uio_offset,
1885 uio->uio_rw);
1886 if (error) {
1887 break;
1888 }
1889 iov->iov_base = (char *)iov->iov_base + len;
1890 iov->iov_len -= len;
1891 uio->uio_offset += len;
1892 uio->uio_resid -= len;
1893 }
1894 }
1895
1896 /*
1897 * Iodone routine for direct I/O. We don't do much here since the request is
1898 * always synchronous, so the caller will do most of the work after biowait().
1899 */
1900
1901 static void
1902 genfs_dio_iodone(struct buf *bp)
1903 {
1904 int s;
1905
1906 KASSERT((bp->b_flags & B_ASYNC) == 0);
1907 s = splbio();
1908 if ((bp->b_flags & (B_READ | B_AGE)) == B_AGE) {
1909 vwakeup(bp);
1910 }
1911 putiobuf(bp);
1912 splx(s);
1913 }
1914
1915 /*
1916 * Process one chunk of a direct I/O request.
1917 */
1918
1919 static int
1920 genfs_do_directio(struct vmspace *vs, vaddr_t uva, size_t len, struct vnode *vp,
1921 off_t off, enum uio_rw rw)
1922 {
1923 struct vm_map *map;
1924 struct pmap *upm, *kpm;
1925 size_t klen = round_page(uva + len) - trunc_page(uva);
1926 off_t spoff, epoff;
1927 vaddr_t kva, puva;
1928 paddr_t pa;
1929 vm_prot_t prot;
1930 int error, rv, poff, koff;
1931 const int pgoflags = PGO_CLEANIT | PGO_SYNCIO |
1932 (rw == UIO_WRITE ? PGO_FREE : 0);
1933
1934 /*
1935 * For writes, verify that this range of the file already has fully
1936 * allocated backing store. If there are any holes, just punt and
1937 * make the caller take the buffered write path.
1938 */
1939
1940 if (rw == UIO_WRITE) {
1941 daddr_t lbn, elbn, blkno;
1942 int bsize, bshift, run;
1943
1944 bshift = vp->v_mount->mnt_fs_bshift;
1945 bsize = 1 << bshift;
1946 lbn = off >> bshift;
1947 elbn = (off + len + bsize - 1) >> bshift;
1948 while (lbn < elbn) {
1949 error = VOP_BMAP(vp, lbn, NULL, &blkno, &run);
1950 if (error) {
1951 return error;
1952 }
1953 if (blkno == (daddr_t)-1) {
1954 return ENOSPC;
1955 }
1956 lbn += 1 + run;
1957 }
1958 }
1959
1960 /*
1961 * Flush any cached pages for parts of the file that we're about to
1962 * access. If we're writing, invalidate pages as well.
1963 */
1964
1965 spoff = trunc_page(off);
1966 epoff = round_page(off + len);
1967 simple_lock(&vp->v_interlock);
1968 error = VOP_PUTPAGES(vp, spoff, epoff, pgoflags);
1969 if (error) {
1970 return error;
1971 }
1972
1973 /*
1974 * Wire the user pages and remap them into kernel memory.
1975 */
1976
1977 prot = rw == UIO_READ ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
1978 error = uvm_vslock(vs, (void *)uva, len, prot);
1979 if (error) {
1980 return error;
1981 }
1982
1983 map = &vs->vm_map;
1984 upm = vm_map_pmap(map);
1985 kpm = vm_map_pmap(kernel_map);
1986 kva = uvm_km_alloc(kernel_map, klen, 0,
1987 UVM_KMF_VAONLY | UVM_KMF_WAITVA);
1988 puva = trunc_page(uva);
1989 for (poff = 0; poff < klen; poff += PAGE_SIZE) {
1990 rv = pmap_extract(upm, puva + poff, &pa);
1991 KASSERT(rv);
1992 pmap_enter(kpm, kva + poff, pa, prot, prot | PMAP_WIRED);
1993 }
1994 pmap_update(kpm);
1995
1996 /*
1997 * Do the I/O.
1998 */
1999
2000 koff = uva - trunc_page(uva);
2001 error = genfs_do_io(vp, off, kva + koff, len, PGO_SYNCIO, rw,
2002 genfs_dio_iodone);
2003
2004 /*
2005 * Tear down the kernel mapping.
2006 */
2007
2008 pmap_remove(kpm, kva, kva + klen);
2009 pmap_update(kpm);
2010 uvm_km_free(kernel_map, kva, klen, UVM_KMF_VAONLY);
2011
2012 /*
2013 * Unwire the user pages.
2014 */
2015
2016 uvm_vsunlock(vs, (void *)uva, len);
2017 return error;
2018 }
2019
2020
2021 static void
2022 filt_genfsdetach(struct knote *kn)
2023 {
2024 struct vnode *vp = (struct vnode *)kn->kn_hook;
2025
2026 /* XXXLUKEM lock the struct? */
2027 SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
2028 }
2029
2030 static int
2031 filt_genfsread(struct knote *kn, long hint)
2032 {
2033 struct vnode *vp = (struct vnode *)kn->kn_hook;
2034
2035 /*
2036 * filesystem is gone, so set the EOF flag and schedule
2037 * the knote for deletion.
2038 */
2039 if (hint == NOTE_REVOKE) {
2040 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2041 return (1);
2042 }
2043
2044 /* XXXLUKEM lock the struct? */
2045 kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
2046 return (kn->kn_data != 0);
2047 }
2048
2049 static int
2050 filt_genfsvnode(struct knote *kn, long hint)
2051 {
2052
2053 if (kn->kn_sfflags & hint)
2054 kn->kn_fflags |= hint;
2055 if (hint == NOTE_REVOKE) {
2056 kn->kn_flags |= EV_EOF;
2057 return (1);
2058 }
2059 return (kn->kn_fflags != 0);
2060 }
2061
2062 static const struct filterops genfsread_filtops =
2063 { 1, NULL, filt_genfsdetach, filt_genfsread };
2064 static const struct filterops genfsvnode_filtops =
2065 { 1, NULL, filt_genfsdetach, filt_genfsvnode };
2066
2067 int
2068 genfs_kqfilter(void *v)
2069 {
2070 struct vop_kqfilter_args /* {
2071 struct vnode *a_vp;
2072 struct knote *a_kn;
2073 } */ *ap = v;
2074 struct vnode *vp;
2075 struct knote *kn;
2076
2077 vp = ap->a_vp;
2078 kn = ap->a_kn;
2079 switch (kn->kn_filter) {
2080 case EVFILT_READ:
2081 kn->kn_fop = &genfsread_filtops;
2082 break;
2083 case EVFILT_VNODE:
2084 kn->kn_fop = &genfsvnode_filtops;
2085 break;
2086 default:
2087 return (1);
2088 }
2089
2090 kn->kn_hook = vp;
2091
2092 /* XXXLUKEM lock the struct? */
2093 SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
2094
2095 return (0);
2096 }
2097
2098 void
2099 genfs_node_wrlock(struct vnode *vp)
2100 {
2101 struct genfs_node *gp = VTOG(vp);
2102
2103 rw_enter(&gp->g_glock, RW_WRITER);
2104 }
2105
2106 void
2107 genfs_node_rdlock(struct vnode *vp)
2108 {
2109 struct genfs_node *gp = VTOG(vp);
2110
2111 rw_enter(&gp->g_glock, RW_READER);
2112 }
2113
2114 void
2115 genfs_node_unlock(struct vnode *vp)
2116 {
2117 struct genfs_node *gp = VTOG(vp);
2118
2119 rw_exit(&gp->g_glock);
2120 }
2121