]> git.saurik.com Git - apple/xnu.git/blob - bsd/vm/vnode_pager.c
552c0afb0bf733fb2d758c5d17d8d1a98b0cfd05
[apple/xnu.git] / bsd / vm / vnode_pager.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Mach Operating System
30 * Copyright (c) 1987 Carnegie-Mellon University
31 * All rights reserved. The CMU software License Agreement specifies
32 * the terms and conditions for use and redistribution.
33 */
34 /*
35 * File: vnode_pager.c
36 *
37 * "Swap" pager that pages to/from vnodes. Also
38 * handles demand paging from files.
39 *
40 */
41
42 #include <mach/boolean.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/user.h>
46 #include <sys/proc.h>
47 #include <sys/kauth.h>
48 #include <sys/buf.h>
49 #include <sys/uio.h>
50 #include <sys/vnode_internal.h>
51 #include <sys/namei.h>
52 #include <sys/mount_internal.h> /* needs internal due to fhandle_t */
53 #include <sys/ubc_internal.h>
54 #include <sys/lock.h>
55
56 #include <mach/mach_types.h>
57 #include <mach/memory_object_types.h>
58
59 #include <vm/vm_map.h>
60 #include <vm/vm_kern.h>
61 #include <kern/zalloc.h>
62 #include <kern/kalloc.h>
63 #include <libkern/libkern.h>
64
65 #include <vm/vnode_pager.h>
66 #include <vm/vm_pageout.h>
67
68 #include <kern/assert.h>
69 #include <sys/kdebug.h>
70 #include <machine/spl.h>
71
72 #include <nfs/rpcv2.h>
73 #include <nfs/nfsproto.h>
74 #include <nfs/nfs.h>
75
76 #include <vm/vm_protos.h>
77
78 unsigned int vp_pagein=0;
79 unsigned int vp_pgodirty=0;
80 unsigned int vp_pgoclean=0;
81 unsigned int dp_pgouts=0; /* Default pager pageouts */
82 unsigned int dp_pgins=0; /* Default pager pageins */
83
84 vm_object_offset_t
85 vnode_pager_get_filesize(struct vnode *vp)
86 {
87
88 return (vm_object_offset_t) ubc_getsize(vp);
89 }
90
91 kern_return_t
92 vnode_pager_get_pathname(
93 struct vnode *vp,
94 char *pathname,
95 vm_size_t *length_p)
96 {
97 int error, len;
98
99 len = (int) *length_p;
100 error = vn_getpath(vp, pathname, &len);
101 if (error != 0) {
102 return KERN_FAILURE;
103 }
104 *length_p = (vm_size_t) len;
105 return KERN_SUCCESS;
106 }
107
108 kern_return_t
109 vnode_pager_get_filename(
110 struct vnode *vp,
111 char **filename)
112 {
113 *filename = vp->v_name;
114 return KERN_SUCCESS;
115 }
116
117 pager_return_t
118 vnode_pageout(struct vnode *vp,
119 upl_t upl,
120 vm_offset_t upl_offset,
121 vm_object_offset_t f_offset,
122 vm_size_t size,
123 int flags,
124 int *errorp)
125 {
126 struct proc *p = current_proc();
127 int result = PAGER_SUCCESS;
128 int error = 0;
129 int error_ret = 0;
130 daddr64_t blkno;
131 int isize;
132 int pg_index;
133 int base_index;
134 int offset;
135 upl_page_info_t *pl;
136 struct vfs_context context;
137
138 context.vc_proc = p;
139 context.vc_ucred = kauth_cred_get();
140
141 isize = (int)size;
142
143 if (isize <= 0) {
144 result = PAGER_ERROR;
145 error_ret = EINVAL;
146 goto out;
147 }
148 UBCINFOCHECK("vnode_pageout", vp);
149
150 if (UBCINVALID(vp)) {
151 result = PAGER_ERROR;
152 error_ret = EINVAL;
153
154 if (upl && !(flags & UPL_NOCOMMIT))
155 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
156 goto out;
157 }
158 if ( !(flags & UPL_VNODE_PAGER)) {
159 /*
160 * This is a pageout from the default pager,
161 * just go ahead and call vnop_pageout since
162 * it has already sorted out the dirty ranges
163 */
164 dp_pgouts++;
165
166 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
167 size, 1, 0, 0, 0);
168
169 if ( (error_ret = VNOP_PAGEOUT(vp, upl, upl_offset, (off_t)f_offset,
170 (size_t)size, flags, &context)) )
171 result = PAGER_ERROR;
172
173 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
174 size, 1, 0, 0, 0);
175
176 goto out;
177 }
178 /*
179 * we come here for pageouts to 'real' files and
180 * for msyncs... the upl may not contain any
181 * dirty pages.. it's our responsibility to sort
182 * through it and find the 'runs' of dirty pages
183 * to call VNOP_PAGEOUT on...
184 */
185 pl = ubc_upl_pageinfo(upl);
186
187 if (ubc_getsize(vp) == 0) {
188 /*
189 * if the file has been effectively deleted, then
190 * we need to go through the UPL and invalidate any
191 * buffer headers we might have that reference any
192 * of it's pages
193 */
194 for (offset = upl_offset; isize; isize -= PAGE_SIZE, offset += PAGE_SIZE) {
195 #if NFSCLIENT
196 if (vp->v_tag == VT_NFS)
197 /* check with nfs if page is OK to drop */
198 error = nfs_buf_page_inval(vp, (off_t)f_offset);
199 else
200 #endif
201 {
202 blkno = ubc_offtoblk(vp, (off_t)f_offset);
203 error = buf_invalblkno(vp, blkno, 0);
204 }
205 if (error) {
206 if ( !(flags & UPL_NOCOMMIT))
207 ubc_upl_abort_range(upl, offset, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
208 if (error_ret == 0)
209 error_ret = error;
210 result = PAGER_ERROR;
211
212 } else if ( !(flags & UPL_NOCOMMIT)) {
213 ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY);
214 }
215 f_offset += PAGE_SIZE;
216 }
217 goto out;
218 }
219 /*
220 * Ignore any non-present pages at the end of the
221 * UPL so that we aren't looking at a upl that
222 * may already have been freed by the preceeding
223 * aborts/completions.
224 */
225 base_index = upl_offset / PAGE_SIZE;
226
227 for (pg_index = (upl_offset + isize) / PAGE_SIZE; pg_index > base_index;) {
228 if (upl_page_present(pl, --pg_index))
229 break;
230 if (pg_index == base_index) {
231 /*
232 * no pages were returned, so release
233 * our hold on the upl and leave
234 */
235 if ( !(flags & UPL_NOCOMMIT))
236 ubc_upl_abort_range(upl, upl_offset, isize, UPL_ABORT_FREE_ON_EMPTY);
237
238 goto out;
239 }
240 }
241 isize = (pg_index + 1) * PAGE_SIZE;
242
243 offset = upl_offset;
244 pg_index = base_index;
245
246 while (isize) {
247 int xsize;
248 int num_of_pages;
249
250 if ( !upl_page_present(pl, pg_index)) {
251 /*
252 * we asked for RET_ONLY_DIRTY, so it's possible
253 * to get back empty slots in the UPL
254 * just skip over them
255 */
256 offset += PAGE_SIZE;
257 isize -= PAGE_SIZE;
258 pg_index++;
259
260 continue;
261 }
262 if ( !upl_dirty_page(pl, pg_index)) {
263 /*
264 * if the page is not dirty and reached here it is
265 * marked precious or it is due to invalidation in
266 * memory_object_lock request as part of truncation
267 * We also get here from vm_object_terminate()
268 * So all you need to do in these
269 * cases is to invalidate incore buffer if it is there
270 * Note we must not sleep here if the buffer is busy - that is
271 * a lock inversion which causes deadlock.
272 */
273 vp_pgoclean++;
274
275 #if NFSCLIENT
276 if (vp->v_tag == VT_NFS)
277 /* check with nfs if page is OK to drop */
278 error = nfs_buf_page_inval(vp, (off_t)(f_offset + offset));
279 else
280 #endif
281 {
282 blkno = ubc_offtoblk(vp, (off_t)(f_offset + offset));
283 error = buf_invalblkno(vp, blkno, 0);
284 }
285 if (error) {
286 if ( !(flags & UPL_NOCOMMIT))
287 ubc_upl_abort_range(upl, offset, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
288 if (error_ret == 0)
289 error_ret = error;
290 result = PAGER_ERROR;
291
292 } else if ( !(flags & UPL_NOCOMMIT)) {
293 ubc_upl_commit_range(upl, offset, PAGE_SIZE, UPL_COMMIT_FREE_ON_EMPTY);
294 }
295 offset += PAGE_SIZE;
296 isize -= PAGE_SIZE;
297 pg_index++;
298
299 continue;
300 }
301 vp_pgodirty++;
302
303 num_of_pages = 1;
304 xsize = isize - PAGE_SIZE;
305
306 while (xsize) {
307 if ( !upl_dirty_page(pl, pg_index + num_of_pages))
308 break;
309 num_of_pages++;
310 xsize -= PAGE_SIZE;
311 }
312 xsize = num_of_pages * PAGE_SIZE;
313
314 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
315 xsize, (int)(f_offset + offset), 0, 0, 0);
316
317 if ( (error = VNOP_PAGEOUT(vp, upl, (vm_offset_t)offset,
318 (off_t)(f_offset + offset), xsize,
319 flags, &context)) ) {
320 if (error_ret == 0)
321 error_ret = error;
322 result = PAGER_ERROR;
323 }
324 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
325 xsize, 0, 0, 0, 0);
326
327 offset += xsize;
328 isize -= xsize;
329 pg_index += num_of_pages;
330 }
331 out:
332 if (errorp)
333 *errorp = error_ret;
334
335 return (result);
336 }
337
338
339 void IOSleep(int);
340
341 pager_return_t
342 vnode_pagein(
343 struct vnode *vp,
344 upl_t upl,
345 vm_offset_t upl_offset,
346 vm_object_offset_t f_offset,
347 vm_size_t size,
348 int flags,
349 int *errorp)
350 {
351 struct proc *p = current_proc();
352 struct uthread *ut;
353 upl_page_info_t *pl;
354 int result = PAGER_SUCCESS;
355 int error = 0;
356 int pages_in_upl;
357 int start_pg;
358 int last_pg;
359 int first_pg;
360 int xsize;
361 int abort_needed = 1;
362
363
364 UBCINFOCHECK("vnode_pagein", vp);
365
366 if (UBCINVALID(vp)) {
367 result = PAGER_ERROR;
368 error = PAGER_ERROR;
369 if (upl && !(flags & UPL_NOCOMMIT)) {
370 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
371 }
372 goto out;
373 }
374 if (upl == (upl_t)NULL) {
375 if (size > (MAX_UPL_TRANSFER * PAGE_SIZE)) {
376 result = PAGER_ERROR;
377 error = PAGER_ERROR;
378 goto out;
379 }
380 ubc_create_upl(vp, f_offset, size, &upl, &pl, UPL_RET_ONLY_ABSENT | UPL_SET_LITE);
381
382 if (upl == (upl_t)NULL) {
383 result = PAGER_ABSENT;
384 error = PAGER_ABSENT;
385 goto out;
386 }
387 upl_offset = 0;
388 /*
389 * if we get here, we've created the upl and
390 * are responsible for commiting/aborting it
391 * regardless of what the caller has passed in
392 */
393 flags &= ~UPL_NOCOMMIT;
394
395 vp_pagein++;
396 } else {
397 pl = ubc_upl_pageinfo(upl);
398
399 dp_pgins++;
400 }
401 pages_in_upl = size / PAGE_SIZE;
402 first_pg = upl_offset / PAGE_SIZE;
403
404 /*
405 * before we start marching forward, we must make sure we end on
406 * a present page, otherwise we will be working with a freed
407 * upl
408 */
409 for (last_pg = pages_in_upl - 1; last_pg >= first_pg; last_pg--) {
410 if (upl_page_present(pl, last_pg))
411 break;
412 }
413 pages_in_upl = last_pg + 1;
414
415 for (last_pg = first_pg; last_pg < pages_in_upl;) {
416 /*
417 * scan the upl looking for the next
418 * page that is present.... if all of the
419 * pages are absent, we're done
420 */
421 for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
422 if (upl_page_present(pl, last_pg))
423 break;
424 }
425 if (last_pg == pages_in_upl)
426 break;
427
428 /*
429 * if we get here, we've sitting on a page
430 * that is present... we want to skip over
431 * any range of 'valid' pages... if this takes
432 * us to the end of the request, than we're done
433 */
434 for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
435 if (!upl_valid_page(pl, last_pg) || !upl_page_present(pl, last_pg))
436 break;
437 }
438 if (last_pg > start_pg) {
439 /*
440 * we've found a range of valid pages
441 * if we've got COMMIT responsibility
442 * commit this range of pages back to the
443 * cache unchanged
444 */
445 xsize = (last_pg - start_pg) * PAGE_SIZE;
446
447 if (!(flags & UPL_NOCOMMIT))
448 ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, xsize, UPL_ABORT_FREE_ON_EMPTY);
449
450 abort_needed = 0;
451 }
452 if (last_pg == pages_in_upl)
453 break;
454
455 if (!upl_page_present(pl, last_pg))
456 /*
457 * if we found a range of valid pages
458 * terminated by a non-present page
459 * than start over
460 */
461 continue;
462
463 /*
464 * scan from the found invalid page looking for a valid
465 * or non-present page before the end of the upl is reached, if we
466 * find one, then it will be the last page of the request to
467 * 'cluster_io'
468 */
469 for (start_pg = last_pg; last_pg < pages_in_upl; last_pg++) {
470 if (upl_valid_page(pl, last_pg) || !upl_page_present(pl, last_pg))
471 break;
472 }
473 if (last_pg > start_pg) {
474 int xoff;
475 struct vfs_context context;
476
477 context.vc_proc = p;
478 context.vc_ucred = kauth_cred_get();
479 xsize = (last_pg - start_pg) * PAGE_SIZE;
480 xoff = start_pg * PAGE_SIZE;
481
482 if ( (error = VNOP_PAGEIN(vp, upl, (vm_offset_t) xoff,
483 (off_t)f_offset + xoff,
484 xsize, flags, &context)) ) {
485 result = PAGER_ERROR;
486 error = PAGER_ERROR;
487
488 }
489 abort_needed = 0;
490 }
491 }
492 if (!(flags & UPL_NOCOMMIT) && abort_needed)
493 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
494 out:
495 if (errorp)
496 *errorp = result;
497
498 ut = get_bsdthread_info(current_thread());
499
500 if (ut->uu_lowpri_delay) {
501 /*
502 * task is marked as a low priority I/O type
503 * and the I/O we issued while in this system call
504 * collided with normal I/O operations... we'll
505 * delay in order to mitigate the impact of this
506 * task on the normal operation of the system
507 */
508 IOSleep(ut->uu_lowpri_delay);
509 ut->uu_lowpri_delay = 0;
510 }
511 return (error);
512 }
513
514 void
515 vnode_pager_shutdown(void)
516 {
517 int i;
518 vnode_t vp;
519
520 for(i = 0; i < MAX_BACKING_STORE; i++) {
521 vp = (vnode_t)(bs_port_table[i]).vp;
522 if (vp) {
523 (bs_port_table[i]).vp = 0;
524
525 /* get rid of macx_swapon() reference */
526 vnode_rele(vp);
527 }
528 }
529 }
530
531
532 void *
533 upl_get_internal_page_list(upl_t upl)
534 {
535 return(UPL_GET_INTERNAL_PAGE_LIST(upl));
536
537 }