Home | History | Annotate | Line # | Download | only in rumpvfs
vm_vfs.c revision 1.5
      1 /*	$NetBSD: vm_vfs.c,v 1.5 2009/03/18 10:22:45 cegger 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.5 2009/03/18 10:22:45 cegger 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 static int vn_get(struct uvm_object *, voff_t, struct vm_page **,
     43 	int *, int, vm_prot_t, int, int);
     44 static int vn_put(struct uvm_object *, voff_t, voff_t, int);
     45 
     46 const struct uvm_pagerops uvm_vnodeops = {
     47 	.pgo_get = vn_get,
     48 	.pgo_put = vn_put,
     49 };
     50 
     51 /*
     52  * vnode pager
     53  */
     54 
     55 static int
     56 vn_get(struct uvm_object *uobj, voff_t off, struct vm_page **pgs,
     57 	int *npages, int centeridx, vm_prot_t access_type,
     58 	int advice, int flags)
     59 {
     60 	struct vnode *vp = (struct vnode *)uobj;
     61 
     62 	return VOP_GETPAGES(vp, off, pgs, npages, centeridx, access_type,
     63 	    advice, flags);
     64 }
     65 
     66 static int
     67 vn_put(struct uvm_object *uobj, voff_t offlo, voff_t offhi, int flags)
     68 {
     69 	struct vnode *vp = (struct vnode *)uobj;
     70 
     71 	return VOP_PUTPAGES(vp, offlo, offhi, flags);
     72 }
     73 
     74 void
     75 uvm_aio_biodone1(struct buf *bp)
     76 {
     77 
     78 	panic("%s: unimplemented", __func__);
     79 }
     80 
     81 void
     82 uvm_aio_biodone(struct buf *bp)
     83 {
     84 
     85 	uvm_aio_aiodone(bp);
     86 }
     87 
     88 void
     89 uvm_aio_aiodone(struct buf *bp)
     90 {
     91 
     92 }
     93 
     94 struct uvm_ractx *
     95 uvm_ra_allocctx(void)
     96 {
     97 
     98 	return NULL;
     99 }
    100 
    101 void
    102 uvm_ra_freectx(struct uvm_ractx *ra)
    103 {
    104 
    105 	return;
    106 }
    107 
    108 bool
    109 uvn_clean_p(struct uvm_object *uobj)
    110 {
    111 	struct vnode *vp = (void *)uobj;
    112 
    113 	return (vp->v_iflag & VI_ONWORKLST) == 0;
    114 }
    115 
    116 void
    117 uvm_vnp_setsize(struct vnode *vp, voff_t newsize)
    118 {
    119 
    120 	mutex_enter(&vp->v_interlock);
    121 	vp->v_size = vp->v_writesize = newsize;
    122 	mutex_exit(&vp->v_interlock);
    123 }
    124 
    125 void
    126 uvm_vnp_setwritesize(struct vnode *vp, voff_t newsize)
    127 {
    128 
    129 	mutex_enter(&vp->v_interlock);
    130 	vp->v_writesize = newsize;
    131 	mutex_exit(&vp->v_interlock);
    132 }
    133 
    134 void
    135 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    136 {
    137 	struct uvm_object *uobj = &vp->v_uobj;
    138 	struct vm_page **pgs;
    139 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    140 	int rv, npages, i;
    141 
    142 	pgs = kmem_zalloc(maxpages * sizeof(pgs), KM_SLEEP);
    143 	while (len) {
    144 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    145 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    146 		mutex_enter(&uobj->vmobjlock);
    147 		rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
    148 		KASSERT(npages > 0);
    149 
    150 		for (i = 0; i < npages; i++) {
    151 			uint8_t *start;
    152 			size_t chunkoff, chunklen;
    153 
    154 			chunkoff = off & PAGE_MASK;
    155 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    156 			start = (uint8_t *)pgs[i]->uanon + chunkoff;
    157 
    158 			memset(start, 0, chunklen);
    159 			pgs[i]->flags &= ~PG_CLEAN;
    160 
    161 			off += chunklen;
    162 			len -= chunklen;
    163 		}
    164 		uvm_page_unbusy(pgs, npages);
    165 	}
    166 	kmem_free(pgs, maxpages * sizeof(pgs));
    167 
    168 	return;
    169 }
    170 
    171 /*
    172  * UBC
    173  */
    174 
    175 /* dumdidumdum */
    176 #define len2npages(off, len)						\
    177   (((((len) + PAGE_MASK) & ~(PAGE_MASK)) >> PAGE_SHIFT)			\
    178     + (((off & PAGE_MASK) + (len & PAGE_MASK)) > PAGE_SIZE))
    179 
    180 int
    181 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    182 	int advice, int flags)
    183 {
    184 	struct vm_page **pgs;
    185 	int npages = len2npages(uio->uio_offset, todo);
    186 	size_t pgalloc;
    187 	int i, rv;
    188 
    189 	pgalloc = npages * sizeof(pgs);
    190 	pgs = kmem_zalloc(pgalloc, KM_SLEEP);
    191 
    192 	do {
    193 		mutex_enter(&uobj->vmobjlock);
    194 		rv = uobj->pgops->pgo_get(uobj, uio->uio_offset, pgs, &npages,
    195 		    0, 0, 0, 0);
    196 		if (rv)
    197 			goto out;
    198 
    199 		for (i = 0; i < npages; i++) {
    200 			size_t xfersize;
    201 			off_t pageoff;
    202 
    203 			pageoff = uio->uio_offset & PAGE_MASK;
    204 			xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
    205 			uiomove((uint8_t *)pgs[i]->uanon + pageoff,
    206 			    xfersize, uio);
    207 			if (uio->uio_rw == UIO_WRITE)
    208 				pgs[i]->flags &= ~PG_CLEAN;
    209 			todo -= xfersize;
    210 		}
    211 		uvm_page_unbusy(pgs, npages);
    212 	} while (todo);
    213 
    214  out:
    215 	kmem_free(pgs, pgalloc);
    216 	return rv;
    217 }
    218