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