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