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