Home | History | Annotate | Line # | Download | only in rumpvfs
vm_vfs.c revision 1.34.34.1
      1  1.34.34.1    martin /*	$NetBSD: vm_vfs.c,v 1.34.34.1 2021/07/06 04:22:34 martin Exp $	*/
      2        1.1     pooka 
      3        1.1     pooka /*
      4       1.28     pooka  * Copyright (c) 2008-2011 Antti Kantee.  All Rights Reserved.
      5        1.1     pooka  *
      6        1.1     pooka  * Redistribution and use in source and binary forms, with or without
      7        1.1     pooka  * modification, are permitted provided that the following conditions
      8        1.1     pooka  * are met:
      9        1.1     pooka  * 1. Redistributions of source code must retain the above copyright
     10        1.1     pooka  *    notice, this list of conditions and the following disclaimer.
     11        1.1     pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12        1.1     pooka  *    notice, this list of conditions and the following disclaimer in the
     13        1.1     pooka  *    documentation and/or other materials provided with the distribution.
     14        1.1     pooka  *
     15        1.1     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16        1.1     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17        1.1     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18        1.1     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19        1.1     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20        1.1     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21        1.1     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22        1.1     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23        1.1     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24        1.1     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25        1.1     pooka  * SUCH DAMAGE.
     26        1.1     pooka  */
     27        1.1     pooka 
     28        1.3     pooka #include <sys/cdefs.h>
     29  1.34.34.1    martin __KERNEL_RCSID(0, "$NetBSD: vm_vfs.c,v 1.34.34.1 2021/07/06 04:22:34 martin Exp $");
     30        1.3     pooka 
     31        1.1     pooka #include <sys/param.h>
     32        1.1     pooka 
     33        1.1     pooka #include <sys/buf.h>
     34        1.1     pooka #include <sys/vnode.h>
     35        1.1     pooka 
     36        1.1     pooka #include <uvm/uvm.h>
     37        1.1     pooka #include <uvm/uvm_readahead.h>
     38        1.1     pooka 
     39  1.34.34.1    martin void
     40  1.34.34.1    martin uvm_aio_aiodone_pages(struct vm_page **pgs, int npages, bool write, int error)
     41  1.34.34.1    martin {
     42  1.34.34.1    martin 	struct uvm_object *uobj = pgs[0]->uobject;
     43  1.34.34.1    martin 	struct vm_page *pg;
     44  1.34.34.1    martin 	int i;
     45  1.34.34.1    martin 
     46  1.34.34.1    martin 	mutex_enter(uobj->vmobjlock);
     47  1.34.34.1    martin 	for (i = 0; i < npages; i++) {
     48  1.34.34.1    martin 		pg = pgs[i];
     49  1.34.34.1    martin 		KASSERT((pg->flags & PG_PAGEOUT) == 0 ||
     50  1.34.34.1    martin 			(pg->flags & PG_FAKE) == 0);
     51  1.34.34.1    martin 
     52  1.34.34.1    martin 		if (pg->flags & PG_FAKE) {
     53  1.34.34.1    martin 			KASSERT(!write);
     54  1.34.34.1    martin 			pg->flags &= ~PG_FAKE;
     55  1.34.34.1    martin 			KASSERT((pg->flags & PG_CLEAN) != 0);
     56  1.34.34.1    martin 			uvm_pageenqueue(pg);
     57  1.34.34.1    martin 			pmap_clear_modify(pg);
     58  1.34.34.1    martin 		}
     59  1.34.34.1    martin 
     60  1.34.34.1    martin 	}
     61  1.34.34.1    martin 	uvm_page_unbusy(pgs, npages);
     62  1.34.34.1    martin 	mutex_exit(uobj->vmobjlock);
     63  1.34.34.1    martin }
     64  1.34.34.1    martin 
     65        1.6     pooka /*
     66  1.34.34.1    martin  * Release resources held during async io.
     67        1.7     pooka  */
     68        1.1     pooka void
     69        1.1     pooka uvm_aio_aiodone(struct buf *bp)
     70        1.1     pooka {
     71       1.33     rmind 	struct uvm_object *uobj = NULL;
     72  1.34.34.1    martin 	int npages = bp->b_bufsize >> PAGE_SHIFT;
     73        1.6     pooka 	struct vm_page **pgs;
     74        1.6     pooka 	vaddr_t va;
     75  1.34.34.1    martin 	int i, error;
     76  1.34.34.1    martin 	bool write;
     77  1.34.34.1    martin 
     78  1.34.34.1    martin 	error = bp->b_error;
     79  1.34.34.1    martin 	write = BUF_ISWRITE(bp);
     80        1.6     pooka 
     81       1.21     pooka 	KASSERT(npages > 0);
     82        1.6     pooka 	pgs = kmem_alloc(npages * sizeof(*pgs), KM_SLEEP);
     83        1.6     pooka 	for (i = 0; i < npages; i++) {
     84        1.6     pooka 		va = (vaddr_t)bp->b_data + (i << PAGE_SHIFT);
     85        1.6     pooka 		pgs[i] = uvm_pageratop(va);
     86       1.33     rmind 
     87       1.33     rmind 		if (uobj == NULL) {
     88       1.33     rmind 			uobj = pgs[i]->uobject;
     89       1.33     rmind 			KASSERT(uobj != NULL);
     90       1.33     rmind 		} else {
     91       1.33     rmind 			KASSERT(uobj == pgs[i]->uobject);
     92       1.33     rmind 		}
     93        1.6     pooka 	}
     94       1.33     rmind 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
     95       1.25     pooka 
     96  1.34.34.1    martin 	uvm_aio_aiodone_pages(pgs, npages, write, error);
     97  1.34.34.1    martin 
     98  1.34.34.1    martin 	if (write && (bp->b_cflags & BC_AGE) != 0) {
     99        1.6     pooka 		mutex_enter(bp->b_objlock);
    100        1.6     pooka 		vwakeup(bp);
    101        1.6     pooka 		mutex_exit(bp->b_objlock);
    102        1.6     pooka 	}
    103        1.6     pooka 
    104        1.6     pooka 	putiobuf(bp);
    105        1.6     pooka 
    106        1.6     pooka 	kmem_free(pgs, npages * sizeof(*pgs));
    107        1.1     pooka }
    108        1.1     pooka 
    109        1.8     pooka void
    110        1.8     pooka uvm_aio_biodone(struct buf *bp)
    111        1.1     pooka {
    112        1.1     pooka 
    113        1.8     pooka 	uvm_aio_aiodone(bp);
    114        1.1     pooka }
    115        1.1     pooka 
    116        1.8     pooka /*
    117        1.8     pooka  * UBC
    118        1.8     pooka  */
    119        1.8     pooka 
    120       1.17     pooka #define PAGERFLAGS (PGO_SYNCIO | PGO_NOBLOCKALLOC | PGO_NOTIMESTAMP)
    121       1.17     pooka 
    122        1.2     pooka void
    123       1.30   hannken ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
    124        1.2     pooka {
    125        1.2     pooka 	struct vm_page **pgs;
    126        1.2     pooka 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    127       1.34  christos 	int npages, i;
    128        1.2     pooka 
    129       1.15     pooka 	if (maxpages == 0)
    130       1.15     pooka 		return;
    131       1.15     pooka 
    132       1.20     pooka 	pgs = kmem_alloc(maxpages * sizeof(pgs), KM_SLEEP);
    133       1.29     rmind 	mutex_enter(uobj->vmobjlock);
    134        1.2     pooka 	while (len) {
    135        1.2     pooka 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    136        1.2     pooka 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    137       1.34  christos 		(void)uobj->pgops->pgo_get(uobj, trunc_page(off),
    138       1.19     pooka 		    pgs, &npages, 0, VM_PROT_READ | VM_PROT_WRITE,
    139       1.19     pooka 		    0, PAGERFLAGS | PGO_PASTEOF);
    140        1.2     pooka 		KASSERT(npages > 0);
    141        1.2     pooka 
    142       1.33     rmind 		mutex_enter(uobj->vmobjlock);
    143       1.33     rmind 		for (i = 0; i < npages; i++) {
    144       1.20     pooka 			struct vm_page *pg;
    145        1.2     pooka 			uint8_t *start;
    146        1.2     pooka 			size_t chunkoff, chunklen;
    147        1.2     pooka 
    148       1.20     pooka 			pg = pgs[i];
    149       1.20     pooka 			if (pg == NULL)
    150       1.20     pooka 				break;
    151       1.33     rmind 
    152       1.33     rmind 			KASSERT(pg->uobject != NULL);
    153       1.33     rmind 			KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
    154       1.20     pooka 
    155        1.2     pooka 			chunkoff = off & PAGE_MASK;
    156        1.2     pooka 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    157       1.20     pooka 			start = (uint8_t *)pg->uanon + chunkoff;
    158        1.2     pooka 
    159        1.2     pooka 			memset(start, 0, chunklen);
    160       1.20     pooka 			pg->flags &= ~PG_CLEAN;
    161        1.2     pooka 
    162        1.2     pooka 			off += chunklen;
    163        1.2     pooka 			len -= chunklen;
    164        1.2     pooka 		}
    165        1.2     pooka 		uvm_page_unbusy(pgs, npages);
    166        1.2     pooka 	}
    167       1.29     rmind 	mutex_exit(uobj->vmobjlock);
    168        1.2     pooka 	kmem_free(pgs, maxpages * sizeof(pgs));
    169        1.2     pooka }
    170        1.2     pooka 
    171        1.2     pooka #define len2npages(off, len)						\
    172       1.18     pooka     ((round_page(off+len) - trunc_page(off)) >> PAGE_SHIFT)
    173        1.2     pooka 
    174        1.2     pooka int
    175        1.2     pooka ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    176        1.2     pooka 	int advice, int flags)
    177        1.2     pooka {
    178        1.2     pooka 	struct vm_page **pgs;
    179        1.2     pooka 	int npages = len2npages(uio->uio_offset, todo);
    180        1.2     pooka 	size_t pgalloc;
    181        1.6     pooka 	int i, rv, pagerflags;
    182       1.27     pooka 	vm_prot_t prot;
    183        1.2     pooka 
    184        1.2     pooka 	pgalloc = npages * sizeof(pgs);
    185       1.20     pooka 	pgs = kmem_alloc(pgalloc, KM_SLEEP);
    186        1.2     pooka 
    187       1.17     pooka 	pagerflags = PAGERFLAGS;
    188        1.6     pooka 	if (flags & UBC_WRITE)
    189        1.6     pooka 		pagerflags |= PGO_PASTEOF;
    190        1.6     pooka 	if (flags & UBC_FAULTBUSY)
    191        1.6     pooka 		pagerflags |= PGO_OVERWRITE;
    192        1.6     pooka 
    193       1.27     pooka 	prot = VM_PROT_READ;
    194       1.27     pooka 	if (flags & UBC_WRITE)
    195       1.27     pooka 		prot |= VM_PROT_WRITE;
    196       1.27     pooka 
    197       1.29     rmind 	mutex_enter(uobj->vmobjlock);
    198        1.2     pooka 	do {
    199       1.20     pooka 		npages = len2npages(uio->uio_offset, todo);
    200       1.20     pooka 		memset(pgs, 0, pgalloc);
    201       1.20     pooka 		rv = uobj->pgops->pgo_get(uobj, trunc_page(uio->uio_offset),
    202       1.27     pooka 		    pgs, &npages, 0, prot, 0, pagerflags);
    203        1.2     pooka 		if (rv)
    204        1.2     pooka 			goto out;
    205        1.2     pooka 
    206       1.33     rmind 		mutex_enter(uobj->vmobjlock);
    207       1.33     rmind 		for (i = 0; i < npages; i++) {
    208       1.20     pooka 			struct vm_page *pg;
    209        1.2     pooka 			size_t xfersize;
    210        1.2     pooka 			off_t pageoff;
    211        1.2     pooka 
    212       1.20     pooka 			pg = pgs[i];
    213       1.20     pooka 			if (pg == NULL)
    214       1.20     pooka 				break;
    215       1.20     pooka 
    216       1.33     rmind 			KASSERT(pg->uobject != NULL);
    217       1.33     rmind 			KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
    218        1.2     pooka 			pageoff = uio->uio_offset & PAGE_MASK;
    219       1.33     rmind 
    220        1.2     pooka 			xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
    221       1.13     pooka 			KASSERT(xfersize > 0);
    222       1.24     pooka 			rv = uiomove((uint8_t *)pg->uanon + pageoff,
    223        1.2     pooka 			    xfersize, uio);
    224       1.24     pooka 			if (rv) {
    225       1.24     pooka 				uvm_page_unbusy(pgs, npages);
    226       1.33     rmind 				mutex_exit(uobj->vmobjlock);
    227       1.24     pooka 				goto out;
    228       1.24     pooka 			}
    229        1.2     pooka 			if (uio->uio_rw == UIO_WRITE)
    230       1.20     pooka 				pg->flags &= ~(PG_CLEAN | PG_FAKE);
    231        1.2     pooka 			todo -= xfersize;
    232        1.2     pooka 		}
    233        1.2     pooka 		uvm_page_unbusy(pgs, npages);
    234        1.2     pooka 	} while (todo);
    235       1.29     rmind 	mutex_exit(uobj->vmobjlock);
    236        1.2     pooka 
    237        1.2     pooka  out:
    238        1.2     pooka 	kmem_free(pgs, pgalloc);
    239        1.2     pooka 	return rv;
    240        1.2     pooka }
    241