2 * Copyright (c) 2000-2013 Apple 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 #include <kern/kern_types.h>
30 #include <kern/locks.h>
31 #include <vm/vm_kern.h>
32 #include <mach/kern_return.h>
33 #include <kern/queue.h>
34 #include <vm/vm_pageout.h>
35 #include <vm/vm_protos.h>
36 #include <vm/vm_compressor.h>
37 #include <libkern/crypto/aes.h>
38 #include <kern/host_statistics.h>
40 #if !XNU_TARGET_OS_OSX
42 #define MIN_SWAP_FILE_SIZE (64 * 1024 * 1024ULL)
44 #define MAX_SWAP_FILE_SIZE (128 * 1024 * 1024ULL)
46 #else /* !XNU_TARGET_OS_OSX */
48 #define MIN_SWAP_FILE_SIZE (256 * 1024 * 1024ULL)
50 #define MAX_SWAP_FILE_SIZE (1 * 1024 * 1024 * 1024ULL)
52 #endif /* !XNU_TARGET_OS_OSX */
54 #define COMPRESSED_SWAP_CHUNK_SIZE (C_SEG_BUFSIZE)
56 #define VM_SWAPFILE_HIWATER_SEGS (MIN_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE)
58 #define SWAPFILE_RECLAIM_THRESHOLD_SEGS ((17 * (MAX_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE)) / 10)
59 #define SWAPFILE_RECLAIM_MINIMUM_SEGS ((13 * (MAX_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE)) / 10)
61 #if defined(XNU_TARGET_OS_OSX)
62 #define SWAP_FILE_NAME "/System/Volumes/VM/swapfile"
64 #define SWAP_FILE_NAME "/private/var/vm/swapfile"
67 #define SWAPFILENAME_LEN (int)(strlen(SWAP_FILE_NAME))
70 #define SWAP_SLOT_MASK 0x1FFFFFFFF
71 #define SWAP_DEVICE_SHIFT 33
73 extern int vm_num_swap_files
;
77 void vm_swap_init(void);
78 boolean_t
vm_swap_create_file(void);
81 struct swapout_io_completion
{
87 c_segment_t swp_c_seg
;
89 struct swapfile
*swp_swf
;
90 uint64_t swp_f_offset
;
92 struct upl_io_completion swp_upl_ctx
;
94 void vm_swapout_iodone(void *, int);
97 static void vm_swapout_finish(c_segment_t
, uint64_t, uint32_t, kern_return_t
);
98 kern_return_t
vm_swap_put_finish(struct swapfile
*, uint64_t *, int, boolean_t
);
99 kern_return_t
vm_swap_put(vm_offset_t
, uint64_t*, uint32_t, c_segment_t
, struct swapout_io_completion
*);
101 void vm_swap_flush(void);
102 void vm_swap_reclaim(void);
103 void vm_swap_encrypt(c_segment_t
);
104 uint64_t vm_swap_get_total_space(void);
105 uint64_t vm_swap_get_used_space(void);
106 uint64_t vm_swap_get_free_space(void);
107 uint64_t vm_swap_get_max_configured_space(void);
110 extern void vm_swapfile_open(const char *path
, struct vnode
**vp
);
111 extern void vm_swapfile_close(uint64_t path
, struct vnode
*vp
);
112 extern int vm_swapfile_preallocate(struct vnode
*vp
, uint64_t *size
, boolean_t
*pin
);
113 extern uint64_t vm_swapfile_get_blksize(struct vnode
*vp
);
114 extern uint64_t vm_swapfile_get_transfer_size(struct vnode
*vp
);
115 extern int vm_swapfile_io(struct vnode
*vp
, uint64_t offset
, uint64_t start
, int npages
, int flags
, void *upl_ctx
);
118 boolean_t
vm_swap_max_budget(uint64_t *);
119 int vm_swap_vol_get_budget(struct vnode
* vp
, uint64_t *freeze_daily_budget
);
120 #endif /* CONFIG_FREEZE */
122 #if RECORD_THE_COMPRESSED_DATA
123 extern int vm_record_file_write(struct vnode
*vp
, uint64_t offset
, char *buf
, int size
);