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