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