1/*
2 * Copyright (c) 2000-2013 Apple 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#include <kern/kern_types.h>
30#include <kern/locks.h>
31#include <kern/kalloc.h>
32#include <vm/vm_kern.h>
33#include <mach/kern_return.h>
34#include <kern/queue.h>
35#include <vm/vm_pageout.h>
36#include <vm/vm_protos.h>
37#include <vm/vm_compressor.h>
38#include <libkern/crypto/aes.h>
39#include <kern/host_statistics.h>
40
41#if CONFIG_EMBEDDED
42
43#define MIN_SWAP_FILE_SIZE (64 * 1024 * 1024)
44
45#define MAX_SWAP_FILE_SIZE (128 * 1024 * 1024)
46
47#else /* CONFIG_EMBEDDED */
48
49#define MIN_SWAP_FILE_SIZE (256 * 1024 * 1024)
50
51#define MAX_SWAP_FILE_SIZE (1 * 1024 * 1024 * 1024)
52
53#endif /* CONFIG_EMBEDDED */
54
55#define COMPRESSED_SWAP_CHUNK_SIZE (C_SEG_BUFSIZE)
56
57#define VM_SWAPFILE_HIWATER_SEGS (MIN_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE)
58
59#define SWAPFILE_RECLAIM_THRESHOLD_SEGS ((17 * (MAX_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE)) / 10)
60#define SWAPFILE_RECLAIM_MINIMUM_SEGS ((13 * (MAX_SWAP_FILE_SIZE / COMPRESSED_SWAP_CHUNK_SIZE)) / 10)
61
62
63#define SWAP_FILE_NAME "/private/var/vm/swapfile"
64#define SWAPFILENAME_LEN (int)(strlen(SWAP_FILE_NAME))
65
66
67#define SWAP_SLOT_MASK 0x1FFFFFFFF
68#define SWAP_DEVICE_SHIFT 33
69
70extern int vm_num_swap_files;
71
72struct swapfile;
73lck_grp_attr_t vm_swap_data_lock_grp_attr;
74lck_grp_t vm_swap_data_lock_grp;
75lck_attr_t vm_swap_data_lock_attr;
76lck_mtx_ext_t vm_swap_data_lock_ext;
77lck_mtx_t vm_swap_data_lock;
78
79void vm_swap_init(void);
80boolean_t vm_swap_create_file(void);
81
82
83struct swapout_io_completion {
84
85 int swp_io_busy;
86 int swp_io_done;
87 int swp_io_error;
88
89 uint32_t swp_c_size;
90 c_segment_t swp_c_seg;
91
92 struct swapfile *swp_swf;
93 uint64_t swp_f_offset;
94
95 struct upl_io_completion swp_upl_ctx;
96};
97void vm_swapout_iodone(void *, int);
98
99
100static void vm_swapout_finish(c_segment_t, uint64_t, uint32_t, kern_return_t);
101kern_return_t vm_swap_put_finish(struct swapfile *, uint64_t *, int);
102kern_return_t vm_swap_put(vm_offset_t, uint64_t*, uint32_t, c_segment_t, struct swapout_io_completion *);
103
104void vm_swap_flush(void);
105void vm_swap_reclaim(void);
106void vm_swap_encrypt(c_segment_t);
107uint64_t vm_swap_get_total_space(void);
108uint64_t vm_swap_get_used_space(void);
109uint64_t vm_swap_get_free_space(void);
110
111struct vnode;
112extern void vm_swapfile_open(const char *path, struct vnode **vp);
113extern void vm_swapfile_close(uint64_t path, struct vnode *vp);
114extern int vm_swapfile_preallocate(struct vnode *vp, uint64_t *size, boolean_t *pin);
115extern uint64_t vm_swapfile_get_blksize(struct vnode *vp);
116extern uint64_t vm_swapfile_get_transfer_size(struct vnode *vp);
117extern int vm_swapfile_io(struct vnode *vp, uint64_t offset, uint64_t start, int npages, int flags, void *upl_ctx);
118
119#if CONFIG_FREEZE
120boolean_t vm_swap_max_budget(uint64_t *);
121int vm_swap_vol_get_budget(struct vnode* vp, uint64_t *freeze_daily_budget);
122#endif /* CONFIG_FREEZE */
123
124#if RECORD_THE_COMPRESSED_DATA
125extern int vm_record_file_write(struct vnode *vp, uint64_t offset, char *buf, int size);
126#endif
127
128