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