genfs_vnops.c revision 1.62 1 /* $NetBSD: genfs_vnops.c,v 1.62 2002/05/14 19:37:18 perseant 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.62 2002/05/14 19:37:18 perseant 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 s = splbio();
1011 if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1012 (vp->v_flag & VONWORKLST)) {
1013 vp->v_flag &= ~VONWORKLST;
1014 LIST_REMOVE(vp, v_synclist);
1015 }
1016 splx(s);
1017 simple_unlock(slock);
1018 return (0);
1019 }
1020
1021 /*
1022 * the vnode has pages, set up to process the request.
1023 */
1024
1025 error = 0;
1026 s = splbio();
1027 wasclean = (vp->v_numoutput == 0);
1028 splx(s);
1029 off = startoff;
1030 if (endoff == 0 || flags & PGO_ALLPAGES) {
1031 endoff = trunc_page(LLONG_MAX);
1032 }
1033 by_list = (uobj->uo_npages <=
1034 ((endoff - startoff) >> PAGE_SHIFT) * UVM_PAGE_HASH_PENALTY);
1035
1036 /*
1037 * start the loop. when scanning by list, hold the last page
1038 * in the list before we start. pages allocated after we start
1039 * will be added to the end of the list, so we can stop at the
1040 * current last page.
1041 */
1042
1043 freeflag = pagedaemon ? PG_PAGEOUT : PG_RELEASED;
1044 curmp.uobject = uobj;
1045 curmp.offset = (voff_t)-1;
1046 curmp.flags = PG_BUSY;
1047 endmp.uobject = uobj;
1048 endmp.offset = (voff_t)-1;
1049 endmp.flags = PG_BUSY;
1050 if (by_list) {
1051 pg = TAILQ_FIRST(&uobj->memq);
1052 TAILQ_INSERT_TAIL(&uobj->memq, &endmp, listq);
1053 PHOLD(curproc);
1054 } else {
1055 pg = uvm_pagelookup(uobj, off);
1056 }
1057 nextpg = NULL;
1058 while (by_list || off < endoff) {
1059
1060 /*
1061 * if the current page is not interesting, move on to the next.
1062 */
1063
1064 KASSERT(pg == NULL || pg->uobject == uobj);
1065 KASSERT(pg == NULL ||
1066 (pg->flags & (PG_RELEASED|PG_PAGEOUT)) == 0 ||
1067 (pg->flags & PG_BUSY) != 0);
1068 if (by_list) {
1069 if (pg == &endmp) {
1070 break;
1071 }
1072 if (pg->offset < startoff || pg->offset >= endoff ||
1073 pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1074 pg = TAILQ_NEXT(pg, listq);
1075 continue;
1076 }
1077 off = pg->offset;
1078 } else if (pg == NULL ||
1079 pg->flags & (PG_RELEASED|PG_PAGEOUT)) {
1080 off += PAGE_SIZE;
1081 if (off < endoff) {
1082 pg = uvm_pagelookup(uobj, off);
1083 }
1084 continue;
1085 }
1086
1087 /*
1088 * if the current page needs to be cleaned and it's busy,
1089 * wait for it to become unbusy.
1090 */
1091
1092 yield = (curproc->p_cpu->ci_schedstate.spc_flags &
1093 SPCF_SHOULDYIELD) && !pagedaemon;
1094 if (pg->flags & PG_BUSY || yield) {
1095 KASSERT(!pagedaemon);
1096 UVMHIST_LOG(ubchist, "busy %p", pg,0,0,0);
1097 if (by_list) {
1098 TAILQ_INSERT_BEFORE(pg, &curmp, listq);
1099 UVMHIST_LOG(ubchist, "curmp next %p",
1100 TAILQ_NEXT(&curmp, listq), 0,0,0);
1101 }
1102 if (yield) {
1103 simple_unlock(slock);
1104 preempt(NULL);
1105 simple_lock(slock);
1106 } else {
1107 pg->flags |= PG_WANTED;
1108 UVM_UNLOCK_AND_WAIT(pg, slock, 0, "genput", 0);
1109 simple_lock(slock);
1110 }
1111 if (by_list) {
1112 UVMHIST_LOG(ubchist, "after next %p",
1113 TAILQ_NEXT(&curmp, listq), 0,0,0);
1114 pg = TAILQ_NEXT(&curmp, listq);
1115 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1116 } else {
1117 pg = uvm_pagelookup(uobj, off);
1118 }
1119 continue;
1120 }
1121
1122 /*
1123 * if we're freeing, remove all mappings of the page now.
1124 * if we're cleaning, check if the page is needs to be cleaned.
1125 */
1126
1127 if (flags & PGO_FREE) {
1128 pmap_page_protect(pg, VM_PROT_NONE);
1129 }
1130 if (flags & PGO_CLEANIT) {
1131 needs_clean = pmap_clear_modify(pg) ||
1132 (pg->flags & PG_CLEAN) == 0;
1133 pg->flags |= PG_CLEAN;
1134 } else {
1135 needs_clean = FALSE;
1136 }
1137
1138 /*
1139 * if we're cleaning, build a cluster.
1140 * the cluster will consist of pages which are currently dirty,
1141 * but they will be returned to us marked clean.
1142 * if not cleaning, just operate on the one page.
1143 */
1144
1145 if (needs_clean) {
1146 wasclean = FALSE;
1147 memset(pgs, 0, sizeof(pgs));
1148 pg->flags |= PG_BUSY;
1149 UVM_PAGE_OWN(pg, "genfs_putpages");
1150
1151 /*
1152 * first look backward.
1153 */
1154
1155 npages = MIN(maxpages >> 1, off >> PAGE_SHIFT);
1156 nback = npages;
1157 uvn_findpages(uobj, off - PAGE_SIZE, &nback, &pgs[0],
1158 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY|UFP_BACKWARD);
1159 if (nback) {
1160 memmove(&pgs[0], &pgs[npages - nback],
1161 nback * sizeof(pgs[0]));
1162 if (npages - nback < nback)
1163 memset(&pgs[nback], 0,
1164 (npages - nback) * sizeof(pgs[0]));
1165 else
1166 memset(&pgs[npages - nback], 0,
1167 nback * sizeof(pgs[0]));
1168 }
1169
1170 /*
1171 * then plug in our page of interest.
1172 */
1173
1174 pgs[nback] = pg;
1175
1176 /*
1177 * then look forward to fill in the remaining space in
1178 * the array of pages.
1179 */
1180
1181 npages = maxpages - nback - 1;
1182 uvn_findpages(uobj, off + PAGE_SIZE, &npages,
1183 &pgs[nback + 1],
1184 UFP_NOWAIT|UFP_NOALLOC|UFP_DIRTYONLY);
1185 npages += nback + 1;
1186 } else {
1187 pgs[0] = pg;
1188 npages = 1;
1189 nback = 0;
1190 }
1191
1192 /*
1193 * apply FREE or DEACTIVATE options if requested.
1194 */
1195
1196 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1197 uvm_lock_pageq();
1198 }
1199 for (i = 0; i < npages; i++) {
1200 tpg = pgs[i];
1201 KASSERT(tpg->uobject == uobj);
1202 if (by_list && tpg == TAILQ_NEXT(pg, listq))
1203 pg = tpg;
1204 if (tpg->offset < startoff || tpg->offset >= endoff)
1205 continue;
1206 if (flags & PGO_DEACTIVATE &&
1207 (tpg->pqflags & PQ_INACTIVE) == 0 &&
1208 tpg->wire_count == 0) {
1209 (void) pmap_clear_reference(tpg);
1210 uvm_pagedeactivate(tpg);
1211 } else if (flags & PGO_FREE) {
1212 pmap_page_protect(tpg, VM_PROT_NONE);
1213 if (tpg->flags & PG_BUSY) {
1214 tpg->flags |= freeflag;
1215 if (pagedaemon) {
1216 uvmexp.paging++;
1217 uvm_pagedequeue(tpg);
1218 }
1219 } else {
1220
1221 /*
1222 * ``page is not busy''
1223 * implies that npages is 1
1224 * and needs_clean is false.
1225 */
1226
1227 nextpg = TAILQ_NEXT(tpg, listq);
1228 uvm_pagefree(tpg);
1229 }
1230 }
1231 }
1232 if (flags & (PGO_DEACTIVATE|PGO_FREE)) {
1233 uvm_unlock_pageq();
1234 }
1235 if (needs_clean) {
1236
1237 /*
1238 * start the i/o. if we're traversing by list,
1239 * keep our place in the list with a marker page.
1240 */
1241
1242 if (by_list) {
1243 TAILQ_INSERT_AFTER(&uobj->memq, pg, &curmp,
1244 listq);
1245 }
1246 simple_unlock(slock);
1247 error = GOP_WRITE(vp, pgs, npages, flags);
1248 simple_lock(slock);
1249 if (by_list) {
1250 pg = TAILQ_NEXT(&curmp, listq);
1251 TAILQ_REMOVE(&uobj->memq, &curmp, listq);
1252 }
1253 if (error) {
1254 break;
1255 }
1256 if (by_list) {
1257 continue;
1258 }
1259 }
1260
1261 /*
1262 * find the next page and continue if there was no error.
1263 */
1264
1265 if (by_list) {
1266 if (nextpg) {
1267 pg = nextpg;
1268 nextpg = NULL;
1269 } else {
1270 pg = TAILQ_NEXT(pg, listq);
1271 }
1272 } else {
1273 off += (npages - nback) << PAGE_SHIFT;
1274 if (off < endoff) {
1275 pg = uvm_pagelookup(uobj, off);
1276 }
1277 }
1278 }
1279 if (by_list) {
1280 TAILQ_REMOVE(&uobj->memq, &endmp, listq);
1281 PRELE(curproc);
1282 }
1283
1284 /*
1285 * if we're cleaning and there was nothing to clean,
1286 * take us off the syncer list. if we started any i/o
1287 * and we're doing sync i/o, wait for all writes to finish.
1288 */
1289
1290 s = splbio();
1291 if ((flags & PGO_CLEANIT) && wasclean &&
1292 startoff == 0 && endoff == trunc_page(LLONG_MAX) &&
1293 LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
1294 (vp->v_flag & VONWORKLST)) {
1295 vp->v_flag &= ~VONWORKLST;
1296 LIST_REMOVE(vp, v_synclist);
1297 }
1298 splx(s);
1299 if (!wasclean && !async) {
1300 s = splbio();
1301 while (vp->v_numoutput != 0) {
1302 vp->v_flag |= VBWAIT;
1303 UVM_UNLOCK_AND_WAIT(&vp->v_numoutput, slock, FALSE,
1304 "genput2", 0);
1305 simple_lock(slock);
1306 }
1307 splx(s);
1308 }
1309 simple_unlock(&uobj->vmobjlock);
1310 return (error);
1311 }
1312
1313 int
1314 genfs_gop_write(struct vnode *vp, struct vm_page **pgs, int npages, int flags)
1315 {
1316 int s, error, run;
1317 int fs_bshift, dev_bshift;
1318 vaddr_t kva;
1319 off_t eof, offset, startoffset;
1320 size_t bytes, iobytes, skipbytes;
1321 daddr_t lbn, blkno;
1322 struct vm_page *pg;
1323 struct buf *mbp, *bp;
1324 struct vnode *devvp;
1325 boolean_t async = (flags & PGO_SYNCIO) == 0;
1326 UVMHIST_FUNC("genfs_gop_write"); UVMHIST_CALLED(ubchist);
1327
1328 UVMHIST_LOG(ubchist, "vp %p pgs %p npages %d flags 0x%x",
1329 vp, pgs, npages, flags);
1330
1331 GOP_SIZE(vp, vp->v_size, &eof);
1332 if (vp->v_type == VREG) {
1333 fs_bshift = vp->v_mount->mnt_fs_bshift;
1334 dev_bshift = vp->v_mount->mnt_dev_bshift;
1335 } else {
1336 fs_bshift = DEV_BSHIFT;
1337 dev_bshift = DEV_BSHIFT;
1338 }
1339 error = 0;
1340 pg = pgs[0];
1341 startoffset = pg->offset;
1342 bytes = MIN(npages << PAGE_SHIFT, eof - startoffset);
1343 skipbytes = 0;
1344 KASSERT(bytes != 0);
1345
1346 kva = uvm_pagermapin(pgs, npages,
1347 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1348
1349 s = splbio();
1350 vp->v_numoutput += 2;
1351 mbp = pool_get(&bufpool, PR_WAITOK);
1352 UVMHIST_LOG(ubchist, "vp %p mbp %p num now %d bytes 0x%x",
1353 vp, mbp, vp->v_numoutput, bytes);
1354 splx(s);
1355 mbp->b_bufsize = npages << PAGE_SHIFT;
1356 mbp->b_data = (void *)kva;
1357 mbp->b_resid = mbp->b_bcount = bytes;
1358 mbp->b_flags = B_BUSY|B_WRITE|B_AGE| (async ? (B_CALL|B_ASYNC) : 0);
1359 mbp->b_iodone = uvm_aio_biodone;
1360 mbp->b_vp = vp;
1361 LIST_INIT(&mbp->b_dep);
1362
1363 bp = NULL;
1364 for (offset = startoffset;
1365 bytes > 0;
1366 offset += iobytes, bytes -= iobytes) {
1367 lbn = offset >> fs_bshift;
1368 error = VOP_BMAP(vp, lbn, &devvp, &blkno, &run);
1369 if (error) {
1370 UVMHIST_LOG(ubchist, "VOP_BMAP() -> %d", error,0,0,0);
1371 skipbytes += bytes;
1372 bytes = 0;
1373 break;
1374 }
1375
1376 iobytes = MIN((((off_t)lbn + 1 + run) << fs_bshift) - offset,
1377 bytes);
1378 if (blkno == (daddr_t)-1) {
1379 skipbytes += iobytes;
1380 continue;
1381 }
1382
1383 /* if it's really one i/o, don't make a second buf */
1384 if (offset == startoffset && iobytes == bytes) {
1385 bp = mbp;
1386 } else {
1387 s = splbio();
1388 vp->v_numoutput++;
1389 bp = pool_get(&bufpool, PR_WAITOK);
1390 UVMHIST_LOG(ubchist, "vp %p bp %p num now %d",
1391 vp, bp, vp->v_numoutput, 0);
1392 splx(s);
1393 bp->b_data = (char *)kva +
1394 (vaddr_t)(offset - pg->offset);
1395 bp->b_resid = bp->b_bcount = iobytes;
1396 bp->b_flags = B_BUSY|B_WRITE|B_CALL|B_ASYNC;
1397 bp->b_iodone = uvm_aio_biodone1;
1398 bp->b_vp = vp;
1399 LIST_INIT(&bp->b_dep);
1400 }
1401 bp->b_lblkno = 0;
1402 bp->b_private = mbp;
1403 if (devvp->v_type == VBLK) {
1404 bp->b_dev = devvp->v_rdev;
1405 }
1406
1407 /* adjust physical blkno for partial blocks */
1408 bp->b_blkno = blkno + ((offset - ((off_t)lbn << fs_bshift)) >>
1409 dev_bshift);
1410 UVMHIST_LOG(ubchist,
1411 "vp %p offset 0x%x bcount 0x%x blkno 0x%x",
1412 vp, offset, bp->b_bcount, bp->b_blkno);
1413 VOP_STRATEGY(bp);
1414 }
1415 if (skipbytes) {
1416 UVMHIST_LOG(ubchist, "skipbytes %d", skipbytes, 0,0,0);
1417 s = splbio();
1418 if (error) {
1419 mbp->b_flags |= B_ERROR;
1420 mbp->b_error = error;
1421 }
1422 mbp->b_resid -= skipbytes;
1423 if (mbp->b_resid == 0) {
1424 biodone(mbp);
1425 }
1426 splx(s);
1427 }
1428 if (async) {
1429 UVMHIST_LOG(ubchist, "returning 0 (async)", 0,0,0,0);
1430 return (0);
1431 }
1432 UVMHIST_LOG(ubchist, "waiting for mbp %p", mbp,0,0,0);
1433 error = biowait(mbp);
1434 uvm_aio_aiodone(mbp);
1435 UVMHIST_LOG(ubchist, "returning, error %d", error,0,0,0);
1436 return (error);
1437 }
1438
1439 /*
1440 * VOP_PUTPAGES() for vnodes which never have pages.
1441 */
1442
1443 int
1444 genfs_null_putpages(void *v)
1445 {
1446 struct vop_putpages_args /* {
1447 struct vnode *a_vp;
1448 voff_t a_offlo;
1449 voff_t a_offhi;
1450 int a_flags;
1451 } */ *ap = v;
1452 struct vnode *vp = ap->a_vp;
1453
1454 KASSERT(vp->v_uobj.uo_npages == 0);
1455 simple_unlock(&vp->v_interlock);
1456 return (0);
1457 }
1458
1459 void
1460 genfs_node_init(struct vnode *vp, struct genfs_ops *ops)
1461 {
1462 struct genfs_node *gp = VTOG(vp);
1463
1464 lockinit(&gp->g_glock, PINOD, "glock", 0, 0);
1465 gp->g_op = ops;
1466 }
1467
1468 void
1469 genfs_size(struct vnode *vp, off_t size, off_t *eobp)
1470 {
1471 int bsize;
1472
1473 bsize = 1 << vp->v_mount->mnt_fs_bshift;
1474 *eobp = (size + bsize - 1) & ~(bsize - 1);
1475 }
1476
1477 int
1478 genfs_compat_getpages(void *v)
1479 {
1480 struct vop_getpages_args /* {
1481 struct vnode *a_vp;
1482 voff_t a_offset;
1483 struct vm_page **a_m;
1484 int *a_count;
1485 int a_centeridx;
1486 vm_prot_t a_access_type;
1487 int a_advice;
1488 int a_flags;
1489 } */ *ap = v;
1490
1491 off_t origoffset;
1492 struct vnode *vp = ap->a_vp;
1493 struct uvm_object *uobj = &vp->v_uobj;
1494 struct vm_page *pg, **pgs;
1495 vaddr_t kva;
1496 int i, error, orignpages, npages;
1497 struct iovec iov;
1498 struct uio uio;
1499 struct ucred *cred = curproc->p_ucred;
1500 boolean_t write = (ap->a_access_type & VM_PROT_WRITE) != 0;
1501
1502 error = 0;
1503 origoffset = ap->a_offset;
1504 orignpages = *ap->a_count;
1505 pgs = ap->a_m;
1506
1507 if (write && (vp->v_flag & VONWORKLST) == 0) {
1508 vn_syncer_add_to_worklist(vp, filedelay);
1509 }
1510 if (ap->a_flags & PGO_LOCKED) {
1511 uvn_findpages(uobj, origoffset, ap->a_count, ap->a_m,
1512 UFP_NOWAIT|UFP_NOALLOC| (write ? UFP_NORDONLY : 0));
1513
1514 return (ap->a_m[ap->a_centeridx] == NULL ? EBUSY : 0);
1515 }
1516 if (origoffset + (ap->a_centeridx << PAGE_SHIFT) >= vp->v_size) {
1517 simple_unlock(&uobj->vmobjlock);
1518 return (EINVAL);
1519 }
1520 npages = orignpages;
1521 uvn_findpages(uobj, origoffset, &npages, pgs, UFP_ALL);
1522 simple_unlock(&uobj->vmobjlock);
1523 kva = uvm_pagermapin(pgs, npages,
1524 UVMPAGER_MAPIN_READ | UVMPAGER_MAPIN_WAITOK);
1525 for (i = 0; i < npages; i++) {
1526 pg = pgs[i];
1527 if ((pg->flags & PG_FAKE) == 0) {
1528 continue;
1529 }
1530 iov.iov_base = (char *)kva + (i << PAGE_SHIFT);
1531 iov.iov_len = PAGE_SIZE;
1532 uio.uio_iov = &iov;
1533 uio.uio_iovcnt = 1;
1534 uio.uio_offset = origoffset + (i << PAGE_SHIFT);
1535 uio.uio_segflg = UIO_SYSSPACE;
1536 uio.uio_rw = UIO_READ;
1537 uio.uio_resid = PAGE_SIZE;
1538 uio.uio_procp = curproc;
1539 error = VOP_READ(vp, &uio, 0, cred);
1540 if (error) {
1541 break;
1542 }
1543 if (uio.uio_resid) {
1544 memset(iov.iov_base, 0, uio.uio_resid);
1545 }
1546 }
1547 uvm_pagermapout(kva, npages);
1548 simple_lock(&uobj->vmobjlock);
1549 uvm_lock_pageq();
1550 for (i = 0; i < npages; i++) {
1551 pg = pgs[i];
1552 if (error && (pg->flags & PG_FAKE) != 0) {
1553 pg->flags |= PG_RELEASED;
1554 } else {
1555 pmap_clear_modify(pg);
1556 uvm_pageactivate(pg);
1557 }
1558 }
1559 if (error) {
1560 uvm_page_unbusy(pgs, npages);
1561 }
1562 uvm_unlock_pageq();
1563 simple_unlock(&uobj->vmobjlock);
1564 return (error);
1565 }
1566
1567 int
1568 genfs_compat_gop_write(struct vnode *vp, struct vm_page **pgs, int npages,
1569 int flags)
1570 {
1571 off_t offset;
1572 struct iovec iov;
1573 struct uio uio;
1574 struct ucred *cred = curproc->p_ucred;
1575 struct buf *bp;
1576 vaddr_t kva;
1577 int s, error;
1578
1579 offset = pgs[0]->offset;
1580 kva = uvm_pagermapin(pgs, npages,
1581 UVMPAGER_MAPIN_WRITE | UVMPAGER_MAPIN_WAITOK);
1582
1583 iov.iov_base = (void *)kva;
1584 iov.iov_len = npages << PAGE_SHIFT;
1585 uio.uio_iov = &iov;
1586 uio.uio_iovcnt = npages;
1587 uio.uio_offset = offset;
1588 uio.uio_segflg = UIO_SYSSPACE;
1589 uio.uio_rw = UIO_WRITE;
1590 uio.uio_resid = npages << PAGE_SHIFT;
1591 uio.uio_procp = curproc;
1592 error = VOP_WRITE(vp, &uio, 0, cred);
1593
1594 s = splbio();
1595 vp->v_numoutput++;
1596 bp = pool_get(&bufpool, PR_WAITOK);
1597 splx(s);
1598
1599 bp->b_flags = B_BUSY | B_WRITE | B_AGE;
1600 bp->b_vp = vp;
1601 bp->b_lblkno = offset >> vp->v_mount->mnt_fs_bshift;
1602 bp->b_data = (char *)kva;
1603 bp->b_bcount = npages << PAGE_SHIFT;
1604 bp->b_bufsize = npages << PAGE_SHIFT;
1605 bp->b_resid = 0;
1606 LIST_INIT(&bp->b_dep);
1607 if (error) {
1608 bp->b_flags |= B_ERROR;
1609 bp->b_error = error;
1610 }
1611 uvm_aio_aiodone(bp);
1612 return (error);
1613 }
1614