]> git.saurik.com Git - apple/xnu.git/blame - bsd/vm/vnode_pager.c
xnu-792.18.15.tar.gz
[apple/xnu.git] / bsd / vm / vnode_pager.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
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>
58
59#include <vm/vm_map.h>
60#include <vm/vm_kern.h>
1c79356b
A
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>
9bccf70c 69#include <sys/kdebug.h>
91447636
A
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>
1c79356b
A
77
78unsigned int vp_pagein=0;
79unsigned int vp_pgodirty=0;
80unsigned int vp_pgoclean=0;
81unsigned int dp_pgouts=0; /* Default pager pageouts */
82unsigned int dp_pgins=0; /* Default pager pageins */
83
0b4e3aa0
A
84vm_object_offset_t
85vnode_pager_get_filesize(struct vnode *vp)
86{
0b4e3aa0
A
87
88 return (vm_object_offset_t) ubc_getsize(vp);
0b4e3aa0
A
89}
90
89b3af67
A
91kern_return_t
92vnode_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
108kern_return_t
109vnode_pager_get_filename(
110 struct vnode *vp,
111 char **filename)
112{
113 *filename = vp->v_name;
114 return KERN_SUCCESS;
115}
116
1c79356b
A
117pager_return_t
118vnode_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{
1c79356b 126 struct proc *p = current_proc();
91447636 127 int result = PAGER_SUCCESS;
1c79356b 128 int error = 0;
91447636
A
129 int error_ret = 0;
130 daddr64_t blkno;
131 int isize;
1c79356b 132 int pg_index;
91447636 133 int base_index;
1c79356b 134 int offset;
1c79356b 135 upl_page_info_t *pl;
91447636 136 struct vfs_context context;
1c79356b 137
91447636
A
138 context.vc_proc = p;
139 context.vc_ucred = kauth_cred_get();
1c79356b 140
1c79356b
A
141 isize = (int)size;
142
9bccf70c 143 if (isize <= 0) {
91447636
A
144 result = PAGER_ERROR;
145 error_ret = EINVAL;
9bccf70c
A
146 goto out;
147 }
1c79356b
A
148 UBCINFOCHECK("vnode_pageout", vp);
149
150 if (UBCINVALID(vp)) {
91447636
A
151 result = PAGER_ERROR;
152 error_ret = EINVAL;
9bccf70c 153
fa4905b1 154 if (upl && !(flags & UPL_NOCOMMIT))
9bccf70c 155 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
156 goto out;
157 }
91447636 158 if ( !(flags & UPL_VNODE_PAGER)) {
1c79356b 159 /*
91447636
A
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
1c79356b
A
163 */
164 dp_pgouts++;
9bccf70c
A
165
166 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
167 size, 1, 0, 0, 0);
168
91447636
A
169 if ( (error_ret = VNOP_PAGEOUT(vp, upl, upl_offset, (off_t)f_offset,
170 (size_t)size, flags, &context)) )
171 result = PAGER_ERROR;
9bccf70c
A
172
173 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
174 size, 1, 0, 0, 0);
175
1c79356b
A
176 goto out;
177 }
9bccf70c 178 /*
91447636
A
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...
9bccf70c 184 */
91447636 185 pl = ubc_upl_pageinfo(upl);
1c79356b 186
fa4905b1 187 if (ubc_getsize(vp) == 0) {
91447636
A
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);
fa4905b1 214 }
91447636 215 f_offset += PAGE_SIZE;
1c79356b 216 }
1c79356b
A
217 goto out;
218 }
91447636
A
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;
1c79356b
A
245
246 while (isize) {
247 int xsize;
248 int num_of_pages;
249
91447636
A
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 */
1c79356b
A
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
91447636 270 * Note we must not sleep here if the buffer is busy - that is
fa4905b1 271 * a lock inversion which causes deadlock.
1c79356b 272 */
1c79356b 273 vp_pgoclean++;
91447636
A
274
275#if NFSCLIENT
276 if (vp->v_tag == VT_NFS)
55e303ae
A
277 /* check with nfs if page is OK to drop */
278 error = nfs_buf_page_inval(vp, (off_t)(f_offset + offset));
91447636
A
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 }
1c79356b
A
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) {
1c79356b
A
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
9bccf70c 314 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_START,
91447636 315 xsize, (int)(f_offset + offset), 0, 0, 0);
9bccf70c 316
91447636 317 if ( (error = VNOP_PAGEOUT(vp, upl, (vm_offset_t)offset,
fa4905b1 318 (off_t)(f_offset + offset), xsize,
91447636
A
319 flags, &context)) ) {
320 if (error_ret == 0)
321 error_ret = error;
322 result = PAGER_ERROR;
323 }
9bccf70c
A
324 KERNEL_DEBUG_CONSTANT((MACHDBG_CODE(DBG_MACH_VM, 1)) | DBG_FUNC_END,
325 xsize, 0, 0, 0, 0);
326
1c79356b
A
327 offset += xsize;
328 isize -= xsize;
329 pg_index += num_of_pages;
330 }
331out:
332 if (errorp)
91447636 333 *errorp = error_ret;
1c79356b 334
91447636 335 return (result);
1c79356b
A
336}
337
338
91447636
A
339void IOSleep(int);
340
1c79356b
A
341pager_return_t
342vnode_pagein(
343 struct vnode *vp,
9bccf70c
A
344 upl_t upl,
345 vm_offset_t upl_offset,
1c79356b
A
346 vm_object_offset_t f_offset,
347 vm_size_t size,
348 int flags,
349 int *errorp)
350{
9bccf70c 351 struct proc *p = current_proc();
91447636 352 struct uthread *ut;
9bccf70c
A
353 upl_page_info_t *pl;
354 int result = PAGER_SUCCESS;
1c79356b 355 int error = 0;
9bccf70c
A
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;
1c79356b 362
1c79356b 363
1c79356b
A
364 UBCINFOCHECK("vnode_pagein", vp);
365
366 if (UBCINVALID(vp)) {
367 result = PAGER_ERROR;
368 error = PAGER_ERROR;
9bccf70c
A
369 if (upl && !(flags & UPL_NOCOMMIT)) {
370 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY | UPL_ABORT_ERROR);
fa4905b1 371 }
1c79356b
A
372 goto out;
373 }
9bccf70c
A
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 }
55e303ae 380 ubc_create_upl(vp, f_offset, size, &upl, &pl, UPL_RET_ONLY_ABSENT | UPL_SET_LITE);
1c79356b 381
9bccf70c
A
382 if (upl == (upl_t)NULL) {
383 result = PAGER_ABSENT;
384 error = PAGER_ABSENT;
385 goto out;
1c79356b 386 }
9bccf70c
A
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++;
1c79356b 396 } else {
9bccf70c 397 pl = ubc_upl_pageinfo(upl);
1c79356b 398
9bccf70c
A
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;
1c79356b 446
9bccf70c
A
447 if (!(flags & UPL_NOCOMMIT))
448 ubc_upl_abort_range(upl, start_pg * PAGE_SIZE, xsize, UPL_ABORT_FREE_ON_EMPTY);
1c79356b 449
9bccf70c
A
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;
91447636 475 struct vfs_context context;
9bccf70c 476
91447636
A
477 context.vc_proc = p;
478 context.vc_ucred = kauth_cred_get();
9bccf70c
A
479 xsize = (last_pg - start_pg) * PAGE_SIZE;
480 xoff = start_pg * PAGE_SIZE;
481
91447636 482 if ( (error = VNOP_PAGEIN(vp, upl, (vm_offset_t) xoff,
9bccf70c 483 (off_t)f_offset + xoff,
91447636 484 xsize, flags, &context)) ) {
0b4e3aa0
A
485 result = PAGER_ERROR;
486 error = PAGER_ERROR;
9bccf70c 487
0b4e3aa0 488 }
9bccf70c 489 abort_needed = 0;
1c79356b 490 }
9bccf70c
A
491 }
492 if (!(flags & UPL_NOCOMMIT) && abort_needed)
493 ubc_upl_abort_range(upl, upl_offset, size, UPL_ABORT_FREE_ON_EMPTY);
1c79356b
A
494out:
495 if (errorp)
fa4905b1 496 *errorp = result;
1c79356b 497
91447636
A
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 }
1c79356b
A
511 return (error);
512}
513
514void
91447636 515vnode_pager_shutdown(void)
1c79356b
A
516{
517 int i;
91447636 518 vnode_t vp;
1c79356b
A
519
520 for(i = 0; i < MAX_BACKING_STORE; i++) {
91447636 521 vp = (vnode_t)(bs_port_table[i]).vp;
1c79356b
A
522 if (vp) {
523 (bs_port_table[i]).vp = 0;
1c79356b 524
91447636
A
525 /* get rid of macx_swapon() reference */
526 vnode_rele(vp);
1c79356b
A
527 }
528 }
529}
530
0b4e3aa0
A
531
532void *
1c79356b
A
533upl_get_internal_page_list(upl_t upl)
534{
0b4e3aa0 535 return(UPL_GET_INTERNAL_PAGE_LIST(upl));
1c79356b
A
536
537}