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