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