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