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