2 * Copyright (c) 2000-2008 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@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
34 * All Rights Reserved.
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * Paging File Management.
62 #include <mach/host_priv.h>
63 #include <mach/memory_object_control.h>
64 #include <mach/memory_object_server.h>
66 #include <default_pager/default_pager_internal.h>
67 #include <default_pager/default_pager_alerts.h>
68 #include <default_pager/default_pager_object_server.h>
70 #include <ipc/ipc_types.h>
71 #include <ipc/ipc_port.h>
72 #include <ipc/ipc_space.h>
74 #include <kern/kern_types.h>
75 #include <kern/host.h>
76 #include <kern/queue.h>
77 #include <kern/counters.h>
78 #include <kern/sched_prim.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_pageout.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_object.h>
84 #include <vm/vm_protos.h>
87 /* todo - need large internal object support */
90 * ALLOC_STRIDE... the maximum number of bytes allocated from
91 * a swap file before moving on to the next swap file... if
92 * all swap files reside on a single disk, this value should
93 * be very large (this is the default assumption)... if the
94 * swap files are spread across multiple disks, than this value
95 * should be small (128 * 1024)...
97 * This should be determined dynamically in the future
100 #define ALLOC_STRIDE (1024 * 1024 * 1024)
101 int physical_transfer_cluster_count
= 0;
103 #define VM_SUPER_CLUSTER 0x40000
104 #define VM_SUPER_PAGES (VM_SUPER_CLUSTER / PAGE_SIZE)
107 * 0 means no shift to pages, so == 1 page/cluster. 1 would mean
108 * 2 pages/cluster, 2 means 4 pages/cluster, and so on.
110 #define VSTRUCT_MIN_CLSHIFT 0
112 #define VSTRUCT_DEF_CLSHIFT 2
113 int default_pager_clsize
= 0;
115 int vstruct_def_clshift
= VSTRUCT_DEF_CLSHIFT
;
118 unsigned int clustered_writes
[VM_SUPER_PAGES
+1];
119 unsigned int clustered_reads
[VM_SUPER_PAGES
+1];
122 * Globals used for asynchronous paging operations:
123 * vs_async_list: head of list of to-be-completed I/O ops
124 * async_num_queued: number of pages completed, but not yet
125 * processed by async thread.
126 * async_requests_out: number of pages of requests not completed.
130 struct vs_async
*vs_async_list
;
131 int async_num_queued
;
132 int async_requests_out
;
136 #define VS_ASYNC_REUSE 1
137 struct vs_async
*vs_async_free_list
;
139 lck_mtx_t default_pager_async_lock
; /* Protects globals above */
142 int vs_alloc_async_failed
= 0; /* statistics */
143 int vs_alloc_async_count
= 0; /* statistics */
144 struct vs_async
*vs_alloc_async(void); /* forward */
145 void vs_free_async(struct vs_async
*vsa
); /* forward */
148 #define VS_ALLOC_ASYNC() vs_alloc_async()
149 #define VS_FREE_ASYNC(vsa) vs_free_async(vsa)
151 #define VS_ASYNC_LOCK() lck_mtx_lock(&default_pager_async_lock)
152 #define VS_ASYNC_UNLOCK() lck_mtx_unlock(&default_pager_async_lock)
153 #define VS_ASYNC_LOCK_INIT() lck_mtx_init(&default_pager_async_lock, &default_pager_lck_grp, &default_pager_lck_attr)
154 #define VS_ASYNC_LOCK_DESTROY() lck_mtx_destroy(&default_pager_async_lock, &default_pager_lck_grp)
155 #define VS_ASYNC_LOCK_ADDR() (&default_pager_async_lock)
157 * Paging Space Hysteresis triggers and the target notification port
160 unsigned int dp_pages_free_drift_count
= 0;
161 unsigned int dp_pages_free_drifted_max
= 0;
162 unsigned int minimum_pages_remaining
= 0;
163 unsigned int maximum_pages_free
= 0;
164 ipc_port_t min_pages_trigger_port
= NULL
;
165 ipc_port_t max_pages_trigger_port
= NULL
;
168 boolean_t use_emergency_swap_file_first
= TRUE
;
170 boolean_t use_emergency_swap_file_first
= FALSE
;
172 boolean_t bs_low
= FALSE
;
173 int backing_store_release_trigger_disable
= 0;
174 boolean_t backing_store_stop_compaction
= FALSE
;
175 boolean_t backing_store_abort_compaction
= FALSE
;
177 /* Have we decided if swap needs to be encrypted yet ? */
178 boolean_t dp_encryption_inited
= FALSE
;
179 /* Should we encrypt swap ? */
180 boolean_t dp_encryption
= FALSE
;
182 boolean_t dp_isssd
= FALSE
;
185 * Object sizes are rounded up to the next power of 2,
186 * unless they are bigger than a given maximum size.
188 vm_size_t max_doubled_size
= 4 * 1024 * 1024; /* 4 meg */
191 * List of all backing store and segments.
193 MACH_PORT_FACE emergency_segment_backing_store
;
194 struct backing_store_list_head backing_store_list
;
195 paging_segment_t paging_segments
[MAX_NUM_PAGING_SEGMENTS
];
196 lck_mtx_t paging_segments_lock
;
197 int paging_segment_max
= 0;
198 int paging_segment_count
= 0;
199 int ps_select_array
[BS_MAXPRI
+1] = { -1,-1,-1,-1,-1 };
203 * Total pages free in system
204 * This differs from clusters committed/avail which is a measure of the
205 * over commitment of paging segments to backing store. An idea which is
206 * likely to be deprecated.
208 unsigned int dp_pages_free
= 0;
209 unsigned int dp_pages_reserve
= 0;
210 unsigned int cluster_transfer_minimum
= 100;
215 struct ps_vnode_trim_data
{
221 /* forward declarations */
222 kern_return_t
ps_write_file(paging_segment_t
, upl_t
, upl_offset_t
, dp_offset_t
, unsigned int, int); /* forward */
223 kern_return_t
ps_read_file (paging_segment_t
, upl_t
, upl_offset_t
, dp_offset_t
, unsigned int, unsigned int *, int); /* forward */
224 default_pager_thread_t
*get_read_buffer( void );
225 kern_return_t
ps_vstruct_transfer_from_segment(
227 paging_segment_t segment
,
229 kern_return_t
ps_read_device(paging_segment_t
, dp_offset_t
, vm_offset_t
*, unsigned int, unsigned int *, int); /* forward */
230 kern_return_t
ps_write_device(paging_segment_t
, dp_offset_t
, vm_offset_t
, unsigned int, struct vs_async
*); /* forward */
231 kern_return_t
vs_cluster_transfer(
236 vs_map_t
vs_get_map_entry(
241 default_pager_backing_store_delete_internal( MACH_PORT_FACE
);
243 static inline void ps_vnode_trim_init(struct ps_vnode_trim_data
*data
);
244 static inline void ps_vnode_trim_now(struct ps_vnode_trim_data
*data
);
245 static inline void ps_vnode_trim_more(struct ps_vnode_trim_data
*data
, struct vs_map
*map
, unsigned int shift
, dp_size_t length
);
247 default_pager_thread_t
*
248 get_read_buffer( void )
254 for (i
=0; i
<default_pager_internal_count
; i
++) {
255 if(dpt_array
[i
]->checked_out
== FALSE
) {
256 dpt_array
[i
]->checked_out
= TRUE
;
257 DPT_UNLOCK(dpt_lock
);
261 DPT_SLEEP(dpt_lock
, &dpt_array
, THREAD_UNINT
);
271 * List of all backing store.
274 queue_init(&backing_store_list
.bsl_queue
);
277 VS_ASYNC_LOCK_INIT();
279 vs_async_free_list
= NULL
;
280 #endif /* VS_ASYNC_REUSE */
282 for (i
= 0; i
< VM_SUPER_PAGES
+ 1; i
++) {
283 clustered_writes
[i
] = 0;
284 clustered_reads
[i
] = 0;
290 * When things do not quite workout...
292 void bs_no_paging_space(boolean_t
); /* forward */
296 boolean_t out_of_memory
)
300 dprintf(("*** OUT OF MEMORY ***\n"));
301 panic("bs_no_paging_space: NOT ENOUGH PAGING SPACE");
304 void bs_more_space(int); /* forward */
305 void bs_commit(int); /* forward */
307 boolean_t user_warned
= FALSE
;
308 unsigned int clusters_committed
= 0;
309 unsigned int clusters_available
= 0;
310 unsigned int clusters_committed_peak
= 0;
318 * Account for new paging space.
320 clusters_available
+= nclusters
;
322 if (clusters_available
>= clusters_committed
) {
323 if (verbose
&& user_warned
) {
324 printf("%s%s - %d excess clusters now.\n",
326 "paging space is OK now",
327 clusters_available
- clusters_committed
);
329 clusters_committed_peak
= 0;
332 if (verbose
&& user_warned
) {
333 printf("%s%s - still short of %d clusters.\n",
335 "WARNING: paging space over-committed",
336 clusters_committed
- clusters_available
);
337 clusters_committed_peak
-= nclusters
;
350 clusters_committed
+= nclusters
;
351 if (clusters_committed
> clusters_available
) {
352 if (verbose
&& !user_warned
) {
354 printf("%s%s - short of %d clusters.\n",
356 "WARNING: paging space over-committed",
357 clusters_committed
- clusters_available
);
359 if (clusters_committed
> clusters_committed_peak
) {
360 clusters_committed_peak
= clusters_committed
;
363 if (verbose
&& user_warned
) {
364 printf("%s%s - was short of up to %d clusters.\n",
366 "paging space is OK now",
367 clusters_committed_peak
- clusters_available
);
369 clusters_committed_peak
= 0;
377 int default_pager_info_verbose
= 1;
384 uint64_t pages_total
, pages_free
;
389 pages_total
= pages_free
= 0;
390 for (i
= 0; i
<= paging_segment_max
; i
++) {
391 ps
= paging_segments
[i
];
392 if (ps
== PAGING_SEGMENT_NULL
)
396 * no need to lock: by the time this data
397 * gets back to any remote requestor it
398 * will be obsolete anyways
400 pages_total
+= ps
->ps_pgnum
;
401 pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
402 DP_DEBUG(DEBUG_BS_INTERNAL
,
403 ("segment #%d: %d total, %d free\n",
404 i
, ps
->ps_pgnum
, ps
->ps_clcount
<< ps
->ps_clshift
));
406 *totalp
= pages_total
;
408 if (verbose
&& user_warned
&& default_pager_info_verbose
) {
409 if (clusters_available
< clusters_committed
) {
410 printf("%s %d clusters committed, %d available.\n",
419 backing_store_t
backing_store_alloc(void); /* forward */
422 backing_store_alloc(void)
426 bs
= (backing_store_t
) kalloc(sizeof (struct backing_store
));
427 if (bs
== BACKING_STORE_NULL
)
428 panic("backing_store_alloc: no memory");
431 bs
->bs_port
= MACH_PORT_NULL
;
434 bs
->bs_pages_total
= 0;
436 bs
->bs_pages_in_fail
= 0;
437 bs
->bs_pages_out
= 0;
438 bs
->bs_pages_out_fail
= 0;
443 backing_store_t
backing_store_lookup(MACH_PORT_FACE
); /* forward */
445 /* Even in both the component space and external versions of this pager, */
446 /* backing_store_lookup will be called from tasks in the application space */
448 backing_store_lookup(
454 port is currently backed with a vs structure in the alias field
455 we could create an ISBS alias and a port_is_bs call but frankly
456 I see no reason for the test, the bs->port == port check below
457 will work properly on junk entries.
459 if ((port == MACH_PORT_NULL) || port_is_vs(port))
461 if (port
== MACH_PORT_NULL
)
462 return BACKING_STORE_NULL
;
465 queue_iterate(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
468 if (bs
->bs_port
== port
) {
470 /* Success, return it locked. */
476 return BACKING_STORE_NULL
;
479 void backing_store_add(backing_store_t
); /* forward */
483 __unused backing_store_t bs
)
485 // MACH_PORT_FACE port = bs->bs_port;
486 // MACH_PORT_FACE pset = default_pager_default_set;
487 kern_return_t kr
= KERN_SUCCESS
;
489 if (kr
!= KERN_SUCCESS
)
490 panic("backing_store_add: add to set");
495 * Set up default page shift, but only if not already
496 * set and argument is within range.
499 bs_set_default_clsize(unsigned int npages
)
506 if (default_pager_clsize
== 0) /* if not yet set */
507 vstruct_def_clshift
= local_log2(npages
);
513 int bs_get_global_clsize(int clsize
); /* forward */
516 bs_get_global_clsize(
520 memory_object_default_t dmm
;
524 * Only allow setting of cluster size once. If called
525 * with no cluster size (default), we use the compiled-in default
526 * for the duration. The same cluster size is used for all
529 if (default_pager_clsize
== 0) {
531 * Keep cluster size in bit shift because it's quicker
532 * arithmetic, and easier to keep at a power of 2.
534 if (clsize
!= NO_CLSIZE
) {
535 for (i
= 0; (1 << i
) < clsize
; i
++);
536 if (i
> MAX_CLUSTER_SHIFT
)
537 i
= MAX_CLUSTER_SHIFT
;
538 vstruct_def_clshift
= i
;
540 default_pager_clsize
= (1 << vstruct_def_clshift
);
543 * Let the user know the new (and definitive) cluster size.
546 printf("%scluster size = %d page%s\n",
547 my_name
, default_pager_clsize
,
548 (default_pager_clsize
== 1) ? "" : "s");
551 * Let the kernel know too, in case it hasn't used the
552 * default value provided in main() yet.
554 dmm
= default_pager_object
;
555 clsize
= default_pager_clsize
* vm_page_size
; /* in bytes */
556 kr
= host_default_memory_manager(host_priv_self(),
559 memory_object_default_deallocate(dmm
);
561 if (kr
!= KERN_SUCCESS
) {
562 panic("bs_get_global_cl_size:host_default_memory_manager");
564 if (dmm
!= default_pager_object
) {
565 panic("bs_get_global_cl_size:there is another default pager");
568 ASSERT(default_pager_clsize
> 0 &&
569 (default_pager_clsize
& (default_pager_clsize
- 1)) == 0);
571 return default_pager_clsize
;
575 default_pager_backing_store_create(
576 memory_object_default_t pager
,
578 int clsize
, /* in bytes */
579 MACH_PORT_FACE
*backing_store
)
584 struct vstruct_alias
*alias_struct
;
586 if (pager
!= default_pager_object
)
587 return KERN_INVALID_ARGUMENT
;
589 bs
= backing_store_alloc();
590 port
= ipc_port_alloc_kernel();
591 ipc_port_make_send(port
);
592 assert (port
!= IP_NULL
);
594 DP_DEBUG(DEBUG_BS_EXTERNAL
,
595 ("priority=%d clsize=%d bs_port=0x%x\n",
596 priority
, clsize
, (int) backing_store
));
598 alias_struct
= (struct vstruct_alias
*)
599 kalloc(sizeof (struct vstruct_alias
));
600 if(alias_struct
!= NULL
) {
601 alias_struct
->vs
= (struct vstruct
*)bs
;
602 alias_struct
->name
= &default_pager_ops
;
603 port
->alias
= (uintptr_t) alias_struct
;
606 ipc_port_dealloc_kernel((MACH_PORT_FACE
)(port
));
609 kfree(bs
, sizeof (struct backing_store
));
611 return KERN_RESOURCE_SHORTAGE
;
615 if (priority
== DEFAULT_PAGER_BACKING_STORE_MAXPRI
)
616 priority
= BS_MAXPRI
;
617 else if (priority
== BS_NOPRI
)
618 priority
= BS_MAXPRI
;
620 priority
= BS_MINPRI
;
621 bs
->bs_priority
= priority
;
623 bs
->bs_clsize
= bs_get_global_clsize(atop_32(clsize
));
626 queue_enter(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
630 backing_store_add(bs
);
632 *backing_store
= port
;
637 default_pager_backing_store_info(
638 MACH_PORT_FACE backing_store
,
639 backing_store_flavor_t flavour
,
640 backing_store_info_t info
,
641 mach_msg_type_number_t
*size
)
644 backing_store_basic_info_t basic
;
648 if (flavour
!= BACKING_STORE_BASIC_INFO
||
649 *size
< BACKING_STORE_BASIC_INFO_COUNT
)
650 return KERN_INVALID_ARGUMENT
;
652 basic
= (backing_store_basic_info_t
)info
;
653 *size
= BACKING_STORE_BASIC_INFO_COUNT
;
655 VSTATS_LOCK(&global_stats
.gs_lock
);
656 basic
->pageout_calls
= global_stats
.gs_pageout_calls
;
657 basic
->pagein_calls
= global_stats
.gs_pagein_calls
;
658 basic
->pages_in
= global_stats
.gs_pages_in
;
659 basic
->pages_out
= global_stats
.gs_pages_out
;
660 basic
->pages_unavail
= global_stats
.gs_pages_unavail
;
661 basic
->pages_init
= global_stats
.gs_pages_init
;
662 basic
->pages_init_writes
= global_stats
.gs_pages_init_writes
;
663 VSTATS_UNLOCK(&global_stats
.gs_lock
);
665 if ((bs
= backing_store_lookup(backing_store
)) == BACKING_STORE_NULL
)
666 return KERN_INVALID_ARGUMENT
;
668 basic
->bs_pages_total
= bs
->bs_pages_total
;
670 bs
->bs_pages_free
= 0;
671 for (i
= 0; i
<= paging_segment_max
; i
++) {
672 ps
= paging_segments
[i
];
673 if (ps
!= PAGING_SEGMENT_NULL
&& ps
->ps_bs
== bs
) {
675 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
680 basic
->bs_pages_free
= bs
->bs_pages_free
;
681 basic
->bs_pages_in
= bs
->bs_pages_in
;
682 basic
->bs_pages_in_fail
= bs
->bs_pages_in_fail
;
683 basic
->bs_pages_out
= bs
->bs_pages_out
;
684 basic
->bs_pages_out_fail
= bs
->bs_pages_out_fail
;
686 basic
->bs_priority
= bs
->bs_priority
;
687 basic
->bs_clsize
= ptoa_32(bs
->bs_clsize
); /* in bytes */
694 int ps_delete(paging_segment_t
); /* forward */
695 boolean_t
current_thread_aborted(void);
702 kern_return_t error
= KERN_SUCCESS
;
705 VSL_LOCK(); /* get the lock on the list of vs's */
707 /* The lock relationship and sequence is farily complicated */
708 /* this code looks at a live list, locking and unlocking the list */
709 /* as it traverses it. It depends on the locking behavior of */
710 /* default_pager_no_senders. no_senders always locks the vstruct */
711 /* targeted for removal before locking the vstruct list. However */
712 /* it will remove that member of the list without locking its */
713 /* neighbors. We can be sure when we hold a lock on a vstruct */
714 /* it cannot be removed from the list but we must hold the list */
715 /* lock to be sure that its pointers to its neighbors are valid. */
716 /* Also, we can hold off destruction of a vstruct when the list */
717 /* lock and the vs locks are not being held by bumping the */
718 /* vs_async_pending count. */
721 while(backing_store_release_trigger_disable
!= 0) {
722 VSL_SLEEP(&backing_store_release_trigger_disable
, THREAD_UNINT
);
725 /* we will choose instead to hold a send right */
726 vs_count
= vstruct_list
.vsl_count
;
727 vs
= (vstruct_t
) queue_first((queue_entry_t
)&(vstruct_list
.vsl_queue
));
728 if(vs
== (vstruct_t
)&vstruct_list
) {
733 vs_async_wait(vs
); /* wait for any pending async writes */
734 if ((vs_count
!= 0) && (vs
!= NULL
))
735 vs
->vs_async_pending
+= 1; /* hold parties calling */
739 backing_store_abort_compaction
= FALSE
;
743 while((vs_count
!= 0) && (vs
!= NULL
)) {
744 /* We take the count of AMO's before beginning the */
745 /* transfer of of the target segment. */
746 /* We are guaranteed that the target segment cannot get */
747 /* more users. We also know that queue entries are */
748 /* made at the back of the list. If some of the entries */
749 /* we would check disappear while we are traversing the */
750 /* list then we will either check new entries which */
751 /* do not have any backing store in the target segment */
752 /* or re-check old entries. This might not be optimal */
753 /* but it will always be correct. The alternative is to */
754 /* take a snapshot of the list. */
757 if(dp_pages_free
< cluster_transfer_minimum
)
758 error
= KERN_FAILURE
;
760 vm_object_t transfer_object
;
765 transfer_object
= vm_object_allocate((vm_object_size_t
)VM_SUPER_CLUSTER
);
767 upl_flags
= (UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
|
768 UPL_SET_LITE
| UPL_SET_INTERNAL
);
770 /* mark the pages as "encrypted" when they come in */
771 upl_flags
|= UPL_ENCRYPT
;
773 error
= vm_object_upl_request(transfer_object
,
774 (vm_object_offset_t
)0, VM_SUPER_CLUSTER
,
775 &upl
, NULL
, &count
, upl_flags
);
777 if(error
== KERN_SUCCESS
) {
778 error
= ps_vstruct_transfer_from_segment(
780 upl_commit(upl
, NULL
, 0);
783 error
= KERN_FAILURE
;
785 vm_object_deallocate(transfer_object
);
787 if(error
|| current_thread_aborted()) {
789 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
790 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
791 vs
->vs_waiting_async
= FALSE
;
793 thread_wakeup(&vs
->vs_async_pending
);
802 while(backing_store_release_trigger_disable
!= 0) {
803 VSL_SLEEP(&backing_store_release_trigger_disable
,
807 next_vs
= (vstruct_t
) queue_next(&(vs
->vs_links
));
808 if((next_vs
!= (vstruct_t
)&vstruct_list
) &&
809 (vs
!= next_vs
) && (vs_count
!= 1)) {
811 vs_async_wait(next_vs
); /* wait for any */
812 /* pending async writes */
813 next_vs
->vs_async_pending
+= 1; /* hold parties */
814 /* calling vs_async_wait */
819 vs
->vs_async_pending
-= 1;
820 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
821 vs
->vs_waiting_async
= FALSE
;
823 thread_wakeup(&vs
->vs_async_pending
);
827 if((vs
== next_vs
) || (next_vs
== (vstruct_t
)&vstruct_list
))
838 default_pager_backing_store_delete_internal(
839 MACH_PORT_FACE backing_store
)
845 int interim_pages_removed
= 0;
846 boolean_t dealing_with_emergency_segment
= ( backing_store
== emergency_segment_backing_store
);
848 if ((bs
= backing_store_lookup(backing_store
)) == BACKING_STORE_NULL
)
849 return KERN_INVALID_ARGUMENT
;
853 error
= KERN_SUCCESS
;
854 for (i
= 0; i
<= paging_segment_max
; i
++) {
855 ps
= paging_segments
[i
];
856 if (ps
!= PAGING_SEGMENT_NULL
&&
858 ! IS_PS_GOING_AWAY(ps
)) {
861 if( IS_PS_GOING_AWAY(ps
) || !IS_PS_OK_TO_USE(ps
)) {
863 * Someone is already busy reclamining this paging segment.
864 * If it's the emergency segment we are looking at then check
865 * that someone has not already recovered it and set the right
866 * state i.e. online but not activated.
872 /* disable access to this segment */
873 ps
->ps_state
&= ~PS_CAN_USE
;
874 ps
->ps_state
|= PS_GOING_AWAY
;
877 * The "ps" segment is "off-line" now,
878 * we can try and delete it...
880 if(dp_pages_free
< (cluster_transfer_minimum
882 error
= KERN_FAILURE
;
886 /* remove all pages associated with the */
887 /* segment from the list of free pages */
888 /* when transfer is through, all target */
889 /* segment pages will appear to be free */
891 dp_pages_free
-= ps
->ps_pgcount
;
892 interim_pages_removed
+= ps
->ps_pgcount
;
894 error
= ps_delete(ps
);
896 if (error
!= KERN_SUCCESS
) {
898 * We couldn't delete the segment,
899 * probably because there's not enough
900 * virtual memory left.
901 * Re-enable all the segments.
910 if (error
!= KERN_SUCCESS
) {
911 for (i
= 0; i
<= paging_segment_max
; i
++) {
912 ps
= paging_segments
[i
];
913 if (ps
!= PAGING_SEGMENT_NULL
&&
915 IS_PS_GOING_AWAY(ps
)) {
918 if( !IS_PS_GOING_AWAY(ps
)) {
922 /* Handle the special clusters that came in while we let go the lock*/
923 if( ps
->ps_special_clusters
) {
924 dp_pages_free
+= ps
->ps_special_clusters
<< ps
->ps_clshift
;
925 ps
->ps_pgcount
+= ps
->ps_special_clusters
<< ps
->ps_clshift
;
926 ps
->ps_clcount
+= ps
->ps_special_clusters
;
927 if ( ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
) {
928 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
930 ps
->ps_special_clusters
= 0;
932 /* re-enable access to this segment */
933 ps
->ps_state
&= ~PS_GOING_AWAY
;
934 ps
->ps_state
|= PS_CAN_USE
;
938 dp_pages_free
+= interim_pages_removed
;
944 for (i
= 0; i
<= paging_segment_max
; i
++) {
945 ps
= paging_segments
[i
];
946 if (ps
!= PAGING_SEGMENT_NULL
&&
948 if(IS_PS_GOING_AWAY(ps
)) {
949 if(IS_PS_EMERGENCY_SEGMENT(ps
)) {
951 ps
->ps_state
&= ~PS_GOING_AWAY
;
952 ps
->ps_special_clusters
= 0;
953 ps
->ps_pgcount
= ps
->ps_pgnum
;
954 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
955 dp_pages_reserve
+= ps
->ps_pgcount
;
958 paging_segments
[i
] = PAGING_SEGMENT_NULL
;
959 paging_segment_count
--;
961 kfree(ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
962 kfree(ps
, sizeof *ps
);
968 /* Scan the entire ps array separately to make certain we find the */
969 /* proper paging_segment_max */
970 for (i
= 0; i
< MAX_NUM_PAGING_SEGMENTS
; i
++) {
971 if(paging_segments
[i
] != PAGING_SEGMENT_NULL
)
972 paging_segment_max
= i
;
977 if( dealing_with_emergency_segment
) {
983 * All the segments have been deleted.
984 * We can remove the backing store.
988 * Disable lookups of this backing store.
990 if((void *)bs
->bs_port
->alias
!= NULL
)
991 kfree((void *) bs
->bs_port
->alias
,
992 sizeof (struct vstruct_alias
));
993 ipc_port_dealloc_kernel((ipc_port_t
) (bs
->bs_port
));
994 bs
->bs_port
= MACH_PORT_NULL
;
998 * Remove backing store from backing_store list.
1001 queue_remove(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
1006 * Free the backing store structure.
1008 BS_LOCK_DESTROY(bs
);
1009 kfree(bs
, sizeof *bs
);
1011 return KERN_SUCCESS
;
1015 default_pager_backing_store_delete(
1016 MACH_PORT_FACE backing_store
)
1018 if( backing_store
!= emergency_segment_backing_store
) {
1019 default_pager_backing_store_delete_internal(emergency_segment_backing_store
);
1021 return(default_pager_backing_store_delete_internal(backing_store
));
1024 int ps_enter(paging_segment_t
); /* forward */
1028 paging_segment_t ps
)
1034 for (i
= 0; i
< MAX_NUM_PAGING_SEGMENTS
; i
++) {
1035 if (paging_segments
[i
] == PAGING_SEGMENT_NULL
)
1039 if (i
< MAX_NUM_PAGING_SEGMENTS
) {
1040 paging_segments
[i
] = ps
;
1041 if (i
> paging_segment_max
)
1042 paging_segment_max
= i
;
1043 paging_segment_count
++;
1044 if ((ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_NOPRI
) ||
1045 (ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
))
1046 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
1050 return KERN_RESOURCE_SHORTAGE
;
1057 #ifdef DEVICE_PAGING
1059 default_pager_add_segment(
1060 MACH_PORT_FACE backing_store
,
1061 MACH_PORT_FACE device
,
1067 paging_segment_t ps
;
1071 if ((bs
= backing_store_lookup(backing_store
))
1072 == BACKING_STORE_NULL
)
1073 return KERN_INVALID_ARGUMENT
;
1076 for (i
= 0; i
<= paging_segment_max
; i
++) {
1077 ps
= paging_segments
[i
];
1078 if (ps
== PAGING_SEGMENT_NULL
)
1082 * Check for overlap on same device.
1084 if (!(ps
->ps_device
!= device
1085 || offset
>= ps
->ps_offset
+ ps
->ps_recnum
1086 || offset
+ count
<= ps
->ps_offset
)) {
1089 return KERN_INVALID_ARGUMENT
;
1095 * Set up the paging segment
1097 ps
= (paging_segment_t
) kalloc(sizeof (struct paging_segment
));
1098 if (ps
== PAGING_SEGMENT_NULL
) {
1100 return KERN_RESOURCE_SHORTAGE
;
1103 ps
->ps_segtype
= PS_PARTITION
;
1104 ps
->ps_device
= device
;
1105 ps
->ps_offset
= offset
;
1106 ps
->ps_record_shift
= local_log2(vm_page_size
/ record_size
);
1107 ps
->ps_recnum
= count
;
1108 ps
->ps_pgnum
= count
>> ps
->ps_record_shift
;
1110 ps
->ps_pgcount
= ps
->ps_pgnum
;
1111 ps
->ps_clshift
= local_log2(bs
->bs_clsize
);
1112 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
1116 ps
->ps_bmap
= (unsigned char *) kalloc(RMAPSIZE(ps
->ps_ncls
));
1118 PS_LOCK_DESTROY(ps
);
1119 kfree(ps
, sizeof *ps
);
1121 return KERN_RESOURCE_SHORTAGE
;
1123 for (i
= 0; i
< ps
->ps_ncls
; i
++) {
1124 clrbit(ps
->ps_bmap
, i
);
1127 if(paging_segment_count
== 0) {
1128 ps
->ps_state
= PS_EMERGENCY_SEGMENT
;
1129 if(use_emergency_swap_file_first
) {
1130 ps
->ps_state
|= PS_CAN_USE
;
1133 ps
->ps_state
= PS_CAN_USE
;
1138 if ((error
= ps_enter(ps
)) != 0) {
1139 kfree(ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
1141 PS_LOCK_DESTROY(ps
);
1142 kfree(ps
, sizeof *ps
);
1144 return KERN_RESOURCE_SHORTAGE
;
1147 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
1148 bs
->bs_pages_total
+= ps
->ps_clcount
<< ps
->ps_clshift
;
1152 if(IS_PS_OK_TO_USE(ps
)) {
1153 dp_pages_free
+= ps
->ps_pgcount
;
1155 dp_pages_reserve
+= ps
->ps_pgcount
;
1159 bs_more_space(ps
->ps_clcount
);
1161 DP_DEBUG(DEBUG_BS_INTERNAL
,
1162 ("device=0x%x,offset=0x%x,count=0x%x,record_size=0x%x,shift=%d,total_size=0x%x\n",
1163 device
, offset
, count
, record_size
,
1164 ps
->ps_record_shift
, ps
->ps_pgnum
));
1166 return KERN_SUCCESS
;
1172 MACH_PORT_FACE master
)
1174 security_token_t null_security_token
= {
1177 MACH_PORT_FACE device
;
1178 int info
[DEV_GET_SIZE_COUNT
];
1179 mach_msg_type_number_t info_count
;
1180 MACH_PORT_FACE bs
= MACH_PORT_NULL
;
1181 unsigned int rec_size
;
1184 MACH_PORT_FACE reply_port
;
1186 if (ds_device_open_sync(master
, MACH_PORT_NULL
, D_READ
| D_WRITE
,
1187 null_security_token
, dev_name
, &device
))
1190 info_count
= DEV_GET_SIZE_COUNT
;
1191 if (!ds_device_get_status(device
, DEV_GET_SIZE
, info
, &info_count
)) {
1192 rec_size
= info
[DEV_GET_SIZE_RECORD_SIZE
];
1193 count
= info
[DEV_GET_SIZE_DEVICE_SIZE
] / rec_size
;
1194 clsize
= bs_get_global_clsize(0);
1195 if (!default_pager_backing_store_create(
1196 default_pager_object
,
1197 DEFAULT_PAGER_BACKING_STORE_MAXPRI
,
1198 (clsize
* vm_page_size
),
1200 if (!default_pager_add_segment(bs
, device
,
1201 0, count
, rec_size
)) {
1204 ipc_port_release_receive(bs
);
1208 ipc_port_release_send(device
);
1211 #endif /* DEVICE_PAGING */
1216 vs_alloc_async(void)
1218 struct vs_async
*vsa
;
1219 MACH_PORT_FACE reply_port
;
1220 // kern_return_t kr;
1223 if (vs_async_free_list
== NULL
) {
1225 vsa
= (struct vs_async
*) kalloc(sizeof (struct vs_async
));
1228 * Try allocating a reply port named after the
1229 * address of the vs_async structure.
1231 struct vstruct_alias
*alias_struct
;
1233 reply_port
= ipc_port_alloc_kernel();
1234 alias_struct
= (struct vstruct_alias
*)
1235 kalloc(sizeof (struct vstruct_alias
));
1236 if(alias_struct
!= NULL
) {
1237 alias_struct
->vs
= (struct vstruct
*)vsa
;
1238 alias_struct
->name
= &default_pager_ops
;
1239 reply_port
->alias
= (uintptr_t) alias_struct
;
1240 vsa
->reply_port
= reply_port
;
1241 vs_alloc_async_count
++;
1244 vs_alloc_async_failed
++;
1245 ipc_port_dealloc_kernel((MACH_PORT_FACE
)
1247 kfree(vsa
, sizeof (struct vs_async
));
1252 vsa
= vs_async_free_list
;
1253 vs_async_free_list
= vs_async_free_list
->vsa_next
;
1262 struct vs_async
*vsa
)
1265 vsa
->vsa_next
= vs_async_free_list
;
1266 vs_async_free_list
= vsa
;
1270 #else /* VS_ASYNC_REUSE */
1273 vs_alloc_async(void)
1275 struct vs_async
*vsa
;
1276 MACH_PORT_FACE reply_port
;
1279 vsa
= (struct vs_async
*) kalloc(sizeof (struct vs_async
));
1282 * Try allocating a reply port named after the
1283 * address of the vs_async structure.
1285 reply_port
= ipc_port_alloc_kernel();
1286 alias_struct
= (vstruct_alias
*)
1287 kalloc(sizeof (struct vstruct_alias
));
1288 if(alias_struct
!= NULL
) {
1289 alias_struct
->vs
= reply_port
;
1290 alias_struct
->name
= &default_pager_ops
;
1291 reply_port
->alias
= (int) vsa
;
1292 vsa
->reply_port
= reply_port
;
1293 vs_alloc_async_count
++;
1296 vs_alloc_async_failed
++;
1297 ipc_port_dealloc_kernel((MACH_PORT_FACE
)
1299 kfree(vsa
, sizeof (struct vs_async
));
1309 struct vs_async
*vsa
)
1311 MACH_PORT_FACE reply_port
;
1314 reply_port
= vsa
->reply_port
;
1315 kfree(reply_port
->alias
, sizeof (struct vstuct_alias
));
1316 kfree(vsa
, sizeof (struct vs_async
));
1317 ipc_port_dealloc_kernel((MACH_PORT_FACE
) (reply_port
));
1320 vs_alloc_async_count
--;
1325 #endif /* VS_ASYNC_REUSE */
1327 zone_t vstruct_zone
;
1336 vs
= (vstruct_t
) zalloc(vstruct_zone
);
1337 if (vs
== VSTRUCT_NULL
) {
1338 return VSTRUCT_NULL
;
1344 * The following fields will be provided later.
1346 vs
->vs_pager_ops
= NULL
;
1347 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
1348 vs
->vs_references
= 1;
1351 vs
->vs_waiting_seqno
= FALSE
;
1352 vs
->vs_waiting_read
= FALSE
;
1353 vs
->vs_waiting_write
= FALSE
;
1354 vs
->vs_waiting_async
= FALSE
;
1361 vs
->vs_clshift
= local_log2(bs_get_global_clsize(0));
1362 vs
->vs_size
= ((atop_32(round_page_32(size
)) - 1) >> vs
->vs_clshift
) + 1;
1363 vs
->vs_async_pending
= 0;
1366 * Allocate the pmap, either CLMAP_SIZE or INDIRECT_CLMAP_SIZE
1367 * depending on the size of the memory object.
1369 if (INDIRECT_CLMAP(vs
->vs_size
)) {
1370 vs
->vs_imap
= (struct vs_map
**)
1371 kalloc(INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1372 vs
->vs_indirect
= TRUE
;
1374 vs
->vs_dmap
= (struct vs_map
*)
1375 kalloc(CLMAP_SIZE(vs
->vs_size
));
1376 vs
->vs_indirect
= FALSE
;
1378 vs
->vs_xfer_pending
= FALSE
;
1379 DP_DEBUG(DEBUG_VS_INTERNAL
,
1380 ("map=0x%x, indirect=%d\n", (int) vs
->vs_dmap
, vs
->vs_indirect
));
1383 * Check to see that we got the space.
1386 kfree(vs
, sizeof *vs
);
1387 return VSTRUCT_NULL
;
1391 * Zero the indirect pointers, or clear the direct pointers.
1393 if (vs
->vs_indirect
)
1394 memset(vs
->vs_imap
, 0,
1395 INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1397 for (i
= 0; i
< vs
->vs_size
; i
++)
1398 VSM_CLR(vs
->vs_dmap
[i
]);
1400 VS_MAP_LOCK_INIT(vs
);
1402 bs_commit(vs
->vs_size
);
1407 paging_segment_t
ps_select_segment(unsigned int, int *); /* forward */
1414 paging_segment_t ps
;
1419 * Optimize case where there's only one segment.
1420 * paging_segment_max will index the one and only segment.
1424 if (paging_segment_count
== 1) {
1425 paging_segment_t lps
= PAGING_SEGMENT_NULL
; /* used to avoid extra PS_UNLOCK */
1426 ipc_port_t trigger
= IP_NULL
;
1428 ps
= paging_segments
[paging_segment_max
];
1429 *psindex
= paging_segment_max
;
1431 if( !IS_PS_EMERGENCY_SEGMENT(ps
) ) {
1432 panic("Emergency paging segment missing\n");
1434 ASSERT(ps
->ps_clshift
>= shift
);
1435 if(IS_PS_OK_TO_USE(ps
)) {
1436 if (ps
->ps_clcount
) {
1438 dp_pages_free
-= 1 << ps
->ps_clshift
;
1439 ps
->ps_pgcount
-= 1 << ps
->ps_clshift
;
1440 if(min_pages_trigger_port
&&
1441 (dp_pages_free
< minimum_pages_remaining
)) {
1442 trigger
= min_pages_trigger_port
;
1443 min_pages_trigger_port
= NULL
;
1445 backing_store_abort_compaction
= TRUE
;
1452 if( lps
== PAGING_SEGMENT_NULL
) {
1454 dp_pages_free_drift_count
++;
1455 if(dp_pages_free
> dp_pages_free_drifted_max
) {
1456 dp_pages_free_drifted_max
= dp_pages_free
;
1458 dprintf(("Emergency swap segment:dp_pages_free before zeroing out: %d\n",dp_pages_free
));
1465 if (trigger
!= IP_NULL
) {
1466 dprintf(("ps_select_segment - send HI_WAT_ALERT\n"));
1468 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1469 ipc_port_release_send(trigger
);
1474 if (paging_segment_count
== 0) {
1476 dp_pages_free_drift_count
++;
1477 if(dp_pages_free
> dp_pages_free_drifted_max
) {
1478 dp_pages_free_drifted_max
= dp_pages_free
;
1480 dprintf(("No paging segments:dp_pages_free before zeroing out: %d\n",dp_pages_free
));
1484 return PAGING_SEGMENT_NULL
;
1488 i
>= BS_MINPRI
; i
--) {
1491 if ((ps_select_array
[i
] == BS_NOPRI
) ||
1492 (ps_select_array
[i
] == BS_FULLPRI
))
1494 start_index
= ps_select_array
[i
];
1496 if(!(paging_segments
[start_index
])) {
1498 physical_transfer_cluster_count
= 0;
1500 else if ((physical_transfer_cluster_count
+1) == (ALLOC_STRIDE
>>
1501 (((paging_segments
[start_index
])->ps_clshift
)
1502 + vm_page_shift
))) {
1503 physical_transfer_cluster_count
= 0;
1504 j
= start_index
+ 1;
1506 physical_transfer_cluster_count
+=1;
1508 if(start_index
== 0)
1509 start_index
= paging_segment_max
;
1511 start_index
= start_index
- 1;
1515 if (j
> paging_segment_max
)
1517 if ((ps
= paging_segments
[j
]) &&
1518 (ps
->ps_bs
->bs_priority
== i
)) {
1520 * Force the ps cluster size to be
1521 * >= that of the vstruct.
1524 if (IS_PS_OK_TO_USE(ps
)) {
1525 if ((ps
->ps_clcount
) &&
1526 (ps
->ps_clshift
>= shift
)) {
1527 ipc_port_t trigger
= IP_NULL
;
1530 dp_pages_free
-= 1 << ps
->ps_clshift
;
1531 ps
->ps_pgcount
-= 1 << ps
->ps_clshift
;
1532 if(min_pages_trigger_port
&&
1534 minimum_pages_remaining
)) {
1535 trigger
= min_pages_trigger_port
;
1536 min_pages_trigger_port
= NULL
;
1538 backing_store_abort_compaction
= TRUE
;
1542 * found one, quit looking.
1544 ps_select_array
[i
] = j
;
1547 if (trigger
!= IP_NULL
) {
1548 dprintf(("ps_select_segment - send HI_WAT_ALERT\n"));
1550 default_pager_space_alert(
1553 ipc_port_release_send(trigger
);
1561 if (j
== start_index
) {
1563 * none at this priority -- mark it full
1565 ps_select_array
[i
] = BS_FULLPRI
;
1573 dp_pages_free_drift_count
++;
1574 if(dp_pages_free
> dp_pages_free_drifted_max
) {
1575 dp_pages_free_drifted_max
= dp_pages_free
;
1577 dprintf(("%d Paging Segments: dp_pages_free before zeroing out: %d\n",paging_segment_count
,dp_pages_free
));
1581 return PAGING_SEGMENT_NULL
;
1584 dp_offset_t
ps_allocate_cluster(vstruct_t
, int *, paging_segment_t
); /*forward*/
1587 ps_allocate_cluster(
1590 paging_segment_t use_ps
)
1592 unsigned int byte_num
;
1594 paging_segment_t ps
;
1595 dp_offset_t cluster
;
1596 ipc_port_t trigger
= IP_NULL
;
1599 * Find best paging segment.
1600 * ps_select_segment will decrement cluster count on ps.
1601 * Must pass cluster shift to find the most appropriate segment.
1603 /* NOTE: The addition of paging segment delete capability threatened
1604 * to seriously complicate the treatment of paging segments in this
1605 * module and the ones that call it (notably ps_clmap), because of the
1606 * difficulty in assuring that the paging segment would continue to
1607 * exist between being unlocked and locked. This was
1608 * avoided because all calls to this module are based in either
1609 * dp_memory_object calls which rely on the vs lock, or by
1610 * the transfer function which is part of the segment delete path.
1611 * The transfer function which is part of paging segment delete is
1612 * protected from multiple callers by the backing store lock.
1613 * The paging segment delete function treats mappings to a paging
1614 * segment on a vstruct by vstruct basis, locking the vstruct targeted
1615 * while data is transferred to the remaining segments. This is in
1616 * line with the view that incomplete or in-transition mappings between
1617 * data, a vstruct, and backing store are protected by the vs lock.
1618 * This and the ordering of the paging segment "going_away" bit setting
1622 if (use_ps
!= PAGING_SEGMENT_NULL
) {
1627 ASSERT(ps
->ps_clcount
!= 0);
1630 dp_pages_free
-= 1 << ps
->ps_clshift
;
1631 ps
->ps_pgcount
-= 1 << ps
->ps_clshift
;
1632 if(min_pages_trigger_port
&&
1633 (dp_pages_free
< minimum_pages_remaining
)) {
1634 trigger
= min_pages_trigger_port
;
1635 min_pages_trigger_port
= NULL
;
1637 backing_store_abort_compaction
= TRUE
;
1641 if (trigger
!= IP_NULL
) {
1642 dprintf(("ps_allocate_cluster - send HI_WAT_ALERT\n"));
1644 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1645 ipc_port_release_send(trigger
);
1648 } else if ((ps
= ps_select_segment(vs
->vs_clshift
, psindex
)) ==
1649 PAGING_SEGMENT_NULL
) {
1650 static clock_sec_t lastnotify
= 0;
1652 clock_nsec_t nanoseconds_dummy
;
1655 * Don't immediately jump to the emergency segment. Give the
1656 * dynamic pager a chance to create it's first normal swap file.
1657 * Unless, of course the very first normal swap file can't be
1658 * created due to some problem and we didn't expect that problem
1659 * i.e. use_emergency_swap_file_first was never set to true initially.
1660 * It then gets set in the swap file creation error handling.
1662 if(paging_segment_count
> 1 || use_emergency_swap_file_first
== TRUE
) {
1664 ps
= paging_segments
[EMERGENCY_PSEG_INDEX
];
1665 if(IS_PS_EMERGENCY_SEGMENT(ps
) && !IS_PS_GOING_AWAY(ps
)) {
1669 if(IS_PS_GOING_AWAY(ps
)) {
1670 /* Someone de-activated the emergency paging segment*/
1674 } else if(dp_pages_free
) {
1676 * Someone has already activated the emergency paging segment
1678 * Between us having rec'd a NULL segment from ps_select_segment
1679 * and reaching here a new normal segment could have been added.
1680 * E.g. we get NULL segment and another thread just added the
1681 * new swap file. Hence check to see if we have more dp_pages_free
1682 * before activating the emergency segment.
1688 } else if(!IS_PS_OK_TO_USE(ps
) && ps
->ps_clcount
) {
1690 * PS_CAN_USE is only reset from the emergency segment when it's
1691 * been successfully recovered. So it's legal to have an emergency
1692 * segment that has PS_CAN_USE but no clusters because it's recovery
1695 backing_store_t bs
= ps
->ps_bs
;
1696 ps
->ps_state
|= PS_CAN_USE
;
1697 if(ps_select_array
[bs
->bs_priority
] == BS_FULLPRI
||
1698 ps_select_array
[bs
->bs_priority
] == BS_NOPRI
) {
1699 ps_select_array
[bs
->bs_priority
] = 0;
1701 dp_pages_free
+= ps
->ps_pgcount
;
1702 dp_pages_reserve
-= ps
->ps_pgcount
;
1705 dprintf(("Switching ON Emergency paging segment\n"));
1715 * Emit a notification of the low-paging resource condition
1716 * but don't issue it more than once every five seconds. This
1717 * prevents us from overflowing logs with thousands of
1718 * repetitions of the message.
1720 clock_get_system_nanotime(&now
, &nanoseconds_dummy
);
1721 if (paging_segment_count
> 1 && (now
> lastnotify
+ 5)) {
1722 /* With an activated emergency paging segment we still
1723 * didn't get any clusters. This could mean that the
1724 * emergency paging segment is exhausted.
1726 dprintf(("System is out of paging space.\n"));
1732 if(min_pages_trigger_port
) {
1733 trigger
= min_pages_trigger_port
;
1734 min_pages_trigger_port
= NULL
;
1736 backing_store_abort_compaction
= TRUE
;
1739 if (trigger
!= IP_NULL
) {
1740 dprintf(("ps_allocate_cluster - send HI_WAT_ALERT\n"));
1742 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1743 ipc_port_release_send(trigger
);
1745 return (dp_offset_t
) -1;
1749 * Look for an available cluster. At the end of the loop,
1750 * byte_num is the byte offset and bit_num is the bit offset of the
1751 * first zero bit in the paging segment bitmap.
1754 byte_num
= ps
->ps_hint
;
1755 for (; byte_num
< howmany(ps
->ps_ncls
, NBBY
); byte_num
++) {
1756 if (*(ps
->ps_bmap
+ byte_num
) != BYTEMASK
) {
1757 for (bit_num
= 0; bit_num
< NBBY
; bit_num
++) {
1758 if (isclr((ps
->ps_bmap
+ byte_num
), bit_num
))
1761 ASSERT(bit_num
!= NBBY
);
1765 ps
->ps_hint
= byte_num
;
1766 cluster
= (byte_num
*NBBY
) + bit_num
;
1768 /* Space was reserved, so this must be true */
1769 ASSERT(cluster
< ps
->ps_ncls
);
1771 setbit(ps
->ps_bmap
, cluster
);
1777 void ps_deallocate_cluster(paging_segment_t
, dp_offset_t
); /* forward */
1780 ps_deallocate_cluster(
1781 paging_segment_t ps
,
1782 dp_offset_t cluster
)
1785 if (cluster
>= ps
->ps_ncls
)
1786 panic("ps_deallocate_cluster: Invalid cluster number");
1789 * Lock the paging segment, clear the cluster's bitmap and increment the
1790 * number of free cluster.
1794 clrbit(ps
->ps_bmap
, cluster
);
1795 if( IS_PS_OK_TO_USE(ps
)) {
1797 ps
->ps_pgcount
+= 1 << ps
->ps_clshift
;
1798 dp_pages_free
+= 1 << ps
->ps_clshift
;
1800 ps
->ps_special_clusters
+= 1;
1804 * Move the hint down to the freed cluster if it is
1805 * less than the current hint.
1807 if ((cluster
/NBBY
) < ps
->ps_hint
) {
1808 ps
->ps_hint
= (cluster
/NBBY
);
1813 * If we're freeing space on a full priority, reset the array.
1815 if ( IS_PS_OK_TO_USE(ps
) && ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
)
1816 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
1823 void ps_dealloc_vsmap(struct vs_map
*, dp_size_t
); /* forward */
1827 struct vs_map
*vsmap
,
1831 struct ps_vnode_trim_data trim_data
;
1833 ps_vnode_trim_init(&trim_data
);
1835 for (i
= 0; i
< size
; i
++) {
1836 if (!VSM_ISCLR(vsmap
[i
]) && !VSM_ISERR(vsmap
[i
])) {
1837 ps_vnode_trim_more(&trim_data
,
1839 VSM_PS(vsmap
[i
])->ps_clshift
,
1840 vm_page_size
<< VSM_PS(vsmap
[i
])->ps_clshift
);
1841 ps_deallocate_cluster(VSM_PS(vsmap
[i
]),
1842 VSM_CLOFF(vsmap
[i
]));
1844 ps_vnode_trim_now(&trim_data
);
1847 ps_vnode_trim_now(&trim_data
);
1860 * If this is an indirect structure, then we walk through the valid
1861 * (non-zero) indirect pointers and deallocate the clusters
1862 * associated with each used map entry (via ps_dealloc_vsmap).
1863 * When all of the clusters in an indirect block have been
1864 * freed, we deallocate the block. When all of the indirect
1865 * blocks have been deallocated we deallocate the memory
1866 * holding the indirect pointers.
1868 if (vs
->vs_indirect
) {
1869 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
1870 if (vs
->vs_imap
[i
] != NULL
) {
1871 ps_dealloc_vsmap(vs
->vs_imap
[i
], CLMAP_ENTRIES
);
1872 kfree(vs
->vs_imap
[i
], CLMAP_THRESHOLD
);
1875 kfree(vs
->vs_imap
, INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1878 * Direct map. Free used clusters, then memory.
1880 ps_dealloc_vsmap(vs
->vs_dmap
, vs
->vs_size
);
1881 kfree(vs
->vs_dmap
, CLMAP_SIZE(vs
->vs_size
));
1885 bs_commit(- vs
->vs_size
);
1887 VS_MAP_LOCK_DESTROY(vs
);
1889 zfree(vstruct_zone
, vs
);
1895 boolean_t return_to_vm
,
1896 boolean_t reclaim_backing_store
)
1899 struct vs_map
*vsmap
;
1900 boolean_t vsmap_all_clear
, vsimap_all_clear
;
1901 struct vm_object_fault_info fault_info
;
1903 unsigned int vsmap_size
;
1908 fault_info
.cluster_size
= VM_SUPER_CLUSTER
;
1909 fault_info
.behavior
= VM_BEHAVIOR_SEQUENTIAL
;
1910 fault_info
.user_tag
= 0;
1911 fault_info
.lo_offset
= 0;
1912 fault_info
.hi_offset
= ptoa_32(vs
->vs_size
<< vs
->vs_clshift
);
1913 fault_info
.io_sync
= reclaim_backing_store
;
1914 fault_info
.batch_pmap_op
= FALSE
;
1917 * If this is an indirect structure, then we walk through the valid
1918 * (non-zero) indirect pointers and deallocate the clusters
1919 * associated with each used map entry (via ps_dealloc_vsmap).
1920 * When all of the clusters in an indirect block have been
1921 * freed, we deallocate the block. When all of the indirect
1922 * blocks have been deallocated we deallocate the memory
1923 * holding the indirect pointers.
1925 if (vs
->vs_indirect
) {
1926 vsimap_all_clear
= TRUE
;
1927 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
1928 vsmap
= vs
->vs_imap
[i
];
1931 /* loop on clusters in this indirect map */
1932 clmap_off
= (vm_page_size
* CLMAP_ENTRIES
*
1934 if (i
+1 == INDIRECT_CLMAP_ENTRIES(vs
->vs_size
))
1935 vsmap_size
= vs
->vs_size
- (CLMAP_ENTRIES
* i
);
1937 vsmap_size
= CLMAP_ENTRIES
;
1938 vsmap_all_clear
= TRUE
;
1940 for (j
= 0; j
< vsmap_size
;) {
1941 if (VSM_ISCLR(vsmap
[j
]) ||
1942 VSM_ISERR(vsmap
[j
])) {
1944 clmap_off
+= vm_page_size
* VSCLSIZE(vs
);
1948 kr
= pvs_cluster_read(
1951 (dp_size_t
) -1, /* read whole cluster */
1953 VS_MAP_LOCK(vs
); /* XXX what if it changed ? */
1954 if (kr
!= KERN_SUCCESS
) {
1955 vsmap_all_clear
= FALSE
;
1956 vsimap_all_clear
= FALSE
;
1960 if (vsmap_all_clear
) {
1961 ps_dealloc_vsmap(vsmap
, CLMAP_ENTRIES
);
1962 kfree(vsmap
, CLMAP_THRESHOLD
);
1963 vs
->vs_imap
[i
] = NULL
;
1966 if (vsimap_all_clear
) {
1967 // kfree(vs->vs_imap, INDIRECT_CLMAP_SIZE(vs->vs_size));
1971 * Direct map. Free used clusters, then memory.
1973 vsmap
= vs
->vs_dmap
;
1974 if (vsmap
== NULL
) {
1977 vsmap_all_clear
= TRUE
;
1978 /* loop on clusters in the direct map */
1980 for (j
= 0; j
< vs
->vs_size
;) {
1981 if (VSM_ISCLR(vsmap
[j
]) ||
1982 VSM_ISERR(vsmap
[j
])) {
1986 clmap_off
= vm_page_size
* (j
<< vs
->vs_clshift
);
1988 kr
= pvs_cluster_read(
1991 (dp_size_t
) -1, /* read whole cluster */
1993 VS_MAP_LOCK(vs
); /* XXX what if it changed ? */
1994 if (kr
!= KERN_SUCCESS
) {
1995 vsmap_all_clear
= FALSE
;
1997 // VSM_CLR(vsmap[j]);
2001 if (vsmap_all_clear
) {
2002 ps_dealloc_vsmap(vs
->vs_dmap
, vs
->vs_size
);
2003 // kfree(vs->vs_dmap, CLMAP_SIZE(vs->vs_size));
2010 int ps_map_extend(vstruct_t
, unsigned int); /* forward */
2014 unsigned int new_size
)
2016 struct vs_map
**new_imap
;
2017 struct vs_map
*new_dmap
= NULL
;
2020 void *old_map
= NULL
;
2021 int old_map_size
= 0;
2023 if (vs
->vs_size
>= new_size
) {
2025 * Someone has already done the work.
2031 * If the new size extends into the indirect range, then we have one
2032 * of two cases: we are going from indirect to indirect, or we are
2033 * going from direct to indirect. If we are going from indirect to
2034 * indirect, then it is possible that the new size will fit in the old
2035 * indirect map. If this is the case, then just reset the size of the
2036 * vstruct map and we are done. If the new size will not
2037 * fit into the old indirect map, then we have to allocate a new
2038 * indirect map and copy the old map pointers into this new map.
2040 * If we are going from direct to indirect, then we have to allocate a
2041 * new indirect map and copy the old direct pages into the first
2042 * indirect page of the new map.
2043 * NOTE: allocating memory here is dangerous, as we're in the
2046 if (INDIRECT_CLMAP(new_size
)) {
2047 int new_map_size
= INDIRECT_CLMAP_SIZE(new_size
);
2050 * Get a new indirect map and zero it.
2052 old_map_size
= INDIRECT_CLMAP_SIZE(vs
->vs_size
);
2053 if (vs
->vs_indirect
&&
2054 (new_map_size
== old_map_size
)) {
2055 bs_commit(new_size
- vs
->vs_size
);
2056 vs
->vs_size
= new_size
;
2060 new_imap
= (struct vs_map
**)kalloc(new_map_size
);
2061 if (new_imap
== NULL
) {
2064 memset(new_imap
, 0, new_map_size
);
2066 if (vs
->vs_indirect
) {
2067 /* Copy old entries into new map */
2068 memcpy(new_imap
, vs
->vs_imap
, old_map_size
);
2069 /* Arrange to free the old map */
2070 old_map
= (void *) vs
->vs_imap
;
2072 } else { /* Old map was a direct map */
2073 /* Allocate an indirect page */
2074 if ((new_imap
[0] = (struct vs_map
*)
2075 kalloc(CLMAP_THRESHOLD
)) == NULL
) {
2076 kfree(new_imap
, new_map_size
);
2079 new_dmap
= new_imap
[0];
2080 newdsize
= CLMAP_ENTRIES
;
2084 newdsize
= new_size
;
2086 * If the new map is a direct map, then the old map must
2087 * also have been a direct map. All we have to do is
2088 * to allocate a new direct map, copy the old entries
2089 * into it and free the old map.
2091 if ((new_dmap
= (struct vs_map
*)
2092 kalloc(CLMAP_SIZE(new_size
))) == NULL
) {
2098 /* Free the old map */
2099 old_map
= (void *) vs
->vs_dmap
;
2100 old_map_size
= CLMAP_SIZE(vs
->vs_size
);
2102 /* Copy info from the old map into the new map */
2103 memcpy(new_dmap
, vs
->vs_dmap
, old_map_size
);
2105 /* Initialize the rest of the new map */
2106 for (i
= vs
->vs_size
; i
< newdsize
; i
++)
2107 VSM_CLR(new_dmap
[i
]);
2110 vs
->vs_imap
= new_imap
;
2111 vs
->vs_indirect
= TRUE
;
2113 vs
->vs_dmap
= new_dmap
;
2114 bs_commit(new_size
- vs
->vs_size
);
2115 vs
->vs_size
= new_size
;
2117 kfree(old_map
, old_map_size
);
2125 struct clmap
*clmap
,
2130 dp_offset_t cluster
; /* The cluster of offset. */
2131 dp_offset_t newcl
; /* The new cluster allocated. */
2134 struct vs_map
*vsmap
;
2138 ASSERT(vs
->vs_dmap
);
2139 cluster
= atop_32(offset
) >> vs
->vs_clshift
;
2142 * Initialize cluster error value
2144 clmap
->cl_error
= 0;
2147 * If the object has grown, extend the page map.
2149 if (cluster
>= vs
->vs_size
) {
2150 if (flag
== CL_FIND
) {
2151 /* Do not allocate if just doing a lookup */
2153 return (dp_offset_t
) -1;
2155 if (ps_map_extend(vs
, cluster
+ 1)) {
2157 return (dp_offset_t
) -1;
2162 * Look for the desired cluster. If the map is indirect, then we
2163 * have a two level lookup. First find the indirect block, then
2164 * find the actual cluster. If the indirect block has not yet
2165 * been allocated, then do so. If the cluster has not yet been
2166 * allocated, then do so.
2168 * If any of the allocations fail, then return an error.
2169 * Don't allocate if just doing a lookup.
2171 if (vs
->vs_indirect
) {
2172 long ind_block
= cluster
/CLMAP_ENTRIES
;
2174 /* Is the indirect block allocated? */
2175 vsmap
= vs
->vs_imap
[ind_block
];
2176 if (vsmap
== NULL
) {
2177 if (flag
== CL_FIND
) {
2179 return (dp_offset_t
) -1;
2182 /* Allocate the indirect block */
2183 vsmap
= (struct vs_map
*) kalloc(CLMAP_THRESHOLD
);
2184 if (vsmap
== NULL
) {
2186 return (dp_offset_t
) -1;
2188 /* Initialize the cluster offsets */
2189 for (i
= 0; i
< CLMAP_ENTRIES
; i
++)
2191 vs
->vs_imap
[ind_block
] = vsmap
;
2194 vsmap
= vs
->vs_dmap
;
2197 vsmap
+= cluster%CLMAP_ENTRIES
;
2200 * At this point, vsmap points to the struct vs_map desired.
2202 * Look in the map for the cluster, if there was an error on a
2203 * previous write, flag it and return. If it is not yet
2204 * allocated, then allocate it, if we're writing; if we're
2205 * doing a lookup and the cluster's not allocated, return error.
2207 if (VSM_ISERR(*vsmap
)) {
2208 clmap
->cl_error
= VSM_GETERR(*vsmap
);
2210 return (dp_offset_t
) -1;
2211 } else if (VSM_ISCLR(*vsmap
)) {
2214 if (flag
== CL_FIND
) {
2216 * If there's an error and the entry is clear, then
2217 * we've run out of swap space. Record the error
2221 VSM_SETERR(*vsmap
, error
);
2224 return (dp_offset_t
) -1;
2227 * Attempt to allocate a cluster from the paging segment
2229 newcl
= ps_allocate_cluster(vs
, &psindex
,
2230 PAGING_SEGMENT_NULL
);
2231 if (newcl
== (dp_offset_t
) -1) {
2233 return (dp_offset_t
) -1;
2236 VSM_SETCLOFF(*vsmap
, newcl
);
2237 VSM_SETPS(*vsmap
, psindex
);
2240 newcl
= VSM_CLOFF(*vsmap
);
2243 * Fill in pertinent fields of the clmap
2245 clmap
->cl_ps
= VSM_PS(*vsmap
);
2246 clmap
->cl_numpages
= VSCLSIZE(vs
);
2247 clmap
->cl_bmap
.clb_map
= (unsigned int) VSM_BMAP(*vsmap
);
2250 * Byte offset in paging segment is byte offset to cluster plus
2251 * byte offset within cluster. It looks ugly, but should be
2254 ASSERT(trunc_page(offset
) == offset
);
2255 newcl
= ptoa_32(newcl
) << vs
->vs_clshift
;
2256 newoff
= offset
& ((1<<(vm_page_shift
+ vs
->vs_clshift
)) - 1);
2257 if (flag
== CL_ALLOC
) {
2259 * set bits in the allocation bitmap according to which
2260 * pages were requested. size is in bytes.
2262 i
= atop_32(newoff
);
2263 while ((size
> 0) && (i
< VSCLSIZE(vs
))) {
2264 VSM_SETALLOC(*vsmap
, i
);
2266 size
-= vm_page_size
;
2269 clmap
->cl_alloc
.clb_map
= (unsigned int) VSM_ALLOC(*vsmap
);
2272 * Offset is not cluster aligned, so number of pages
2273 * and bitmaps must be adjusted
2275 clmap
->cl_numpages
-= atop_32(newoff
);
2276 CLMAP_SHIFT(clmap
, vs
);
2277 CLMAP_SHIFTALLOC(clmap
, vs
);
2282 * The setting of valid bits and handling of write errors
2283 * must be done here, while we hold the lock on the map.
2284 * It logically should be done in ps_vs_write_complete().
2285 * The size and error information has been passed from
2286 * ps_vs_write_complete(). If the size parameter is non-zero,
2287 * then there is work to be done. If error is also non-zero,
2288 * then the error number is recorded in the cluster and the
2289 * entire cluster is in error.
2291 if (size
&& flag
== CL_FIND
) {
2292 dp_offset_t off
= (dp_offset_t
) 0;
2295 for (i
= VSCLSIZE(vs
) - clmap
->cl_numpages
; size
> 0;
2297 VSM_SETPG(*vsmap
, i
);
2298 size
-= vm_page_size
;
2300 ASSERT(i
<= VSCLSIZE(vs
));
2302 BS_STAT(clmap
->cl_ps
->ps_bs
,
2303 clmap
->cl_ps
->ps_bs
->bs_pages_out_fail
+=
2305 off
= VSM_CLOFF(*vsmap
);
2306 VSM_SETERR(*vsmap
, error
);
2309 * Deallocate cluster if error, and no valid pages
2312 if (off
!= (dp_offset_t
) 0)
2313 ps_deallocate_cluster(clmap
->cl_ps
, off
);
2315 return (dp_offset_t
) 0;
2319 DP_DEBUG(DEBUG_VS_INTERNAL
,
2320 ("returning 0x%X,vs=0x%X,vsmap=0x%X,flag=%d\n",
2321 newcl
+newoff
, (int) vs
, (int) vsmap
, flag
));
2322 DP_DEBUG(DEBUG_VS_INTERNAL
,
2323 (" clmap->cl_ps=0x%X,cl_numpages=%d,clbmap=0x%x,cl_alloc=%x\n",
2324 (int) clmap
->cl_ps
, clmap
->cl_numpages
,
2325 (int) clmap
->cl_bmap
.clb_map
, (int) clmap
->cl_alloc
.clb_map
));
2327 return (newcl
+ newoff
);
2330 void ps_clunmap(vstruct_t
, dp_offset_t
, dp_size_t
); /* forward */
2338 dp_offset_t cluster
; /* The cluster number of offset */
2339 struct vs_map
*vsmap
;
2340 struct ps_vnode_trim_data trim_data
;
2342 ps_vnode_trim_init(&trim_data
);
2347 * Loop through all clusters in this range, freeing paging segment
2348 * clusters and map entries as encountered.
2350 while (length
> 0) {
2354 cluster
= atop_32(offset
) >> vs
->vs_clshift
;
2355 if (vs
->vs_indirect
) /* indirect map */
2356 vsmap
= vs
->vs_imap
[cluster
/CLMAP_ENTRIES
];
2358 vsmap
= vs
->vs_dmap
;
2359 if (vsmap
== NULL
) {
2360 ps_vnode_trim_now(&trim_data
);
2364 vsmap
+= cluster%CLMAP_ENTRIES
;
2365 if (VSM_ISCLR(*vsmap
)) {
2366 ps_vnode_trim_now(&trim_data
);
2367 length
-= vm_page_size
;
2368 offset
+= vm_page_size
;
2372 * We've got a valid mapping. Clear it and deallocate
2373 * paging segment cluster pages.
2374 * Optimize for entire cluster cleraing.
2376 if ( (newoff
= (offset
&((1<<(vm_page_shift
+vs
->vs_clshift
))-1))) ) {
2378 * Not cluster aligned.
2380 ASSERT(trunc_page(newoff
) == newoff
);
2381 i
= atop_32(newoff
);
2384 while ((i
< VSCLSIZE(vs
)) && (length
> 0)) {
2385 VSM_CLRPG(*vsmap
, i
);
2386 VSM_CLRALLOC(*vsmap
, i
);
2387 length
-= vm_page_size
;
2388 offset
+= vm_page_size
;
2393 * If map entry is empty, clear and deallocate cluster.
2395 if (!VSM_BMAP(*vsmap
)) {
2396 ps_vnode_trim_more(&trim_data
,
2399 VSCLSIZE(vs
) * vm_page_size
);
2400 ps_deallocate_cluster(VSM_PS(*vsmap
),
2404 ps_vnode_trim_now(&trim_data
);
2407 ps_vnode_trim_now(&trim_data
);
2412 void ps_vs_write_complete(vstruct_t
, dp_offset_t
, dp_size_t
, int); /* forward */
2415 ps_vs_write_complete(
2424 * Get the struct vsmap for this cluster.
2425 * Use READ, even though it was written, because the
2426 * cluster MUST be present, unless there was an error
2427 * in the original ps_clmap (e.g. no space), in which
2428 * case, nothing happens.
2430 * Must pass enough information to ps_clmap to allow it
2431 * to set the vs_map structure bitmap under lock.
2433 (void) ps_clmap(vs
, offset
, &clmap
, CL_FIND
, size
, error
);
2436 void vs_cl_write_complete(vstruct_t
, paging_segment_t
, dp_offset_t
, vm_offset_t
, dp_size_t
, boolean_t
, int); /* forward */
2439 vs_cl_write_complete(
2441 __unused paging_segment_t ps
,
2443 __unused vm_offset_t addr
,
2448 // kern_return_t kr;
2452 * For internal objects, the error is recorded on a
2453 * per-cluster basis by ps_clmap() which is called
2454 * by ps_vs_write_complete() below.
2456 dprintf(("write failed error = 0x%x\n", error
));
2457 /* add upl_abort code here */
2459 GSTAT(global_stats
.gs_pages_out
+= atop_32(size
));
2461 * Notify the vstruct mapping code, so it can do its accounting.
2463 ps_vs_write_complete(vs
, offset
, size
, error
);
2467 ASSERT(vs
->vs_async_pending
> 0);
2468 vs
->vs_async_pending
-= size
;
2469 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
2470 vs
->vs_waiting_async
= FALSE
;
2472 thread_wakeup(&vs
->vs_async_pending
);
2479 #ifdef DEVICE_PAGING
2480 kern_return_t
device_write_reply(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2484 MACH_PORT_FACE reply_port
,
2485 kern_return_t device_code
,
2486 io_buf_len_t bytes_written
)
2488 struct vs_async
*vsa
;
2490 vsa
= (struct vs_async
*)
2491 ((struct vstruct_alias
*)(reply_port
->alias
))->vs
;
2493 if (device_code
== KERN_SUCCESS
&& bytes_written
!= vsa
->vsa_size
) {
2494 device_code
= KERN_FAILURE
;
2497 vsa
->vsa_error
= device_code
;
2500 ASSERT(vsa
->vsa_vs
!= VSTRUCT_NULL
);
2501 if(vsa
->vsa_flags
& VSA_TRANSFER
) {
2502 /* revisit when async disk segments redone */
2503 if(vsa
->vsa_error
) {
2504 /* need to consider error condition. re-write data or */
2505 /* throw it away here. */
2506 vm_map_copy_discard((vm_map_copy_t
)vsa
->vsa_addr
);
2508 ps_vs_write_complete(vsa
->vsa_vs
, vsa
->vsa_offset
,
2509 vsa
->vsa_size
, vsa
->vsa_error
);
2511 vs_cl_write_complete(vsa
->vsa_vs
, vsa
->vsa_ps
, vsa
->vsa_offset
,
2512 vsa
->vsa_addr
, vsa
->vsa_size
, TRUE
,
2517 return KERN_SUCCESS
;
2520 kern_return_t
device_write_reply_inband(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2522 device_write_reply_inband(
2523 MACH_PORT_FACE reply_port
,
2524 kern_return_t return_code
,
2525 io_buf_len_t bytes_written
)
2527 panic("device_write_reply_inband: illegal");
2528 return KERN_SUCCESS
;
2531 kern_return_t
device_read_reply(MACH_PORT_FACE
, kern_return_t
, io_buf_ptr_t
, mach_msg_type_number_t
);
2534 MACH_PORT_FACE reply_port
,
2535 kern_return_t return_code
,
2537 mach_msg_type_number_t dataCnt
)
2539 struct vs_async
*vsa
;
2540 vsa
= (struct vs_async
*)
2541 ((struct vstruct_alias
*)(reply_port
->alias
))->vs
;
2542 vsa
->vsa_addr
= (vm_offset_t
)data
;
2543 vsa
->vsa_size
= (vm_size_t
)dataCnt
;
2544 vsa
->vsa_error
= return_code
;
2545 thread_wakeup(&vsa
);
2546 return KERN_SUCCESS
;
2549 kern_return_t
device_read_reply_inband(MACH_PORT_FACE
, kern_return_t
, io_buf_ptr_inband_t
, mach_msg_type_number_t
);
2551 device_read_reply_inband(
2552 MACH_PORT_FACE reply_port
,
2553 kern_return_t return_code
,
2554 io_buf_ptr_inband_t data
,
2555 mach_msg_type_number_t dataCnt
)
2557 panic("device_read_reply_inband: illegal");
2558 return KERN_SUCCESS
;
2561 kern_return_t
device_read_reply_overwrite(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2563 device_read_reply_overwrite(
2564 MACH_PORT_FACE reply_port
,
2565 kern_return_t return_code
,
2566 io_buf_len_t bytes_read
)
2568 panic("device_read_reply_overwrite: illegal\n");
2569 return KERN_SUCCESS
;
2572 kern_return_t
device_open_reply(MACH_PORT_FACE
, kern_return_t
, MACH_PORT_FACE
);
2575 MACH_PORT_FACE reply_port
,
2576 kern_return_t return_code
,
2577 MACH_PORT_FACE device_port
)
2579 panic("device_open_reply: illegal\n");
2580 return KERN_SUCCESS
;
2585 paging_segment_t ps
,
2587 vm_offset_t
*bufferp
,
2589 unsigned int *residualp
,
2593 recnum_t dev_offset
;
2594 unsigned int bytes_wanted
;
2595 unsigned int bytes_read
;
2596 unsigned int total_read
;
2597 vm_offset_t dev_buffer
;
2598 vm_offset_t buf_ptr
;
2599 unsigned int records_read
;
2600 struct vs_async
*vsa
;
2603 vm_map_copy_t device_data
= NULL
;
2604 default_pager_thread_t
*dpt
= NULL
;
2606 device
= dev_port_lookup(ps
->ps_device
);
2607 clustered_reads
[atop_32(size
)]++;
2609 dev_offset
= (ps
->ps_offset
+
2610 (offset
>> (vm_page_shift
- ps
->ps_record_shift
)));
2611 bytes_wanted
= size
;
2613 *bufferp
= (vm_offset_t
)NULL
;
2616 vsa
= VS_ALLOC_ASYNC();
2620 vsa
->vsa_offset
= 0;
2624 ip_lock(vsa
->reply_port
);
2625 vsa
->reply_port
->ip_sorights
++;
2626 ip_reference(vsa
->reply_port
);
2627 ip_unlock(vsa
->reply_port
);
2628 kr
= ds_device_read_common(device
,
2630 (mach_msg_type_name_t
)
2631 MACH_MSG_TYPE_MOVE_SEND_ONCE
,
2635 (IO_READ
| IO_CALL
),
2636 (io_buf_ptr_t
*) &dev_buffer
,
2637 (mach_msg_type_number_t
*) &bytes_read
);
2638 if(kr
== MIG_NO_REPLY
) {
2639 assert_wait(&vsa
, THREAD_UNINT
);
2640 thread_block(THREAD_CONTINUE_NULL
);
2642 dev_buffer
= vsa
->vsa_addr
;
2643 bytes_read
= (unsigned int)vsa
->vsa_size
;
2644 kr
= vsa
->vsa_error
;
2647 if (kr
!= KERN_SUCCESS
|| bytes_read
== 0) {
2650 total_read
+= bytes_read
;
2653 * If we got the entire range, use the returned dev_buffer.
2655 if (bytes_read
== size
) {
2656 *bufferp
= (vm_offset_t
)dev_buffer
;
2661 dprintf(("read only %d bytes out of %d\n",
2662 bytes_read
, bytes_wanted
));
2665 dpt
= get_read_buffer();
2666 buf_ptr
= dpt
->dpt_buffer
;
2667 *bufferp
= (vm_offset_t
)buf_ptr
;
2670 * Otherwise, copy the data into the provided buffer (*bufferp)
2671 * and append the rest of the range as it comes in.
2673 memcpy((void *) buf_ptr
, (void *) dev_buffer
, bytes_read
);
2674 buf_ptr
+= bytes_read
;
2675 bytes_wanted
-= bytes_read
;
2676 records_read
= (bytes_read
>>
2677 (vm_page_shift
- ps
->ps_record_shift
));
2678 dev_offset
+= records_read
;
2679 DP_DEBUG(DEBUG_VS_INTERNAL
,
2680 ("calling vm_deallocate(addr=0x%X,size=0x%X)\n",
2681 dev_buffer
, bytes_read
));
2682 if (vm_deallocate(kernel_map
, dev_buffer
, bytes_read
)
2684 Panic("dealloc buf");
2685 } while (bytes_wanted
);
2687 *residualp
= size
- total_read
;
2688 if((dev_buffer
!= *bufferp
) && (total_read
!= 0)) {
2689 vm_offset_t temp_buffer
;
2690 vm_allocate(kernel_map
, &temp_buffer
, total_read
, VM_FLAGS_ANYWHERE
);
2691 memcpy((void *) temp_buffer
, (void *) *bufferp
, total_read
);
2692 if(vm_map_copyin_page_list(kernel_map
, temp_buffer
, total_read
,
2693 VM_MAP_COPYIN_OPT_SRC_DESTROY
|
2694 VM_MAP_COPYIN_OPT_STEAL_PAGES
|
2695 VM_MAP_COPYIN_OPT_PMAP_ENTER
,
2696 (vm_map_copy_t
*)&device_data
, FALSE
))
2697 panic("ps_read_device: cannot copyin locally provided buffer\n");
2699 else if((kr
== KERN_SUCCESS
) && (total_read
!= 0) && (dev_buffer
!= 0)){
2700 if(vm_map_copyin_page_list(kernel_map
, dev_buffer
, bytes_read
,
2701 VM_MAP_COPYIN_OPT_SRC_DESTROY
|
2702 VM_MAP_COPYIN_OPT_STEAL_PAGES
|
2703 VM_MAP_COPYIN_OPT_PMAP_ENTER
,
2704 (vm_map_copy_t
*)&device_data
, FALSE
))
2705 panic("ps_read_device: cannot copyin backing store provided buffer\n");
2710 *bufferp
= (vm_offset_t
)device_data
;
2713 /* Free the receive buffer */
2714 dpt
->checked_out
= 0;
2715 thread_wakeup(&dpt_array
);
2717 return KERN_SUCCESS
;
2722 paging_segment_t ps
,
2726 struct vs_async
*vsa
)
2728 recnum_t dev_offset
;
2729 io_buf_len_t bytes_to_write
, bytes_written
;
2730 recnum_t records_written
;
2732 MACH_PORT_FACE reply_port
;
2736 clustered_writes
[atop_32(size
)]++;
2738 dev_offset
= (ps
->ps_offset
+
2739 (offset
>> (vm_page_shift
- ps
->ps_record_shift
)));
2740 bytes_to_write
= size
;
2744 * Asynchronous write.
2746 reply_port
= vsa
->reply_port
;
2747 ip_lock(reply_port
);
2748 reply_port
->ip_sorights
++;
2749 ip_reference(reply_port
);
2750 ip_unlock(reply_port
);
2753 device
= dev_port_lookup(ps
->ps_device
);
2755 vsa
->vsa_addr
= addr
;
2756 kr
=ds_device_write_common(device
,
2758 (mach_msg_type_name_t
) MACH_MSG_TYPE_MOVE_SEND_ONCE
,
2761 (io_buf_ptr_t
) addr
,
2763 (IO_WRITE
| IO_CALL
),
2766 if ((kr
!= KERN_SUCCESS
) && (kr
!= MIG_NO_REPLY
)) {
2768 dprintf(("%s0x%x, addr=0x%x,"
2769 "size=0x%x,offset=0x%x\n",
2770 "device_write_request returned ",
2771 kr
, addr
, size
, offset
));
2773 ps
->ps_bs
->bs_pages_out_fail
+= atop_32(size
));
2774 /* do the completion notification to free resources */
2775 device_write_reply(reply_port
, kr
, 0);
2780 * Synchronous write.
2784 device
= dev_port_lookup(ps
->ps_device
);
2785 kr
=ds_device_write_common(device
,
2789 (io_buf_ptr_t
) addr
,
2791 (IO_WRITE
| IO_SYNC
| IO_KERNEL_BUF
),
2794 if (kr
!= KERN_SUCCESS
) {
2795 dprintf(("%s0x%x, addr=0x%x,size=0x%x,offset=0x%x\n",
2796 "device_write returned ",
2797 kr
, addr
, size
, offset
));
2799 ps
->ps_bs
->bs_pages_out_fail
+= atop_32(size
));
2802 if (bytes_written
& ((vm_page_size
>> ps
->ps_record_shift
) - 1))
2803 Panic("fragmented write");
2804 records_written
= (bytes_written
>>
2805 (vm_page_shift
- ps
->ps_record_shift
));
2806 dev_offset
+= records_written
;
2808 if (bytes_written
!= bytes_to_write
) {
2809 dprintf(("wrote only %d bytes out of %d\n",
2810 bytes_written
, bytes_to_write
));
2813 bytes_to_write
-= bytes_written
;
2814 addr
+= bytes_written
;
2815 } while (bytes_to_write
> 0);
2817 return PAGER_SUCCESS
;
2821 #else /* !DEVICE_PAGING */
2825 __unused paging_segment_t ps
,
2826 __unused dp_offset_t offset
,
2827 __unused vm_offset_t
*bufferp
,
2828 __unused
unsigned int size
,
2829 __unused
unsigned int *residualp
,
2832 panic("ps_read_device not supported");
2833 return KERN_FAILURE
;
2838 __unused paging_segment_t ps
,
2839 __unused dp_offset_t offset
,
2840 __unused vm_offset_t addr
,
2841 __unused
unsigned int size
,
2842 __unused
struct vs_async
*vsa
)
2844 panic("ps_write_device not supported");
2845 return KERN_FAILURE
;
2848 #endif /* DEVICE_PAGING */
2849 void pvs_object_data_provided(vstruct_t
, upl_t
, upl_offset_t
, upl_size_t
); /* forward */
2852 pvs_object_data_provided(
2853 __unused vstruct_t vs
,
2855 __unused upl_offset_t offset
,
2859 DP_DEBUG(DEBUG_VS_INTERNAL
,
2860 ("buffer=0x%x,offset=0x%x,size=0x%x\n",
2861 upl
, offset
, size
));
2864 GSTAT(global_stats
.gs_pages_in
+= atop_32(size
));
2866 /* check upl iosync flag instead of using RECLAIM_SWAP*/
2868 if (size
!= upl
->size
) {
2869 upl_abort(upl
, UPL_ABORT_ERROR
);
2870 upl_deallocate(upl
);
2872 ps_clunmap(vs
, offset
, size
);
2873 upl_commit(upl
, NULL
, 0);
2874 upl_deallocate(upl
);
2876 #endif /* RECLAIM_SWAP */
2880 static memory_object_offset_t last_start
;
2881 static vm_size_t last_length
;
2884 * A "cnt" of 0 means that the caller just wants to check if the page at
2885 * offset "vs_offset" exists in the backing store. That page hasn't been
2886 * prepared, so no need to release it.
2888 * A "cnt" of -1 means that the caller wants to bring back from the backing
2889 * store all existing pages in the cluster containing "vs_offset".
2894 dp_offset_t vs_offset
,
2898 kern_return_t error
= KERN_SUCCESS
;
2900 unsigned int residual
;
2901 unsigned int request_flags
;
2908 unsigned int xfer_size
;
2909 dp_offset_t orig_vs_offset
;
2910 dp_offset_t ps_offset
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_MIN_CLSHIFT
];
2911 paging_segment_t psp
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_MIN_CLSHIFT
];
2914 unsigned int page_list_count
;
2915 memory_object_offset_t cluster_start
;
2916 vm_size_t cluster_length
;
2917 uint32_t io_streaming
;
2919 boolean_t io_sync
= FALSE
;
2921 pages_in_cl
= 1 << vs
->vs_clshift
;
2922 cl_size
= pages_in_cl
* vm_page_size
;
2923 cl_mask
= cl_size
- 1;
2925 request_flags
= UPL_NO_SYNC
| UPL_RET_ONLY_ABSENT
| UPL_SET_LITE
;
2927 if (cnt
== (dp_size_t
) -1) {
2929 * We've been called from ps_vstruct_reclaim() to move all
2930 * the object's swapped pages back to VM pages.
2931 * This can put memory pressure on the system, so we do want
2932 * to wait for free pages, to avoid getting in the way of the
2933 * vm_pageout_scan() thread.
2934 * Let's not use UPL_NOBLOCK in this case.
2936 vs_offset
&= ~cl_mask
;
2942 * if the I/O cluster size == PAGE_SIZE, we don't want to set
2943 * the UPL_NOBLOCK since we may be trying to recover from a
2944 * previous partial pagein I/O that occurred because we were low
2945 * on memory and bailed early in order to honor the UPL_NOBLOCK...
2946 * since we're only asking for a single page, we can block w/o fear
2947 * of tying up pages while waiting for more to become available
2949 if (fault_info
== NULL
|| ((vm_object_fault_info_t
)fault_info
)->cluster_size
> PAGE_SIZE
)
2950 request_flags
|= UPL_NOBLOCK
;
2954 cl_index
= (vs_offset
& cl_mask
) / vm_page_size
;
2956 if ((ps_clmap(vs
, vs_offset
& ~cl_mask
, &clmap
, CL_FIND
, 0, 0) == (dp_offset_t
)-1) ||
2957 !CLMAP_ISSET(clmap
, cl_index
)) {
2959 * the needed page doesn't exist in the backing store...
2960 * we don't want to try to do any I/O, just abort the
2961 * page and let the fault handler provide a zero-fill
2965 * The caller was just poking at us to see if
2966 * the page has been paged out. No need to
2967 * mess with the page at all.
2968 * Just let the caller know we don't have that page.
2970 return KERN_FAILURE
;
2972 if (cnt
== (dp_size_t
) -1) {
2975 /* no more pages in this cluster */
2976 return KERN_FAILURE
;
2978 /* try the next page in this cluster */
2979 vs_offset
+= vm_page_size
;
2983 page_list_count
= 0;
2985 memory_object_super_upl_request(vs
->vs_control
, (memory_object_offset_t
)vs_offset
,
2986 PAGE_SIZE
, PAGE_SIZE
,
2987 &upl
, NULL
, &page_list_count
,
2988 request_flags
| UPL_SET_INTERNAL
);
2989 upl_range_needed(upl
, 0, 1);
2992 upl_abort(upl
, UPL_ABORT_ERROR
);
2994 upl_abort(upl
, UPL_ABORT_UNAVAILABLE
);
2995 upl_deallocate(upl
);
2997 return KERN_SUCCESS
;
3002 * The caller was just poking at us to see if
3003 * the page has been paged out. No need to
3004 * mess with the page at all.
3005 * Just let the caller know we do have that page.
3007 return KERN_SUCCESS
;
3010 if(((vm_object_fault_info_t
)fault_info
)->io_sync
== TRUE
) {
3015 #endif /* RECLAIM_SWAP */
3018 if( io_sync
== TRUE
) {
3020 io_flags
|= UPL_IOSYNC
| UPL_NOCOMMIT
;
3022 request_flags
|= UPL_PRECIOUS
| UPL_CLEAN_IN_PLACE
;
3023 #else /* USE_PRECIOUS */
3024 request_flags
|= UPL_REQUEST_SET_DIRTY
;
3025 #endif /* USE_PRECIOUS */
3028 assert(dp_encryption_inited
);
3029 if (dp_encryption
) {
3032 * request that the UPL be prepared for
3035 request_flags
|= UPL_ENCRYPT
;
3036 io_flags
|= UPL_PAGING_ENCRYPTED
;
3038 orig_vs_offset
= vs_offset
;
3041 cnt
= VM_SUPER_CLUSTER
;
3042 cluster_start
= (memory_object_offset_t
) vs_offset
;
3043 cluster_length
= (vm_size_t
) cnt
;
3047 * determine how big a speculative I/O we should try for...
3049 if (memory_object_cluster_size(vs
->vs_control
, &cluster_start
, &cluster_length
, &io_streaming
, (memory_object_fault_info_t
)fault_info
) == KERN_SUCCESS
) {
3050 assert(vs_offset
>= (dp_offset_t
) cluster_start
&&
3051 vs_offset
< (dp_offset_t
) (cluster_start
+ cluster_length
));
3052 vs_offset
= (dp_offset_t
) cluster_start
;
3053 cnt
= (dp_size_t
) cluster_length
;
3055 cluster_length
= PAGE_SIZE
;
3060 io_flags
|= UPL_IOSTREAMING
;
3062 last_start
= cluster_start
;
3063 last_length
= cluster_length
;
3066 * This loop will be executed multiple times until the entire
3067 * range has been looked at or we issue an I/O... if the request spans cluster
3068 * boundaries, the clusters will be checked for logical continunity,
3069 * if contiguous the I/O request will span multiple clusters...
3070 * at most only 1 I/O will be issued... it will encompass the original offset
3072 while (cnt
&& error
== KERN_SUCCESS
) {
3075 if ((vs_offset
& cl_mask
) && (cnt
> (VM_SUPER_CLUSTER
- (vs_offset
& cl_mask
)))) {
3076 size
= VM_SUPER_CLUSTER
;
3077 size
-= vs_offset
& cl_mask
;
3078 } else if (cnt
> VM_SUPER_CLUSTER
)
3079 size
= VM_SUPER_CLUSTER
;
3088 while (size
> 0 && error
== KERN_SUCCESS
) {
3089 unsigned int abort_size
;
3093 dp_offset_t cur_offset
;
3095 if ( !ps_info_valid
) {
3096 ps_offset
[seg_index
] = ps_clmap(vs
, vs_offset
& ~cl_mask
, &clmap
, CL_FIND
, 0, 0);
3097 psp
[seg_index
] = CLMAP_PS(clmap
);
3101 * skip over unallocated physical segments
3103 if (ps_offset
[seg_index
] == (dp_offset_t
) -1) {
3104 abort_size
= cl_size
- (vs_offset
& cl_mask
);
3105 abort_size
= MIN(abort_size
, size
);
3108 vs_offset
+= abort_size
;
3115 cl_index
= (vs_offset
& cl_mask
) / vm_page_size
;
3117 for (abort_size
= 0; cl_index
< pages_in_cl
&& abort_size
< size
; cl_index
++) {
3119 * skip over unallocated pages
3121 if (CLMAP_ISSET(clmap
, cl_index
))
3123 abort_size
+= vm_page_size
;
3127 vs_offset
+= abort_size
;
3129 if (cl_index
== pages_in_cl
) {
3131 * if we're at the end of this physical cluster
3132 * then bump to the next one and continue looking
3143 * remember the starting point of the first allocated page
3144 * for the I/O we're about to issue
3146 beg_pseg
= seg_index
;
3147 beg_indx
= cl_index
;
3148 cur_offset
= vs_offset
;
3151 * calculate the size of the I/O that we can do...
3152 * this may span multiple physical segments if
3153 * they are contiguous
3155 for (xfer_size
= 0; xfer_size
< size
; ) {
3157 while (cl_index
< pages_in_cl
&& xfer_size
< size
) {
3159 * accumulate allocated pages within
3160 * a physical segment
3162 if (CLMAP_ISSET(clmap
, cl_index
)) {
3163 xfer_size
+= vm_page_size
;
3164 cur_offset
+= vm_page_size
;
3167 BS_STAT(psp
[seg_index
]->ps_bs
,
3168 psp
[seg_index
]->ps_bs
->bs_pages_in
++);
3172 if (cl_index
< pages_in_cl
|| xfer_size
>= size
) {
3174 * we've hit an unallocated page or
3175 * the end of this request... see if
3176 * it's time to fire the I/O
3181 * we've hit the end of the current physical
3182 * segment and there's more to do, so try
3183 * moving to the next one
3187 ps_offset
[seg_index
] = ps_clmap(vs
, cur_offset
& ~cl_mask
, &clmap
, CL_FIND
, 0, 0);
3188 psp
[seg_index
] = CLMAP_PS(clmap
);
3191 if ((ps_offset
[seg_index
- 1] != (ps_offset
[seg_index
] - cl_size
)) || (psp
[seg_index
- 1] != psp
[seg_index
])) {
3193 * if the physical segment we're about
3194 * to step into is not contiguous to
3195 * the one we're currently in, or it's
3196 * in a different paging file, or
3197 * it hasn't been allocated....
3198 * we stop this run and go check
3199 * to see if it's time to fire the I/O
3204 * start with first page of the next physical
3209 if (xfer_size
== 0) {
3211 * no I/O to generate for this segment
3215 if (cur_offset
<= orig_vs_offset
) {
3217 * we've hit a hole in our speculative cluster
3218 * before the offset that we're really after...
3219 * don't issue the I/O since it doesn't encompass
3220 * the original offset and we're looking to only
3221 * pull in the speculative pages if they can be
3222 * made part of a single I/O
3225 vs_offset
+= xfer_size
;
3230 * we have a contiguous range of allocated pages
3231 * to read from that encompasses the original offset
3233 page_list_count
= 0;
3234 memory_object_super_upl_request(vs
->vs_control
, (memory_object_offset_t
)vs_offset
,
3235 xfer_size
, xfer_size
,
3236 &upl
, NULL
, &page_list_count
,
3237 request_flags
| UPL_SET_INTERNAL
);
3239 error
= ps_read_file(psp
[beg_pseg
],
3240 upl
, (upl_offset_t
) 0,
3241 ps_offset
[beg_pseg
] + (beg_indx
* vm_page_size
),
3242 xfer_size
, &residual
, io_flags
);
3247 * Adjust counts and send response to VM. Optimize
3248 * for the common case, i.e. no error and/or partial
3249 * data. If there was an error, then we need to error
3250 * the entire range, even if some data was successfully
3251 * read. If there was a partial read we may supply some
3252 * data and may error some as well. In all cases the
3253 * VM must receive some notification for every page
3256 if ((error
== KERN_SUCCESS
) && (residual
== 0)) {
3258 * Got everything we asked for, supply the data
3259 * to the VM. Note that as a side effect of
3260 * supplying the data, the buffer holding the
3261 * supplied data is deallocated from the pager's
3264 pvs_object_data_provided(vs
, upl
, vs_offset
, xfer_size
);
3266 failed_size
= xfer_size
;
3268 if (error
== KERN_SUCCESS
) {
3269 if (residual
== xfer_size
) {
3271 * If a read operation returns no error
3272 * and no data moved, we turn it into
3273 * an error, assuming we're reading at
3275 * Fall through and error the entire range.
3277 error
= KERN_FAILURE
;
3280 * Otherwise, we have partial read. If
3281 * the part read is a integral number
3282 * of pages supply it. Otherwise round
3283 * it up to a page boundary, zero fill
3284 * the unread part, and supply it.
3285 * Fall through and error the remainder
3286 * of the range, if any.
3291 fill
= residual
& ~vm_page_size
;
3292 lsize
= (xfer_size
- residual
) + fill
;
3294 pvs_object_data_provided(vs
, upl
, vs_offset
, lsize
);
3296 if (lsize
< xfer_size
) {
3297 failed_size
= xfer_size
- lsize
;
3298 error
= KERN_FAILURE
;
3303 if (error
!= KERN_SUCCESS
) {
3305 * There was an error in some part of the range, tell
3306 * the VM. Note that error is explicitly checked again
3307 * since it can be modified above.
3309 BS_STAT(psp
[beg_pseg
]->ps_bs
,
3310 psp
[beg_pseg
]->ps_bs
->bs_pages_in_fail
+= atop_32(failed_size
));
3313 * we've issued a single I/O that encompassed the original offset
3314 * at this point we either met our speculative request length or
3315 * we ran into a 'hole' (i.e. page not present in the cluster, cluster
3316 * not present or not physically contiguous to the previous one), so
3317 * we're done issuing I/O at this point
3325 int vs_do_async_write
= 1;
3331 upl_offset_t offset
,
3333 boolean_t dp_internal
,
3336 upl_size_t transfer_size
;
3340 dp_offset_t actual_offset
; /* Offset within paging segment */
3341 paging_segment_t ps
;
3342 dp_offset_t mobj_base_addr
;
3343 dp_offset_t mobj_target_addr
;
3346 upl_page_info_t
*pl
;
3348 unsigned int page_max_index
;
3351 unsigned int cl_size
;
3353 unsigned int seg_size
;
3354 unsigned int upl_offset_in_object
;
3355 boolean_t minimal_clustering
= FALSE
;
3356 boolean_t found_dirty
;
3358 if (!dp_encryption_inited
) {
3361 * Once we've started using swap, we
3362 * can't change our mind on whether
3363 * it needs to be encrypted or
3366 dp_encryption_inited
= TRUE
;
3368 if (dp_encryption
) {
3371 * the UPL will need to be encrypted...
3373 flags
|= UPL_PAGING_ENCRYPTED
;
3376 pages_in_cl
= 1 << vs
->vs_clshift
;
3377 cl_size
= pages_in_cl
* vm_page_size
;
3380 minimal_clustering
= TRUE
;
3382 if (dp_isssd
== TRUE
)
3383 minimal_clustering
= TRUE
;
3386 unsigned int page_list_count
;
3388 unsigned int super_size
;
3393 upl_offset_t upl_offset
;
3394 upl_offset_t upl_offset_aligned
;
3395 dp_offset_t seg_offset
;
3396 dp_offset_t ps_offset
[((VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_MIN_CLSHIFT
) + 1];
3397 paging_segment_t psp
[((VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_MIN_CLSHIFT
) + 1];
3401 super_size
= cl_size
;
3403 super_size
= VM_SUPER_CLUSTER
;
3405 request_flags
= UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
|
3406 UPL_RET_ONLY_DIRTY
| UPL_COPYOUT_FROM
|
3407 UPL_NO_SYNC
| UPL_SET_INTERNAL
| UPL_SET_LITE
;
3409 if (dp_encryption
) {
3412 * request that the UPL be prepared for
3415 request_flags
|= UPL_ENCRYPT
;
3416 flags
|= UPL_PAGING_ENCRYPTED
;
3419 page_list_count
= 0;
3420 memory_object_super_upl_request(vs
->vs_control
,
3421 (memory_object_offset_t
)offset
,
3423 &upl
, NULL
, &page_list_count
,
3424 request_flags
| UPL_FOR_PAGEOUT
);
3427 * The default pager does not handle objects larger than
3428 * 4GB, so it does not deal with offset that don't fit in
3429 * 32-bit. Cast down upl->offset now and make sure we
3430 * did not lose any valuable bits.
3432 upl_offset_in_object
= (unsigned int) upl
->offset
;
3433 assert(upl
->offset
== upl_offset_in_object
);
3435 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
3437 seg_size
= cl_size
- (upl_offset_in_object
% cl_size
);
3438 upl_offset_aligned
= upl_offset_in_object
& ~(cl_size
- 1);
3440 page_max_index
= upl
->size
/ PAGE_SIZE
;
3443 for (seg_index
= 0, transfer_size
= upl
->size
; transfer_size
> 0; ) {
3445 unsigned int seg_pgcnt
;
3447 seg_pgcnt
= seg_size
/ PAGE_SIZE
;
3449 if (minimal_clustering
== TRUE
) {
3450 unsigned int non_dirty
;
3453 found_dirty
= FALSE
;
3455 for (; non_dirty
< seg_pgcnt
; non_dirty
++) {
3456 if ((page_index
+ non_dirty
) >= page_max_index
)
3459 if (UPL_DIRTY_PAGE(pl
, page_index
+ non_dirty
) ||
3460 UPL_PRECIOUS_PAGE(pl
, page_index
+ non_dirty
)) {
3466 if (found_dirty
== TRUE
) {
3467 ps_offset
[seg_index
] =
3473 if (ps_offset
[seg_index
] == (dp_offset_t
) -1) {
3475 upl_deallocate(upl
);
3477 return KERN_FAILURE
;
3479 psp
[seg_index
] = CLMAP_PS(clmap
);
3481 if (transfer_size
> seg_size
) {
3482 page_index
+= seg_pgcnt
;
3483 transfer_size
-= seg_size
;
3484 upl_offset_aligned
+= cl_size
;
3491 * Ignore any non-present pages at the end of the
3494 for (page_index
= upl
->size
/ vm_page_size
; page_index
> 0;) {
3495 if (UPL_PAGE_PRESENT(pl
, --page_index
)) {
3500 if (page_index
== 0) {
3502 * no pages in the UPL
3506 upl_deallocate(upl
);
3508 return KERN_SUCCESS
;
3510 num_of_pages
= page_index
;
3512 base_index
= (upl_offset_in_object
% cl_size
) / PAGE_SIZE
;
3514 for (page_index
= 0; page_index
< num_of_pages
; ) {
3516 * skip over non-dirty pages
3518 for ( ; page_index
< num_of_pages
; page_index
++) {
3519 if (UPL_DIRTY_PAGE(pl
, page_index
)
3520 || UPL_PRECIOUS_PAGE(pl
, page_index
))
3522 * this is a page we need to write
3523 * go see if we can buddy it up with
3524 * others that are contiguous to it
3528 * if the page is not-dirty, but present we
3529 * need to commit it... This is an unusual
3530 * case since we only asked for dirty pages
3532 if (UPL_PAGE_PRESENT(pl
, page_index
)) {
3533 boolean_t empty
= FALSE
;
3534 upl_commit_range(upl
,
3535 page_index
* vm_page_size
,
3537 UPL_COMMIT_NOTIFY_EMPTY
,
3542 assert(page_index
==
3544 upl_deallocate(upl
);
3548 if (page_index
== num_of_pages
)
3550 * no more pages to look at, we're out of here
3555 * gather up contiguous dirty pages... we have at
3556 * least 1 * otherwise we would have bailed above
3557 * make sure that each physical segment that we step
3558 * into is contiguous to the one we're currently in
3559 * if it's not, we have to stop and write what we have
3561 for (first_dirty
= page_index
;
3562 page_index
< num_of_pages
; ) {
3563 if ( !UPL_DIRTY_PAGE(pl
, page_index
)
3564 && !UPL_PRECIOUS_PAGE(pl
, page_index
))
3568 * if we just looked at the last page in the UPL
3569 * we don't need to check for physical segment
3572 if (page_index
< num_of_pages
) {
3576 cur_seg
= (base_index
+ (page_index
- 1))/pages_in_cl
;
3577 nxt_seg
= (base_index
+ page_index
)/pages_in_cl
;
3579 if (cur_seg
!= nxt_seg
) {
3580 if ((ps_offset
[cur_seg
] != (ps_offset
[nxt_seg
] - cl_size
)) || (psp
[cur_seg
] != psp
[nxt_seg
]))
3582 * if the segment we're about
3583 * to step into is not
3584 * contiguous to the one we're
3585 * currently in, or it's in a
3586 * different paging file....
3587 * we stop here and generate
3594 num_dirty
= page_index
- first_dirty
;
3597 upl_offset
= first_dirty
* vm_page_size
;
3598 transfer_size
= num_dirty
* vm_page_size
;
3600 while (transfer_size
) {
3602 if ((seg_size
= cl_size
-
3603 ((upl_offset_in_object
+
3604 upl_offset
) % cl_size
))
3606 seg_size
= transfer_size
;
3608 ps_vs_write_complete(
3610 (upl_offset_in_object
+
3614 transfer_size
-= seg_size
;
3615 upl_offset
+= seg_size
;
3617 upl_offset
= first_dirty
* vm_page_size
;
3618 transfer_size
= num_dirty
* vm_page_size
;
3620 seg_index
= (base_index
+ first_dirty
) / pages_in_cl
;
3621 seg_offset
= (upl_offset_in_object
+ upl_offset
) % cl_size
;
3623 error
= ps_write_file(psp
[seg_index
],
3625 ps_offset
[seg_index
]
3627 transfer_size
, flags
);
3632 assert(cnt
<= (unsigned) (vm_page_size
<< vs
->vs_clshift
));
3636 /* The caller provides a mapped_data which is derived */
3637 /* from a temporary object. The targeted pages are */
3638 /* guaranteed to be set at offset 0 in the mapped_data */
3639 /* The actual offset however must still be derived */
3640 /* from the offset in the vs in question */
3641 mobj_base_addr
= offset
;
3642 mobj_target_addr
= mobj_base_addr
;
3644 for (transfer_size
= list_size
; transfer_size
!= 0;) {
3645 actual_offset
= ps_clmap(vs
, mobj_target_addr
,
3647 transfer_size
< cl_size
?
3648 transfer_size
: cl_size
, 0);
3649 if(actual_offset
== (dp_offset_t
) -1) {
3653 cnt
= MIN(transfer_size
,
3654 (unsigned) CLMAP_NPGS(clmap
) * vm_page_size
);
3655 ps
= CLMAP_PS(clmap
);
3656 /* Assume that the caller has given us contiguous */
3659 ps_vs_write_complete(vs
, mobj_target_addr
,
3661 error
= ps_write_file(ps
, internal_upl
,
3669 actual_offset
+= cnt
;
3670 mobj_target_addr
+= cnt
;
3671 transfer_size
-= cnt
;
3679 return KERN_FAILURE
;
3681 return KERN_SUCCESS
;
3685 ps_vstruct_allocated_size(
3689 struct vs_map
*vsmap
;
3690 unsigned int i
, j
, k
;
3693 if (vs
->vs_indirect
) {
3694 /* loop on indirect maps */
3695 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3696 vsmap
= vs
->vs_imap
[i
];
3699 /* loop on clusters in this indirect map */
3700 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3701 if (VSM_ISCLR(vsmap
[j
]) ||
3702 VSM_ISERR(vsmap
[j
]))
3704 /* loop on pages in this cluster */
3705 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3706 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
))
3712 vsmap
= vs
->vs_dmap
;
3715 /* loop on clusters in the direct map */
3716 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3717 if (VSM_ISCLR(vsmap
[j
]) ||
3718 VSM_ISERR(vsmap
[j
]))
3720 /* loop on pages in this cluster */
3721 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3722 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
))
3728 return ptoa_32(num_pages
);
3732 ps_vstruct_allocated_pages(
3734 default_pager_page_t
*pages
,
3735 unsigned int pages_size
)
3737 unsigned int num_pages
;
3738 struct vs_map
*vsmap
;
3740 unsigned int i
, j
, k
;
3744 if (vs
->vs_indirect
) {
3745 /* loop on indirect maps */
3746 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3747 vsmap
= vs
->vs_imap
[i
];
3748 if (vsmap
== NULL
) {
3749 offset
+= (vm_page_size
* CLMAP_ENTRIES
*
3753 /* loop on clusters in this indirect map */
3754 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3755 if (VSM_ISCLR(vsmap
[j
]) ||
3756 VSM_ISERR(vsmap
[j
])) {
3757 offset
+= vm_page_size
* VSCLSIZE(vs
);
3760 /* loop on pages in this cluster */
3761 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3762 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
)) {
3764 if (num_pages
< pages_size
)
3765 pages
++->dpp_offset
=
3768 offset
+= vm_page_size
;
3773 vsmap
= vs
->vs_dmap
;
3776 /* loop on clusters in the direct map */
3777 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3778 if (VSM_ISCLR(vsmap
[j
]) ||
3779 VSM_ISERR(vsmap
[j
])) {
3780 offset
+= vm_page_size
* VSCLSIZE(vs
);
3783 /* loop on pages in this cluster */
3784 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3785 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
)) {
3787 if (num_pages
< pages_size
)
3788 pages
++->dpp_offset
= offset
;
3790 offset
+= vm_page_size
;
3800 ps_vstruct_transfer_from_segment(
3802 paging_segment_t segment
,
3805 struct vs_map
*vsmap
;
3806 // struct vs_map old_vsmap;
3807 // struct vs_map new_vsmap;
3810 VS_LOCK(vs
); /* block all work on this vstruct */
3811 /* can't allow the normal multiple write */
3812 /* semantic because writes may conflict */
3813 vs
->vs_xfer_pending
= TRUE
;
3814 vs_wait_for_sync_writers(vs
);
3816 vs_wait_for_readers(vs
);
3817 /* we will unlock the vs to allow other writes while transferring */
3818 /* and will be guaranteed of the persistance of the vs struct */
3819 /* because the caller of ps_vstruct_transfer_from_segment bumped */
3820 /* vs_async_pending */
3821 /* OK we now have guaranteed no other parties are accessing this */
3822 /* vs. Now that we are also supporting simple lock versions of */
3823 /* vs_lock we cannot hold onto VS_LOCK as we may block below. */
3824 /* our purpose in holding it before was the multiple write case */
3825 /* we now use the boolean xfer_pending to do that. We can use */
3826 /* a boolean instead of a count because we have guaranteed single */
3827 /* file access to this code in its caller */
3830 if (vs
->vs_indirect
) {
3831 unsigned int vsmap_size
;
3833 /* loop on indirect maps */
3834 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3835 vsmap
= vs
->vs_imap
[i
];
3838 /* loop on clusters in this indirect map */
3839 clmap_off
= (vm_page_size
* CLMAP_ENTRIES
*
3841 if(i
+1 == INDIRECT_CLMAP_ENTRIES(vs
->vs_size
))
3842 vsmap_size
= vs
->vs_size
- (CLMAP_ENTRIES
* i
);
3844 vsmap_size
= CLMAP_ENTRIES
;
3845 for (j
= 0; j
< vsmap_size
; j
++) {
3846 if (VSM_ISCLR(vsmap
[j
]) ||
3847 VSM_ISERR(vsmap
[j
]) ||
3848 (VSM_PS(vsmap
[j
]) != segment
))
3850 if(vs_cluster_transfer(vs
,
3851 (vm_page_size
* (j
<< vs
->vs_clshift
))
3853 vm_page_size
<< vs
->vs_clshift
,
3857 vs
->vs_xfer_pending
= FALSE
;
3859 vs_finish_write(vs
);
3860 return KERN_FAILURE
;
3862 /* allow other readers/writers during transfer*/
3864 vs
->vs_xfer_pending
= FALSE
;
3866 vs_finish_write(vs
);
3868 if (backing_store_abort_compaction
|| backing_store_stop_compaction
) {
3869 backing_store_abort_compaction
= FALSE
;
3870 dprintf(("ps_vstruct_transfer_from_segment - ABORTED\n"));
3871 return KERN_FAILURE
;
3873 vnode_pager_throttle();
3876 vs
->vs_xfer_pending
= TRUE
;
3877 vs_wait_for_sync_writers(vs
);
3879 vs_wait_for_readers(vs
);
3881 if (!(vs
->vs_indirect
)) {
3887 vsmap
= vs
->vs_dmap
;
3888 if (vsmap
== NULL
) {
3890 vs
->vs_xfer_pending
= FALSE
;
3892 vs_finish_write(vs
);
3893 return KERN_SUCCESS
;
3895 /* loop on clusters in the direct map */
3896 for (j
= 0; j
< vs
->vs_size
; j
++) {
3897 if (VSM_ISCLR(vsmap
[j
]) ||
3898 VSM_ISERR(vsmap
[j
]) ||
3899 (VSM_PS(vsmap
[j
]) != segment
))
3901 if(vs_cluster_transfer(vs
,
3902 vm_page_size
* (j
<< vs
->vs_clshift
),
3903 vm_page_size
<< vs
->vs_clshift
,
3904 upl
) != KERN_SUCCESS
) {
3906 vs
->vs_xfer_pending
= FALSE
;
3908 vs_finish_write(vs
);
3909 return KERN_FAILURE
;
3911 /* allow other readers/writers during transfer*/
3913 vs
->vs_xfer_pending
= FALSE
;
3915 vs_finish_write(vs
);
3917 vs
->vs_xfer_pending
= TRUE
;
3918 vs_wait_for_sync_writers(vs
);
3920 vs_wait_for_readers(vs
);
3922 if (vs
->vs_indirect
) {
3929 vs
->vs_xfer_pending
= FALSE
;
3931 vs_finish_write(vs
);
3932 return KERN_SUCCESS
;
3942 struct vs_map
*vsmap
;
3943 dp_offset_t cluster
;
3945 cluster
= atop_32(offset
) >> vs
->vs_clshift
;
3946 if (vs
->vs_indirect
) {
3947 long ind_block
= cluster
/CLMAP_ENTRIES
;
3949 /* Is the indirect block allocated? */
3950 vsmap
= vs
->vs_imap
[ind_block
];
3951 if(vsmap
== (vs_map_t
) NULL
)
3954 vsmap
= vs
->vs_dmap
;
3955 vsmap
+= cluster%CLMAP_ENTRIES
;
3960 vs_cluster_transfer(
3966 dp_offset_t actual_offset
;
3967 paging_segment_t ps
;
3969 kern_return_t error
= KERN_SUCCESS
;
3970 unsigned int size
, size_wanted
;
3972 unsigned int residual
= 0;
3973 unsigned int unavail_size
;
3974 // default_pager_thread_t *dpt;
3975 // boolean_t dealloc;
3976 struct vs_map
*vsmap_ptr
= NULL
;
3977 struct vs_map read_vsmap
;
3978 struct vs_map original_read_vsmap
;
3979 struct vs_map write_vsmap
;
3981 // vm_offset_t ioaddr;
3983 /* vs_cluster_transfer reads in the pages of a cluster and
3984 * then writes these pages back to new backing store. The
3985 * segment the pages are being read from is assumed to have
3986 * been taken off-line and is no longer considered for new
3991 * This loop will be executed once per cluster referenced.
3992 * Typically this means once, since it's unlikely that the
3993 * VM system will ask for anything spanning cluster boundaries.
3995 * If there are holes in a cluster (in a paging segment), we stop
3996 * reading at the hole, then loop again, hoping to
3997 * find valid pages later in the cluster. This continues until
3998 * the entire range has been examined, and read, if present. The
3999 * pages are written as they are read. If a failure occurs after
4000 * some pages are written the unmap call at the bottom of the loop
4001 * recovers the backing store and the old backing store remains
4005 VSM_CLR(write_vsmap
);
4006 VSM_CLR(original_read_vsmap
);
4007 /* grab the actual object's pages to sync with I/O */
4008 while (cnt
&& (error
== KERN_SUCCESS
)) {
4009 vsmap_ptr
= vs_get_map_entry(vs
, offset
);
4010 actual_offset
= ps_clmap(vs
, offset
, &clmap
, CL_FIND
, 0, 0);
4012 if (actual_offset
== (dp_offset_t
) -1) {
4015 * Nothing left to write in this cluster at least
4016 * set write cluster information for any previous
4017 * write, clear for next cluster, if there is one
4019 unsigned int local_size
, clmask
, clsize
;
4021 clsize
= vm_page_size
<< vs
->vs_clshift
;
4022 clmask
= clsize
- 1;
4023 local_size
= clsize
- (offset
& clmask
);
4025 local_size
= MIN(local_size
, cnt
);
4027 /* This cluster has no data in it beyond what may */
4028 /* have been found on a previous iteration through */
4029 /* the loop "write_vsmap" */
4030 *vsmap_ptr
= write_vsmap
;
4031 VSM_CLR(write_vsmap
);
4032 VSM_CLR(original_read_vsmap
);
4035 offset
+= local_size
;
4040 * Count up contiguous available or unavailable
4043 ps
= CLMAP_PS(clmap
);
4048 (size
< cnt
) && (unavail_size
< cnt
) &&
4049 (i
< CLMAP_NPGS(clmap
)); i
++) {
4050 if (CLMAP_ISSET(clmap
, i
)) {
4051 if (unavail_size
!= 0)
4053 size
+= vm_page_size
;
4055 ps
->ps_bs
->bs_pages_in
++);
4059 unavail_size
+= vm_page_size
;
4064 ASSERT(unavail_size
);
4065 ps_clunmap(vs
, offset
, unavail_size
);
4066 cnt
-= unavail_size
;
4067 offset
+= unavail_size
;
4068 if((offset
& ((vm_page_size
<< vs
->vs_clshift
) - 1))
4070 /* There is no more to transfer in this
4073 *vsmap_ptr
= write_vsmap
;
4074 VSM_CLR(write_vsmap
);
4075 VSM_CLR(original_read_vsmap
);
4080 if(VSM_ISCLR(original_read_vsmap
))
4081 original_read_vsmap
= *vsmap_ptr
;
4083 if(ps
->ps_segtype
== PS_PARTITION
) {
4084 panic("swap partition not supported\n");
4086 error
= KERN_FAILURE
;
4089 NEED TO ISSUE WITH SYNC & NO COMMIT
4090 error = ps_read_device(ps, actual_offset, &buffer,
4091 size, &residual, flags);
4094 /* NEED TO ISSUE WITH SYNC & NO COMMIT */
4095 error
= ps_read_file(ps
, upl
, (upl_offset_t
) 0, actual_offset
,
4097 (UPL_IOSYNC
| UPL_NOCOMMIT
| (dp_encryption
? UPL_PAGING_ENCRYPTED
: 0)));
4100 read_vsmap
= *vsmap_ptr
;
4104 * Adjust counts and put data in new BS. Optimize for the
4105 * common case, i.e. no error and/or partial data.
4106 * If there was an error, then we need to error the entire
4107 * range, even if some data was successfully read.
4110 if ((error
== KERN_SUCCESS
) && (residual
== 0)) {
4113 * Got everything we asked for, supply the data to
4114 * the new BS. Note that as a side effect of supplying
4115 * the data, the buffer holding the supplied data is
4116 * deallocated from the pager's address space unless
4117 * the write is unsuccessful.
4120 /* note buffer will be cleaned up in all cases by */
4121 /* internal_cluster_write or if an error on write */
4122 /* the vm_map_copy_page_discard call */
4123 *vsmap_ptr
= write_vsmap
;
4125 if(vs_cluster_write(vs
, upl
, offset
,
4126 size
, TRUE
, UPL_IOSYNC
| UPL_NOCOMMIT
) != KERN_SUCCESS
) {
4127 error
= KERN_FAILURE
;
4128 if(!(VSM_ISCLR(*vsmap_ptr
))) {
4129 /* unmap the new backing store object */
4130 ps_clunmap(vs
, offset
, size
);
4132 /* original vsmap */
4133 *vsmap_ptr
= original_read_vsmap
;
4134 VSM_CLR(write_vsmap
);
4136 if((offset
+ size
) &
4137 ((vm_page_size
<< vs
->vs_clshift
)
4139 /* There is more to transfer in this
4142 write_vsmap
= *vsmap_ptr
;
4143 *vsmap_ptr
= read_vsmap
;
4144 ps_clunmap(vs
, offset
, size
);
4146 /* discard the old backing object */
4147 write_vsmap
= *vsmap_ptr
;
4148 *vsmap_ptr
= read_vsmap
;
4149 ps_clunmap(vs
, offset
, size
);
4150 *vsmap_ptr
= write_vsmap
;
4151 VSM_CLR(write_vsmap
);
4152 VSM_CLR(original_read_vsmap
);
4157 if (error
== KERN_SUCCESS
) {
4158 if (residual
== size
) {
4160 * If a read operation returns no error
4161 * and no data moved, we turn it into
4162 * an error, assuming we're reading at
4164 * Fall through and error the entire
4167 error
= KERN_FAILURE
;
4168 *vsmap_ptr
= write_vsmap
;
4169 if(!(VSM_ISCLR(*vsmap_ptr
))) {
4170 /* unmap the new backing store object */
4171 ps_clunmap(vs
, offset
, size
);
4173 *vsmap_ptr
= original_read_vsmap
;
4174 VSM_CLR(write_vsmap
);
4178 * Otherwise, we have partial read.
4179 * This is also considered an error
4180 * for the purposes of cluster transfer
4182 error
= KERN_FAILURE
;
4183 *vsmap_ptr
= write_vsmap
;
4184 if(!(VSM_ISCLR(*vsmap_ptr
))) {
4185 /* unmap the new backing store object */
4186 ps_clunmap(vs
, offset
, size
);
4188 *vsmap_ptr
= original_read_vsmap
;
4189 VSM_CLR(write_vsmap
);
4198 } /* END while (cnt && (error == 0)) */
4199 if(!VSM_ISCLR(write_vsmap
))
4200 *vsmap_ptr
= write_vsmap
;
4206 default_pager_add_file(
4207 MACH_PORT_FACE backing_store
,
4213 paging_segment_t ps
;
4218 if ((bs
= backing_store_lookup(backing_store
))
4219 == BACKING_STORE_NULL
)
4220 return KERN_INVALID_ARGUMENT
;
4223 for (i
= 0; i
<= paging_segment_max
; i
++) {
4224 ps
= paging_segments
[i
];
4225 if (ps
== PAGING_SEGMENT_NULL
)
4227 if (ps
->ps_segtype
!= PS_FILE
)
4231 * Check for overlap on same device.
4233 if (ps
->ps_vnode
== (struct vnode
*)vp
) {
4236 return KERN_INVALID_ARGUMENT
;
4242 * Set up the paging segment
4244 ps
= (paging_segment_t
) kalloc(sizeof (struct paging_segment
));
4245 if (ps
== PAGING_SEGMENT_NULL
) {
4247 return KERN_RESOURCE_SHORTAGE
;
4250 ps
->ps_segtype
= PS_FILE
;
4251 ps
->ps_vnode
= (struct vnode
*)vp
;
4253 ps
->ps_record_shift
= local_log2(vm_page_size
/ record_size
);
4254 assert((dp_size_t
) size
== size
);
4255 ps
->ps_recnum
= (dp_size_t
) size
;
4256 ps
->ps_pgnum
= ((dp_size_t
) size
) >> ps
->ps_record_shift
;
4258 ps
->ps_pgcount
= ps
->ps_pgnum
;
4259 ps
->ps_clshift
= local_log2(bs
->bs_clsize
);
4260 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
4261 ps
->ps_special_clusters
= 0;
4265 ps
->ps_bmap
= (unsigned char *) kalloc(RMAPSIZE(ps
->ps_ncls
));
4267 PS_LOCK_DESTROY(ps
);
4268 kfree(ps
, sizeof *ps
);
4270 return KERN_RESOURCE_SHORTAGE
;
4272 for (j
= 0; j
< ps
->ps_ncls
; j
++) {
4273 clrbit(ps
->ps_bmap
, j
);
4276 if(paging_segment_count
== 0) {
4277 ps
->ps_state
= PS_EMERGENCY_SEGMENT
;
4278 if(use_emergency_swap_file_first
) {
4279 ps
->ps_state
|= PS_CAN_USE
;
4281 emergency_segment_backing_store
= backing_store
;
4283 ps
->ps_state
= PS_CAN_USE
;
4288 if ((error
= ps_enter(ps
)) != 0) {
4289 kfree(ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
4290 PS_LOCK_DESTROY(ps
);
4291 kfree(ps
, sizeof *ps
);
4293 return KERN_RESOURCE_SHORTAGE
;
4296 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
4297 bs
->bs_pages_total
+= ps
->ps_clcount
<< ps
->ps_clshift
;
4299 if(IS_PS_OK_TO_USE(ps
)) {
4300 dp_pages_free
+= ps
->ps_pgcount
;
4302 dp_pages_reserve
+= ps
->ps_pgcount
;
4308 bs_more_space(ps
->ps_clcount
);
4311 * If the paging segment being activated is not the emergency
4312 * segment and we notice that the emergency segment is being
4313 * used then we help recover it. If all goes well, the
4314 * emergency segment will be back to its original state of
4315 * online but not activated (till it's needed the next time).
4318 if (!memorystatus_freeze_enabled
)
4321 ps
= paging_segments
[EMERGENCY_PSEG_INDEX
];
4322 if(IS_PS_EMERGENCY_SEGMENT(ps
) && IS_PS_OK_TO_USE(ps
)) {
4323 if(default_pager_backing_store_delete(emergency_segment_backing_store
)) {
4324 dprintf(("Failed to recover emergency paging segment\n"));
4326 dprintf(("Recovered emergency paging segment\n"));
4331 DP_DEBUG(DEBUG_BS_INTERNAL
,
4332 ("device=0x%x,offset=0x%x,count=0x%x,record_size=0x%x,shift=%d,total_size=0x%x\n",
4333 device
, offset
, (dp_size_t
) size
, record_size
,
4334 ps
->ps_record_shift
, ps
->ps_pgnum
));
4336 return KERN_SUCCESS
;
4343 paging_segment_t ps
,
4345 upl_offset_t upl_offset
,
4348 unsigned int *residualp
,
4351 vm_object_offset_t f_offset
;
4355 assert(dp_encryption_inited
);
4357 clustered_reads
[atop_32(size
)]++;
4359 f_offset
= (vm_object_offset_t
)(ps
->ps_offset
+ offset
);
4362 * for transfer case we need to pass uploffset and flags
4364 assert((upl_size_t
) size
== size
);
4365 error
= vnode_pagein(ps
->ps_vnode
, upl
, upl_offset
, f_offset
, (upl_size_t
)size
, flags
, NULL
);
4367 /* The vnode_pagein semantic is somewhat at odds with the existing */
4368 /* device_read semantic. Partial reads are not experienced at this */
4369 /* level. It is up to the bit map code and cluster read code to */
4370 /* check that requested data locations are actually backed, and the */
4371 /* pagein code to either read all of the requested data or return an */
4375 result
= KERN_FAILURE
;
4378 result
= KERN_SUCCESS
;
4385 paging_segment_t ps
,
4387 upl_offset_t upl_offset
,
4392 vm_object_offset_t f_offset
;
4393 kern_return_t result
;
4395 assert(dp_encryption_inited
);
4397 clustered_writes
[atop_32(size
)]++;
4398 f_offset
= (vm_object_offset_t
)(ps
->ps_offset
+ offset
);
4400 if (flags
& UPL_PAGING_ENCRYPTED
) {
4403 * encrypt all the pages that we're going
4406 upl_encrypt(upl
, upl_offset
, size
);
4408 assert((upl_size_t
) size
== size
);
4409 if (vnode_pageout(ps
->ps_vnode
, upl
, upl_offset
, f_offset
, (upl_size_t
)size
, flags
, NULL
))
4410 result
= KERN_FAILURE
;
4412 result
= KERN_SUCCESS
;
4417 static inline void ps_vnode_trim_init(struct ps_vnode_trim_data
*data
)
4424 #pragma unused(data)
4428 static inline void ps_vnode_trim_now(struct ps_vnode_trim_data
*data
)
4431 if ((data
->vp
) != NULL
) {
4432 vnode_trim(data
->vp
,
4435 ps_vnode_trim_init(data
);
4438 #pragma unused(data)
4442 static inline void ps_vnode_trim_more(struct ps_vnode_trim_data
*data
, struct vs_map
*map
, unsigned int shift
, dp_size_t length
)
4445 struct vnode
*vp
= VSM_PS(*map
)->ps_vnode
;
4446 dp_offset_t offset
= ptoa_32(VSM_CLOFF(*map
)) << shift
;
4448 if ((vp
!= data
->vp
) || (offset
) != (data
->offset
+ data
->length
)) {
4449 ps_vnode_trim_now(data
);
4451 data
->offset
= offset
;
4454 data
->length
+= (length
);
4456 #pragma unused(data, map, shift, length)
4461 default_pager_triggers( __unused MACH_PORT_FACE default_pager
,
4465 MACH_PORT_FACE trigger_port
)
4467 MACH_PORT_FACE release
= IPC_PORT_NULL
;
4470 clock_nsec_t nanoseconds_dummy
;
4471 static clock_sec_t error_notify
= 0;
4474 if (flags
== SWAP_ENCRYPT_ON
) {
4475 /* ENCRYPTED SWAP: turn encryption on */
4476 release
= trigger_port
;
4477 if (!dp_encryption_inited
) {
4478 dp_encryption_inited
= TRUE
;
4479 dp_encryption
= TRUE
;
4484 } else if (flags
== SWAP_ENCRYPT_OFF
) {
4485 /* ENCRYPTED SWAP: turn encryption off */
4486 release
= trigger_port
;
4487 if (!dp_encryption_inited
) {
4488 dp_encryption_inited
= TRUE
;
4489 dp_encryption
= FALSE
;
4494 } else if (flags
== HI_WAT_ALERT
) {
4495 release
= min_pages_trigger_port
;
4497 /* High and low water signals aren't applicable when freeze is */
4498 /* enabled, so release the trigger ports here and return */
4500 if (memorystatus_freeze_enabled
) {
4501 if (IP_VALID( trigger_port
)){
4502 ipc_port_release_send( trigger_port
);
4504 min_pages_trigger_port
= IPC_PORT_NULL
;
4510 min_pages_trigger_port
= trigger_port
;
4511 minimum_pages_remaining
= hi_wat
/vm_page_size
;
4515 } else if (flags
== LO_WAT_ALERT
) {
4516 release
= max_pages_trigger_port
;
4518 if (memorystatus_freeze_enabled
) {
4519 if (IP_VALID( trigger_port
)){
4520 ipc_port_release_send( trigger_port
);
4522 max_pages_trigger_port
= IPC_PORT_NULL
;
4528 max_pages_trigger_port
= trigger_port
;
4529 maximum_pages_free
= lo_wat
/vm_page_size
;
4532 } else if (flags
== USE_EMERGENCY_SWAP_FILE_FIRST
) {
4533 use_emergency_swap_file_first
= TRUE
;
4534 release
= trigger_port
;
4536 } else if (flags
== SWAP_FILE_CREATION_ERROR
) {
4537 release
= trigger_port
;
4539 if( paging_segment_count
== 1) {
4540 use_emergency_swap_file_first
= TRUE
;
4542 no_paging_space_action();
4543 clock_get_system_nanotime(&now
, &nanoseconds_dummy
);
4544 if (now
> error_notify
+ 5) {
4545 dprintf(("Swap File Error.\n"));
4549 release
= trigger_port
;
4550 kr
= KERN_INVALID_ARGUMENT
;
4554 if (IP_VALID(release
))
4555 ipc_port_release_send(release
);
4561 * Monitor the amount of available backing store vs. the amount of
4562 * required backing store, notify a listener (if present) when
4563 * backing store may safely be removed.
4565 * We attempt to avoid the situation where backing store is
4566 * discarded en masse, as this can lead to thrashing as the
4567 * backing store is compacted.
4570 #define PF_INTERVAL 3 /* time between free level checks */
4571 #define PF_LATENCY 10 /* number of intervals before release */
4573 static int dp_pages_free_low_count
= 0;
4574 thread_call_t default_pager_backing_store_monitor_callout
;
4577 default_pager_backing_store_monitor(__unused thread_call_param_t p1
,
4578 __unused thread_call_param_t p2
)
4580 // unsigned long long average;
4585 * We determine whether it will be safe to release some
4586 * backing store by watching the free page level. If
4587 * it remains below the maximum_pages_free threshold for
4588 * at least PF_LATENCY checks (taken at PF_INTERVAL seconds)
4589 * then we deem it safe.
4591 * Note that this establishes a maximum rate at which backing
4592 * store will be released, as each notification (currently)
4593 * only results in a single backing store object being
4596 if (dp_pages_free
> maximum_pages_free
) {
4597 dp_pages_free_low_count
++;
4599 dp_pages_free_low_count
= 0;
4602 /* decide whether to send notification */
4604 if (max_pages_trigger_port
&&
4605 (backing_store_release_trigger_disable
== 0) &&
4606 (dp_pages_free_low_count
> PF_LATENCY
)) {
4607 trigger
= max_pages_trigger_port
;
4608 max_pages_trigger_port
= NULL
;
4611 /* send notification */
4612 if (trigger
!= IP_NULL
) {
4614 if(backing_store_release_trigger_disable
!= 0) {
4615 assert_wait((event_t
)
4616 &backing_store_release_trigger_disable
,
4619 thread_block(THREAD_CONTINUE_NULL
);
4623 dprintf(("default_pager_backing_store_monitor - send LO_WAT_ALERT\n"));
4625 default_pager_space_alert(trigger
, LO_WAT_ALERT
);
4626 ipc_port_release_send(trigger
);
4627 dp_pages_free_low_count
= 0;
4630 clock_interval_to_deadline(PF_INTERVAL
, NSEC_PER_SEC
, &deadline
);
4631 thread_call_enter_delayed(default_pager_backing_store_monitor_callout
, deadline
);
4635 unsigned int default_pager_swap_pages_free() {
4636 return dp_pages_free
;