1 /*	$NetBSD: vm_vfs.c,v 1.34 2013/10/18 19:56:11 christos 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.34 2013/10/18 19:56:11 christos 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
uvm_aio_aiodone(struct buf * bp)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 			mutex_enter(uobj->vmobjlock);
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(mutex_owned(uobj->vmobjlock));
75 
76 	mutex_enter(&uvm_pageqlock);
77 	uvm_page_unbusy(pgs, npages);
78 	mutex_exit(&uvm_pageqlock);
79 	mutex_exit(uobj->vmobjlock);
80 
81 	uvm_pagermapout((vaddr_t)bp->b_data, npages);
82 	uvm_pageout_done(pageout);
83 
84 	if (BUF_ISWRITE(bp) && (bp->b_cflags & BC_AGE) != 0) {
85 		mutex_enter(bp->b_objlock);
86 		vwakeup(bp);
87 		mutex_exit(bp->b_objlock);
88 	}
89 
90 	putiobuf(bp);
91 
92 	kmem_free(pgs, npages * sizeof(*pgs));
93 }
94 
95 void
uvm_aio_biodone(struct buf * bp)96 uvm_aio_biodone(struct buf *bp)
97 {
98 
99 	uvm_aio_aiodone(bp);
100 }
101 
102 /*
103  * UBC
104  */
105 
106 #define PAGERFLAGS (PGO_SYNCIO | PGO_NOBLOCKALLOC | PGO_NOTIMESTAMP)
107 
108 void
ubc_zerorange(struct uvm_object * uobj,off_t off,size_t len,int flags)109 ubc_zerorange(struct uvm_object *uobj, off_t off, size_t len, int flags)
110 {
111 	struct vm_page **pgs;
112 	int maxpages = MIN(32, round_page(len) >> PAGE_SHIFT);
113 	int npages, i;
114 
115 	if (maxpages == 0)
116 		return;
117 
118 	pgs = kmem_alloc(maxpages * sizeof(pgs), KM_SLEEP);
119 	mutex_enter(uobj->vmobjlock);
120 	while (len) {
121 		npages = MIN(maxpages, round_page(len) >> PAGE_SHIFT);
122 		memset(pgs, 0, npages * sizeof(struct vm_page *));
123 		(void)uobj->pgops->pgo_get(uobj, trunc_page(off),
124 		    pgs, &npages, 0, VM_PROT_READ | VM_PROT_WRITE,
125 		    0, PAGERFLAGS | PGO_PASTEOF);
126 		KASSERT(npages > 0);
127 
128 		mutex_enter(uobj->vmobjlock);
129 		for (i = 0; i < npages; i++) {
130 			struct vm_page *pg;
131 			uint8_t *start;
132 			size_t chunkoff, chunklen;
133 
134 			pg = pgs[i];
135 			if (pg == NULL)
136 				break;
137 
138 			KASSERT(pg->uobject != NULL);
139 			KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
140 
141 			chunkoff = off & PAGE_MASK;
142 			chunklen = MIN(PAGE_SIZE - chunkoff, len);
143 			start = (uint8_t *)pg->uanon + chunkoff;
144 
145 			memset(start, 0, chunklen);
146 			pg->flags &= ~PG_CLEAN;
147 
148 			off += chunklen;
149 			len -= chunklen;
150 		}
151 		uvm_page_unbusy(pgs, npages);
152 	}
153 	mutex_exit(uobj->vmobjlock);
154 	kmem_free(pgs, maxpages * sizeof(pgs));
155 }
156 
157 #define len2npages(off, len)						\
158     ((round_page(off+len) - trunc_page(off)) >> PAGE_SHIFT)
159 
160 int
ubc_uiomove(struct uvm_object * uobj,struct uio * uio,vsize_t todo,int advice,int flags)161 ubc_uiomove(struct uvm_object *uobj, struct uio *uio, vsize_t todo,
162 	int advice, int flags)
163 {
164 	struct vm_page **pgs;
165 	int npages = len2npages(uio->uio_offset, todo);
166 	size_t pgalloc;
167 	int i, rv, pagerflags;
168 	vm_prot_t prot;
169 
170 	pgalloc = npages * sizeof(pgs);
171 	pgs = kmem_alloc(pgalloc, KM_SLEEP);
172 
173 	pagerflags = PAGERFLAGS;
174 	if (flags & UBC_WRITE)
175 		pagerflags |= PGO_PASTEOF;
176 	if (flags & UBC_FAULTBUSY)
177 		pagerflags |= PGO_OVERWRITE;
178 
179 	prot = VM_PROT_READ;
180 	if (flags & UBC_WRITE)
181 		prot |= VM_PROT_WRITE;
182 
183 	mutex_enter(uobj->vmobjlock);
184 	do {
185 		npages = len2npages(uio->uio_offset, todo);
186 		memset(pgs, 0, pgalloc);
187 		rv = uobj->pgops->pgo_get(uobj, trunc_page(uio->uio_offset),
188 		    pgs, &npages, 0, prot, 0, pagerflags);
189 		if (rv)
190 			goto out;
191 
192 		mutex_enter(uobj->vmobjlock);
193 		for (i = 0; i < npages; i++) {
194 			struct vm_page *pg;
195 			size_t xfersize;
196 			off_t pageoff;
197 
198 			pg = pgs[i];
199 			if (pg == NULL)
200 				break;
201 
202 			KASSERT(pg->uobject != NULL);
203 			KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
204 			pageoff = uio->uio_offset & PAGE_MASK;
205 
206 			xfersize = MIN(MIN(todo, PAGE_SIZE), PAGE_SIZE-pageoff);
207 			KASSERT(xfersize > 0);
208 			rv = uiomove((uint8_t *)pg->uanon + pageoff,
209 			    xfersize, uio);
210 			if (rv) {
211 				uvm_page_unbusy(pgs, npages);
212 				mutex_exit(uobj->vmobjlock);
213 				goto out;
214 			}
215 			if (uio->uio_rw == UIO_WRITE)
216 				pg->flags &= ~(PG_CLEAN | PG_FAKE);
217 			todo -= xfersize;
218 		}
219 		uvm_page_unbusy(pgs, npages);
220 	} while (todo);
221 	mutex_exit(uobj->vmobjlock);
222 
223  out:
224 	kmem_free(pgs, pgalloc);
225 	return rv;
226 }
227