xref: /netbsd-src/sys/rump/librump/rumpvfs/vm_vfs.c (revision 7d23e041989df5ddafff1bb408b7dba9c8e91b3b)
1*7d23e041Sriastradh /*	$NetBSD: vm_vfs.c,v 1.42 2023/04/22 13:53:53 riastradh Exp $	*/
2e10fef6cSpooka 
3e10fef6cSpooka /*
423bbd0e0Spooka  * Copyright (c) 2008-2011 Antti Kantee.  All Rights Reserved.
5e10fef6cSpooka  *
6e10fef6cSpooka  * Redistribution and use in source and binary forms, with or without
7e10fef6cSpooka  * modification, are permitted provided that the following conditions
8e10fef6cSpooka  * are met:
9e10fef6cSpooka  * 1. Redistributions of source code must retain the above copyright
10e10fef6cSpooka  *    notice, this list of conditions and the following disclaimer.
11e10fef6cSpooka  * 2. Redistributions in binary form must reproduce the above copyright
12e10fef6cSpooka  *    notice, this list of conditions and the following disclaimer in the
13e10fef6cSpooka  *    documentation and/or other materials provided with the distribution.
14e10fef6cSpooka  *
15e10fef6cSpooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16e10fef6cSpooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17e10fef6cSpooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18e10fef6cSpooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19e10fef6cSpooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20e10fef6cSpooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21e10fef6cSpooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22e10fef6cSpooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23e10fef6cSpooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24e10fef6cSpooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25e10fef6cSpooka  * SUCH DAMAGE.
26e10fef6cSpooka  */
27e10fef6cSpooka 
28a768afd6Spooka #include <sys/cdefs.h>
29*7d23e041Sriastradh __KERNEL_RCSID(0, "$NetBSD: vm_vfs.c,v 1.42 2023/04/22 13:53:53 riastradh Exp $");
30a768afd6Spooka 
31e10fef6cSpooka #include <sys/param.h>
32e10fef6cSpooka 
33e10fef6cSpooka #include <sys/buf.h>
34e10fef6cSpooka #include <sys/vnode.h>
35e10fef6cSpooka 
36e10fef6cSpooka #include <uvm/uvm.h>
37e10fef6cSpooka #include <uvm/uvm_readahead.h>
38e10fef6cSpooka 
399d18193cSchs void
uvm_aio_aiodone_pages(struct vm_page ** pgs,int npages,bool write,int error)409d18193cSchs uvm_aio_aiodone_pages(struct vm_page **pgs, int npages, bool write, int error)
419d18193cSchs {
429d18193cSchs 	struct uvm_object *uobj = pgs[0]->uobject;
439d18193cSchs 	struct vm_page *pg;
449d18193cSchs 	int i;
459d18193cSchs 
469d18193cSchs 	rw_enter(uobj->vmobjlock, RW_WRITER);
479d18193cSchs 	for (i = 0; i < npages; i++) {
489d18193cSchs 		pg = pgs[i];
4972d70d6aSchs 		KASSERT((pg->flags & PG_PAGEOUT) == 0 ||
5072d70d6aSchs 			(pg->flags & PG_FAKE) == 0);
51ffc294f9Schs 
52ffc294f9Schs 		if (pg->flags & PG_FAKE) {
53ffc294f9Schs 			KASSERT(!write);
54ffc294f9Schs 			pg->flags &= ~PG_FAKE;
55ffc294f9Schs 			KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
56ffc294f9Schs 			uvm_pagelock(pg);
57ffc294f9Schs 			uvm_pageenqueue(pg);
58ffc294f9Schs 			uvm_pageunlock(pg);
59ffc294f9Schs 		}
60ffc294f9Schs 
619d18193cSchs 	}
629d18193cSchs 	uvm_page_unbusy(pgs, npages);
639d18193cSchs 	rw_exit(uobj->vmobjlock);
649d18193cSchs }
659d18193cSchs 
6616de900cSpooka /*
679d18193cSchs  * Release resources held during async io.
68c8c1730cSpooka  */
69e10fef6cSpooka void
uvm_aio_aiodone(struct buf * bp)70e10fef6cSpooka uvm_aio_aiodone(struct buf *bp)
71e10fef6cSpooka {
720d6cd2ebSrmind 	struct uvm_object *uobj = NULL;
739d18193cSchs 	int npages = bp->b_bufsize >> PAGE_SHIFT;
7416de900cSpooka 	struct vm_page **pgs;
7516de900cSpooka 	vaddr_t va;
769d18193cSchs 	int i, error;
779d18193cSchs 	bool write;
789d18193cSchs 
799d18193cSchs 	error = bp->b_error;
809d18193cSchs 	write = BUF_ISWRITE(bp);
81e10fef6cSpooka 
823b79ed5dSpooka 	KASSERT(npages > 0);
8316de900cSpooka 	pgs = kmem_alloc(npages * sizeof(*pgs), KM_SLEEP);
8416de900cSpooka 	for (i = 0; i < npages; i++) {
8516de900cSpooka 		va = (vaddr_t)bp->b_data + (i << PAGE_SHIFT);
8616de900cSpooka 		pgs[i] = uvm_pageratop(va);
870d6cd2ebSrmind 
880d6cd2ebSrmind 		if (uobj == NULL) {
890d6cd2ebSrmind 			uobj = pgs[i]->uobject;
900d6cd2ebSrmind 			KASSERT(uobj != NULL);
910d6cd2ebSrmind 		} else {
920d6cd2ebSrmind 			KASSERT(uobj == pgs[i]->uobject);
930d6cd2ebSrmind 		}
9416de900cSpooka 	}
950d6cd2ebSrmind 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
96db3f3667Spooka 
979d18193cSchs 	uvm_aio_aiodone_pages(pgs, npages, write, error);
989d18193cSchs 
999d18193cSchs 	if (write && (bp->b_cflags & BC_AGE) != 0) {
10016de900cSpooka 		mutex_enter(bp->b_objlock);
10116de900cSpooka 		vwakeup(bp);
10216de900cSpooka 		mutex_exit(bp->b_objlock);
10316de900cSpooka 	}
10416de900cSpooka 
10516de900cSpooka 	putiobuf(bp);
10616de900cSpooka 
10716de900cSpooka 	kmem_free(pgs, npages * sizeof(*pgs));
108e10fef6cSpooka }
109