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