Home | History | Annotate | Line # | Download | only in rumpvfs
vm_vfs.c revision 1.5.2.3
      1 /*	$NetBSD: vm_vfs.c,v 1.5.2.3 2009/08/19 18:48:30 yamt 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.2.3 2009/08/19 18:48:30 yamt 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 
     54 	pgs = kmem_alloc(npages * sizeof(*pgs), KM_SLEEP);
     55 	for (i = 0; i < npages; i++) {
     56 		va = (vaddr_t)bp->b_data + (i << PAGE_SHIFT);
     57 		pgs[i] = uvm_pageratop(va);
     58 		pgs[i]->flags &= ~PG_PAGEOUT;
     59 	}
     60 
     61 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
     62 	uvm_page_unbusy(pgs, npages);
     63 
     64 	if (BUF_ISWRITE(bp) && (bp->b_cflags & BC_AGE) != 0) {
     65 		mutex_enter(bp->b_objlock);
     66 		vwakeup(bp);
     67 		mutex_exit(bp->b_objlock);
     68 	}
     69 
     70 	putiobuf(bp);
     71 
     72 	kmem_free(pgs, npages * sizeof(*pgs));
     73 }
     74 
     75 void
     76 uvm_aio_biodone(struct buf *bp)
     77 {
     78 
     79 	uvm_aio_aiodone(bp);
     80 }
     81 
     82 struct uvm_ractx *
     83 uvm_ra_allocctx(void)
     84 {
     85 
     86 	return NULL;
     87 }
     88 
     89 void
     90 uvm_ra_request(struct uvm_ractx *ra, int advice, struct uvm_object *uobj,
     91 	off_t reqoff, size_t reqsize)
     92 {
     93 
     94 	return;
     95 }
     96 
     97 void
     98 uvm_ra_freectx(struct uvm_ractx *ra)
     99 {
    100 
    101 	return;
    102 }
    103 
    104 /*
    105  * UBC
    106  */
    107 
    108 void
    109 uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    110 {
    111 	struct uvm_object *uobj = &vp->v_uobj;
    112 	struct vm_page **pgs;
    113 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    114 	int rv, npages, i;
    115 
    116 	pgs = kmem_zalloc(maxpages * sizeof(pgs), KM_SLEEP);
    117 	while (len) {
    118 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    119 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    120 		mutex_enter(&uobj->vmobjlock);
    121 		rv = uobj->pgops->pgo_get(uobj, off, pgs, &npages, 0, 0, 0, 0);
    122 		KASSERT(npages > 0);
    123 
    124 		for (i = 0; i < npages; i++) {
    125 			uint8_t *start;
    126 			size_t chunkoff, chunklen;
    127 
    128 			chunkoff = off & PAGE_MASK;
    129 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    130 			start = (uint8_t *)pgs[i]->uanon + chunkoff;
    131 
    132 			memset(start, 0, chunklen);
    133 			pgs[i]->flags &= ~PG_CLEAN;
    134 
    135 			off += chunklen;
    136 			len -= chunklen;
    137 		}
    138 		uvm_page_unbusy(pgs, npages);
    139 	}
    140 	kmem_free(pgs, maxpages * sizeof(pgs));
    141 
    142 	return;
    143 }
    144 
    145 /* dumdidumdum */
    146 #define len2npages(off, len)						\
    147   (((((len) + PAGE_MASK) & ~(PAGE_MASK)) >> PAGE_SHIFT)			\
    148     + (((off & PAGE_MASK) + (len & PAGE_MASK)) > PAGE_SIZE))
    149 
    150 int
    151 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    152 	int advice, int flags)
    153 {
    154 	struct vm_page **pgs;
    155 	int npages = len2npages(uio->uio_offset, todo);
    156 	size_t pgalloc;
    157 	int i, rv, pagerflags;
    158 
    159 	pgalloc = npages * sizeof(pgs);
    160 	pgs = kmem_zalloc(pgalloc, KM_SLEEP);
    161 
    162 	pagerflags = PGO_SYNCIO | PGO_NOBLOCKALLOC | PGO_NOTIMESTAMP;
    163 	if (flags & UBC_WRITE)
    164 		pagerflags |= PGO_PASTEOF;
    165 	if (flags & UBC_FAULTBUSY)
    166 		pagerflags |= PGO_OVERWRITE;
    167 
    168 	do {
    169 		mutex_enter(&uobj->vmobjlock);
    170 		rv = uobj->pgops->pgo_get(uobj, uio->uio_offset, pgs, &npages,
    171 		    0, VM_PROT_READ | VM_PROT_WRITE, 0, pagerflags);
    172 		if (rv)
    173 			goto out;
    174 
    175 		for (i = 0; i < npages; i++) {
    176 			size_t xfersize;
    177 			off_t pageoff;
    178 
    179 			pageoff = uio->uio_offset & PAGE_MASK;
    180 			xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
    181 			uiomove((uint8_t *)pgs[i]->uanon + pageoff,
    182 			    xfersize, uio);
    183 			if (uio->uio_rw == UIO_WRITE)
    184 				pgs[i]->flags &= ~PG_CLEAN;
    185 			todo -= xfersize;
    186 		}
    187 		uvm_page_unbusy(pgs, npages);
    188 	} while (todo);
    189 
    190  out:
    191 	kmem_free(pgs, pgalloc);
    192 	return rv;
    193 }
    194