genfs_vnops.c revision 1.116 1 /* $NetBSD: genfs_vnops.c,v 1.116 2005/12/11 12:24:50 christos 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.116 2005/12/11 12:24:50 christos 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, raoffset;
443 daddr_t lbn, blkno;
444 int s, 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 == VBLK);
468
469 /* XXXUBC temp limit */
470 if (*ap->a_count > MAX_READ_PAGES) {
471 panic("genfs_getpages: too many pages");
472 }
473
474 error = 0;
475 origoffset = ap->a_offset;
476 orignpages = *ap->a_count;
477 GOP_SIZE(vp, vp->v_size, &diskeof, GOP_SIZE_READ);
478 if (flags & PGO_PASTEOF) {
479 newsize = MAX(vp->v_size,
480 origoffset + (orignpages << PAGE_SHIFT));
481 GOP_SIZE(vp, newsize, &memeof, GOP_SIZE_READ|GOP_SIZE_MEM);
482 } else {
483 GOP_SIZE(vp, vp->v_size, &memeof, GOP_SIZE_READ|GOP_SIZE_MEM);
484 }
485 KASSERT(ap->a_centeridx >= 0 || ap->a_centeridx <= orignpages);
486 KASSERT((origoffset & (PAGE_SIZE - 1)) == 0 && origoffset >= 0);
487 KASSERT(orignpages > 0);
488
489 /*
490 * Bounds-check the request.
491 */
492
493 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= memeof) {
494 if ((flags & PGO_LOCKED) == 0) {
495 simple_unlock(&uobj->vmobjlock);
496 }
497 UVMHIST_LOG(ubchist, "off 0x%x count %d goes past EOF 0x%x",
498 origoffset, *ap->a_count, memeof,0);
499 return (EINVAL);
500 }
501
502 /* uobj is locked */
503
504 if ((flags & PGO_NOTIMESTAMP) == 0 &&
505 (vp->v_type == VREG ||
506 (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
507 int updflags = 0;
508
509 if ((vp->v_mount->mnt_flag & MNT_NOATIME) == 0) {
510 updflags = GOP_UPDATE_ACCESSED;
511 }
512 if (write) {
513 updflags |= GOP_UPDATE_MODIFIED;
514 }
515 if (updflags != 0) {
516 GOP_MARKUPDATE(vp, updflags);
517 }
518 }
519
520 if (write) {
521 gp->g_dirtygen++;
522 if ((vp->v_flag & VONWORKLST) == 0) {
523 vn_syncer_add_to_worklist(vp, filedelay);
524 }
525 if ((vp->v_flag & (VWRITEMAP|VWRITEMAPDIRTY)) == VWRITEMAP) {
526 vp->v_flag |= VWRITEMAPDIRTY;
527 }
528 }
529
530 /*
531 * For PGO_LOCKED requests, just return whatever's in memory.
532 */
533
534 if (flags & PGO_LOCKED) {
535 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
536 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
537
538 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
539 }
540
541 /*
542 * find the requested pages and make some simple checks.
543 * leave space in the page array for a whole block.
544 */
545
546 if (vp->v_type == VREG) {
547 fs_bshift = vp->v_mount->mnt_fs_bshift;
548 dev_bshift = vp->v_mount->mnt_dev_bshift;
549 } else {
550 fs_bshift = DEV_BSHIFT;
551 dev_bshift = DEV_BSHIFT;
552 }
553 fs_bsize = 1 << fs_bshift;
554
555 orignpages = MIN(orignpages,
556 round_page(memeof - origoffset) >> PAGE_SHIFT);
557 npages = orignpages;
558 startoffset = origoffset & ~(fs_bsize - 1);
559 endoffset = round_page((origoffset + (npages << PAGE_SHIFT) +
560 fs_bsize - 1) & ~(fs_bsize - 1));
561 endoffset = MIN(endoffset, round_page(memeof));
562 ridx = (origoffset - startoffset) >> PAGE_SHIFT;
563
564 pgs_size = sizeof(struct vm_page *) *
565 ((endoffset - startoffset) >> PAGE_SHIFT);
566 if (pgs_size > sizeof(pgs_onstack)) {
567 pgs = malloc(pgs_size, M_DEVBUF, M_NOWAIT | M_ZERO);
568 if (pgs == NULL) {
569 simple_unlock(&uobj->vmobjlock);
570 return (ENOMEM);
571 }
572 } else {
573 pgs = pgs_onstack;
574 memset(pgs, 0, pgs_size);
575 }
576 UVMHIST_LOG(ubchist, "ridx %d npages %d startoff %ld endoff %ld",
577 ridx, npages, startoffset, endoffset);
578 if (uvn_findpages(uobj, origoffset, &npages, &pgs[ridx],
579 async ? UFP_NOWAIT : UFP_ALL) != orignpages) {
580 KASSERT(async != 0);
581 genfs_rel_pages(&pgs[ridx], orignpages);
582 simple_unlock(&uobj->vmobjlock);
583 if (pgs != pgs_onstack)
584 free(pgs, M_DEVBUF);
585 return (EBUSY);
586 }
587
588 /*
589 * if the pages are already resident, just return them.
590 */
591
592 for (i = 0; i < npages; i++) {
593 struct vm_page *pg1 = pgs[ridx + i];
594
595 if ((pg1->flags & PG_FAKE) ||
596 (blockalloc && (pg1->flags & PG_RDONLY))) {
597 break;
598 }
599 }
600 if (i == npages) {
601 UVMHIST_LOG(ubchist, "returning cached pages", 0,0,0,0);
602 raoffset = origoffset + (orignpages << PAGE_SHIFT);
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 (flags & PGO_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 s = splbio();
669 mbp = pool_get(&bufpool, PR_WAITOK);
670 splx(s);
671 BUF_INIT(mbp);
672 mbp->b_bufsize = totalbytes;
673 mbp->b_data = (void *)kva;
674 mbp->b_resid = mbp->b_bcount = bytes;
675 mbp->b_flags = B_BUSY|B_READ| (async ? B_CALL|B_ASYNC : 0);
676 mbp->b_iodone = (async ? uvm_aio_biodone : 0);
677 mbp->b_vp = vp;
678
679 /*
680 * if EOF is in the middle of the range, zero the part past EOF.
681 * if the page including EOF is not PG_FAKE, skip over it since
682 * in that case it has valid data that we need to preserve.
683 */
684
685 if (tailbytes > 0) {
686 size_t tailstart = bytes;
687
688 if ((pgs[bytes >> PAGE_SHIFT]->flags & PG_FAKE) == 0) {
689 tailstart = round_page(tailstart);
690 tailbytes -= tailstart - bytes;
691 }
692 UVMHIST_LOG(ubchist, "tailbytes %p 0x%x 0x%x",
693 kva, tailstart, tailbytes,0);
694 memset((void *)(kva + tailstart), 0, tailbytes);
695 }
696
697 /*
698 * now loop over the pages, reading as needed.
699 */
700
701 if (blockalloc) {
702 lockmgr(&gp->g_glock, LK_EXCLUSIVE, NULL);
703 } else {
704 lockmgr(&gp->g_glock, LK_SHARED, NULL);
705 }
706
707 bp = NULL;
708 for (offset = startoffset;
709 bytes > 0;
710 offset += iobytes, bytes -= iobytes) {
711
712 /*
713 * skip pages which don't need to be read.
714 */
715
716 pidx = (offset - startoffset) >> PAGE_SHIFT;
717 while ((pgs[pidx]->flags & PG_FAKE) == 0) {
718 size_t b;
719
720 KASSERT((offset & (PAGE_SIZE - 1)) == 0);
721 if ((pgs[pidx]->flags & PG_RDONLY)) {
722 sawhole = TRUE;
723 }
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. unless we are going to allocate blocks,
772 * mark the 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 }
789 if (!blockalloc) {
790 pgs[pidx + i]->flags |= PG_RDONLY;
791 }
792 }
793 continue;
794 }
795
796 /*
797 * allocate a sub-buf for this piece of the i/o
798 * (or just use mbp if there's only 1 piece),
799 * and start it going.
800 */
801
802 if (offset == startoffset && iobytes == bytes) {
803 bp = mbp;
804 } else {
805 s = splbio();
806 bp = pool_get(&bufpool, PR_WAITOK);
807 splx(s);
808 BUF_INIT(bp);
809 bp->b_data = (char *)kva + offset - startoffset;
810 bp->b_resid = bp->b_bcount = iobytes;
811 bp->b_flags = B_BUSY|B_READ|B_CALL|B_ASYNC;
812 bp->b_iodone = uvm_aio_biodone1;
813 bp->b_vp = vp;
814 bp->b_proc = NULL;
815 }
816 bp->b_lblkno = 0;
817 bp->b_private = mbp;
818
819 /* adjust physical blkno for partial blocks */
820 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
821 dev_bshift);
822
823 UVMHIST_LOG(ubchist,
824 "bp %p offset 0x%x bcount 0x%x blkno 0x%x",
825 bp, offset, iobytes, bp->b_blkno);
826
827 if (async)
828 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
829 else
830 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
831
832 VOP_STRATEGY(devvp, bp);
833 }
834
835 loopdone:
836 if (skipbytes) {
837 s = splbio();
838 if (error) {
839 mbp->b_flags |= B_ERROR;
840 mbp->b_error = error;
841 }
842 mbp->b_resid -= skipbytes;
843 if (mbp->b_resid == 0) {
844 biodone(mbp);
845 }
846 splx(s);
847 }
848
849 if (async) {
850 UVMHIST_LOG(ubchist, "returning 0 (async)",0,0,0,0);
851 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
852 if (pgs != pgs_onstack)
853 free(pgs, M_DEVBUF);
854 return (0);
855 }
856 if (bp != NULL) {
857 error = biowait(mbp);
858 }
859 s = splbio();
860 pool_put(&bufpool, mbp);
861 splx(s);
862 uvm_pagermapout(kva, npages);
863 raoffset = startoffset + totalbytes;
864
865 /*
866 * if this we encountered a hole then we have to do a little more work.
867 * for read faults, we marked the page PG_RDONLY so that future
868 * write accesses to the page will fault again.
869 * for write faults, we must make sure that the backing store for
870 * the page is completely allocated while the pages are locked.
871 */
872
873 if (!error && sawhole && blockalloc) {
874 error = GOP_ALLOC(vp, startoffset, npages << PAGE_SHIFT, 0,
875 cred);
876 UVMHIST_LOG(ubchist, "gop_alloc off 0x%x/0x%x -> %d",
877 startoffset, npages << PAGE_SHIFT, error,0);
878 if (!error) {
879 for (i = 0; i < npages; i++) {
880 if (pgs[i] == NULL) {
881 continue;
882 }
883 pgs[i]->flags &= ~(PG_CLEAN|PG_RDONLY);
884 UVMHIST_LOG(ubchist, "mark dirty pg %p",
885 pgs[i],0,0,0);
886 }
887 }
888 }
889 lockmgr(&gp->g_glock, LK_RELEASE, NULL);
890 simple_lock(&uobj->vmobjlock);
891
892 /*
893 * we're almost done! release the pages...
894 * for errors, we free the pages.
895 * otherwise we activate them and mark them as valid and clean.
896 * also, unbusy pages that were not actually requested.
897 */
898
899 if (error) {
900 for (i = 0; i < npages; i++) {
901 if (pgs[i] == NULL) {
902 continue;
903 }
904 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
905 pgs[i], pgs[i]->flags, 0,0);
906 if (pgs[i]->flags & PG_FAKE) {
907 pgs[i]->flags |= PG_RELEASED;
908 }
909 }
910 uvm_lock_pageq();
911 uvm_page_unbusy(pgs, npages);
912 uvm_unlock_pageq();
913 simple_unlock(&uobj->vmobjlock);
914 UVMHIST_LOG(ubchist, "returning error %d", error,0,0,0);
915 if (pgs != pgs_onstack)
916 free(pgs, M_DEVBUF);
917 return (error);
918 }
919
920 out:
921 UVMHIST_LOG(ubchist, "succeeding, npages %d", npages,0,0,0);
922 uvm_lock_pageq();
923 for (i = 0; i < npages; i++) {
924 pg = pgs[i];
925 if (pg == NULL) {
926 continue;
927 }
928 UVMHIST_LOG(ubchist, "examining pg %p flags 0x%x",
929 pg, pg->flags, 0,0);
930 if (pg->flags & PG_FAKE && !overwrite) {
931 pg->flags &= ~(PG_FAKE);
932 pmap_clear_modify(pgs[i]);
933 }
934 KASSERT(!write || !blockalloc || (pg->flags & PG_RDONLY) == 0);
935 if (i < ridx || i >= ridx + orignpages || async) {
936 UVMHIST_LOG(ubchist, "unbusy pg %p offset 0x%x",
937 pg, pg->offset,0,0);
938 if (pg->flags & PG_WANTED) {
939 wakeup(pg);
940 }
941 if (pg->flags & PG_FAKE) {
942 KASSERT(overwrite);
943 uvm_pagezero(pg);
944 }
945 if (pg->flags & PG_RELEASED) {
946 uvm_pagefree(pg);
947 continue;
948 }
949 uvm_pageactivate(pg);
950 pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
951 UVM_PAGE_OWN(pg, NULL);
952 }
953 }
954 uvm_unlock_pageq();
955 simple_unlock(&uobj->vmobjlock);
956 if (ap->a_m != NULL) {
957 memcpy(ap->a_m, &pgs[ridx],
958 orignpages * sizeof(struct vm_page *));
959 }
960 if (pgs != pgs_onstack)
961 free(pgs, M_DEVBUF);
962 return (0);
963 }
964
965 /*
966 * generic VM putpages routine.
967 * Write the given range of pages to backing store.
968 *
969 * => "offhi == 0" means flush all pages at or after "offlo".
970 * => object should be locked by caller. we may _unlock_ the object
971 * if (and only if) we need to clean a page (PGO_CLEANIT), or
972 * if PGO_SYNCIO is set and there are pages busy.
973 * we return with the object locked.
974 * => if PGO_CLEANIT or PGO_SYNCIO is set, we may block (due to I/O).
975 * thus, a caller might want to unlock higher level resources
976 * (e.g. vm_map) before calling flush.
977 * => if neither PGO_CLEANIT nor PGO_SYNCIO is set, then we will neither
978 * unlock the object nor block.
979 * => if PGO_ALLPAGES is set, then all pages in the object will be processed.
980 * => NOTE: we rely on the fact that the object's memq is a TAILQ and
981 * that new pages are inserted on the tail end of the list. thus,
982 * we can make a complete pass through the object in one go by starting
983 * at the head and working towards the tail (new pages are put in
984 * front of us).
985 * => NOTE: we are allowed to lock the page queues, so the caller
986 * must not be holding the page queue lock.
987 *
988 * note on "cleaning" object and PG_BUSY pages:
989 * this routine is holding the lock on the object. the only time
990 * that it can run into a PG_BUSY page that it does not own is if
991 * some other process has started I/O on the page (e.g. either
992 * a pagein, or a pageout). if the PG_BUSY page is being paged
993 * in, then it can not be dirty (!PG_CLEAN) because no one has
994 * had a chance to modify it yet. if the PG_BUSY page is being
995 * paged out then it means that someone else has already started
996 * cleaning the page for us (how nice!). in this case, if we
997 * have syncio specified, then after we make our pass through the
998 * object we need to wait for the other PG_BUSY pages to clear
999 * off (i.e. we need to do an iosync). also note that once a
1000 * page is PG_BUSY it must stay in its object until it is un-busyed.
1001 *
1002 * note on page traversal:
1003 * we can traverse the pages in an object either by going down the
1004 * linked list in "uobj->memq", or we can go over the address range
1005 * by page doing hash table lookups for each address. depending
1006 * on how many pages are in the object it may be cheaper to do one
1007 * or the other. we set "by_list" to true if we are using memq.
1008 * if the cost of a hash lookup was equal to the cost of the list
1009 * traversal we could compare the number of pages in the start->stop
1010 * range to the total number of pages in the object. however, it
1011 * seems that a hash table lookup is more expensive than the linked
1012 * list traversal, so we multiply the number of pages in the
1013 * range by an estimate of the relatively higher cost of the hash lookup.
1014 */
1015
1016 int
1017 genfs_putpages(void *v)
1018 {
1019 struct vop_putpages_args /* {
1020 struct vnode *a_vp;
1021 voff_t a_offlo;
1022 voff_t a_offhi;
1023 int a_flags;
1024 } */ *ap = v;
1025 struct vnode *vp = ap->a_vp;
1026 struct uvm_object *uobj = &vp->v_uobj;
1027 struct simplelock *slock = &uobj->vmobjlock;
1028 off_t startoff = ap->a_offlo;
1029 off_t endoff = ap->a_offhi;
1030 off_t off;
1031 int flags = ap->a_flags;
1032 /* Even for strange MAXPHYS, the shift rounds down to a page */
1033 const int maxpages = MAXPHYS >> PAGE_SHIFT;
1034 int i, s, error, npages, nback;
1035 int freeflag;
1036 struct vm_page *pgs[maxpages], *pg, *nextpg, *tpg, curmp, endmp;
1037 boolean_t wasclean, by_list, needs_clean, yld;
1038 boolean_t async = (flags & PGO_SYNCIO) == 0;
1039 boolean_t pagedaemon = curproc == uvm.pagedaemon_proc;
1040 struct lwp *l = curlwp ? curlwp : &lwp0;
1041 struct genfs_node *gp = VTOG(vp);
1042 int dirtygen;
1043 boolean_t modified = FALSE;
1044 boolean_t cleanall;
1045
1046 UVMHIST_FUNC("genfs_putpages"); UVMHIST_CALLED(ubchist);
1047
1048 KASSERT(flags & (PGO_CLEANIT|PGO_FREE|PGO_DEACTIVATE));
1049 KASSERT((startoff & PAGE_MASK) == 0 && (endoff & PAGE_MASK) == 0);
1050 KASSERT(startoff < endoff || endoff == 0);
1051
1052 UVMHIST_LOG(ubchist, "vp %p pages %d off 0x%x len 0x%x",
1053 vp, uobj->uo_npages, startoff, endoff - startoff);
1054
1055 KASSERT((vp->v_flag & VONWORKLST) != 0 ||
1056 (vp->v_flag & VWRITEMAPDIRTY) == 0);
1057 if (uobj->uo_npages == 0) {
1058 s = splbio();
1059 if (vp->v_flag & VONWORKLST) {
1060 vp->v_flag &= ~VWRITEMAPDIRTY;
1061 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
1062 vp->v_flag &= ~VONWORKLST;
1063 LIST_REMOVE(vp, v_synclist);
1064 }
1065 }
1066 splx(s);
1067 simple_unlock(slock);
1068 return (0);
1069 }
1070
1071 /*
1072 * the vnode has pages, set up to process the request.
1073 */
1074
1075 error = 0;
1076 s = splbio();
1077 simple_lock(&global_v_numoutput_slock);
1078 wasclean = (vp->v_numoutput == 0);
1079 simple_unlock(&global_v_numoutput_slock);
1080 splx(s);
1081 off = startoff;
1082 if (endoff == 0 || flags & PGO_ALLPAGES) {
1083 endoff = trunc_page(LLONG_MAX);
1084 }
1085 by_list = (uobj->uo_npages <=
1086 ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
1087
1088 #if !defined(DEBUG)
1089 /*
1090 * if this vnode is known not to have dirty pages,
1091 * don't bother to clean it out.
1092 */
1093
1094 if ((vp->v_flag & VONWORKLST) == 0) {
1095 if ((flags & (PGO_FREE|PGO_DEACTIVATE)) == 0) {
1096 goto skip_scan;
1097 }
1098 flags &= ~PGO_CLEANIT;
1099 }
1100 #endif /* !defined(DEBUG) */
1101
1102 /*
1103 * start the loop. when scanning by list, hold the last page
1104 * in the list before we start. pages allocated after we start
1105 * will be added to the end of the list, so we can stop at the
1106 * current last page.
1107 */
1108
1109 cleanall = (flags & PGO_CLEANIT) != 0 && wasclean &&
1110 startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
1111 (vp->v_flag & VONWORKLST) != 0;
1112 dirtygen = gp->g_dirtygen;
1113 freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
1114 if (by_list) {
1115 curmp.uobject = uobj;
1116 curmp.offset = (voff_t)-1;
1117 curmp.flags = PG_BUSY;
1118 endmp.uobject = uobj;
1119 endmp.offset = (voff_t)-1;
1120 endmp.flags = PG_BUSY;
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 if (pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1145 wasclean = FALSE;
1146 }
1147 pg = TAILQ_NEXT(pg, listq);
1148 continue;
1149 }
1150 off = pg->offset;
1151 } else if (pg == NULL || pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1152 if (pg != NULL) {
1153 wasclean = FALSE;
1154 }
1155 off += PAGE_SIZE;
1156 if (off < endoff) {
1157 pg = uvm_pagelookup(uobj, off);
1158 }
1159 continue;
1160 }
1161
1162 /*
1163 * if the current page needs to be cleaned and it's busy,
1164 * wait for it to become unbusy.
1165 */
1166
1167 yld = (l->l_cpu->ci_schedstate.spc_flags &
1168 SPCF_SHOULDYIELD) && !pagedaemon;
1169 if (pg->flags & PG_BUSY || yld) {
1170 UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
1171 if (flags & PGO_BUSYFAIL && pg->flags & PG_BUSY) {
1172 UVMHIST_LOG(ubchist, "busyfail %p", pg, 0,0,0);
1173 error = EDEADLK;
1174 break;
1175 }
1176 KASSERT(!pagedaemon);
1177 if (by_list) {
1178 TAILQ_INSERT_BEFORE(pg, &curmp, listq);
1179 UVMHIST_LOG(ubchist, "curmp next %p",
1180 TAILQ_NEXT(&curmp, listq), 0,0,0);
1181 }
1182 if (yld) {
1183 simple_unlock(slock);
1184 preempt(1);
1185 simple_lock(slock);
1186 } else {
1187 pg->flags |= PG_WANTED;
1188 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
1189 simple_lock(slock);
1190 }
1191 if (by_list) {
1192 UVMHIST_LOG(ubchist, "after next %p",
1193 TAILQ_NEXT(&curmp, listq), 0,0,0);
1194 pg = TAILQ_NEXT(&curmp, listq);
1195 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1196 } else {
1197 pg = uvm_pagelookup(uobj, off);
1198 }
1199 continue;
1200 }
1201
1202 /*
1203 * if we're freeing, remove all mappings of the page now.
1204 * if we're cleaning, check if the page is needs to be cleaned.
1205 */
1206
1207 if (flags & PGO_FREE) {
1208 pmap_page_protect(pg, VM_PROT_NONE);
1209 } else if (flags & PGO_CLEANIT) {
1210
1211 /*
1212 * if we still have some hope to pull this vnode off
1213 * from the syncer queue, write-protect the page.
1214 */
1215
1216 if (cleanall && wasclean &&
1217 gp->g_dirtygen == dirtygen) {
1218
1219 /*
1220 * uobj pages get wired only by uvm_fault
1221 * where uobj is locked.
1222 */
1223
1224 if (pg->wire_count == 0) {
1225 pmap_page_protect(pg,
1226 VM_PROT_READ|VM_PROT_EXECUTE);
1227 } else {
1228 cleanall = FALSE;
1229 }
1230 }
1231 }
1232
1233 if (flags & PGO_CLEANIT) {
1234 needs_clean = pmap_clear_modify(pg) ||
1235 (pg->flags & PG_CLEAN) == 0;
1236 pg->flags |= PG_CLEAN;
1237 } else {
1238 needs_clean = FALSE;
1239 }
1240
1241 /*
1242 * if we're cleaning, build a cluster.
1243 * the cluster will consist of pages which are currently dirty,
1244 * but they will be returned to us marked clean.
1245 * if not cleaning, just operate on the one page.
1246 */
1247
1248 if (needs_clean) {
1249 KDASSERT((vp->v_flag & VONWORKLST));
1250 wasclean = FALSE;
1251 memset(pgs, 0, sizeof(pgs));
1252 pg->flags |= PG_BUSY;
1253 UVM_PAGE_OWN(pg, "genfs_putpages");
1254
1255 /*
1256 * first look backward.
1257 */
1258
1259 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1260 nback = npages;
1261 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1262 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1263 if (nback) {
1264 memmove(&pgs[0], &pgs[npages - nback],
1265 nback * sizeof(pgs[0]));
1266 if (npages - nback < nback)
1267 memset(&pgs[nback], 0,
1268 (npages - nback) * sizeof(pgs[0]));
1269 else
1270 memset(&pgs[npages - nback], 0,
1271 nback * sizeof(pgs[0]));
1272 }
1273
1274 /*
1275 * then plug in our page of interest.
1276 */
1277
1278 pgs[nback] = pg;
1279
1280 /*
1281 * then look forward to fill in the remaining space in
1282 * the array of pages.
1283 */
1284
1285 npages = maxpages - nback - 1;
1286 uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1287 &pgs[nback + 1],
1288 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1289 npages += nback + 1;
1290 } else {
1291 pgs[0] = pg;
1292 npages = 1;
1293 nback = 0;
1294 }
1295
1296 /*
1297 * apply FREE or DEACTIVATE options if requested.
1298 */
1299
1300 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1301 uvm_lock_pageq();
1302 }
1303 for (i = 0; i < npages; i++) {
1304 tpg = pgs[i];
1305 KASSERT(tpg->uobject == uobj);
1306 if (by_list && tpg == TAILQ_NEXT(pg, listq))
1307 pg = tpg;
1308 if (tpg->offset < startoff || tpg->offset >= endoff)
1309 continue;
1310 if (flags & PGO_DEACTIVATE &&
1311 (tpg->pqflags & PQ_INACTIVE) == 0 &&
1312 tpg->wire_count == 0) {
1313 (void) pmap_clear_reference(tpg);
1314 uvm_pagedeactivate(tpg);
1315 } else if (flags & PGO_FREE) {
1316 pmap_page_protect(tpg, VM_PROT_NONE);
1317 if (tpg->flags & PG_BUSY) {
1318 tpg->flags |= freeflag;
1319 if (pagedaemon) {
1320 uvmexp.paging++;
1321 uvm_pagedequeue(tpg);
1322 }
1323 } else {
1324
1325 /*
1326 * ``page is not busy''
1327 * implies that npages is 1
1328 * and needs_clean is false.
1329 */
1330
1331 nextpg = TAILQ_NEXT(tpg, listq);
1332 uvm_pagefree(tpg);
1333 if (pagedaemon)
1334 uvmexp.pdfreed++;
1335 }
1336 }
1337 }
1338 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1339 uvm_unlock_pageq();
1340 }
1341 if (needs_clean) {
1342 modified = TRUE;
1343
1344 /*
1345 * start the i/o. if we're traversing by list,
1346 * keep our place in the list with a marker page.
1347 */
1348
1349 if (by_list) {
1350 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1351 listq);
1352 }
1353 simple_unlock(slock);
1354 error = GOP_WRITE(vp, pgs, npages, flags);
1355 simple_lock(slock);
1356 if (by_list) {
1357 pg = TAILQ_NEXT(&curmp, listq);
1358 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1359 }
1360 if (error) {
1361 break;
1362 }
1363 if (by_list) {
1364 continue;
1365 }
1366 }
1367
1368 /*
1369 * find the next page and continue if there was no error.
1370 */
1371
1372 if (by_list) {
1373 if (nextpg) {
1374 pg = nextpg;
1375 nextpg = NULL;
1376 } else {
1377 pg = TAILQ_NEXT(pg, listq);
1378 }
1379 } else {
1380 off += (npages - nback) << PAGE_SHIFT;
1381 if (off < endoff) {
1382 pg = uvm_pagelookup(uobj, off);
1383 }
1384 }
1385 }
1386 if (by_list) {
1387 TAILQ_REMOVE(&uobj->memq, &endmp, listq);
1388 PRELE(l);
1389 }
1390
1391 if (modified && (vp->v_flag & VWRITEMAPDIRTY) != 0 &&
1392 (vp->v_type == VREG ||
1393 (vp->v_mount->mnt_flag & MNT_NODEVMTIME) == 0)) {
1394 GOP_MARKUPDATE(vp, GOP_UPDATE_MODIFIED);
1395 }
1396
1397 /*
1398 * if we're cleaning and there was nothing to clean,
1399 * take us off the syncer list. if we started any i/o
1400 * and we're doing sync i/o, wait for all writes to finish.
1401 */
1402
1403 s = splbio();
1404 if (cleanall && wasclean && gp->g_dirtygen == dirtygen &&
1405 (vp->v_flag & VONWORKLST) != 0) {
1406 vp->v_flag &= ~VWRITEMAPDIRTY;
1407 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL) {
1408 vp->v_flag &= ~VONWORKLST;
1409 LIST_REMOVE(vp, v_synclist);
1410 }
1411 }
1412 splx(s);
1413
1414 #if !defined(DEBUG)
1415 skip_scan:
1416 #endif /* !defined(DEBUG) */
1417 if (!wasclean && !async) {
1418 s = splbio();
1419 /*
1420 * XXX - we want simple_unlock(&global_v_numoutput_slock);
1421 * but the slot in ltsleep() is taken!
1422 * XXX - try to recover from missed wakeups with a timeout..
1423 * must think of something better.
1424 */
1425 while (vp->v_numoutput != 0) {
1426 vp->v_flag |= VBWAIT;
1427 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, FALSE,
1428 "genput2", hz);
1429 simple_lock(slock);
1430 }
1431 splx(s);
1432 }
1433 simple_unlock(&uobj->vmobjlock);
1434 return (error);
1435 }
1436
1437 int
1438 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1439 {
1440 int s, error, run;
1441 int fs_bshift, dev_bshift;
1442 vaddr_t kva;
1443 off_t eof, offset, startoffset;
1444 size_t bytes, iobytes, skipbytes;
1445 daddr_t lbn, blkno;
1446 struct vm_page *pg;
1447 struct buf *mbp, *bp;
1448 struct vnode *devvp;
1449 boolean_t async = (flags & PGO_SYNCIO) == 0;
1450 UVMHIST_FUNC("genfs_gop_write"); UVMHIST_CALLED(ubchist);
1451
1452 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1453 vp, pgs, npages, flags);
1454
1455 GOP_SIZE(vp, vp->v_size, &eof, GOP_SIZE_WRITE);
1456 if (vp->v_type == VREG) {
1457 fs_bshift = vp->v_mount->mnt_fs_bshift;
1458 dev_bshift = vp->v_mount->mnt_dev_bshift;
1459 } else {
1460 fs_bshift = DEV_BSHIFT;
1461 dev_bshift = DEV_BSHIFT;
1462 }
1463 error = 0;
1464 pg = pgs[0];
1465 startoffset = pg->offset;
1466 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1467 skipbytes = 0;
1468 KASSERT(bytes != 0);
1469
1470 kva = uvm_pagermapin(pgs, npages,
1471 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1472
1473 s = splbio();
1474 simple_lock(&global_v_numoutput_slock);
1475 vp->v_numoutput += 2;
1476 simple_unlock(&global_v_numoutput_slock);
1477 mbp = pool_get(&bufpool, PR_WAITOK);
1478 BUF_INIT(mbp);
1479 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1480 vp, mbp, vp->v_numoutput, bytes);
1481 splx(s);
1482 mbp->b_bufsize = npages << PAGE_SHIFT;
1483 mbp->b_data = (void *)kva;
1484 mbp->b_resid = mbp->b_bcount = bytes;
1485 mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? (B_CALL|B_ASYNC) : 0);
1486 mbp->b_iodone = uvm_aio_biodone;
1487 mbp->b_vp = vp;
1488
1489 bp = NULL;
1490 for (offset = startoffset;
1491 bytes > 0;
1492 offset += iobytes, bytes -= iobytes) {
1493 lbn = offset >> fs_bshift;
1494 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1495 if (error) {
1496 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1497 skipbytes += bytes;
1498 bytes = 0;
1499 break;
1500 }
1501
1502 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1503 bytes);
1504 if (blkno == (daddr_t)-1) {
1505 skipbytes += iobytes;
1506 continue;
1507 }
1508
1509 /* if it's really one i/o, don't make a second buf */
1510 if (offset == startoffset && iobytes == bytes) {
1511 bp = mbp;
1512 } else {
1513 s = splbio();
1514 V_INCR_NUMOUTPUT(vp);
1515 bp = pool_get(&bufpool, PR_WAITOK);
1516 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1517 vp, bp, vp->v_numoutput, 0);
1518 splx(s);
1519 BUF_INIT(bp);
1520 bp->b_data = (char *)kva +
1521 (vaddr_t)(offset - pg->offset);
1522 bp->b_resid = bp->b_bcount = iobytes;
1523 bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1524 bp->b_iodone = uvm_aio_biodone1;
1525 bp->b_vp = vp;
1526 }
1527 bp->b_lblkno = 0;
1528 bp->b_private = mbp;
1529
1530 /* adjust physical blkno for partial blocks */
1531 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1532 dev_bshift);
1533 UVMHIST_LOG(ubchist,
1534 "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1535 vp, offset, bp->b_bcount, bp->b_blkno);
1536 if (curproc == uvm.pagedaemon_proc)
1537 BIO_SETPRIO(bp, BPRIO_TIMELIMITED);
1538 else if (async)
1539 BIO_SETPRIO(bp, BPRIO_TIMENONCRITICAL);
1540 else
1541 BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
1542
1543 VOP_STRATEGY(devvp, bp);
1544 }
1545 if (skipbytes) {
1546 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1547 s = splbio();
1548 if (error) {
1549 mbp->b_flags |= B_ERROR;
1550 mbp->b_error = error;
1551 }
1552 mbp->b_resid -= skipbytes;
1553 if (mbp->b_resid == 0) {
1554 biodone(mbp);
1555 }
1556 splx(s);
1557 }
1558 if (async) {
1559 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1560 return (0);
1561 }
1562 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1563 error = biowait(mbp);
1564 uvm_aio_aiodone(mbp);
1565 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1566 return (error);
1567 }
1568
1569 /*
1570 * VOP_PUTPAGES() for vnodes which never have pages.
1571 */
1572
1573 int
1574 genfs_null_putpages(void *v)
1575 {
1576 struct vop_putpages_args /* {
1577 struct vnode *a_vp;
1578 voff_t a_offlo;
1579 voff_t a_offhi;
1580 int a_flags;
1581 } */ *ap = v;
1582 struct vnode *vp = ap->a_vp;
1583
1584 KASSERT(vp->v_uobj.uo_npages == 0);
1585 simple_unlock(&vp->v_interlock);
1586 return (0);
1587 }
1588
1589 void
1590 genfs_node_init(struct vnode *vp, const struct genfs_ops *ops)
1591 {
1592 struct genfs_node *gp = VTOG(vp);
1593
1594 lockinit(&gp->g_glock, PINOD, "glock", 0, 0);
1595 gp->g_op = ops;
1596 }
1597
1598 void
1599 genfs_size(struct vnode *vp, off_t size, off_t *eobp, int flags)
1600 {
1601 int bsize;
1602
1603 bsize = 1 << vp->v_mount->mnt_fs_bshift;
1604 *eobp = (size + bsize - 1) & ~(bsize - 1);
1605 }
1606
1607 int
1608 genfs_compat_getpages(void *v)
1609 {
1610 struct vop_getpages_args /* {
1611 struct vnode *a_vp;
1612 voff_t a_offset;
1613 struct vm_page **a_m;
1614 int *a_count;
1615 int a_centeridx;
1616 vm_prot_t a_access_type;
1617 int a_advice;
1618 int a_flags;
1619 } */ *ap = v;
1620
1621 off_t origoffset;
1622 struct vnode *vp = ap->a_vp;
1623 struct uvm_object *uobj = &vp->v_uobj;
1624 struct vm_page *pg, **pgs;
1625 vaddr_t kva;
1626 int i, error, orignpages, npages;
1627 struct iovec iov;
1628 struct uio uio;
1629 struct ucred *cred = curproc->p_ucred;
1630 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1631
1632 error = 0;
1633 origoffset = ap->a_offset;
1634 orignpages = *ap->a_count;
1635 pgs = ap->a_m;
1636
1637 if (write && (vp->v_flag & VONWORKLST) == 0) {
1638 vn_syncer_add_to_worklist(vp, filedelay);
1639 }
1640 if (ap->a_flags & PGO_LOCKED) {
1641 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1642 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
1643
1644 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1645 }
1646 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1647 simple_unlock(&uobj->vmobjlock);
1648 return (EINVAL);
1649 }
1650 if ((ap->a_flags & PGO_SYNCIO) == 0) {
1651 return 0;
1652 }
1653 npages = orignpages;
1654 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1655 simple_unlock(&uobj->vmobjlock);
1656 kva = uvm_pagermapin(pgs, npages,
1657 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1658 for (i = 0; i < npages; i++) {
1659 pg = pgs[i];
1660 if ((pg->flags & PG_FAKE) == 0) {
1661 continue;
1662 }
1663 iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1664 iov.iov_len = PAGE_SIZE;
1665 uio.uio_iov = &iov;
1666 uio.uio_iovcnt = 1;
1667 uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1668 uio.uio_segflg = UIO_SYSSPACE;
1669 uio.uio_rw = UIO_READ;
1670 uio.uio_resid = PAGE_SIZE;
1671 uio.uio_lwp = NULL;
1672 /* XXX vn_lock */
1673 error = VOP_READ(vp, &uio, 0, cred);
1674 if (error) {
1675 break;
1676 }
1677 if (uio.uio_resid) {
1678 memset(iov.iov_base, 0, uio.uio_resid);
1679 }
1680 }
1681 uvm_pagermapout(kva, npages);
1682 simple_lock(&uobj->vmobjlock);
1683 uvm_lock_pageq();
1684 for (i = 0; i < npages; i++) {
1685 pg = pgs[i];
1686 if (error && (pg->flags & PG_FAKE) != 0) {
1687 pg->flags |= PG_RELEASED;
1688 } else {
1689 pmap_clear_modify(pg);
1690 uvm_pageactivate(pg);
1691 }
1692 }
1693 if (error) {
1694 uvm_page_unbusy(pgs, npages);
1695 }
1696 uvm_unlock_pageq();
1697 simple_unlock(&uobj->vmobjlock);
1698 return (error);
1699 }
1700
1701 int
1702 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1703 int flags)
1704 {
1705 off_t offset;
1706 struct iovec iov;
1707 struct uio uio;
1708 struct ucred *cred = curproc->p_ucred;
1709 struct buf *bp;
1710 vaddr_t kva;
1711 int s, error;
1712
1713 offset = pgs[0]->offset;
1714 kva = uvm_pagermapin(pgs, npages,
1715 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1716
1717 iov.iov_base = (void *)kva;
1718 iov.iov_len = npages << PAGE_SHIFT;
1719 uio.uio_iov = &iov;
1720 uio.uio_iovcnt = 1;
1721 uio.uio_offset = offset;
1722 uio.uio_segflg = UIO_SYSSPACE;
1723 uio.uio_rw = UIO_WRITE;
1724 uio.uio_resid = npages << PAGE_SHIFT;
1725 uio.uio_lwp = NULL;
1726 /* XXX vn_lock */
1727 error = VOP_WRITE(vp, &uio, 0, cred);
1728
1729 s = splbio();
1730 V_INCR_NUMOUTPUT(vp);
1731 bp = pool_get(&bufpool, PR_WAITOK);
1732 splx(s);
1733
1734 BUF_INIT(bp);
1735 bp->b_flags = B_BUSY | B_WRITE | B_AGE;
1736 bp->b_vp = vp;
1737 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1738 bp->b_data = (char *)kva;
1739 bp->b_bcount = npages << PAGE_SHIFT;
1740 bp->b_bufsize = npages << PAGE_SHIFT;
1741 bp->b_resid = 0;
1742 if (error) {
1743 bp->b_flags |= B_ERROR;
1744 bp->b_error = error;
1745 }
1746 uvm_aio_aiodone(bp);
1747 return (error);
1748 }
1749
1750 static void
1751 filt_genfsdetach(struct knote *kn)
1752 {
1753 struct vnode *vp = (struct vnode *)kn->kn_hook;
1754
1755 /* XXXLUKEM lock the struct? */
1756 SLIST_REMOVE(&vp->v_klist, kn, knote, kn_selnext);
1757 }
1758
1759 static int
1760 filt_genfsread(struct knote *kn, long hint)
1761 {
1762 struct vnode *vp = (struct vnode *)kn->kn_hook;
1763
1764 /*
1765 * filesystem is gone, so set the EOF flag and schedule
1766 * the knote for deletion.
1767 */
1768 if (hint == NOTE_REVOKE) {
1769 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1770 return (1);
1771 }
1772
1773 /* XXXLUKEM lock the struct? */
1774 kn->kn_data = vp->v_size - kn->kn_fp->f_offset;
1775 return (kn->kn_data != 0);
1776 }
1777
1778 static int
1779 filt_genfsvnode(struct knote *kn, long hint)
1780 {
1781
1782 if (kn->kn_sfflags & hint)
1783 kn->kn_fflags |= hint;
1784 if (hint == NOTE_REVOKE) {
1785 kn->kn_flags |= EV_EOF;
1786 return (1);
1787 }
1788 return (kn->kn_fflags != 0);
1789 }
1790
1791 static const struct filterops genfsread_filtops =
1792 { 1, NULL, filt_genfsdetach, filt_genfsread };
1793 static const struct filterops genfsvnode_filtops =
1794 { 1, NULL, filt_genfsdetach, filt_genfsvnode };
1795
1796 int
1797 genfs_kqfilter(void *v)
1798 {
1799 struct vop_kqfilter_args /* {
1800 struct vnode *a_vp;
1801 struct knote *a_kn;
1802 } */ *ap = v;
1803 struct vnode *vp;
1804 struct knote *kn;
1805
1806 vp = ap->a_vp;
1807 kn = ap->a_kn;
1808 switch (kn->kn_filter) {
1809 case EVFILT_READ:
1810 kn->kn_fop = &genfsread_filtops;
1811 break;
1812 case EVFILT_VNODE:
1813 kn->kn_fop = &genfsvnode_filtops;
1814 break;
1815 default:
1816 return (1);
1817 }
1818
1819 kn->kn_hook = vp;
1820
1821 /* XXXLUKEM lock the struct? */
1822 SLIST_INSERT_HEAD(&vp->v_klist, kn, kn_selnext);
1823
1824 return (0);
1825 }
1826