Home | History | Annotate | Line # | Download | only in rumpvfs
vm_vfs.c revision 1.24.2.1
      1  1.24.2.1  jruoho /*	$NetBSD: vm_vfs.c,v 1.24.2.1 2011/06/06 09:10:09 jruoho Exp $	*/
      2       1.1   pooka 
      3       1.1   pooka /*
      4  1.24.2.1  jruoho  * 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.24.2.1  jruoho __KERNEL_RCSID(0, "$NetBSD: vm_vfs.c,v 1.24.2.1 2011/06/06 09:10:09 jruoho 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.6   pooka /*
     40       1.6   pooka  * release resources held during async io.  this is almost the
     41       1.6   pooka  * same as uvm_aio_aiodone() from uvm_pager.c and only lacks the
     42       1.6   pooka  * call to uvm_aio_aiodone_pages(): unbusies pages directly here.
     43       1.7   pooka  */
     44       1.1   pooka void
     45       1.1   pooka uvm_aio_aiodone(struct buf *bp)
     46       1.1   pooka {
     47      1.21   pooka 	struct uvm_object *uobj;
     48       1.6   pooka 	int i, npages = bp->b_bufsize >> PAGE_SHIFT;
     49       1.6   pooka 	struct vm_page **pgs;
     50       1.6   pooka 	vaddr_t va;
     51      1.16   pooka 	int pageout = 0;
     52       1.6   pooka 
     53      1.21   pooka 	KASSERT(npages > 0);
     54       1.6   pooka 	pgs = kmem_alloc(npages * sizeof(*pgs), KM_SLEEP);
     55       1.6   pooka 	for (i = 0; i < npages; i++) {
     56       1.6   pooka 		va = (vaddr_t)bp->b_data + (i << PAGE_SHIFT);
     57       1.6   pooka 		pgs[i] = uvm_pageratop(va);
     58      1.16   pooka 		if (pgs[i]->flags & PG_PAGEOUT) {
     59      1.16   pooka 			KASSERT((pgs[i]->flags & PG_FAKE) == 0);
     60      1.16   pooka 			pageout++;
     61      1.16   pooka 			pgs[i]->flags &= ~PG_PAGEOUT;
     62      1.23   pooka 			pgs[i]->flags |= PG_RELEASED;
     63      1.16   pooka 		}
     64       1.6   pooka 	}
     65       1.6   pooka 
     66       1.6   pooka 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
     67      1.21   pooka 
     68      1.21   pooka 	/* get uobj because we need it after pages might be recycled */
     69      1.21   pooka 	uobj = pgs[0]->uobject;
     70      1.21   pooka 	KASSERT(uobj);
     71      1.21   pooka 
     72      1.21   pooka 	mutex_enter(&uobj->vmobjlock);
     73      1.21   pooka 	mutex_enter(&uvm_pageqlock);
     74       1.6   pooka 	uvm_page_unbusy(pgs, npages);
     75      1.21   pooka 	mutex_exit(&uvm_pageqlock);
     76      1.21   pooka 	mutex_exit(&uobj->vmobjlock);
     77       1.1   pooka 
     78  1.24.2.1  jruoho 	uvm_pageout_done(pageout);
     79  1.24.2.1  jruoho 
     80       1.6   pooka 	if (BUF_ISWRITE(bp) && (bp->b_cflags & BC_AGE) != 0) {
     81       1.6   pooka 		mutex_enter(bp->b_objlock);
     82       1.6   pooka 		vwakeup(bp);
     83       1.6   pooka 		mutex_exit(bp->b_objlock);
     84       1.6   pooka 	}
     85       1.6   pooka 
     86       1.6   pooka 	putiobuf(bp);
     87       1.6   pooka 
     88       1.6   pooka 	kmem_free(pgs, npages * sizeof(*pgs));
     89       1.1   pooka }
     90       1.1   pooka 
     91       1.8   pooka void
     92       1.8   pooka uvm_aio_biodone(struct buf *bp)
     93       1.1   pooka {
     94       1.1   pooka 
     95       1.8   pooka 	uvm_aio_aiodone(bp);
     96       1.1   pooka }
     97       1.1   pooka 
     98       1.8   pooka /*
     99       1.8   pooka  * UBC
    100       1.8   pooka  */
    101       1.8   pooka 
    102      1.17   pooka #define PAGERFLAGS (PGO_SYNCIO | PGO_NOBLOCKALLOC | PGO_NOTIMESTAMP)
    103      1.17   pooka 
    104       1.2   pooka void
    105       1.2   pooka uvm_vnp_zerorange(struct vnode *vp, off_t off, size_t len)
    106       1.2   pooka {
    107       1.2   pooka 	struct uvm_object *uobj = &vp->v_uobj;
    108       1.2   pooka 	struct vm_page **pgs;
    109  1.24.2.1  jruoho 	struct uvm_object *pguobj;
    110       1.2   pooka 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
    111       1.2   pooka 	int rv, npages, i;
    112       1.2   pooka 
    113      1.15   pooka 	if (maxpages == 0)
    114      1.15   pooka 		return;
    115      1.15   pooka 
    116      1.20   pooka 	pgs = kmem_alloc(maxpages * sizeof(pgs), KM_SLEEP);
    117      1.22   pooka 	mutex_enter(&uobj->vmobjlock);
    118       1.2   pooka 	while (len) {
    119       1.2   pooka 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
    120       1.2   pooka 		memset(pgs, 0, npages * sizeof(struct vm_page *));
    121      1.19   pooka 		rv = uobj->pgops->pgo_get(uobj, trunc_page(off),
    122      1.19   pooka 		    pgs, &npages, 0, VM_PROT_READ | VM_PROT_WRITE,
    123      1.19   pooka 		    0, PAGERFLAGS | PGO_PASTEOF);
    124       1.2   pooka 		KASSERT(npages > 0);
    125       1.2   pooka 
    126  1.24.2.1  jruoho 		for (i = 0, pguobj = NULL; i < npages; i++) {
    127      1.20   pooka 			struct vm_page *pg;
    128       1.2   pooka 			uint8_t *start;
    129       1.2   pooka 			size_t chunkoff, chunklen;
    130       1.2   pooka 
    131      1.20   pooka 			pg = pgs[i];
    132      1.20   pooka 			if (pg == NULL)
    133      1.20   pooka 				break;
    134  1.24.2.1  jruoho 			if (pguobj == NULL)
    135  1.24.2.1  jruoho 				pguobj = pg->uobject;
    136  1.24.2.1  jruoho 			KASSERT(pguobj == pg->uobject);
    137      1.20   pooka 
    138       1.2   pooka 			chunkoff = off & PAGE_MASK;
    139       1.2   pooka 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
    140      1.20   pooka 			start = (uint8_t *)pg->uanon + chunkoff;
    141       1.2   pooka 
    142       1.2   pooka 			memset(start, 0, chunklen);
    143      1.20   pooka 			pg->flags &= ~PG_CLEAN;
    144       1.2   pooka 
    145       1.2   pooka 			off += chunklen;
    146       1.2   pooka 			len -= chunklen;
    147       1.2   pooka 		}
    148  1.24.2.1  jruoho 		mutex_enter(&pguobj->vmobjlock);
    149       1.2   pooka 		uvm_page_unbusy(pgs, npages);
    150  1.24.2.1  jruoho 		if (pguobj != uobj) {
    151  1.24.2.1  jruoho 			mutex_exit(&pguobj->vmobjlock);
    152  1.24.2.1  jruoho 			mutex_enter(&uobj->vmobjlock);
    153  1.24.2.1  jruoho 		}
    154       1.2   pooka 	}
    155      1.22   pooka 	mutex_exit(&uobj->vmobjlock);
    156       1.2   pooka 	kmem_free(pgs, maxpages * sizeof(pgs));
    157       1.2   pooka 
    158       1.2   pooka 	return;
    159       1.2   pooka }
    160       1.2   pooka 
    161       1.2   pooka #define len2npages(off, len)						\
    162      1.18   pooka     ((round_page(off+len) - trunc_page(off)) >> PAGE_SHIFT)
    163       1.2   pooka 
    164       1.2   pooka int
    165       1.2   pooka ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
    166       1.2   pooka 	int advice, int flags)
    167       1.2   pooka {
    168       1.2   pooka 	struct vm_page **pgs;
    169  1.24.2.1  jruoho 	struct uvm_object *pguobj;
    170       1.2   pooka 	int npages = len2npages(uio->uio_offset, todo);
    171       1.2   pooka 	size_t pgalloc;
    172       1.6   pooka 	int i, rv, pagerflags;
    173  1.24.2.1  jruoho 	vm_prot_t prot;
    174       1.2   pooka 
    175       1.2   pooka 	pgalloc = npages * sizeof(pgs);
    176      1.20   pooka 	pgs = kmem_alloc(pgalloc, KM_SLEEP);
    177       1.2   pooka 
    178      1.17   pooka 	pagerflags = PAGERFLAGS;
    179       1.6   pooka 	if (flags & UBC_WRITE)
    180       1.6   pooka 		pagerflags |= PGO_PASTEOF;
    181       1.6   pooka 	if (flags & UBC_FAULTBUSY)
    182       1.6   pooka 		pagerflags |= PGO_OVERWRITE;
    183       1.6   pooka 
    184  1.24.2.1  jruoho 	prot = VM_PROT_READ;
    185  1.24.2.1  jruoho 	if (flags & UBC_WRITE)
    186  1.24.2.1  jruoho 		prot |= VM_PROT_WRITE;
    187  1.24.2.1  jruoho 
    188      1.22   pooka 	mutex_enter(&uobj->vmobjlock);
    189       1.2   pooka 	do {
    190      1.20   pooka 		npages = len2npages(uio->uio_offset, todo);
    191      1.20   pooka 		memset(pgs, 0, pgalloc);
    192      1.20   pooka 		rv = uobj->pgops->pgo_get(uobj, trunc_page(uio->uio_offset),
    193  1.24.2.1  jruoho 		    pgs, &npages, 0, prot, 0, pagerflags);
    194       1.2   pooka 		if (rv)
    195       1.2   pooka 			goto out;
    196       1.2   pooka 
    197  1.24.2.1  jruoho 		for (i = 0, pguobj = NULL; i < npages; i++) {
    198      1.20   pooka 			struct vm_page *pg;
    199       1.2   pooka 			size_t xfersize;
    200       1.2   pooka 			off_t pageoff;
    201       1.2   pooka 
    202      1.20   pooka 			pg = pgs[i];
    203      1.20   pooka 			if (pg == NULL)
    204      1.20   pooka 				break;
    205  1.24.2.1  jruoho 			if (pguobj == NULL)
    206  1.24.2.1  jruoho 				pguobj = pg->uobject;
    207  1.24.2.1  jruoho 			KASSERT(pguobj == pg->uobject);
    208      1.20   pooka 
    209       1.2   pooka 			pageoff = uio->uio_offset & PAGE_MASK;
    210       1.2   pooka 			xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
    211      1.13   pooka 			KASSERT(xfersize > 0);
    212      1.24   pooka 			rv = uiomove((uint8_t *)pg->uanon + pageoff,
    213       1.2   pooka 			    xfersize, uio);
    214      1.24   pooka 			if (rv) {
    215  1.24.2.1  jruoho 				mutex_enter(&pguobj->vmobjlock);
    216      1.24   pooka 				uvm_page_unbusy(pgs, npages);
    217  1.24.2.1  jruoho 				mutex_exit(&pguobj->vmobjlock);
    218      1.24   pooka 				goto out;
    219      1.24   pooka 			}
    220       1.2   pooka 			if (uio->uio_rw == UIO_WRITE)
    221      1.20   pooka 				pg->flags &= ~(PG_CLEAN | PG_FAKE);
    222       1.2   pooka 			todo -= xfersize;
    223       1.2   pooka 		}
    224  1.24.2.1  jruoho 		mutex_enter(&pguobj->vmobjlock);
    225       1.2   pooka 		uvm_page_unbusy(pgs, npages);
    226  1.24.2.1  jruoho 		if (pguobj != uobj) {
    227  1.24.2.1  jruoho 			mutex_exit(&pguobj->vmobjlock);
    228  1.24.2.1  jruoho 			mutex_enter(&uobj->vmobjlock);
    229  1.24.2.1  jruoho 		}
    230       1.2   pooka 	} while (todo);
    231      1.22   pooka 	mutex_exit(&uobj->vmobjlock);
    232       1.2   pooka 
    233       1.2   pooka  out:
    234       1.2   pooka 	kmem_free(pgs, pgalloc);
    235       1.2   pooka 	return rv;
    236       1.2   pooka }
    237