genfs_vnops.c revision 1.79 1 /* $NetBSD: genfs_vnops.c,v 1.79 2003/06/28 14:22:02 darrenr 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: genfs_vnops.c,v 1.79 2003/06/28 14:22:02 darrenr Exp $");
39
40 #include "opt_nfsserver.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/mount.h>
47 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/fcntl.h>
50 #include <sys/malloc.h>
51 #include <sys/poll.h>
52 #include <sys/mman.h>
53 #include <sys/file.h>
54
55 #include <miscfs/genfs/genfs.h>
56 #include <miscfs/genfs/genfs_node.h>
57 #include <miscfs/specfs/specdev.h>
58
59 #include <uvm/uvm.h>
60 #include <uvm/uvm_pager.h>
61
62 #ifdef NFSSERVER
63 #include <nfs/rpcv2.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/nfs.h>
66 #include <nfs/nqnfs.h>
67 #include <nfs/nfs_var.h>
68 #endif
69
70 static __inline void genfs_rel_pages(struct vm_page **, int);
71 static void filt_genfsdetach(struct knote *);
72 static int filt_genfsread(struct knote *, long);
73 static int filt_genfsvnode(struct knote *, long);
74
75
76 #define MAX_READ_AHEAD 16 /* XXXUBC 16 */
77 int genfs_rapages = MAX_READ_AHEAD; /* # of pages in each chunk of readahead */
78 int genfs_racount = 2; /* # of page chunks to readahead */
79 int genfs_raskip = 2; /* # of busy page chunks allowed to skip */
80
81 int
82 genfs_poll(void *v)
83 {
84 struct vop_poll_args /* {
85 struct vnode *a_vp;
86 int a_events;
87 struct lwp *a_l;
88 } */ *ap = v;
89
90 return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
91 }
92
93 int
94 genfs_fsync(void *v)
95 {
96 struct vop_fsync_args /* {
97 struct vnode *a_vp;
98 struct ucred *a_cred;
99 int a_flags;
100 off_t offlo;
101 off_t offhi;
102 struct lwp *a_l;
103 } */ *ap = v;
104 struct vnode *vp = ap->a_vp;
105 int wait;
106
107 wait = (ap->a_flags & FSYNC_WAIT) != 0;
108 vflushbuf(vp, wait);
109 if ((ap->a_flags & FSYNC_DATAONLY) != 0)
110 return (0);
111 else
112 return (VOP_UPDATE(vp, NULL, NULL, wait ? UPDATE_WAIT : 0));
113 }
114
115 int
116 genfs_seek(void *v)
117 {
118 struct vop_seek_args /* {
119 struct vnode *a_vp;
120 off_t a_oldoff;
121 off_t a_newoff;
122 struct ucred *a_ucred;
123 } */ *ap = v;
124
125 if (ap->a_newoff < 0)
126 return (EINVAL);
127
128 return (0);
129 }
130
131 int
132 genfs_abortop(void *v)
133 {
134 struct vop_abortop_args /* {
135 struct vnode *a_dvp;
136 struct componentname *a_cnp;
137 } */ *ap = v;
138
139 if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
140 PNBUF_PUT(ap->a_cnp->cn_pnbuf);
141 return (0);
142 }
143
144 int
145 genfs_fcntl(void *v)
146 {
147 struct vop_fcntl_args /* {
148 struct vnode *a_vp;
149 u_int a_command;
150 caddr_t a_data;
151 int a_fflag;
152 struct ucred *a_cred;
153 struct lwp *a_l;
154 } */ *ap = v;
155
156 if (ap->a_command == F_SETFL)
157 return (0);
158 else
159 return (EOPNOTSUPP);
160 }
161
162 /*ARGSUSED*/
163 int
164 genfs_badop(void *v)
165 {
166
167 panic("genfs: bad op");
168 }
169
170 /*ARGSUSED*/
171 int
172 genfs_nullop(void *v)
173 {
174
175 return (0);
176 }
177
178 /*ARGSUSED*/
179 int
180 genfs_einval(void *v)
181 {
182
183 return (EINVAL);
184 }
185
186 /*
187 * Called when an fs doesn't support a particular vop.
188 * This takes care to vrele, vput, or vunlock passed in vnodes.
189 */
190 int
191 genfs_eopnotsupp(void *v)
192 {
193 struct vop_generic_args /*
194 struct vnodeop_desc *a_desc;
195 / * other random data follows, presumably * /
196 } */ *ap = v;
197 struct vnodeop_desc *desc = ap->a_desc;
198 struct vnode *vp, *vp_last = NULL;
199 int flags, i, j, offset;
200
201 flags = desc->vdesc_flags;
202 for (i = 0; i < VDESC_MAX_VPS; flags >>=1, i++) {
203 if ((offset = desc->vdesc_vp_offsets[i]) == VDESC_NO_OFFSET)
204 break; /* stop at end of list */
205 if ((j = flags & VDESC_VP0_WILLPUT)) {
206 vp = *VOPARG_OFFSETTO(struct vnode **, offset, ap);
207
208 /* Skip if NULL */
209 if (!vp)
210 continue;
211
212 switch (j) {
213 case VDESC_VP0_WILLPUT:
214 /* Check for dvp == vp cases */
215 if (vp == vp_last)
216 vrele(vp);
217 else {
218 vput(vp);
219 vp_last = vp;
220 }
221 break;
222 case VDESC_VP0_WILLUNLOCK:
223 VOP_UNLOCK(vp, 0);
224 break;
225 case VDESC_VP0_WILLRELE:
226 vrele(vp);
227 break;
228 }
229 }
230 }
231
232 return (EOPNOTSUPP);
233 }
234
235 /*ARGSUSED*/
236 int
237 genfs_ebadf(void *v)
238 {
239
240 return (EBADF);
241 }
242
243 /* ARGSUSED */
244 int
245 genfs_enoioctl(void *v)
246 {
247
248 return (EPASSTHROUGH);
249 }
250
251
252 /*
253 * Eliminate all activity associated with the requested vnode
254 * and with all vnodes aliased to the requested vnode.
255 */
256 int
257 genfs_revoke(void *v)
258 {
259 struct vop_revoke_args /* {
260 struct vnode *a_vp;
261 int a_flags;
262 } */ *ap = v;
263 struct vnode *vp, *vq;
264 struct lwp *l = curlwp; /* XXX */
265
266 #ifdef DIAGNOSTIC
267 if ((ap->a_flags & REVOKEALL) == 0)
268 panic("genfs_revoke: not revokeall");
269 #endif
270
271 vp = ap->a_vp;
272 simple_lock(&vp->v_interlock);
273
274 if (vp->v_flag & VALIASED) {
275 /*
276 * If a vgone (or vclean) is already in progress,
277 * wait until it is done and return.
278 */
279 if (vp->v_flag & VXLOCK) {
280 vp->v_flag |= VXWANT;
281 simple_unlock(&vp->v_interlock);
282 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
283 return (0);
284 }
285 /*
286 * Ensure that vp will not be vgone'd while we
287 * are eliminating its aliases.
288 */
289 vp->v_flag |= VXLOCK;
290 simple_unlock(&vp->v_interlock);
291 while (vp->v_flag & VALIASED) {
292 simple_lock(&spechash_slock);
293 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
294 if (vq->v_rdev != vp->v_rdev ||
295 vq->v_type != vp->v_type || vp == vq)
296 continue;
297 simple_unlock(&spechash_slock);
298 vgone(vq);
299 break;
300 }
301 if (vq == NULLVP)
302 simple_unlock(&spechash_slock);
303 }
304 /*
305 * Remove the lock so that vgone below will
306 * really eliminate the vnode after which time
307 * vgone will awaken any sleepers.
308 */
309 simple_lock(&vp->v_interlock);
310 vp->v_flag &= ~VXLOCK;
311 }
312 vgonel(vp, l);
313 return (0);
314 }
315
316 /*
317 * Lock the node.
318 */
319 int
320 genfs_lock(void *v)
321 {
322 struct vop_lock_args /* {
323 struct vnode *a_vp;
324 int a_flags;
325 } */ *ap = v;
326 struct vnode *vp = ap->a_vp;
327
328 return (lockmgr(&vp->v_lock, ap->a_flags, &vp->v_interlock));
329 }
330
331 /*
332 * Unlock the node.
333 */
334 int
335 genfs_unlock(void *v)
336 {
337 struct vop_unlock_args /* {
338 struct vnode *a_vp;
339 int a_flags;
340 } */ *ap = v;
341 struct vnode *vp = ap->a_vp;
342
343 return (lockmgr(&vp->v_lock, ap->a_flags | LK_RELEASE,
344 &vp->v_interlock));
345 }
346
347 /*
348 * Return whether or not the node is locked.
349 */
350 int
351 genfs_islocked(void *v)
352 {
353 struct vop_islocked_args /* {
354 struct vnode *a_vp;
355 } */ *ap = v;
356 struct vnode *vp = ap->a_vp;
357
358 return (lockstatus(&vp->v_lock));
359 }
360
361 /*
362 * Stubs to use when there is no locking to be done on the underlying object.
363 */
364 int
365 genfs_nolock(void *v)
366 {
367 struct vop_lock_args /* {
368 struct vnode *a_vp;
369 int a_flags;
370 struct lwp *a_l;
371 } */ *ap = v;
372
373 /*
374 * Since we are not using the lock manager, we must clear
375 * the interlock here.
376 */
377 if (ap->a_flags & LK_INTERLOCK)
378 simple_unlock(&ap->a_vp->v_interlock);
379 return (0);
380 }
381
382 int
383 genfs_nounlock(void *v)
384 {
385
386 return (0);
387 }
388
389 int
390 genfs_noislocked(void *v)
391 {
392
393 return (0);
394 }
395
396 /*
397 * Local lease check for NFS servers. Just set up args and let
398 * nqsrv_getlease() do the rest. If NFSSERVER is not in the kernel,
399 * this is a null operation.
400 */
401 int
402 genfs_lease_check(void *v)
403 {
404 #ifdef NFSSERVER
405 struct vop_lease_args /* {
406 struct vnode *a_vp;
407 struct lwp *a_l;
408 struct ucred *a_cred;
409 int a_flag;
410 } */ *ap = v;
411 u_int32_t duration = 0;
412 int cache;
413 u_quad_t frev;
414
415 (void) nqsrv_getlease(ap->a_vp, &duration, ND_CHECK | ap->a_flag,
416 NQLOCALSLP, ap->a_l, (struct mbuf *)0, &cache, &frev, ap->a_cred);
417 return (0);
418 #else
419 return (0);
420 #endif /* NFSSERVER */
421 }
422
423 int
424 genfs_mmap(void *v)
425 {
426
427 return (0);
428 }
429
430 static __inline void
431 genfs_rel_pages(struct vm_page **pgs, int npages)
432 {
433 int i;
434
435 for (i = 0; i < npages; i++) {
436 struct vm_page *pg = pgs[i];
437
438 if (pg == NULL)
439 continue;
440 if (pg->flags & PG_FAKE) {
441 pg->flags |= PG_RELEASED;
442 }
443 }
444 uvm_lock_pageq();
445 uvm_page_unbusy(pgs, npages);
446 uvm_unlock_pageq();
447 }
448
449 /*
450 * generic VM getpages routine.
451 * Return PG_BUSY pages for the given range,
452 * reading from backing store if necessary.
453 */
454
455 int
456 genfs_getpages(void *v)
457 {
458 struct vop_getpages_args /* {
459 struct vnode *a_vp;
460 voff_t a_offset;
461 struct vm_page **a_m;
462 int *a_count;
463 int a_centeridx;
464 vm_prot_t a_access_type;
465 int a_advice;
466 int a_flags;
467 } */ *ap = v;
468
469 off_t newsize, diskeof, memeof;
470 off_t offset, origoffset, startoffset, endoffset, raoffset;
471 daddr_t lbn, blkno;
472 int s, i, error, npages, orignpages, npgs, run, ridx, pidx, pcount;
473 int fs_bshift, fs_bsize, dev_bshift;
474 int flags = ap->a_flags;
475 size_t bytes, iobytes, tailbytes, totalbytes, skipbytes;
476 vaddr_t kva;
477 struct buf *bp, *mbp;
478 struct vnode *vp = ap->a_vp;
479 struct vnode *devvp;
480 struct genfs_node *gp = VTOG(vp);
481 struct uvm_object *uobj = &vp->v_uobj;
482 struct vm_page *pg, **pgs, *pgs_onstack[MAX_READ_AHEAD];
483 int pgs_size;
484 struct ucred *cred = curproc->p_ucred; /* XXXUBC curlwp */
485 boolean_t async = (flags & PGO_SYNCIO) == 0;
486 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
487 boolean_t sawhole = FALSE;
488 boolean_t overwrite = (flags & PGO_OVERWRITE) != 0;
489 UVMHIST_FUNC("genfs_getpages"); UVMHIST_CALLED(ubchist);
490
491 UVMHIST_LOG(ubchist, "vp %p off 0x%x/%x count %d",
492 vp, ap->a_offset >> 32, ap->a_offset, *ap->a_count);
493
494 /* XXXUBC temp limit */
495 if (*ap->a_count > MAX_READ_AHEAD) {
496 panic("genfs_getpages: too many pages");
497 }
498
499 error = 0;
500 origoffset = ap->a_offset;
501 orignpages = *ap->a_count;
502 GOP_SIZE(vp, vp->v_size, &diskeof, GOP_SIZE_READ);
503 if (flags & PGO_PASTEOF) {
504 newsize = MAX(vp->v_size,
505 origoffset + (orignpages << PAGE_SHIFT));
506 GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_READ);
507 } else {
508 memeof = diskeof;
509 }
510 KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
511 KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
512 KASSERT(orignpages > 0);
513
514 /*
515 * Bounds-check the request.
516 */
517
518 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
519 if ((flags & PGO_LOCKED) == 0) {
520 simple_unlock(&uobj->vmobjlock);
521 }
522 UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
523 origoffset, *ap->a_count, memeof,0);
524 return (EINVAL);
525 }
526
527 /*
528 * For PGO_LOCKED requests, just return whatever's in memory.
529 */
530
531 if (flags & PGO_LOCKED) {
532 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
533 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
534
535 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
536 }
537
538 /* vnode is VOP_LOCKed, uobj is locked */
539
540 if (write && (vp->v_flag & VONWORKLST) == 0) {
541 vn_syncer_add_to_worklist(vp, filedelay);
542 }
543
544 /*
545 * find the requested pages and make some simple checks.
546 * leave space in the page array for a whole block.
547 */
548
549 if (vp->v_type == VREG) {
550 fs_bshift = vp->v_mount->mnt_fs_bshift;
551 dev_bshift = vp->v_mount->mnt_dev_bshift;
552 } else {
553 fs_bshift = DEV_BSHIFT;
554 dev_bshift = DEV_BSHIFT;
555 }
556 fs_bsize = 1 << fs_bshift;
557
558 orignpages = MIN(orignpages,
559 round_page(memeof - origoffset) >> PAGE_SHIFT);
560 npages = orignpages;
561 startoffset = origoffset & ~(fs_bsize - 1);
562 endoffset = round_page((origoffset + (npages << PAGE_SHIFT) +
563 fs_bsize - 1) & ~(fs_bsize - 1));
564 endoffset = MIN(endoffset, round_page(memeof));
565 ridx = (origoffset - startoffset) >> PAGE_SHIFT;
566
567 pgs_size = sizeof(struct vm_page *) *
568 ((endoffset - startoffset) >> PAGE_SHIFT);
569 if (pgs_size > sizeof(pgs_onstack)) {
570 pgs = malloc(pgs_size, M_DEVBUF, M_NOWAIT | M_ZERO);
571 if (pgs == NULL) {
572 simple_unlock(&uobj->vmobjlock);
573 return (ENOMEM);
574 }
575 } else {
576 pgs = pgs_onstack;
577 memset(pgs, 0, pgs_size);
578 }
579 UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld",
580 ridx, npages, startoffset, endoffset);
581 if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
582 async ? UFP_NOWAIT : UFP_ALL) != orignpages) {
583 KASSERT(async != 0);
584 genfs_rel_pages(&pgs[ridx], orignpages);
585 simple_unlock(&uobj->vmobjlock);
586 if (pgs != pgs_onstack)
587 free(pgs, M_DEVBUF);
588 return (EBUSY);
589 }
590
591 /*
592 * if the pages are already resident, just return them.
593 */
594
595 for (i = 0; i < npages; i++) {
596 struct vm_page *pg = pgs[ridx + i];
597
598 if ((pg->flags & PG_FAKE) ||
599 (write && (pg->flags & PG_RDONLY))) {
600 break;
601 }
602 }
603 if (i == npages) {
604 UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
605 raoffset = origoffset + (orignpages << PAGE_SHIFT);
606 npages += ridx;
607 goto raout;
608 }
609
610 /*
611 * if PGO_OVERWRITE is set, don't bother reading the pages.
612 */
613
614 if (flags & PGO_OVERWRITE) {
615 UVMHIST_LOG(ubchist, "PGO_OVERWRITE",0,0,0,0);
616
617 for (i = 0; i < npages; i++) {
618 struct vm_page *pg = pgs[ridx + i];
619
620 pg->flags &= ~(PG_RDONLY|PG_CLEAN);
621 }
622 npages += ridx;
623 goto out;
624 }
625
626 /*
627 * the page wasn't resident and we're not overwriting,
628 * so we're going to have to do some i/o.
629 * find any additional pages needed to cover the expanded range.
630 */
631
632 npages = (endoffset - startoffset) >> PAGE_SHIFT;
633 if (startoffset != origoffset || npages != orignpages) {
634
635 /*
636 * we need to avoid deadlocks caused by locking
637 * additional pages at lower offsets than pages we
638 * already have locked. unlock them all and start over.
639 */
640
641 genfs_rel_pages(&pgs[ridx], orignpages);
642 memset(pgs, 0, pgs_size);
643
644 UVMHIST_LOG(ubchist, "reset npages start 0x%x end 0x%x",
645 startoffset, endoffset, 0,0);
646 npgs = npages;
647 if (uvn_findpages(uobj, startoffset, &npgs, pgs,
648 async ? UFP_NOWAIT : UFP_ALL) != npages) {
649 KASSERT(async != 0);
650 genfs_rel_pages(pgs, npages);
651 simple_unlock(&uobj->vmobjlock);
652 if (pgs != pgs_onstack)
653 free(pgs, M_DEVBUF);
654 return (EBUSY);
655 }
656 }
657 simple_unlock(&uobj->vmobjlock);
658
659 /*
660 * read the desired page(s).
661 */
662
663 totalbytes = npages << PAGE_SHIFT;
664 bytes = MIN(totalbytes, MAX(diskeof - startoffset, 0));
665 tailbytes = totalbytes - bytes;
666 skipbytes = 0;
667
668 kva = uvm_pagermapin(pgs, npages,
669 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
670
671 s = splbio();
672 mbp = pool_get(&bufpool, PR_WAITOK);
673 splx(s);
674 BUF_INIT(mbp);
675 mbp->b_bufsize = totalbytes;
676 mbp->b_data = (void *)kva;
677 mbp->b_resid = mbp->b_bcount = bytes;
678 mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
679 mbp->b_iodone = (async ? uvm_aio_biodone : 0);
680 mbp->b_vp = vp;
681
682 /*
683 * if EOF is in the middle of the range, zero the part past EOF.
684 * if the page including EOF is not PG_FAKE, skip over it since
685 * in that case it has valid data that we need to preserve.
686 */
687
688 if (tailbytes > 0) {
689 size_t tailstart = bytes;
690
691 if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) {
692 tailstart = round_page(tailstart);
693 tailbytes -= tailstart - bytes;
694 }
695 UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
696 kva, tailstart, tailbytes,0);
697 memset((void *)(kva + tailstart), 0, tailbytes);
698 }
699
700 /*
701 * now loop over the pages, reading as needed.
702 */
703
704 if (write) {
705 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
706 } else {
707 lockmgr(&gp->g_glock, LK_SHARED, NULL);
708 }
709
710 bp = NULL;
711 for (offset = startoffset;
712 bytes > 0;
713 offset += iobytes, bytes -= iobytes) {
714
715 /*
716 * skip pages which don't need to be read.
717 */
718
719 pidx = (offset - startoffset) >> PAGE_SHIFT;
720 while ((pgs[pidx]->flags & (PG_FAKE|PG_RDONLY)) == 0) {
721 size_t b;
722
723 KASSERT((offset & (PAGE_SIZE - 1)) == 0);
724 b = MIN(PAGE_SIZE, bytes);
725 offset += b;
726 bytes -= b;
727 skipbytes += b;
728 pidx++;
729 UVMHIST_LOG(ubchist, "skipping, new offset 0x%x",
730 offset, 0,0,0);
731 if (bytes == 0) {
732 goto loopdone;
733 }
734 }
735
736 /*
737 * bmap the file to find out the blkno to read from and
738 * how much we can read in one i/o. if bmap returns an error,
739 * skip the rest of the top-level i/o.
740 */
741
742 lbn = offset >> fs_bshift;
743 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
744 if (error) {
745 UVMHIST_LOG(ubchist, "VOP_BMAP lbn 0x%x -> %d\n",
746 lbn, error,0,0);
747 skipbytes += bytes;
748 goto loopdone;
749 }
750
751 /*
752 * see how many pages can be read with this i/o.
753 * reduce the i/o size if necessary to avoid
754 * overwriting pages with valid data.
755 */
756
757 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
758 bytes);
759 if (offset + iobytes > round_page(offset)) {
760 pcount = 1;
761 while (pidx + pcount < npages &&
762 pgs[pidx + pcount]->flags & PG_FAKE) {
763 pcount++;
764 }
765 iobytes = MIN(iobytes, (pcount << PAGE_SHIFT) -
766 (offset - trunc_page(offset)));
767 }
768
769 /*
770 * if this block isn't allocated, zero it instead of
771 * reading it. if this is a read access, mark the
772 * pages we zeroed PG_RDONLY.
773 */
774
775 if (blkno < 0) {
776 int holepages = (round_page(offset + iobytes) -
777 trunc_page(offset)) >> PAGE_SHIFT;
778 UVMHIST_LOG(ubchist, "lbn 0x%x -> HOLE", lbn,0,0,0);
779
780 sawhole = TRUE;
781 memset((char *)kva + (offset - startoffset), 0,
782 iobytes);
783 skipbytes += iobytes;
784
785 for (i = 0; i < holepages; i++) {
786 if (write) {
787 pgs[pidx + i]->flags &= ~PG_CLEAN;
788 } else {
789 pgs[pidx + i]->flags |= PG_RDONLY;
790 }
791 }
792 continue;
793 }
794
795 /*
796 * allocate a sub-buf for this piece of the i/o
797 * (or just use mbp if there's only 1 piece),
798 * and start it going.
799 */
800
801 if (offset == startoffset && iobytes == bytes) {
802 bp = mbp;
803 } else {
804 s = splbio();
805 bp = pool_get(&bufpool, PR_WAITOK);
806 splx(s);
807 BUF_INIT(bp);
808 bp->b_data = (char *)kva + offset - startoffset;
809 bp->b_resid = bp->b_bcount = iobytes;
810 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
811 bp->b_iodone = uvm_aio_biodone1;
812 bp->b_vp = vp;
813 bp->b_proc = NULL;
814 }
815 bp->b_lblkno = 0;
816 bp->b_private = mbp;
817 if (devvp->v_type == VBLK) {
818 bp->b_dev = devvp->v_rdev;
819 }
820
821 /* adjust physical blkno for partial blocks */
822 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
823 dev_bshift);
824
825 UVMHIST_LOG(ubchist,
826 "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
827 bp, offset, iobytes, bp->b_blkno);
828
829 VOP_STRATEGY(bp);
830 }
831
832 loopdone:
833 if (skipbytes) {
834 s = splbio();
835 if (error) {
836 mbp->b_flags |= B_ERROR;
837 mbp->b_error = error;
838 }
839 mbp->b_resid -= skipbytes;
840 if (mbp->b_resid == 0) {
841 biodone(mbp);
842 }
843 splx(s);
844 }
845
846 if (async) {
847 UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
848 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
849 if (pgs != pgs_onstack)
850 free(pgs, M_DEVBUF);
851 return (0);
852 }
853 if (bp != NULL) {
854 error = biowait(mbp);
855 }
856 s = splbio();
857 pool_put(&bufpool, mbp);
858 splx(s);
859 uvm_pagermapout(kva, npages);
860 raoffset = startoffset + totalbytes;
861
862 /*
863 * if this we encountered a hole then we have to do a little more work.
864 * for read faults, we marked the page PG_RDONLY so that future
865 * write accesses to the page will fault again.
866 * for write faults, we must make sure that the backing store for
867 * the page is completely allocated while the pages are locked.
868 */
869
870 if (!error && sawhole && write) {
871 for (i = 0; i < npages; i++) {
872 if (pgs[i] == NULL) {
873 continue;
874 }
875 pgs[i]->flags &= ~PG_CLEAN;
876 UVMHIST_LOG(ubchist, "mark dirty pg %p", pgs[i],0,0,0);
877 }
878 error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
879 cred);
880 UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
881 startoffset, npages << PAGE_SHIFT, error,0);
882 }
883 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
884 simple_lock(&uobj->vmobjlock);
885
886 /*
887 * see if we want to start any readahead.
888 * XXXUBC for now, just read the next 128k on 64k boundaries.
889 * this is pretty nonsensical, but it is 50% faster than reading
890 * just the next 64k.
891 */
892
893 raout:
894 if (!error && !async && !write && ((int)raoffset & 0xffff) == 0 &&
895 PAGE_SHIFT <= 16) {
896 off_t rasize;
897 int rapages, err, i, skipped;
898
899 /* XXXUBC temp limit, from above */
900 rapages = MIN(MIN(1 << (16 - PAGE_SHIFT), MAX_READ_AHEAD),
901 genfs_rapages);
902 rasize = rapages << PAGE_SHIFT;
903 for (i = skipped = 0; i < genfs_racount; i++) {
904 err = VOP_GETPAGES(vp, raoffset, NULL, &rapages, 0,
905 VM_PROT_READ, 0, 0);
906 simple_lock(&uobj->vmobjlock);
907 if (err) {
908 if (err != EBUSY ||
909 skipped++ == genfs_raskip)
910 break;
911 }
912 raoffset += rasize;
913 rapages = rasize >> PAGE_SHIFT;
914 }
915 }
916
917 /*
918 * we're almost done! release the pages...
919 * for errors, we free the pages.
920 * otherwise we activate them and mark them as valid and clean.
921 * also, unbusy pages that were not actually requested.
922 */
923
924 if (error) {
925 for (i = 0; i < npages; i++) {
926 if (pgs[i] == NULL) {
927 continue;
928 }
929 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
930 pgs[i], pgs[i]->flags, 0,0);
931 if (pgs[i]->flags & PG_FAKE) {
932 pgs[i]->flags |= PG_RELEASED;
933 }
934 }
935 uvm_lock_pageq();
936 uvm_page_unbusy(pgs, npages);
937 uvm_unlock_pageq();
938 simple_unlock(&uobj->vmobjlock);
939 UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
940 if (pgs != pgs_onstack)
941 free(pgs, M_DEVBUF);
942 return (error);
943 }
944
945 out:
946 UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,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 if (write) {
960 pg->flags &= ~(PG_RDONLY);
961 }
962 if (i < ridx || i >= ridx + orignpages || async) {
963 UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
964 pg, pg->offset,0,0);
965 if (pg->flags & PG_WANTED) {
966 wakeup(pg);
967 }
968 if (pg->flags & PG_FAKE) {
969 KASSERT(overwrite);
970 uvm_pagezero(pg);
971 }
972 if (pg->flags & PG_RELEASED) {
973 uvm_pagefree(pg);
974 continue;
975 }
976 uvm_pageactivate(pg);
977 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
978 UVM_PAGE_OWN(pg, NULL);
979 }
980 }
981 uvm_unlock_pageq();
982 simple_unlock(&uobj->vmobjlock);
983 if (ap->a_m != NULL) {
984 memcpy(ap->a_m, &pgs[ridx],
985 orignpages * sizeof(struct vm_page *));
986 }
987 if (pgs != pgs_onstack)
988 free(pgs, M_DEVBUF);
989 return (0);
990 }
991
992 /*
993 * generic VM putpages routine.
994 * Write the given range of pages to backing store.
995 *
996 * => "offhi == 0" means flush all pages at or after "offlo".
997 * => object should be locked by caller. we may _unlock_ the object
998 * if (and only if) we need to clean a page (PGO_CLEANIT), or
999 * if PGO_SYNCIO is set and there are pages busy.
1000 * we return with the object locked.
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, then we will neither
1005 * unlock the object nor block.
1006 * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
1007 * => NOTE: we rely on the fact that the object's memq is a TAILQ and
1008 * that new pages are inserted on the tail end of the list. thus,
1009 * we can make a complete pass through the object in one go by starting
1010 * at the head and working towards the tail (new pages are put in
1011 * front of us).
1012 * => NOTE: we are allowed to lock the page queues, so the caller
1013 * must not be holding the page queue lock.
1014 *
1015 * note on "cleaning" object and PG_BUSY pages:
1016 * this routine is holding the lock on the object. the only time
1017 * that it can run into a PG_BUSY page that it does not own is if
1018 * some other process has started I/O on the page (e.g. either
1019 * a pagein, or a pageout). if the PG_BUSY page is being paged
1020 * in, then it can not be dirty (!PG_CLEAN) because no one has
1021 * had a chance to modify it yet. if the PG_BUSY page is being
1022 * paged out then it means that someone else has already started
1023 * cleaning the page for us (how nice!). in this case, if we
1024 * have syncio specified, then after we make our pass through the
1025 * object we need to wait for the other PG_BUSY pages to clear
1026 * off (i.e. we need to do an iosync). also note that once a
1027 * page is PG_BUSY it must stay in its object until it is un-busyed.
1028 *
1029 * note on page traversal:
1030 * we can traverse the pages in an object either by going down the
1031 * linked list in "uobj->memq", or we can go over the address range
1032 * by page doing hash table lookups for each address. depending
1033 * on how many pages are in the object it may be cheaper to do one
1034 * or the other. we set "by_list" to true if we are using memq.
1035 * if the cost of a hash lookup was equal to the cost of the list
1036 * traversal we could compare the number of pages in the start->stop
1037 * range to the total number of pages in the object. however, it
1038 * seems that a hash table lookup is more expensive than the linked
1039 * list traversal, so we multiply the number of pages in the
1040 * range by an estimate of the relatively higher cost of the hash lookup.
1041 */
1042
1043 int
1044 genfs_putpages(void *v)
1045 {
1046 struct vop_putpages_args /* {
1047 struct vnode *a_vp;
1048 voff_t a_offlo;
1049 voff_t a_offhi;
1050 int a_flags;
1051 } */ *ap = v;
1052 struct vnode *vp = ap->a_vp;
1053 struct uvm_object *uobj = &vp->v_uobj;
1054 struct simplelock *slock = &uobj->vmobjlock;
1055 off_t startoff = ap->a_offlo;
1056 off_t endoff = ap->a_offhi;
1057 off_t off;
1058 int flags = ap->a_flags;
1059 /* Even for strange MAXPHYS, the shift rounds down to a page */
1060 const int maxpages = MAXPHYS >> PAGE_SHIFT;
1061 int i, s, error, npages, nback;
1062 int freeflag;
1063 struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
1064 boolean_t wasclean, by_list, needs_clean, yield;
1065 boolean_t async = (flags & PGO_SYNCIO) == 0;
1066 boolean_t pagedaemon = curproc == uvm.pagedaemon_proc;
1067 struct lwp *l = curlwp ? curlwp : &lwp0;
1068
1069 UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
1070
1071 KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
1072 KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
1073 KASSERT(startoff < endoff || endoff == 0);
1074
1075 UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
1076 vp, uobj->uo_npages, startoff, endoff - startoff);
1077 if (uobj->uo_npages == 0) {
1078 s = splbio();
1079 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1080 (vp->v_flag & VONWORKLST)) {
1081 vp->v_flag &= ~VONWORKLST;
1082 LIST_REMOVE(vp, v_synclist);
1083 }
1084 splx(s);
1085 simple_unlock(slock);
1086 return (0);
1087 }
1088
1089 /*
1090 * the vnode has pages, set up to process the request.
1091 */
1092
1093 error = 0;
1094 s = splbio();
1095 simple_lock(&global_v_numoutput_slock);
1096 wasclean = (vp->v_numoutput == 0);
1097 simple_unlock(&global_v_numoutput_slock);
1098 splx(s);
1099 off = startoff;
1100 if (endoff == 0 || flags & PGO_ALLPAGES) {
1101 endoff = trunc_page(LLONG_MAX);
1102 }
1103 by_list = (uobj->uo_npages <=
1104 ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
1105
1106 /*
1107 * start the loop. when scanning by list, hold the last page
1108 * in the list before we start. pages allocated after we start
1109 * will be added to the end of the list, so we can stop at the
1110 * current last page.
1111 */
1112
1113 freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
1114 curmp.uobject = uobj;
1115 curmp.offset = (voff_t)-1;
1116 curmp.flags = PG_BUSY;
1117 endmp.uobject = uobj;
1118 endmp.offset = (voff_t)-1;
1119 endmp.flags = PG_BUSY;
1120 if (by_list) {
1121 pg = TAILQ_FIRST(&uobj->memq);
1122 TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
1123 PHOLD(l);
1124 } else {
1125 pg = uvm_pagelookup(uobj, off);
1126 }
1127 nextpg = NULL;
1128 while (by_list || off < endoff) {
1129
1130 /*
1131 * if the current page is not interesting, move on to the next.
1132 */
1133
1134 KASSERT(pg == NULL || pg->uobject == uobj);
1135 KASSERT(pg == NULL ||
1136 (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1137 (pg->flags & PG_BUSY) != 0);
1138 if (by_list) {
1139 if (pg == &endmp) {
1140 break;
1141 }
1142 if (pg->offset < startoff || pg->offset >= endoff ||
1143 pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1144 pg = TAILQ_NEXT(pg, listq);
1145 continue;
1146 }
1147 off = pg->offset;
1148 } else if (pg == NULL ||
1149 pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1150 off += PAGE_SIZE;
1151 if (off < endoff) {
1152 pg = uvm_pagelookup(uobj, off);
1153 }
1154 continue;
1155 }
1156
1157 /*
1158 * if the current page needs to be cleaned and it's busy,
1159 * wait for it to become unbusy.
1160 */
1161
1162 yield = (l->l_cpu->ci_schedstate.spc_flags &
1163 SPCF_SHOULDYIELD) && !pagedaemon;
1164 if (pg->flags & PG_BUSY || yield) {
1165 UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
1166 if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
1167 UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
1168 error = EDEADLK;
1169 break;
1170 }
1171 KASSERT(!pagedaemon);
1172 if (by_list) {
1173 TAILQ_INSERT_BEFORE(pg, &curmp, listq);
1174 UVMHIST_LOG(ubchist, "curmp next %p",
1175 TAILQ_NEXT(&curmp, listq), 0,0,0);
1176 }
1177 if (yield) {
1178 simple_unlock(slock);
1179 preempt(1);
1180 simple_lock(slock);
1181 } else {
1182 pg->flags |= PG_WANTED;
1183 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
1184 simple_lock(slock);
1185 }
1186 if (by_list) {
1187 UVMHIST_LOG(ubchist, "after next %p",
1188 TAILQ_NEXT(&curmp, listq), 0,0,0);
1189 pg = TAILQ_NEXT(&curmp, listq);
1190 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1191 } else {
1192 pg = uvm_pagelookup(uobj, off);
1193 }
1194 continue;
1195 }
1196
1197 /*
1198 * if we're freeing, remove all mappings of the page now.
1199 * if we're cleaning, check if the page is needs to be cleaned.
1200 */
1201
1202 if (flags & PGO_FREE) {
1203 pmap_page_protect(pg, VM_PROT_NONE);
1204 }
1205 if (flags & PGO_CLEANIT) {
1206 needs_clean = pmap_clear_modify(pg) ||
1207 (pg->flags & PG_CLEAN) == 0;
1208 pg->flags |= PG_CLEAN;
1209 } else {
1210 needs_clean = FALSE;
1211 }
1212
1213 /*
1214 * if we're cleaning, build a cluster.
1215 * the cluster will consist of pages which are currently dirty,
1216 * but they will be returned to us marked clean.
1217 * if not cleaning, just operate on the one page.
1218 */
1219
1220 if (needs_clean) {
1221 wasclean = FALSE;
1222 memset(pgs, 0, sizeof(pgs));
1223 pg->flags |= PG_BUSY;
1224 UVM_PAGE_OWN(pg, "genfs_putpages");
1225
1226 /*
1227 * first look backward.
1228 */
1229
1230 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1231 nback = npages;
1232 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1233 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1234 if (nback) {
1235 memmove(&pgs[0], &pgs[npages - nback],
1236 nback * sizeof(pgs[0]));
1237 if (npages - nback < nback)
1238 memset(&pgs[nback], 0,
1239 (npages - nback) * sizeof(pgs[0]));
1240 else
1241 memset(&pgs[npages - nback], 0,
1242 nback * sizeof(pgs[0]));
1243 }
1244
1245 /*
1246 * then plug in our page of interest.
1247 */
1248
1249 pgs[nback] = pg;
1250
1251 /*
1252 * then look forward to fill in the remaining space in
1253 * the array of pages.
1254 */
1255
1256 npages = maxpages - nback - 1;
1257 uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1258 &pgs[nback + 1],
1259 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1260 npages += nback + 1;
1261 } else {
1262 pgs[0] = pg;
1263 npages = 1;
1264 nback = 0;
1265 }
1266
1267 /*
1268 * apply FREE or DEACTIVATE options if requested.
1269 */
1270
1271 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1272 uvm_lock_pageq();
1273 }
1274 for (i = 0; i < npages; i++) {
1275 tpg = pgs[i];
1276 KASSERT(tpg->uobject == uobj);
1277 if (by_list && tpg == TAILQ_NEXT(pg, listq))
1278 pg = tpg;
1279 if (tpg->offset < startoff || tpg->offset >= endoff)
1280 continue;
1281 if (flags & PGO_DEACTIVATE &&
1282 (tpg->pqflags & PQ_INACTIVE) == 0 &&
1283 tpg->wire_count == 0) {
1284 (void) pmap_clear_reference(tpg);
1285 uvm_pagedeactivate(tpg);
1286 } else if (flags & PGO_FREE) {
1287 pmap_page_protect(tpg, VM_PROT_NONE);
1288 if (tpg->flags & PG_BUSY) {
1289 tpg->flags |= freeflag;
1290 if (pagedaemon) {
1291 uvmexp.paging++;
1292 uvm_pagedequeue(tpg);
1293 }
1294 } else {
1295
1296 /*
1297 * ``page is not busy''
1298 * implies that npages is 1
1299 * and needs_clean is false.
1300 */
1301
1302 nextpg = TAILQ_NEXT(tpg, listq);
1303 uvm_pagefree(tpg);
1304 }
1305 }
1306 }
1307 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1308 uvm_unlock_pageq();
1309 }
1310 if (needs_clean) {
1311
1312 /*
1313 * start the i/o. if we're traversing by list,
1314 * keep our place in the list with a marker page.
1315 */
1316
1317 if (by_list) {
1318 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1319 listq);
1320 }
1321 simple_unlock(slock);
1322 error = GOP_WRITE(vp, pgs, npages, flags);
1323 simple_lock(slock);
1324 if (by_list) {
1325 pg = TAILQ_NEXT(&curmp, listq);
1326 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1327 }
1328 if (error) {
1329 break;
1330 }
1331 if (by_list) {
1332 continue;
1333 }
1334 }
1335
1336 /*
1337 * find the next page and continue if there was no error.
1338 */
1339
1340 if (by_list) {
1341 if (nextpg) {
1342 pg = nextpg;
1343 nextpg = NULL;
1344 } else {
1345 pg = TAILQ_NEXT(pg, listq);
1346 }
1347 } else {
1348 off += (npages - nback) << PAGE_SHIFT;
1349 if (off < endoff) {
1350 pg = uvm_pagelookup(uobj, off);
1351 }
1352 }
1353 }
1354 if (by_list) {
1355 TAILQ_REMOVE(&uobj->memq, &endmp, listq);
1356 PRELE(l);
1357 }
1358
1359 /*
1360 * if we're cleaning and there was nothing to clean,
1361 * take us off the syncer list. if we started any i/o
1362 * and we're doing sync i/o, wait for all writes to finish.
1363 */
1364
1365 s = splbio();
1366 if ((flags & PGO_CLEANIT) && wasclean &&
1367 startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
1368 LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1369 (vp->v_flag & VONWORKLST)) {
1370 vp->v_flag &= ~VONWORKLST;
1371 LIST_REMOVE(vp, v_synclist);
1372 }
1373 splx(s);
1374 if (!wasclean && !async) {
1375 s = splbio();
1376 /*
1377 * XXX - we want simple_unlock(&global_v_numoutput_slock);
1378 * but the slot in ltsleep() is taken!
1379 * XXX - try to recover from missed wakeups with a timeout..
1380 * must think of something better.
1381 */
1382 while (vp->v_numoutput != 0) {
1383 vp->v_flag |= VBWAIT;
1384 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, FALSE,
1385 "genput2", hz);
1386 simple_lock(slock);
1387 }
1388 splx(s);
1389 }
1390 simple_unlock(&uobj->vmobjlock);
1391 return (error);
1392 }
1393
1394 int
1395 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1396 {
1397 int s, error, run;
1398 int fs_bshift, dev_bshift;
1399 vaddr_t kva;
1400 off_t eof, offset, startoffset;
1401 size_t bytes, iobytes, skipbytes;
1402 daddr_t lbn, blkno;
1403 struct vm_page *pg;
1404 struct buf *mbp, *bp;
1405 struct vnode *devvp;
1406 boolean_t async = (flags & PGO_SYNCIO) == 0;
1407 UVMHIST_FUNC("genfs_gop_write"); UVMHIST_CALLED(ubchist);
1408
1409 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1410 vp, pgs, npages, flags);
1411
1412 GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
1413 if (vp->v_type == VREG) {
1414 fs_bshift = vp->v_mount->mnt_fs_bshift;
1415 dev_bshift = vp->v_mount->mnt_dev_bshift;
1416 } else {
1417 fs_bshift = DEV_BSHIFT;
1418 dev_bshift = DEV_BSHIFT;
1419 }
1420 error = 0;
1421 pg = pgs[0];
1422 startoffset = pg->offset;
1423 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1424 skipbytes = 0;
1425 KASSERT(bytes != 0);
1426
1427 kva = uvm_pagermapin(pgs, npages,
1428 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1429
1430 s = splbio();
1431 simple_lock(&global_v_numoutput_slock);
1432 vp->v_numoutput += 2;
1433 simple_unlock(&global_v_numoutput_slock);
1434 mbp = pool_get(&bufpool, PR_WAITOK);
1435 BUF_INIT(mbp);
1436 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1437 vp, mbp, vp->v_numoutput, bytes);
1438 splx(s);
1439 mbp->b_bufsize = npages << PAGE_SHIFT;
1440 mbp->b_data = (void *)kva;
1441 mbp->b_resid = mbp->b_bcount = bytes;
1442 mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? (B_CALL|B_ASYNC) : 0);
1443 mbp->b_iodone = uvm_aio_biodone;
1444 mbp->b_vp = vp;
1445
1446 bp = NULL;
1447 for (offset = startoffset;
1448 bytes > 0;
1449 offset += iobytes, bytes -= iobytes) {
1450 lbn = offset >> fs_bshift;
1451 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1452 if (error) {
1453 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1454 skipbytes += bytes;
1455 bytes = 0;
1456 break;
1457 }
1458
1459 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1460 bytes);
1461 if (blkno == (daddr_t)-1) {
1462 skipbytes += iobytes;
1463 continue;
1464 }
1465
1466 /* if it's really one i/o, don't make a second buf */
1467 if (offset == startoffset && iobytes == bytes) {
1468 bp = mbp;
1469 } else {
1470 s = splbio();
1471 V_INCR_NUMOUTPUT(vp);
1472 bp = pool_get(&bufpool, PR_WAITOK);
1473 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1474 vp, bp, vp->v_numoutput, 0);
1475 splx(s);
1476 BUF_INIT(bp);
1477 bp->b_data = (char *)kva +
1478 (vaddr_t)(offset - pg->offset);
1479 bp->b_resid = bp->b_bcount = iobytes;
1480 bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1481 bp->b_iodone = uvm_aio_biodone1;
1482 bp->b_vp = vp;
1483 }
1484 bp->b_lblkno = 0;
1485 bp->b_private = mbp;
1486 if (devvp->v_type == VBLK) {
1487 bp->b_dev = devvp->v_rdev;
1488 }
1489
1490 /* adjust physical blkno for partial blocks */
1491 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1492 dev_bshift);
1493 UVMHIST_LOG(ubchist,
1494 "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1495 vp, offset, bp->b_bcount, bp->b_blkno);
1496 VOP_STRATEGY(bp);
1497 }
1498 if (skipbytes) {
1499 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1500 s = splbio();
1501 if (error) {
1502 mbp->b_flags |= B_ERROR;
1503 mbp->b_error = error;
1504 }
1505 mbp->b_resid -= skipbytes;
1506 if (mbp->b_resid == 0) {
1507 biodone(mbp);
1508 }
1509 splx(s);
1510 }
1511 if (async) {
1512 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1513 return (0);
1514 }
1515 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1516 error = biowait(mbp);
1517 uvm_aio_aiodone(mbp);
1518 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1519 return (error);
1520 }
1521
1522 /*
1523 * VOP_PUTPAGES() for vnodes which never have pages.
1524 */
1525
1526 int
1527 genfs_null_putpages(void *v)
1528 {
1529 struct vop_putpages_args /* {
1530 struct vnode *a_vp;
1531 voff_t a_offlo;
1532 voff_t a_offhi;
1533 int a_flags;
1534 } */ *ap = v;
1535 struct vnode *vp = ap->a_vp;
1536
1537 KASSERT(vp->v_uobj.uo_npages == 0);
1538 simple_unlock(&vp->v_interlock);
1539 return (0);
1540 }
1541
1542 void
1543 genfs_node_init(struct vnode *vp, struct genfs_ops *ops)
1544 {
1545 struct genfs_node *gp = VTOG(vp);
1546
1547 lockinit(&gp->g_glock, PINOD, "glock", 0, 0);
1548 gp->g_op = ops;
1549 }
1550
1551 void
1552 genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
1553 {
1554 int bsize;
1555
1556 bsize = 1 << vp->v_mount->mnt_fs_bshift;
1557 *eobp = (size + bsize - 1) & ~(bsize - 1);
1558 }
1559
1560 int
1561 genfs_compat_getpages(void *v)
1562 {
1563 struct vop_getpages_args /* {
1564 struct vnode *a_vp;
1565 voff_t a_offset;
1566 struct vm_page **a_m;
1567 int *a_count;
1568 int a_centeridx;
1569 vm_prot_t a_access_type;
1570 int a_advice;
1571 int a_flags;
1572 } */ *ap = v;
1573
1574 off_t origoffset;
1575 struct vnode *vp = ap->a_vp;
1576 struct uvm_object *uobj = &vp->v_uobj;
1577 struct vm_page *pg, **pgs;
1578 vaddr_t kva;
1579 int i, error, orignpages, npages;
1580 struct iovec iov;
1581 struct uio uio;
1582 struct ucred *cred = curproc->p_ucred;
1583 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1584
1585 error = 0;
1586 origoffset = ap->a_offset;
1587 orignpages = *ap->a_count;
1588 pgs = ap->a_m;
1589
1590 if (write && (vp->v_flag & VONWORKLST) == 0) {
1591 vn_syncer_add_to_worklist(vp, filedelay);
1592 }
1593 if (ap->a_flags & PGO_LOCKED) {
1594 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1595 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
1596
1597 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1598 }
1599 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1600 simple_unlock(&uobj->vmobjlock);
1601 return (EINVAL);
1602 }
1603 npages = orignpages;
1604 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1605 simple_unlock(&uobj->vmobjlock);
1606 kva = uvm_pagermapin(pgs, npages,
1607 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1608 for (i = 0; i < npages; i++) {
1609 pg = pgs[i];
1610 if ((pg->flags & PG_FAKE) == 0) {
1611 continue;
1612 }
1613 iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1614 iov.iov_len = PAGE_SIZE;
1615 uio.uio_iov = &iov;
1616 uio.uio_iovcnt = 1;
1617 uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1618 uio.uio_segflg = UIO_SYSSPACE;
1619 uio.uio_rw = UIO_READ;
1620 uio.uio_resid = PAGE_SIZE;
1621 uio.uio_lwp = curlwp;
1622 error = VOP_READ(vp, &uio, 0, cred);
1623 if (error) {
1624 break;
1625 }
1626 if (uio.uio_resid) {
1627 memset(iov.iov_base, 0, uio.uio_resid);
1628 }
1629 }
1630 uvm_pagermapout(kva, npages);
1631 simple_lock(&uobj->vmobjlock);
1632 uvm_lock_pageq();
1633 for (i = 0; i < npages; i++) {
1634 pg = pgs[i];
1635 if (error && (pg->flags & PG_FAKE) != 0) {
1636 pg->flags |= PG_RELEASED;
1637 } else {
1638 pmap_clear_modify(pg);
1639 uvm_pageactivate(pg);
1640 }
1641 }
1642 if (error) {
1643 uvm_page_unbusy(pgs, npages);
1644 }
1645 uvm_unlock_pageq();
1646 simple_unlock(&uobj->vmobjlock);
1647 return (error);
1648 }
1649
1650 int
1651 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1652 int flags)
1653 {
1654 off_t offset;
1655 struct iovec iov;
1656 struct uio uio;
1657 struct ucred *cred = curproc->p_ucred;
1658 struct buf *bp;
1659 vaddr_t kva;
1660 int s, error;
1661
1662 offset = pgs[0]->offset;
1663 kva = uvm_pagermapin(pgs, npages,
1664 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1665
1666 iov.iov_base = (void *)kva;
1667 iov.iov_len = npages << PAGE_SHIFT;
1668 uio.uio_iov = &iov;
1669 uio.uio_iovcnt = 1;
1670 uio.uio_offset = offset;
1671 uio.uio_segflg = UIO_SYSSPACE;
1672 uio.uio_rw = UIO_WRITE;
1673 uio.uio_resid = npages << PAGE_SHIFT;
1674 uio.uio_lwp = curlwp;
1675 error = VOP_WRITE(vp, &uio, 0, cred);
1676
1677 s = splbio();
1678 V_INCR_NUMOUTPUT(vp);
1679 bp = pool_get(&bufpool, PR_WAITOK);
1680 splx(s);
1681
1682 BUF_INIT(bp);
1683 bp->b_flags = B_BUSY | B_WRITE | B_AGE;
1684 bp->b_vp = vp;
1685 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1686 bp->b_data = (char *)kva;
1687 bp->b_bcount = npages << PAGE_SHIFT;
1688 bp->b_bufsize = npages << PAGE_SHIFT;
1689 bp->b_resid = 0;
1690 if (error) {
1691 bp->b_flags |= B_ERROR;
1692 bp->b_error = error;
1693 }
1694 uvm_aio_aiodone(bp);
1695 return (error);
1696 }
1697
1698 static void
1699 filt_genfsdetach(struct knote *kn)
1700 {
1701 struct vnode *vp = (struct vnode *)kn->kn_hook;
1702
1703 /* XXXLUKEM lock the struct? */
1704 SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
1705 }
1706
1707 static int
1708 filt_genfsread(struct knote *kn, long hint)
1709 {
1710 struct vnode *vp = (struct vnode *)kn->kn_hook;
1711
1712 /*
1713 * filesystem is gone, so set the EOF flag and schedule
1714 * the knote for deletion.
1715 */
1716 if (hint == NOTE_REVOKE) {
1717 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1718 return (1);
1719 }
1720
1721 /* XXXLUKEM lock the struct? */
1722 kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
1723 return (kn->kn_data != 0);
1724 }
1725
1726 static int
1727 filt_genfsvnode(struct knote *kn, long hint)
1728 {
1729
1730 if (kn->kn_sfflags & hint)
1731 kn->kn_fflags |= hint;
1732 if (hint == NOTE_REVOKE) {
1733 kn->kn_flags |= EV_EOF;
1734 return (1);
1735 }
1736 return (kn->kn_fflags != 0);
1737 }
1738
1739 static const struct filterops genfsread_filtops =
1740 { 1, NULL, filt_genfsdetach, filt_genfsread };
1741 static const struct filterops genfsvnode_filtops =
1742 { 1, NULL, filt_genfsdetach, filt_genfsvnode };
1743
1744 int
1745 genfs_kqfilter(void *v)
1746 {
1747 struct vop_kqfilter_args /* {
1748 struct vnode *a_vp;
1749 struct knote *a_kn;
1750 } */ *ap = v;
1751 struct vnode *vp;
1752 struct knote *kn;
1753
1754 vp = ap->a_vp;
1755 kn = ap->a_kn;
1756 switch (kn->kn_filter) {
1757 case EVFILT_READ:
1758 kn->kn_fop = &genfsread_filtops;
1759 break;
1760 case EVFILT_VNODE:
1761 kn->kn_fop = &genfsvnode_filtops;
1762 break;
1763 default:
1764 return (1);
1765 }
1766
1767 kn->kn_hook = vp;
1768
1769 /* XXXLUKEM lock the struct? */
1770 SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
1771
1772 return (0);
1773 }
1774