]> git.saurik.com Git - apple/xnu.git/blame - bsd/vm/dp_backing_file.c
xnu-123.5.tar.gz
[apple/xnu.git] / bsd / vm / dp_backing_file.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23#include <mach/boolean.h>
24#include <sys/param.h>
25#include <sys/systm.h>
26#include <sys/lock.h>
27#include <sys/proc.h>
28#include <sys/buf.h>
29#include <sys/uio.h>
30#include <sys/vnode.h>
31#include <sys/namei.h>
32#include <sys/ubc.h>
33
34#include <mach/mach_types.h>
35#include <vm/vm_map.h>
36#include <vm/vm_kern.h>
37#include <kern/host.h>
38#include <kern/parallel.h>
39#include <kern/zalloc.h>
40#include <kern/kalloc.h>
41#include <libkern/libkern.h>
42#include <sys/malloc.h>
43
44#include <vm/vnode_pager.h>
45
46/*
47 * temporary support for delayed instantiation
48 * of default_pager
49 */
50int default_pager_init_flag = 0;
51
52struct bs_map bs_port_table[MAX_BACKING_STORE] = {
53 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
54 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
55 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
56 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
57 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
58 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
59 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
60 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
61 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},
62 {0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}};
63
64/* ###################################################### */
65
66
67#include <kern/assert.h>
68
69/*
70 * Routine: macx_swapon
71 * Function:
72 * Syscall interface to add a file to backing store
73 */
74int
75macx_swapon(
76 char *filename,
77 int flags,
78 long size,
79 long priority)
80{
81 struct vnode *vp = 0;
82 struct nameidata nd, *ndp;
83 struct proc *p = current_proc();
84 pager_file_t pf;
85 register int error;
86 kern_return_t kr;
87 mach_port_t backing_store;
88 mach_port_t default_pager_port = MACH_PORT_NULL;
89 int i;
90 boolean_t funnel_state;
91
92 struct vattr vattr;
93
94 funnel_state = thread_funnel_set(kernel_flock, TRUE);
95 ndp = &nd;
96
97 if ((error = suser(p->p_ucred, &p->p_acflag)))
98 goto swapon_bailout;
99
100 unix_master();
101
102 if(default_pager_init_flag == 0) {
103 start_def_pager(NULL);
104 default_pager_init_flag = 1;
105 }
106
107 /*
108 * Get a vnode for the paging area.
109 */
110 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
111 filename, p);
112
113 if ((error = namei(ndp)))
114 goto swapon_bailout;
115 vp = ndp->ni_vp;
116
117 if (vp->v_type != VREG) {
118 error = EINVAL;
119 VOP_UNLOCK(vp, 0, p);
120 goto swapon_bailout;
121 }
122 UBCINFOCHECK("macx_swapon", vp);
123
124 if (error = VOP_GETATTR(vp, &vattr, p->p_ucred, p)) {
125 VOP_UNLOCK(vp, 0, p);
126 goto swapon_bailout;
127 }
128
129 if (vattr.va_size < (u_quad_t)size) {
130 vattr_null(&vattr);
131 vattr.va_size = (u_quad_t)size;
132 error = VOP_SETATTR(vp, &vattr, p->p_ucred, p);
133 if (error) {
134 VOP_UNLOCK(vp, 0, p);
135 goto swapon_bailout;
136 }
137 }
138
139 /* add new backing store to list */
140 i = 0;
141 while(bs_port_table[i].vp != 0) {
142 if(i == MAX_BACKING_STORE)
143 break;
144 i++;
145 }
146 if(i == MAX_BACKING_STORE) {
147 error = ENOMEM;
148 VOP_UNLOCK(vp, 0, p);
149 goto swapon_bailout;
150 }
151
152 /* remember the vnode. This vnode has namei() reference */
153 bs_port_table[i].vp = vp;
154
155 /*
156 * Look to see if we are already paging to this file.
157 */
158 /* make certain the copy send of kernel call will work */
159 kr = host_default_memory_manager(host_priv_self(), &default_pager_port, 0);
160 if(kr != KERN_SUCCESS) {
161 error = EAGAIN;
162 VOP_UNLOCK(vp, 0, p);
163 bs_port_table[i].vp = 0;
164 goto swapon_bailout;
165 }
166
167 kr = default_pager_backing_store_create(default_pager_port,
168 -1, /* default priority */
169 0, /* default cluster size */
170 &backing_store);
171 if(kr != KERN_SUCCESS) {
172 error = ENOMEM;
173 VOP_UNLOCK(vp, 0, p);
174 bs_port_table[i].vp = 0;
175 goto swapon_bailout;
176 }
177
178 /*
179 * NOTE: we are able to supply PAGE_SIZE here instead of
180 * an actual record size or block number because:
181 * a: we do not support offsets from the beginning of the
182 * file (allowing for non page size/record modulo offsets.
183 * b: because allow paging will be done modulo page size
184 */
185
186 VOP_UNLOCK(vp, 0, p);
187 kr = default_pager_add_file(backing_store, vp, PAGE_SIZE,
188 ((int)vattr.va_size)/PAGE_SIZE);
189 if(kr != KERN_SUCCESS) {
190 bs_port_table[i].vp = 0;
191 if(kr == KERN_INVALID_ARGUMENT)
192 error = EINVAL;
193 else
194 error = ENOMEM;
195 goto swapon_bailout;
196 }
197 bs_port_table[i].bs = (void *)backing_store;
198 error = 0;
199 if (!ubc_hold(vp))
200 panic("macx_swapon: hold");
201
202 /* Mark this vnode as being used for swapfile */
203 SET(vp->v_flag, VSWAP);
204
205 /*
206 * take an extra reference on the vnode to keep
207 * vnreclaim() away from this vnode.
208 */
209 VREF(vp);
210
211 /* Hold on to the namei reference to the paging file vnode */
212 vp = 0;
213
214swapon_bailout:
215 if (vp) {
216 vrele(vp);
217 }
218 unix_release();
219 (void) thread_funnel_set(kernel_flock, FALSE);
220 return(error);
221}
222
223/*
224 * Routine: macx_swapoff
225 * Function:
226 * Syscall interface to remove a file from backing store
227 */
228int
229macx_swapoff(
230 char *filename,
231 int flags)
232{
233 kern_return_t kr;
234 mach_port_t backing_store;
235
236 struct vnode *vp = 0;
237 struct nameidata nd, *ndp;
238 struct proc *p = current_proc();
239 int i;
240 int error;
241 boolean_t funnel_state;
242
243 funnel_state = thread_funnel_set(kernel_flock, TRUE);
244 backing_store = NULL;
245 ndp = &nd;
246
247 if ((error = suser(p->p_ucred, &p->p_acflag)))
248 goto swapoff_bailout;
249
250 unix_master();
251
252 /*
253 * Get the vnode for the paging area.
254 */
255 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE,
256 filename, p);
257
258 if ((error = namei(ndp)))
259 goto swapoff_bailout;
260 vp = ndp->ni_vp;
261
262 if (vp->v_type != VREG) {
263 error = EINVAL;
264 VOP_UNLOCK(vp, 0, p);
265 goto swapoff_bailout;
266 }
267
268 for(i = 0; i < MAX_BACKING_STORE; i++) {
269 if(bs_port_table[i].vp == vp) {
270 backing_store;
271 break;
272 }
273 }
274 if (i == MAX_BACKING_STORE) {
275 error = EINVAL;
276 VOP_UNLOCK(vp, 0, p);
277 goto swapoff_bailout;
278 }
279 backing_store = (mach_port_t)bs_port_table[i].bs;
280
281 VOP_UNLOCK(vp, 0, p);
282 kr = default_pager_backing_store_delete(backing_store);
283 switch (kr) {
284 case KERN_SUCCESS:
285 error = 0;
286 bs_port_table[i].vp = 0;
287 ubc_rele(vp);
288 /* This vnode is no longer used for swapfile */
289 CLR(vp->v_flag, VSWAP);
290
291 /* get rid of macx_swapon() namei() reference */
292 vrele(vp);
293
294 /* get rid of macx_swapon() "extra" reference */
295 vrele(vp);
296 break;
297 case KERN_FAILURE:
298 error = EAGAIN;
299 break;
300 default:
301 error = EAGAIN;
302 break;
303 }
304
305swapoff_bailout:
306 /* get rid of macx_swapoff() namei() reference */
307 if (vp)
308 vrele(vp);
309
310 unix_release();
311 (void) thread_funnel_set(kernel_flock, FALSE);
312 return(error);
313}