2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
37 * "Swap" pager that pages to/from vnodes. Also
38 * handles demand paging from files.
42 #include <mach/boolean.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/kauth.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>
55 #include <sys/disk.h> /* For DKIOC calls */
57 #include <mach/mach_types.h>
58 #include <mach/memory_object_types.h>
59 #include <mach/memory_object_control.h>
60 #include <mach/vm_map.h>
61 #include <mach/mach_vm.h>
65 #include <vm/vm_map.h>
66 #include <vm/vm_kern.h>
67 #include <kern/zalloc.h>
68 #include <kern/kalloc.h>
69 #include <libkern/libkern.h>
71 #include <vm/vnode_pager.h>
72 #include <vm/vm_pageout.h>
74 #include <kern/assert.h>
75 #include <sys/kdebug.h>
76 #include <nfs/rpcv2.h>
77 #include <nfs/nfsproto.h>
80 #include <vm/vm_protos.h>
82 #include <vfs/vfs_disk_conditioner.h>
85 vnode_pager_throttle()
89 ut
= get_bsdthread_info(current_thread());
91 if (ut
->uu_lowpri_window
)
92 throttle_lowpri_io(1);
96 vnode_pager_isSSD(vnode_t vp
)
98 return disk_conditioner_mount_is_ssd(vp
->v_mount
);
103 vnode_pager_issue_reprioritize_io(struct vnode
*devvp
, uint64_t blkno
, uint32_t len
, int priority
)
105 u_int32_t blocksize
= 0;
107 dk_set_tier_t set_tier
;
110 error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blocksize
, 0, vfs_context_kernel());
114 memset(&extent
, 0, sizeof(dk_extent_t
));
115 memset(&set_tier
, 0, sizeof(dk_set_tier_t
));
117 extent
.offset
= blkno
* (u_int64_t
) blocksize
;
120 set_tier
.extents
= &extent
;
121 set_tier
.extentsCount
= 1;
122 set_tier
.tier
= priority
;
124 error
= VNOP_IOCTL(devvp
, DKIOCSETTIER
, (caddr_t
)&set_tier
, 0, vfs_context_kernel());
130 vnode_pager_isinuse(struct vnode
*vp
)
132 if (vp
->v_usecount
> vp
->v_kusecount
)
138 vnode_pager_return_throttle_io_limit(struct vnode
*vp
, uint32_t *limit
)
140 return(cluster_throttle_io_limit(vp
, limit
));
144 vnode_pager_get_filesize(struct vnode
*vp
)
146 return (vm_object_offset_t
) ubc_getsize(vp
);
149 extern int safe_getpath(struct vnode
*dvp
, char *leafname
, char *path
, int _len
, int *truncated_path
);
152 vnode_pager_get_name(
155 vm_size_t pathname_len
,
157 vm_size_t filename_len
,
158 boolean_t
*truncated_path_p
)
160 *truncated_path_p
= FALSE
;
161 if (pathname
!= NULL
) {
162 /* get the path name */
163 safe_getpath(vp
, NULL
,
164 pathname
, (int) pathname_len
,
167 if ((pathname
== NULL
|| *truncated_path_p
) &&
169 /* get the file name */
172 name
= vnode_getname_printable(vp
);
173 strlcpy(filename
, name
, (size_t) filename_len
);
174 vnode_putname_printable(name
);
180 vnode_pager_get_mtime(
182 struct timespec
*current_mtime
,
183 struct timespec
*cs_mtime
)
185 vnode_mtime(vp
, current_mtime
, vfs_context_current());
186 if (cs_mtime
!= NULL
) {
187 ubc_get_cs_mtime(vp
, cs_mtime
);
193 vnode_pager_get_cs_blobs(
197 *blobs
= ubc_get_cs_blobs(vp
);
203 * Used to call the DKIOCUNMAP ioctl on the underlying disk device for the specified vnode.
204 * Trims the region at offset bytes into the file, for length bytes.
206 * Care must be taken to ensure that the vnode is sufficiently reference counted at the time this
207 * function is called; no iocounts or usecounts are taken on the vnode.
208 * This function is non-idempotent in error cases; We cannot un-discard the blocks if only some of them
209 * are successfully discarded.
211 u_int32_t
vnode_trim (
216 daddr64_t io_blockno
; /* Block number corresponding to the start of the extent */
217 size_t io_bytecount
; /* Number of bytes in current extent for the specified range */
219 off_t current_offset
= offset
;
220 size_t remaining_length
= length
;
222 u_int32_t blocksize
= 0;
228 /* Get the underlying device vnode */
229 devvp
= vp
->v_mount
->mnt_devvp
;
231 /* Figure out the underlying device block size */
232 error
= VNOP_IOCTL(devvp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blocksize
, 0, vfs_context_kernel());
238 * We may not get the entire range from offset -> offset+length in a single
239 * extent from the blockmap call. Keep looping/going until we are sure we've hit
240 * the whole range or if we encounter an error.
242 while (trimmed
< length
) {
244 * VNOP_BLOCKMAP will tell us the logical to physical block number mapping for the
245 * specified offset. It returns blocks in contiguous chunks, so if the logical range is
246 * broken into multiple extents, it must be called multiple times, increasing the offset
247 * in each call to ensure that the entire range is covered.
249 error
= VNOP_BLOCKMAP (vp
, current_offset
, remaining_length
,
250 &io_blockno
, &io_bytecount
, NULL
, VNODE_READ
| VNODE_BLOCKMAP_NO_TRACK
, NULL
);
256 * We have a contiguous run. Prepare & issue the ioctl for the device.
257 * the DKIOCUNMAP ioctl takes offset in bytes from the start of the device.
259 memset (&extent
, 0, sizeof(dk_extent_t
));
260 memset (&unmap
, 0, sizeof(dk_unmap_t
));
261 extent
.offset
= (uint64_t) io_blockno
* (u_int64_t
) blocksize
;
262 extent
.length
= io_bytecount
;
263 unmap
.extents
= &extent
;
264 unmap
.extentsCount
= 1;
265 error
= VNOP_IOCTL(devvp
, DKIOCUNMAP
, (caddr_t
)&unmap
, 0, vfs_context_kernel());
270 remaining_length
= remaining_length
- io_bytecount
;
271 trimmed
= trimmed
+ io_bytecount
;
272 current_offset
= current_offset
+ io_bytecount
;
281 vnode_pageout(struct vnode
*vp
,
283 upl_offset_t upl_offset
,
284 vm_object_offset_t f_offset
,
289 int result
= PAGER_SUCCESS
;
298 vfs_context_t ctx
= vfs_context_current(); /* pager context */
303 result
= PAGER_ERROR
;
308 if (UBCINFOEXISTS(vp
) == 0) {
309 result
= PAGER_ERROR
;
312 if (upl
&& !(flags
& UPL_NOCOMMIT
))
313 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
316 if ( !(flags
& UPL_VNODE_PAGER
)) {
318 * This is a pageout from the default pager,
319 * just go ahead and call vnop_pageout since
320 * it has already sorted out the dirty ranges
322 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
323 (MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
326 if ( (error_ret
= VNOP_PAGEOUT(vp
, upl
, upl_offset
, (off_t
)f_offset
,
327 (size_t)size
, flags
, ctx
)) )
328 result
= PAGER_ERROR
;
330 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
331 (MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
339 if (vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSVNOP_PAGEOUTV2
) {
341 * filesystem has requested the new form of VNOP_PAGEOUT for file
342 * backed objects... we will not grab the UPL befofe calling VNOP_PAGEOUT...
343 * it is the fileystem's responsibility to grab the range we're denoting
344 * via 'f_offset' and 'size' into a UPL... this allows the filesystem to first
345 * take any locks it needs, before effectively locking the pages into a UPL...
347 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
348 (MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
349 size
, (int)f_offset
, 0, 0, 0);
351 if ( (error_ret
= VNOP_PAGEOUT(vp
, NULL
, upl_offset
, (off_t
)f_offset
,
352 size
, flags
, ctx
)) ) {
353 result
= PAGER_ERROR
;
355 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
356 (MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
361 if (flags
& UPL_MSYNC
)
362 request_flags
= UPL_UBC_MSYNC
| UPL_RET_ONLY_DIRTY
;
364 request_flags
= UPL_UBC_PAGEOUT
| UPL_RET_ONLY_DIRTY
;
366 if (ubc_create_upl_kernel(vp
, f_offset
, size
, &upl
, &pl
, request_flags
, VM_KERN_MEMORY_FILE
) != KERN_SUCCESS
) {
367 result
= PAGER_ERROR
;
373 pl
= ubc_upl_pageinfo(upl
);
376 * Ignore any non-present pages at the end of the
377 * UPL so that we aren't looking at a upl that
378 * may already have been freed by the preceeding
379 * aborts/completions.
381 base_index
= upl_offset
/ PAGE_SIZE
;
383 for (pg_index
= (upl_offset
+ isize
) / PAGE_SIZE
; pg_index
> base_index
;) {
384 if (upl_page_present(pl
, --pg_index
))
386 if (pg_index
== base_index
) {
388 * no pages were returned, so release
389 * our hold on the upl and leave
391 if ( !(flags
& UPL_NOCOMMIT
))
392 ubc_upl_abort_range(upl
, upl_offset
, isize
, UPL_ABORT_FREE_ON_EMPTY
);
397 isize
= ((pg_index
+ 1) - base_index
) * PAGE_SIZE
;
400 * we come here for pageouts to 'real' files and
401 * for msyncs... the upl may not contain any
402 * dirty pages.. it's our responsibility to sort
403 * through it and find the 'runs' of dirty pages
404 * to call VNOP_PAGEOUT on...
407 if (ubc_getsize(vp
) == 0) {
409 * if the file has been effectively deleted, then
410 * we need to go through the UPL and invalidate any
411 * buffer headers we might have that reference any
414 for (offset
= upl_offset
; isize
; isize
-= PAGE_SIZE
, offset
+= PAGE_SIZE
) {
416 if (vp
->v_tag
== VT_NFS
)
417 /* check with nfs if page is OK to drop */
418 error
= nfs_buf_page_inval(vp
, (off_t
)f_offset
);
422 blkno
= ubc_offtoblk(vp
, (off_t
)f_offset
);
423 error
= buf_invalblkno(vp
, blkno
, 0);
426 if ( !(flags
& UPL_NOCOMMIT
))
427 ubc_upl_abort_range(upl
, offset
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
430 result
= PAGER_ERROR
;
432 } else if ( !(flags
& UPL_NOCOMMIT
)) {
433 ubc_upl_commit_range(upl
, offset
, PAGE_SIZE
, UPL_COMMIT_FREE_ON_EMPTY
);
435 f_offset
+= PAGE_SIZE
;
441 pg_index
= base_index
;
447 if ( !upl_page_present(pl
, pg_index
)) {
449 * we asked for RET_ONLY_DIRTY, so it's possible
450 * to get back empty slots in the UPL
451 * just skip over them
453 f_offset
+= PAGE_SIZE
;
460 if ( !upl_dirty_page(pl
, pg_index
)) {
462 * if the page is not dirty and reached here it is
463 * marked precious or it is due to invalidation in
464 * memory_object_lock request as part of truncation
465 * We also get here from vm_object_terminate()
466 * So all you need to do in these
467 * cases is to invalidate incore buffer if it is there
468 * Note we must not sleep here if the buffer is busy - that is
469 * a lock inversion which causes deadlock.
472 if (vp
->v_tag
== VT_NFS
)
473 /* check with nfs if page is OK to drop */
474 error
= nfs_buf_page_inval(vp
, (off_t
)f_offset
);
478 blkno
= ubc_offtoblk(vp
, (off_t
)f_offset
);
479 error
= buf_invalblkno(vp
, blkno
, 0);
482 if ( !(flags
& UPL_NOCOMMIT
))
483 ubc_upl_abort_range(upl
, offset
, PAGE_SIZE
, UPL_ABORT_FREE_ON_EMPTY
);
486 result
= PAGER_ERROR
;
488 } else if ( !(flags
& UPL_NOCOMMIT
)) {
489 ubc_upl_commit_range(upl
, offset
, PAGE_SIZE
, UPL_COMMIT_FREE_ON_EMPTY
);
491 f_offset
+= PAGE_SIZE
;
499 xsize
= isize
- PAGE_SIZE
;
502 if ( !upl_dirty_page(pl
, pg_index
+ num_of_pages
))
507 xsize
= num_of_pages
* PAGE_SIZE
;
509 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
510 (MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_START
,
511 xsize
, (int)f_offset
, 0, 0, 0);
513 if ( (error
= VNOP_PAGEOUT(vp
, upl
, offset
, (off_t
)f_offset
,
514 xsize
, flags
, ctx
)) ) {
517 result
= PAGER_ERROR
;
519 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE
,
520 (MACHDBG_CODE(DBG_MACH_VM
, 1)) | DBG_FUNC_END
,
526 pg_index
+= num_of_pages
;
540 upl_offset_t upl_offset
,
541 vm_object_offset_t f_offset
,
547 int result
= PAGER_SUCCESS
;
555 int ignore_valid_page_check
= 0;
557 if (flags
& UPL_NOCOMMIT
)
560 if (flags
& UPL_IGNORE_VALID_PAGE_CHECK
)
561 ignore_valid_page_check
= 1;
563 if (UBCINFOEXISTS(vp
) == 0) {
564 result
= PAGER_ERROR
;
567 if (upl
&& must_commit
)
568 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
572 if (upl
== (upl_t
)NULL
) {
573 flags
&= ~UPL_NOCOMMIT
;
575 if (size
> MAX_UPL_SIZE_BYTES
) {
576 result
= PAGER_ERROR
;
580 if (vp
->v_mount
->mnt_vtable
->vfc_vfsflags
& VFC_VFSVNOP_PAGEINV2
) {
582 * filesystem has requested the new form of VNOP_PAGEIN for file
583 * backed objects... we will not grab the UPL befofe calling VNOP_PAGEIN...
584 * it is the fileystem's responsibility to grab the range we're denoting
585 * via 'f_offset' and 'size' into a UPL... this allows the filesystem to first
586 * take any locks it needs, before effectively locking the pages into a UPL...
587 * so we pass a NULL into the filesystem instead of a UPL pointer... the 'upl_offset'
588 * is used to identify the "must have" page in the extent... the filesystem is free
589 * to clip the extent to better fit the underlying FS blocksize if it desires as
590 * long as it continues to include the "must have" page... 'f_offset' + 'upl_offset'
591 * identifies that page
593 if ( (error
= VNOP_PAGEIN(vp
, NULL
, upl_offset
, (off_t
)f_offset
,
594 size
, flags
, vfs_context_current())) ) {
595 result
= PAGER_ERROR
;
600 ubc_create_upl_kernel(vp
, f_offset
, size
, &upl
, &pl
, UPL_UBC_PAGEIN
| UPL_RET_ONLY_ABSENT
, VM_KERN_MEMORY_FILE
);
602 if (upl
== (upl_t
)NULL
) {
603 result
= PAGER_ABSENT
;
604 error
= PAGER_ABSENT
;
607 ubc_upl_range_needed(upl
, upl_offset
/ PAGE_SIZE
, 1);
613 * if we get here, we've created the upl and
614 * are responsible for commiting/aborting it
615 * regardless of what the caller has passed in
619 pl
= ubc_upl_pageinfo(upl
);
620 first_pg
= upl_offset
/ PAGE_SIZE
;
622 pages_in_upl
= size
/ PAGE_SIZE
;
623 DTRACE_VM2(pgpgin
, int, pages_in_upl
, (uint64_t *), NULL
);
626 * before we start marching forward, we must make sure we end on
627 * a present page, otherwise we will be working with a freed
630 for (last_pg
= pages_in_upl
- 1; last_pg
>= first_pg
; last_pg
--) {
631 if (upl_page_present(pl
, last_pg
))
633 if (last_pg
== first_pg
) {
635 * empty UPL, no pages are present
638 ubc_upl_abort_range(upl
, upl_offset
, size
, UPL_ABORT_FREE_ON_EMPTY
);
642 pages_in_upl
= last_pg
+ 1;
645 while (last_pg
< pages_in_upl
) {
647 * skip over missing pages...
649 for ( ; last_pg
< pages_in_upl
; last_pg
++) {
650 if (upl_page_present(pl
, last_pg
))
654 if (ignore_valid_page_check
== 1) {
658 * skip over 'valid' pages... we don't want to issue I/O for these
660 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
661 if (!upl_valid_page(pl
, last_pg
))
666 if (last_pg
> start_pg
) {
668 * we've found a range of valid pages
669 * if we've got COMMIT responsibility
670 * commit this range of pages back to the
673 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
676 ubc_upl_abort_range(upl
, start_pg
* PAGE_SIZE
, xsize
, UPL_ABORT_FREE_ON_EMPTY
);
678 if (last_pg
== pages_in_upl
)
680 * we're done... all pages that were present
681 * have either had I/O issued on them or
682 * were aborted unchanged...
686 if (!upl_page_present(pl
, last_pg
)) {
688 * we found a range of valid pages
689 * terminated by a missing page...
690 * bump index to the next page and continue on
696 * scan from the found invalid page looking for a valid
697 * or non-present page before the end of the upl is reached, if we
698 * find one, then it will be the last page of the request to
701 for (start_pg
= last_pg
; last_pg
< pages_in_upl
; last_pg
++) {
702 if (( !ignore_valid_page_check
&& upl_valid_page(pl
, last_pg
)) || !upl_page_present(pl
, last_pg
))
705 if (last_pg
> start_pg
) {
707 xsize
= (last_pg
- start_pg
) * PAGE_SIZE
;
708 xoff
= start_pg
* PAGE_SIZE
;
710 if ( (error
= VNOP_PAGEIN(vp
, upl
, (upl_offset_t
) xoff
,
711 (off_t
)f_offset
+ xoff
,
712 xsize
, flags
, vfs_context_current())) ) {
714 * Usually this UPL will be aborted/committed by the lower cluster layer.
716 * a) In the case of decmpfs, however, we may return an error (EAGAIN) to avoid
717 * a deadlock with another thread already inflating the file.
719 * b) In the case of content protection, EPERM is a valid error and we should respect it.
721 * In those cases, we must take care of our UPL at this layer itself.
724 if(error
== EAGAIN
) {
725 ubc_upl_abort_range(upl
, (upl_offset_t
) xoff
, xsize
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_RESTART
);
728 ubc_upl_abort_range(upl
, (upl_offset_t
) xoff
, xsize
, UPL_ABORT_FREE_ON_EMPTY
| UPL_ABORT_ERROR
);
731 result
= PAGER_ERROR
;
745 upl_get_internal_page_list(upl_t upl
)
747 return(UPL_GET_INTERNAL_PAGE_LIST(upl
));