3 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * The contents of this file constitute Original Code as defined in and
8 * are subject to the Apple Public Source License Version 1.1 (the
9 * "License"). You may not use this file except in compliance with the
10 * License. Please obtain a copy of the License at
11 * http://www.apple.com/publicsource and read it before using this file.
13 * This Original Code and all software distributed under the License are
14 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
18 * License for the specific language governing rights and limitations
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * Paging File Management.
57 #include <mach/memory_object_control.h>
58 #include <mach/memory_object_server.h>
59 #include "default_pager_internal.h"
60 #include <default_pager/default_pager_alerts.h>
61 #include <ipc/ipc_port.h>
62 #include <ipc/ipc_space.h>
63 #include <kern/queue.h>
64 #include <kern/counters.h>
65 #include <kern/sched_prim.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_pageout.h>
69 #include <vm/vm_map.h>
72 * ALLOC_STRIDE... the maximum number of bytes allocated from
73 * a swap file before moving on to the next swap file... if
74 * all swap files reside on a single disk, this value should
75 * be very large (this is the default assumption)... if the
76 * swap files are spread across multiple disks, than this value
77 * should be small (128 * 1024)...
79 * This should be determined dynamically in the future
82 #define ALLOC_STRIDE (1024 * 1024 * 1024)
83 int physical_transfer_cluster_count
= 0;
85 #define VM_SUPER_CLUSTER 0x20000
86 #define VM_SUPER_PAGES 32
89 * 0 means no shift to pages, so == 1 page/cluster. 1 would mean
90 * 2 pages/cluster, 2 means 4 pages/cluster, and so on.
92 #define VSTRUCT_DEF_CLSHIFT 2
93 int vstruct_def_clshift
= VSTRUCT_DEF_CLSHIFT
;
94 int default_pager_clsize
= 0;
97 unsigned int clustered_writes
[VM_SUPER_PAGES
+1];
98 unsigned int clustered_reads
[VM_SUPER_PAGES
+1];
101 * Globals used for asynchronous paging operations:
102 * vs_async_list: head of list of to-be-completed I/O ops
103 * async_num_queued: number of pages completed, but not yet
104 * processed by async thread.
105 * async_requests_out: number of pages of requests not completed.
109 struct vs_async
*vs_async_list
;
110 int async_num_queued
;
111 int async_requests_out
;
115 #define VS_ASYNC_REUSE 1
116 struct vs_async
*vs_async_free_list
;
118 mutex_t default_pager_async_lock
; /* Protects globals above */
121 int vs_alloc_async_failed
= 0; /* statistics */
122 int vs_alloc_async_count
= 0; /* statistics */
123 struct vs_async
*vs_alloc_async(void); /* forward */
124 void vs_free_async(struct vs_async
*vsa
); /* forward */
127 #define VS_ALLOC_ASYNC() vs_alloc_async()
128 #define VS_FREE_ASYNC(vsa) vs_free_async(vsa)
130 #define VS_ASYNC_LOCK() mutex_lock(&default_pager_async_lock)
131 #define VS_ASYNC_UNLOCK() mutex_unlock(&default_pager_async_lock)
132 #define VS_ASYNC_LOCK_INIT() mutex_init(&default_pager_async_lock, \
134 #define VS_ASYNC_LOCK_ADDR() (&default_pager_async_lock)
136 * Paging Space Hysteresis triggers and the target notification port
140 unsigned int minimum_pages_remaining
= 0;
141 unsigned int maximum_pages_free
= 0;
142 ipc_port_t min_pages_trigger_port
= NULL
;
143 ipc_port_t max_pages_trigger_port
= NULL
;
145 boolean_t bs_low
= FALSE
;
146 int backing_store_release_trigger_disable
= 0;
151 * Object sizes are rounded up to the next power of 2,
152 * unless they are bigger than a given maximum size.
154 vm_size_t max_doubled_size
= 4 * 1024 * 1024; /* 4 meg */
157 * List of all backing store and segments.
159 struct backing_store_list_head backing_store_list
;
160 paging_segment_t paging_segments
[MAX_NUM_PAGING_SEGMENTS
];
161 mutex_t paging_segments_lock
;
162 int paging_segment_max
= 0;
163 int paging_segment_count
= 0;
164 int ps_select_array
[BS_MAXPRI
+1] = { -1,-1,-1,-1,-1 };
168 * Total pages free in system
169 * This differs from clusters committed/avail which is a measure of the
170 * over commitment of paging segments to backing store. An idea which is
171 * likely to be deprecated.
173 unsigned int dp_pages_free
= 0;
174 unsigned int cluster_transfer_minimum
= 100;
176 kern_return_t
ps_write_file(paging_segment_t
, upl_t
, vm_offset_t
, vm_offset_t
, unsigned int, int); /* forward */
177 kern_return_t
ps_read_file (paging_segment_t
, upl_t
, vm_offset_t
, vm_offset_t
, unsigned int, unsigned int *, int); /* forward */
180 default_pager_thread_t
*
187 for (i
=0; i
<default_pager_internal_count
; i
++) {
188 if(dpt_array
[i
]->checked_out
== FALSE
) {
189 dpt_array
[i
]->checked_out
= TRUE
;
190 DPT_UNLOCK(dpt_lock
);
194 assert_wait(&dpt_array
, THREAD_UNINT
);
195 DPT_UNLOCK(dpt_lock
);
196 thread_block((void(*)(void))0);
206 * List of all backing store.
209 queue_init(&backing_store_list
.bsl_queue
);
212 VS_ASYNC_LOCK_INIT();
214 vs_async_free_list
= NULL
;
215 #endif /* VS_ASYNC_REUSE */
217 for (i
= 0; i
< VM_SUPER_PAGES
+ 1; i
++) {
218 clustered_writes
[i
] = 0;
219 clustered_reads
[i
] = 0;
225 * When things do not quite workout...
227 void bs_no_paging_space(boolean_t
); /* forward */
231 boolean_t out_of_memory
)
235 dprintf(("*** OUT OF MEMORY ***\n"));
236 panic("bs_no_paging_space: NOT ENOUGH PAGING SPACE");
239 void bs_more_space(int); /* forward */
240 void bs_commit(int); /* forward */
242 boolean_t user_warned
= FALSE
;
243 unsigned int clusters_committed
= 0;
244 unsigned int clusters_available
= 0;
245 unsigned int clusters_committed_peak
= 0;
253 * Account for new paging space.
255 clusters_available
+= nclusters
;
257 if (clusters_available
>= clusters_committed
) {
258 if (verbose
&& user_warned
) {
259 printf("%s%s - %d excess clusters now.\n",
261 "paging space is OK now",
262 clusters_available
- clusters_committed
);
264 clusters_committed_peak
= 0;
267 if (verbose
&& user_warned
) {
268 printf("%s%s - still short of %d clusters.\n",
270 "WARNING: paging space over-committed",
271 clusters_committed
- clusters_available
);
272 clusters_committed_peak
-= nclusters
;
285 clusters_committed
+= nclusters
;
286 if (clusters_committed
> clusters_available
) {
287 if (verbose
&& !user_warned
) {
289 printf("%s%s - short of %d clusters.\n",
291 "WARNING: paging space over-committed",
292 clusters_committed
- clusters_available
);
294 if (clusters_committed
> clusters_committed_peak
) {
295 clusters_committed_peak
= clusters_committed
;
298 if (verbose
&& user_warned
) {
299 printf("%s%s - was short of up to %d clusters.\n",
301 "paging space is OK now",
302 clusters_committed_peak
- clusters_available
);
304 clusters_committed_peak
= 0;
312 int default_pager_info_verbose
= 1;
319 vm_size_t pages_total
, pages_free
;
324 pages_total
= pages_free
= 0;
325 for (i
= 0; i
<= paging_segment_max
; i
++) {
326 ps
= paging_segments
[i
];
327 if (ps
== PAGING_SEGMENT_NULL
)
331 * no need to lock: by the time this data
332 * gets back to any remote requestor it
333 * will be obsolete anyways
335 pages_total
+= ps
->ps_pgnum
;
336 pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
337 DEBUG(DEBUG_BS_INTERNAL
,
338 ("segment #%d: %d total, %d free\n",
339 i
, ps
->ps_pgnum
, ps
->ps_clcount
<< ps
->ps_clshift
));
341 *totalp
= pages_total
;
343 if (verbose
&& user_warned
&& default_pager_info_verbose
) {
344 if (clusters_available
< clusters_committed
) {
345 printf("%s %d clusters committed, %d available.\n",
354 backing_store_t
backing_store_alloc(void); /* forward */
357 backing_store_alloc(void)
361 bs
= (backing_store_t
) kalloc(sizeof (struct backing_store
));
362 if (bs
== BACKING_STORE_NULL
)
363 panic("backing_store_alloc: no memory");
366 bs
->bs_port
= MACH_PORT_NULL
;
369 bs
->bs_pages_total
= 0;
371 bs
->bs_pages_in_fail
= 0;
372 bs
->bs_pages_out
= 0;
373 bs
->bs_pages_out_fail
= 0;
378 backing_store_t
backing_store_lookup(MACH_PORT_FACE
); /* forward */
380 /* Even in both the component space and external versions of this pager, */
381 /* backing_store_lookup will be called from tasks in the application space */
383 backing_store_lookup(
389 port is currently backed with a vs structure in the alias field
390 we could create an ISBS alias and a port_is_bs call but frankly
391 I see no reason for the test, the bs->port == port check below
392 will work properly on junk entries.
394 if ((port == MACH_PORT_NULL) || port_is_vs(port))
396 if ((port
== MACH_PORT_NULL
))
397 return BACKING_STORE_NULL
;
400 queue_iterate(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
403 if (bs
->bs_port
== port
) {
405 /* Success, return it locked. */
411 return BACKING_STORE_NULL
;
414 void backing_store_add(backing_store_t
); /* forward */
420 MACH_PORT_FACE port
= bs
->bs_port
;
421 MACH_PORT_FACE pset
= default_pager_default_set
;
422 kern_return_t kr
= KERN_SUCCESS
;
424 if (kr
!= KERN_SUCCESS
)
425 panic("backing_store_add: add to set");
430 * Set up default page shift, but only if not already
431 * set and argument is within range.
434 bs_set_default_clsize(unsigned int npages
)
441 if (default_pager_clsize
== 0) /* if not yet set */
442 vstruct_def_clshift
= local_log2(npages
);
448 int bs_get_global_clsize(int clsize
); /* forward */
451 bs_get_global_clsize(
455 memory_object_default_t dmm
;
459 * Only allow setting of cluster size once. If called
460 * with no cluster size (default), we use the compiled-in default
461 * for the duration. The same cluster size is used for all
464 if (default_pager_clsize
== 0) {
466 * Keep cluster size in bit shift because it's quicker
467 * arithmetic, and easier to keep at a power of 2.
469 if (clsize
!= NO_CLSIZE
) {
470 for (i
= 0; (1 << i
) < clsize
; i
++);
471 if (i
> MAX_CLUSTER_SHIFT
)
472 i
= MAX_CLUSTER_SHIFT
;
473 vstruct_def_clshift
= i
;
475 default_pager_clsize
= (1 << vstruct_def_clshift
);
478 * Let the user know the new (and definitive) cluster size.
481 printf("%scluster size = %d page%s\n",
482 my_name
, default_pager_clsize
,
483 (default_pager_clsize
== 1) ? "" : "s");
486 * Let the kernel know too, in case it hasn't used the
487 * default value provided in main() yet.
489 dmm
= default_pager_object
;
490 clsize
= default_pager_clsize
* vm_page_size
; /* in bytes */
491 kr
= host_default_memory_manager(host_priv_self(),
494 memory_object_default_deallocate(dmm
);
496 if (kr
!= KERN_SUCCESS
) {
497 panic("bs_get_global_cl_size:host_default_memory_manager");
499 if (dmm
!= default_pager_object
) {
500 panic("bs_get_global_cl_size:there is another default pager");
503 ASSERT(default_pager_clsize
> 0 &&
504 (default_pager_clsize
& (default_pager_clsize
- 1)) == 0);
506 return default_pager_clsize
;
510 default_pager_backing_store_create(
511 memory_object_default_t pager
,
513 int clsize
, /* in bytes */
514 MACH_PORT_FACE
*backing_store
)
519 struct vstruct_alias
*alias_struct
;
521 if (pager
!= default_pager_object
)
522 return KERN_INVALID_ARGUMENT
;
524 bs
= backing_store_alloc();
525 port
= ipc_port_alloc_kernel();
526 ipc_port_make_send(port
);
527 assert (port
!= IP_NULL
);
529 DEBUG(DEBUG_BS_EXTERNAL
,
530 ("priority=%d clsize=%d bs_port=0x%x\n",
531 priority
, clsize
, (int) backing_store
));
533 alias_struct
= (struct vstruct_alias
*)
534 kalloc(sizeof (struct vstruct_alias
));
535 if(alias_struct
!= NULL
) {
536 alias_struct
->vs
= (struct vstruct
*)bs
;
537 alias_struct
->name
= ISVS
;
538 port
->alias
= (int) alias_struct
;
541 ipc_port_dealloc_kernel((MACH_PORT_FACE
)(port
));
542 kfree((vm_offset_t
)bs
, sizeof (struct backing_store
));
543 return KERN_RESOURCE_SHORTAGE
;
547 if (priority
== DEFAULT_PAGER_BACKING_STORE_MAXPRI
)
548 priority
= BS_MAXPRI
;
549 else if (priority
== BS_NOPRI
)
550 priority
= BS_MAXPRI
;
552 priority
= BS_MINPRI
;
553 bs
->bs_priority
= priority
;
555 bs
->bs_clsize
= bs_get_global_clsize(atop(clsize
));
558 queue_enter(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
562 backing_store_add(bs
);
564 *backing_store
= port
;
569 default_pager_backing_store_info(
570 MACH_PORT_FACE backing_store
,
571 backing_store_flavor_t flavour
,
572 backing_store_info_t info
,
573 mach_msg_type_number_t
*size
)
576 backing_store_basic_info_t basic
;
580 if (flavour
!= BACKING_STORE_BASIC_INFO
||
581 *size
< BACKING_STORE_BASIC_INFO_COUNT
)
582 return KERN_INVALID_ARGUMENT
;
584 basic
= (backing_store_basic_info_t
)info
;
585 *size
= BACKING_STORE_BASIC_INFO_COUNT
;
587 VSTATS_LOCK(&global_stats
.gs_lock
);
588 basic
->pageout_calls
= global_stats
.gs_pageout_calls
;
589 basic
->pagein_calls
= global_stats
.gs_pagein_calls
;
590 basic
->pages_in
= global_stats
.gs_pages_in
;
591 basic
->pages_out
= global_stats
.gs_pages_out
;
592 basic
->pages_unavail
= global_stats
.gs_pages_unavail
;
593 basic
->pages_init
= global_stats
.gs_pages_init
;
594 basic
->pages_init_writes
= global_stats
.gs_pages_init_writes
;
595 VSTATS_UNLOCK(&global_stats
.gs_lock
);
597 if ((bs
= backing_store_lookup(backing_store
)) == BACKING_STORE_NULL
)
598 return KERN_INVALID_ARGUMENT
;
600 basic
->bs_pages_total
= bs
->bs_pages_total
;
602 bs
->bs_pages_free
= 0;
603 for (i
= 0; i
<= paging_segment_max
; i
++) {
604 ps
= paging_segments
[i
];
605 if (ps
!= PAGING_SEGMENT_NULL
&& ps
->ps_bs
== bs
) {
607 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
612 basic
->bs_pages_free
= bs
->bs_pages_free
;
613 basic
->bs_pages_in
= bs
->bs_pages_in
;
614 basic
->bs_pages_in_fail
= bs
->bs_pages_in_fail
;
615 basic
->bs_pages_out
= bs
->bs_pages_out
;
616 basic
->bs_pages_out_fail
= bs
->bs_pages_out_fail
;
618 basic
->bs_priority
= bs
->bs_priority
;
619 basic
->bs_clsize
= ptoa(bs
->bs_clsize
); /* in bytes */
626 int ps_delete(paging_segment_t
); /* forward */
633 kern_return_t error
= KERN_SUCCESS
;
636 VSL_LOCK(); /* get the lock on the list of vs's */
638 /* The lock relationship and sequence is farily complicated */
639 /* this code looks at a live list, locking and unlocking the list */
640 /* as it traverses it. It depends on the locking behavior of */
641 /* default_pager_no_senders. no_senders always locks the vstruct */
642 /* targeted for removal before locking the vstruct list. However */
643 /* it will remove that member of the list without locking its */
644 /* neighbors. We can be sure when we hold a lock on a vstruct */
645 /* it cannot be removed from the list but we must hold the list */
646 /* lock to be sure that its pointers to its neighbors are valid. */
647 /* Also, we can hold off destruction of a vstruct when the list */
648 /* lock and the vs locks are not being held by bumping the */
649 /* vs_async_pending count. */
652 while(backing_store_release_trigger_disable
!= 0) {
653 assert_wait((event_t
)
654 &backing_store_release_trigger_disable
,
657 thread_block((void (*)(void)) 0);
661 /* we will choose instead to hold a send right */
662 vs_count
= vstruct_list
.vsl_count
;
663 vs
= (vstruct_t
) queue_first((queue_entry_t
)&(vstruct_list
.vsl_queue
));
664 if(vs
== (vstruct_t
)&vstruct_list
) {
669 vs_async_wait(vs
); /* wait for any pending async writes */
670 if ((vs_count
!= 0) && (vs
!= NULL
))
671 vs
->vs_async_pending
+= 1; /* hold parties calling */
675 while((vs_count
!= 0) && (vs
!= NULL
)) {
676 /* We take the count of AMO's before beginning the */
677 /* transfer of of the target segment. */
678 /* We are guaranteed that the target segment cannot get */
679 /* more users. We also know that queue entries are */
680 /* made at the back of the list. If some of the entries */
681 /* we would check disappear while we are traversing the */
682 /* list then we will either check new entries which */
683 /* do not have any backing store in the target segment */
684 /* or re-check old entries. This might not be optimal */
685 /* but it will always be correct. The alternative is to */
686 /* take a snapshot of the list. */
689 if(dp_pages_free
< cluster_transfer_minimum
)
690 error
= KERN_FAILURE
;
692 vm_object_t transfer_object
;
696 transfer_object
= vm_object_allocate(VM_SUPER_CLUSTER
);
698 error
= vm_object_upl_request(transfer_object
,
699 (vm_object_offset_t
)0, VM_SUPER_CLUSTER
,
701 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
703 if(error
== KERN_SUCCESS
) {
704 #ifndef ubc_sync_working
705 upl_commit(upl
, NULL
);
707 error
= ps_vstruct_transfer_from_segment(
708 vs
, ps
, transfer_object
);
710 error
= ps_vstruct_transfer_from_segment(
712 upl_commit(upl
, NULL
);
715 vm_object_deallocate(transfer_object
);
717 vm_object_deallocate(transfer_object
);
718 error
= KERN_FAILURE
;
723 vs
->vs_async_pending
-= 1; /* release vs_async_wait */
724 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
725 vs
->vs_waiting_async
= FALSE
;
727 thread_wakeup(&vs
->vs_async_pending
);
736 while(backing_store_release_trigger_disable
!= 0) {
737 assert_wait((event_t
)
738 &backing_store_release_trigger_disable
,
741 thread_block((void (*)(void)) 0);
745 next_vs
= (vstruct_t
) queue_next(&(vs
->vs_links
));
746 if((next_vs
!= (vstruct_t
)&vstruct_list
) &&
747 (vs
!= next_vs
) && (vs_count
!= 1)) {
749 vs_async_wait(next_vs
); /* wait for any */
750 /* pending async writes */
751 next_vs
->vs_async_pending
+= 1; /* hold parties */
752 /* calling vs_async_wait */
757 vs
->vs_async_pending
-= 1;
758 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
759 vs
->vs_waiting_async
= FALSE
;
761 thread_wakeup(&vs
->vs_async_pending
);
765 if((vs
== next_vs
) || (next_vs
== (vstruct_t
)&vstruct_list
))
776 default_pager_backing_store_delete(
777 MACH_PORT_FACE backing_store
)
783 int interim_pages_removed
= 0;
786 if ((bs
= backing_store_lookup(backing_store
)) == BACKING_STORE_NULL
)
787 return KERN_INVALID_ARGUMENT
;
790 /* not implemented */
797 error
= KERN_SUCCESS
;
798 for (i
= 0; i
<= paging_segment_max
; i
++) {
799 ps
= paging_segments
[i
];
800 if (ps
!= PAGING_SEGMENT_NULL
&&
802 ! ps
->ps_going_away
) {
804 /* disable access to this segment */
805 ps
->ps_going_away
= TRUE
;
808 * The "ps" segment is "off-line" now,
809 * we can try and delete it...
811 if(dp_pages_free
< (cluster_transfer_minimum
813 error
= KERN_FAILURE
;
817 /* remove all pages associated with the */
818 /* segment from the list of free pages */
819 /* when transfer is through, all target */
820 /* segment pages will appear to be free */
822 dp_pages_free
-= ps
->ps_pgcount
;
823 interim_pages_removed
+= ps
->ps_pgcount
;
825 error
= ps_delete(ps
);
827 if (error
!= KERN_SUCCESS
) {
829 * We couldn't delete the segment,
830 * probably because there's not enough
831 * virtual memory left.
832 * Re-enable all the segments.
841 if (error
!= KERN_SUCCESS
) {
842 for (i
= 0; i
<= paging_segment_max
; i
++) {
843 ps
= paging_segments
[i
];
844 if (ps
!= PAGING_SEGMENT_NULL
&&
848 /* re-enable access to this segment */
849 ps
->ps_going_away
= FALSE
;
853 dp_pages_free
+= interim_pages_removed
;
859 for (i
= 0; i
<= paging_segment_max
; i
++) {
860 ps
= paging_segments
[i
];
861 if (ps
!= PAGING_SEGMENT_NULL
&&
863 if(ps
->ps_going_away
) {
864 paging_segments
[i
] = PAGING_SEGMENT_NULL
;
865 paging_segment_count
--;
867 kfree((vm_offset_t
)ps
->ps_bmap
,
868 RMAPSIZE(ps
->ps_ncls
));
869 kfree((vm_offset_t
)ps
, sizeof *ps
);
874 /* Scan the entire ps array separately to make certain we find the */
875 /* proper paging_segment_max */
876 for (i
= 0; i
< MAX_NUM_PAGING_SEGMENTS
; i
++) {
877 if(paging_segments
[i
] != PAGING_SEGMENT_NULL
)
878 paging_segment_max
= i
;
884 * All the segments have been deleted.
885 * We can remove the backing store.
889 * Disable lookups of this backing store.
891 if((void *)bs
->bs_port
->alias
!= NULL
)
892 kfree((vm_offset_t
) bs
->bs_port
->alias
,
893 sizeof (struct vstruct_alias
));
894 ipc_port_dealloc_kernel((ipc_port_t
) (bs
->bs_port
));
895 bs
->bs_port
= MACH_PORT_NULL
;
899 * Remove backing store from backing_store list.
902 queue_remove(&backing_store_list
.bsl_queue
, bs
, backing_store_t
,
907 * Free the backing store structure.
909 kfree((vm_offset_t
)bs
, sizeof *bs
);
914 int ps_enter(paging_segment_t
); /* forward */
924 for (i
= 0; i
< MAX_NUM_PAGING_SEGMENTS
; i
++) {
925 if (paging_segments
[i
] == PAGING_SEGMENT_NULL
)
929 if (i
< MAX_NUM_PAGING_SEGMENTS
) {
930 paging_segments
[i
] = ps
;
931 if (i
> paging_segment_max
)
932 paging_segment_max
= i
;
933 paging_segment_count
++;
934 if ((ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_NOPRI
) ||
935 (ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
))
936 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
940 return KERN_RESOURCE_SHORTAGE
;
949 default_pager_add_segment(
950 MACH_PORT_FACE backing_store
,
951 MACH_PORT_FACE device
,
961 if ((bs
= backing_store_lookup(backing_store
))
962 == BACKING_STORE_NULL
)
963 return KERN_INVALID_ARGUMENT
;
966 for (i
= 0; i
<= paging_segment_max
; i
++) {
967 ps
= paging_segments
[i
];
968 if (ps
== PAGING_SEGMENT_NULL
)
972 * Check for overlap on same device.
974 if (!(ps
->ps_device
!= device
975 || offset
>= ps
->ps_offset
+ ps
->ps_recnum
976 || offset
+ count
<= ps
->ps_offset
)) {
979 return KERN_INVALID_ARGUMENT
;
985 * Set up the paging segment
987 ps
= (paging_segment_t
) kalloc(sizeof (struct paging_segment
));
988 if (ps
== PAGING_SEGMENT_NULL
) {
990 return KERN_RESOURCE_SHORTAGE
;
993 ps
->ps_segtype
= PS_PARTITION
;
994 ps
->ps_device
= device
;
995 ps
->ps_offset
= offset
;
996 ps
->ps_record_shift
= local_log2(vm_page_size
/ record_size
);
997 ps
->ps_recnum
= count
;
998 ps
->ps_pgnum
= count
>> ps
->ps_record_shift
;
1000 ps
->ps_pgcount
= ps
->ps_pgnum
;
1001 ps
->ps_clshift
= local_log2(bs
->bs_clsize
);
1002 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
1006 ps
->ps_bmap
= (unsigned char *) kalloc(RMAPSIZE(ps
->ps_ncls
));
1008 kfree((vm_offset_t
)ps
, sizeof *ps
);
1010 return KERN_RESOURCE_SHORTAGE
;
1012 for (i
= 0; i
< ps
->ps_ncls
; i
++) {
1013 clrbit(ps
->ps_bmap
, i
);
1016 ps
->ps_going_away
= FALSE
;
1019 if ((error
= ps_enter(ps
)) != 0) {
1020 kfree((vm_offset_t
)ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
1021 kfree((vm_offset_t
)ps
, sizeof *ps
);
1023 return KERN_RESOURCE_SHORTAGE
;
1026 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
1027 bs
->bs_pages_total
+= ps
->ps_clcount
<< ps
->ps_clshift
;
1031 dp_pages_free
+= ps
->ps_pgcount
;
1034 bs_more_space(ps
->ps_clcount
);
1036 DEBUG(DEBUG_BS_INTERNAL
,
1037 ("device=0x%x,offset=0x%x,count=0x%x,record_size=0x%x,shift=%d,total_size=0x%x\n",
1038 device
, offset
, count
, record_size
,
1039 ps
->ps_record_shift
, ps
->ps_pgnum
));
1041 return KERN_SUCCESS
;
1047 MACH_PORT_FACE master
)
1049 security_token_t null_security_token
= {
1052 MACH_PORT_FACE device
;
1053 int info
[DEV_GET_SIZE_COUNT
];
1054 mach_msg_type_number_t info_count
;
1055 MACH_PORT_FACE bs
= MACH_PORT_NULL
;
1056 unsigned int rec_size
;
1059 MACH_PORT_FACE reply_port
;
1061 if (ds_device_open_sync(master
, MACH_PORT_NULL
, D_READ
| D_WRITE
,
1062 null_security_token
, dev_name
, &device
))
1065 info_count
= DEV_GET_SIZE_COUNT
;
1066 if (!ds_device_get_status(device
, DEV_GET_SIZE
, info
, &info_count
)) {
1067 rec_size
= info
[DEV_GET_SIZE_RECORD_SIZE
];
1068 count
= info
[DEV_GET_SIZE_DEVICE_SIZE
] / rec_size
;
1069 clsize
= bs_get_global_clsize(0);
1070 if (!default_pager_backing_store_create(
1071 default_pager_object
,
1072 DEFAULT_PAGER_BACKING_STORE_MAXPRI
,
1073 (clsize
* vm_page_size
),
1075 if (!default_pager_add_segment(bs
, device
,
1076 0, count
, rec_size
)) {
1079 ipc_port_release_receive(bs
);
1083 ipc_port_release_send(device
);
1086 #endif /* DEVICE_PAGING */
1091 vs_alloc_async(void)
1093 struct vs_async
*vsa
;
1094 MACH_PORT_FACE reply_port
;
1098 if (vs_async_free_list
== NULL
) {
1100 vsa
= (struct vs_async
*) kalloc(sizeof (struct vs_async
));
1103 * Try allocating a reply port named after the
1104 * address of the vs_async structure.
1106 struct vstruct_alias
*alias_struct
;
1108 reply_port
= ipc_port_alloc_kernel();
1109 alias_struct
= (struct vstruct_alias
*)
1110 kalloc(sizeof (struct vstruct_alias
));
1111 if(alias_struct
!= NULL
) {
1112 alias_struct
->vs
= (struct vstruct
*)vsa
;
1113 alias_struct
->name
= ISVS
;
1114 reply_port
->alias
= (int) alias_struct
;
1115 vsa
->reply_port
= reply_port
;
1116 vs_alloc_async_count
++;
1119 vs_alloc_async_failed
++;
1120 ipc_port_dealloc_kernel((MACH_PORT_FACE
)
1122 kfree((vm_offset_t
)vsa
,
1123 sizeof (struct vs_async
));
1128 vsa
= vs_async_free_list
;
1129 vs_async_free_list
= vs_async_free_list
->vsa_next
;
1138 struct vs_async
*vsa
)
1141 vsa
->vsa_next
= vs_async_free_list
;
1142 vs_async_free_list
= vsa
;
1146 #else /* VS_ASYNC_REUSE */
1149 vs_alloc_async(void)
1151 struct vs_async
*vsa
;
1152 MACH_PORT_FACE reply_port
;
1155 vsa
= (struct vs_async
*) kalloc(sizeof (struct vs_async
));
1158 * Try allocating a reply port named after the
1159 * address of the vs_async structure.
1161 reply_port
= ipc_port_alloc_kernel();
1162 alias_struct
= (vstruct_alias
*)
1163 kalloc(sizeof (struct vstruct_alias
));
1164 if(alias_struct
!= NULL
) {
1165 alias_struct
->vs
= reply_port
;
1166 alias_struct
->name
= ISVS
;
1167 reply_port
->alias
= (int) vsa
;
1168 vsa
->reply_port
= reply_port
;
1169 vs_alloc_async_count
++;
1172 vs_alloc_async_failed
++;
1173 ipc_port_dealloc_kernel((MACH_PORT_FACE
)
1175 kfree((vm_offset_t
) vsa
,
1176 sizeof (struct vs_async
));
1186 struct vs_async
*vsa
)
1188 MACH_PORT_FACE reply_port
;
1191 reply_port
= vsa
->reply_port
;
1192 kfree((vm_offset_t
) reply_port
->alias
, sizeof (struct vstuct_alias
));
1193 kfree((vm_offset_t
) vsa
, sizeof (struct vs_async
));
1194 ipc_port_dealloc_kernel((MACH_PORT_FACE
) (reply_port
));
1197 vs_alloc_async_count
--;
1202 #endif /* VS_ASYNC_REUSE */
1204 zone_t vstruct_zone
;
1213 vs
= (vstruct_t
) zalloc(vstruct_zone
);
1214 if (vs
== VSTRUCT_NULL
) {
1215 return VSTRUCT_NULL
;
1221 * The following fields will be provided later.
1223 vs
->vs_mem_obj
= NULL
;
1224 vs
->vs_control
= MEMORY_OBJECT_CONTROL_NULL
;
1225 vs
->vs_references
= 1;
1229 vs
->vs_waiting_seqno
= FALSE
;
1230 vs
->vs_waiting_read
= FALSE
;
1231 vs
->vs_waiting_write
= FALSE
;
1232 vs
->vs_waiting_async
= FALSE
;
1234 mutex_init(&vs
->vs_waiting_seqno
, ETAP_DPAGE_VSSEQNO
);
1235 mutex_init(&vs
->vs_waiting_read
, ETAP_DPAGE_VSREAD
);
1236 mutex_init(&vs
->vs_waiting_write
, ETAP_DPAGE_VSWRITE
);
1237 mutex_init(&vs
->vs_waiting_refs
, ETAP_DPAGE_VSREFS
);
1238 mutex_init(&vs
->vs_waiting_async
, ETAP_DPAGE_VSASYNC
);
1246 vs
->vs_clshift
= local_log2(bs_get_global_clsize(0));
1247 vs
->vs_size
= ((atop(round_page(size
)) - 1) >> vs
->vs_clshift
) + 1;
1248 vs
->vs_async_pending
= 0;
1251 * Allocate the pmap, either CLMAP_SIZE or INDIRECT_CLMAP_SIZE
1252 * depending on the size of the memory object.
1254 if (INDIRECT_CLMAP(vs
->vs_size
)) {
1255 vs
->vs_imap
= (struct vs_map
**)
1256 kalloc(INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1257 vs
->vs_indirect
= TRUE
;
1259 vs
->vs_dmap
= (struct vs_map
*)
1260 kalloc(CLMAP_SIZE(vs
->vs_size
));
1261 vs
->vs_indirect
= FALSE
;
1263 vs
->vs_xfer_pending
= FALSE
;
1264 DEBUG(DEBUG_VS_INTERNAL
,
1265 ("map=0x%x, indirect=%d\n", (int) vs
->vs_dmap
, vs
->vs_indirect
));
1268 * Check to see that we got the space.
1271 kfree((vm_offset_t
)vs
, sizeof *vs
);
1272 return VSTRUCT_NULL
;
1276 * Zero the indirect pointers, or clear the direct pointers.
1278 if (vs
->vs_indirect
)
1279 memset(vs
->vs_imap
, 0,
1280 INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1282 for (i
= 0; i
< vs
->vs_size
; i
++)
1283 VSM_CLR(vs
->vs_dmap
[i
]);
1285 VS_MAP_LOCK_INIT(vs
);
1287 bs_commit(vs
->vs_size
);
1292 paging_segment_t
ps_select_segment(int, int *); /* forward */
1299 paging_segment_t ps
;
1304 * Optimize case where there's only one segment.
1305 * paging_segment_max will index the one and only segment.
1309 if (paging_segment_count
== 1) {
1310 paging_segment_t lps
; /* used to avoid extra PS_UNLOCK */
1311 ipc_port_t trigger
= IP_NULL
;
1313 ps
= paging_segments
[paging_segment_max
];
1314 *psindex
= paging_segment_max
;
1316 if (ps
->ps_going_away
) {
1317 /* this segment is being turned off */
1318 lps
= PAGING_SEGMENT_NULL
;
1320 ASSERT(ps
->ps_clshift
>= shift
);
1321 if (ps
->ps_clcount
) {
1323 dp_pages_free
-= 1 << ps
->ps_clshift
;
1324 if(min_pages_trigger_port
&&
1325 (dp_pages_free
< minimum_pages_remaining
)) {
1326 trigger
= min_pages_trigger_port
;
1327 min_pages_trigger_port
= NULL
;
1332 lps
= PAGING_SEGMENT_NULL
;
1337 if (trigger
!= IP_NULL
) {
1338 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1339 ipc_port_release_send(trigger
);
1344 if (paging_segment_count
== 0) {
1346 return PAGING_SEGMENT_NULL
;
1350 i
>= BS_MINPRI
; i
--) {
1353 if ((ps_select_array
[i
] == BS_NOPRI
) ||
1354 (ps_select_array
[i
] == BS_FULLPRI
))
1356 start_index
= ps_select_array
[i
];
1358 if(!(paging_segments
[start_index
])) {
1360 physical_transfer_cluster_count
= 0;
1362 else if ((physical_transfer_cluster_count
+1) == (ALLOC_STRIDE
>>
1363 (((paging_segments
[start_index
])->ps_clshift
)
1364 + vm_page_shift
))) {
1365 physical_transfer_cluster_count
= 0;
1366 j
= start_index
+ 1;
1368 physical_transfer_cluster_count
+=1;
1370 if(start_index
== 0)
1371 start_index
= paging_segment_max
;
1373 start_index
= start_index
- 1;
1377 if (j
> paging_segment_max
)
1379 if ((ps
= paging_segments
[j
]) &&
1380 (ps
->ps_bs
->bs_priority
== i
)) {
1382 * Force the ps cluster size to be
1383 * >= that of the vstruct.
1386 if (ps
->ps_going_away
) {
1387 /* this segment is being turned off */
1388 } else if ((ps
->ps_clcount
) &&
1389 (ps
->ps_clshift
>= shift
)) {
1390 ipc_port_t trigger
= IP_NULL
;
1393 dp_pages_free
-= 1 << ps
->ps_clshift
;
1394 if(min_pages_trigger_port
&&
1396 minimum_pages_remaining
)) {
1397 trigger
= min_pages_trigger_port
;
1398 min_pages_trigger_port
= NULL
;
1402 * found one, quit looking.
1404 ps_select_array
[i
] = j
;
1407 if (trigger
!= IP_NULL
) {
1408 default_pager_space_alert(
1411 ipc_port_release_send(trigger
);
1418 if (j
== start_index
) {
1420 * none at this priority -- mark it full
1422 ps_select_array
[i
] = BS_FULLPRI
;
1429 return PAGING_SEGMENT_NULL
;
1432 vm_offset_t
ps_allocate_cluster(vstruct_t
, int *, paging_segment_t
); /*forward*/
1435 ps_allocate_cluster(
1438 paging_segment_t use_ps
)
1442 paging_segment_t ps
;
1443 vm_offset_t cluster
;
1444 ipc_port_t trigger
= IP_NULL
;
1447 * Find best paging segment.
1448 * ps_select_segment will decrement cluster count on ps.
1449 * Must pass cluster shift to find the most appropriate segment.
1451 /* NOTE: The addition of paging segment delete capability threatened
1452 * to seriously complicate the treatment of paging segments in this
1453 * module and the ones that call it (notably ps_clmap), because of the
1454 * difficulty in assuring that the paging segment would continue to
1455 * exist between being unlocked and locked. This was
1456 * avoided because all calls to this module are based in either
1457 * dp_memory_object calls which rely on the vs lock, or by
1458 * the transfer function which is part of the segment delete path.
1459 * The transfer function which is part of paging segment delete is
1460 * protected from multiple callers by the backing store lock.
1461 * The paging segment delete function treats mappings to a paging
1462 * segment on a vstruct by vstruct basis, locking the vstruct targeted
1463 * while data is transferred to the remaining segments. This is in
1464 * line with the view that incomplete or in-transition mappings between
1465 * data, a vstruct, and backing store are protected by the vs lock.
1466 * This and the ordering of the paging segment "going_away" bit setting
1469 if (use_ps
!= PAGING_SEGMENT_NULL
) {
1474 dp_pages_free
-= 1 << ps
->ps_clshift
;
1475 if(min_pages_trigger_port
&&
1476 (dp_pages_free
< minimum_pages_remaining
)) {
1477 trigger
= min_pages_trigger_port
;
1478 min_pages_trigger_port
= NULL
;
1482 if (trigger
!= IP_NULL
) {
1483 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1484 ipc_port_release_send(trigger
);
1487 } else if ((ps
= ps_select_segment(vs
->vs_clshift
, psindex
)) ==
1488 PAGING_SEGMENT_NULL
) {
1490 bs_no_paging_space(TRUE
);
1495 dprintf(("no space in available paging segments; "
1496 "swapon suggested\n"));
1497 /* the count got off maybe, reset to zero */
1500 if(min_pages_trigger_port
) {
1501 trigger
= min_pages_trigger_port
;
1502 min_pages_trigger_port
= NULL
;
1506 if (trigger
!= IP_NULL
) {
1507 default_pager_space_alert(trigger
, HI_WAT_ALERT
);
1508 ipc_port_release_send(trigger
);
1510 return (vm_offset_t
) -1;
1512 ASSERT(ps
->ps_clcount
!= 0);
1515 * Look for an available cluster. At the end of the loop,
1516 * byte_num is the byte offset and bit_num is the bit offset of the
1517 * first zero bit in the paging segment bitmap.
1520 byte_num
= ps
->ps_hint
;
1521 for (; byte_num
< howmany(ps
->ps_ncls
, NBBY
); byte_num
++) {
1522 if (*(ps
->ps_bmap
+ byte_num
) != BYTEMASK
) {
1523 for (bit_num
= 0; bit_num
< NBBY
; bit_num
++) {
1524 if (isclr((ps
->ps_bmap
+ byte_num
), bit_num
))
1527 ASSERT(bit_num
!= NBBY
);
1531 ps
->ps_hint
= byte_num
;
1532 cluster
= (byte_num
*NBBY
) + bit_num
;
1534 /* Space was reserved, so this must be true */
1535 ASSERT(cluster
< ps
->ps_ncls
);
1537 setbit(ps
->ps_bmap
, cluster
);
1543 void ps_deallocate_cluster(paging_segment_t
, vm_offset_t
); /* forward */
1546 ps_deallocate_cluster(
1547 paging_segment_t ps
,
1548 vm_offset_t cluster
)
1550 ipc_port_t trigger
= IP_NULL
;
1552 if (cluster
>= (vm_offset_t
) ps
->ps_ncls
)
1553 panic("ps_deallocate_cluster: Invalid cluster number");
1556 * Lock the paging segment, clear the cluster's bitmap and increment the
1557 * number of free cluster.
1561 clrbit(ps
->ps_bmap
, cluster
);
1563 dp_pages_free
+= 1 << ps
->ps_clshift
;
1564 if(max_pages_trigger_port
1565 && (backing_store_release_trigger_disable
== 0)
1566 && (dp_pages_free
> maximum_pages_free
)) {
1567 trigger
= max_pages_trigger_port
;
1568 max_pages_trigger_port
= NULL
;
1573 * Move the hint down to the freed cluster if it is
1574 * less than the current hint.
1576 if ((cluster
/NBBY
) < ps
->ps_hint
) {
1577 ps
->ps_hint
= (cluster
/NBBY
);
1583 * If we're freeing space on a full priority, reset the array.
1586 if (ps_select_array
[ps
->ps_bs
->bs_priority
] == BS_FULLPRI
)
1587 ps_select_array
[ps
->ps_bs
->bs_priority
] = 0;
1590 if (trigger
!= IP_NULL
) {
1592 if(backing_store_release_trigger_disable
!= 0) {
1593 assert_wait((event_t
)
1594 &backing_store_release_trigger_disable
,
1597 thread_block((void (*)(void)) 0);
1601 default_pager_space_alert(trigger
, LO_WAT_ALERT
);
1602 ipc_port_release_send(trigger
);
1608 void ps_dealloc_vsmap(struct vs_map
*, vm_size_t
); /* forward */
1612 struct vs_map
*vsmap
,
1616 for (i
= 0; i
< size
; i
++)
1617 if (!VSM_ISCLR(vsmap
[i
]) && !VSM_ISERR(vsmap
[i
]))
1618 ps_deallocate_cluster(VSM_PS(vsmap
[i
]),
1619 VSM_CLOFF(vsmap
[i
]));
1632 * If this is an indirect structure, then we walk through the valid
1633 * (non-zero) indirect pointers and deallocate the clusters
1634 * associated with each used map entry (via ps_dealloc_vsmap).
1635 * When all of the clusters in an indirect block have been
1636 * freed, we deallocate the block. When all of the indirect
1637 * blocks have been deallocated we deallocate the memory
1638 * holding the indirect pointers.
1640 if (vs
->vs_indirect
) {
1641 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
1642 if (vs
->vs_imap
[i
] != NULL
) {
1643 ps_dealloc_vsmap(vs
->vs_imap
[i
], CLMAP_ENTRIES
);
1644 kfree((vm_offset_t
)vs
->vs_imap
[i
],
1648 kfree((vm_offset_t
)vs
->vs_imap
,
1649 INDIRECT_CLMAP_SIZE(vs
->vs_size
));
1652 * Direct map. Free used clusters, then memory.
1654 ps_dealloc_vsmap(vs
->vs_dmap
, vs
->vs_size
);
1655 kfree((vm_offset_t
)vs
->vs_dmap
, CLMAP_SIZE(vs
->vs_size
));
1659 bs_commit(- vs
->vs_size
);
1661 zfree(vstruct_zone
, (vm_offset_t
)vs
);
1664 int ps_map_extend(vstruct_t
, int); /* forward */
1670 struct vs_map
**new_imap
;
1671 struct vs_map
*new_dmap
= NULL
;
1674 void *old_map
= NULL
;
1675 int old_map_size
= 0;
1677 if (vs
->vs_size
>= new_size
) {
1679 * Someone has already done the work.
1685 * If the new size extends into the indirect range, then we have one
1686 * of two cases: we are going from indirect to indirect, or we are
1687 * going from direct to indirect. If we are going from indirect to
1688 * indirect, then it is possible that the new size will fit in the old
1689 * indirect map. If this is the case, then just reset the size of the
1690 * vstruct map and we are done. If the new size will not
1691 * fit into the old indirect map, then we have to allocate a new
1692 * indirect map and copy the old map pointers into this new map.
1694 * If we are going from direct to indirect, then we have to allocate a
1695 * new indirect map and copy the old direct pages into the first
1696 * indirect page of the new map.
1697 * NOTE: allocating memory here is dangerous, as we're in the
1700 if (INDIRECT_CLMAP(new_size
)) {
1701 int new_map_size
= INDIRECT_CLMAP_SIZE(new_size
);
1704 * Get a new indirect map and zero it.
1706 old_map_size
= INDIRECT_CLMAP_SIZE(vs
->vs_size
);
1707 if (vs
->vs_indirect
&&
1708 (new_map_size
== old_map_size
)) {
1709 bs_commit(new_size
- vs
->vs_size
);
1710 vs
->vs_size
= new_size
;
1714 new_imap
= (struct vs_map
**)kalloc(new_map_size
);
1715 if (new_imap
== NULL
) {
1718 memset(new_imap
, 0, new_map_size
);
1720 if (vs
->vs_indirect
) {
1721 /* Copy old entries into new map */
1722 memcpy(new_imap
, vs
->vs_imap
, old_map_size
);
1723 /* Arrange to free the old map */
1724 old_map
= (void *) vs
->vs_imap
;
1726 } else { /* Old map was a direct map */
1727 /* Allocate an indirect page */
1728 if ((new_imap
[0] = (struct vs_map
*)
1729 kalloc(CLMAP_THRESHOLD
)) == NULL
) {
1730 kfree((vm_offset_t
)new_imap
, new_map_size
);
1733 new_dmap
= new_imap
[0];
1734 newdsize
= CLMAP_ENTRIES
;
1738 newdsize
= new_size
;
1740 * If the new map is a direct map, then the old map must
1741 * also have been a direct map. All we have to do is
1742 * to allocate a new direct map, copy the old entries
1743 * into it and free the old map.
1745 if ((new_dmap
= (struct vs_map
*)
1746 kalloc(CLMAP_SIZE(new_size
))) == NULL
) {
1752 /* Free the old map */
1753 old_map
= (void *) vs
->vs_dmap
;
1754 old_map_size
= CLMAP_SIZE(vs
->vs_size
);
1756 /* Copy info from the old map into the new map */
1757 memcpy(new_dmap
, vs
->vs_dmap
, old_map_size
);
1759 /* Initialize the rest of the new map */
1760 for (i
= vs
->vs_size
; i
< newdsize
; i
++)
1761 VSM_CLR(new_dmap
[i
]);
1764 vs
->vs_imap
= new_imap
;
1765 vs
->vs_indirect
= TRUE
;
1767 vs
->vs_dmap
= new_dmap
;
1768 bs_commit(new_size
- vs
->vs_size
);
1769 vs
->vs_size
= new_size
;
1771 kfree((vm_offset_t
)old_map
, old_map_size
);
1779 struct clmap
*clmap
,
1784 vm_offset_t cluster
; /* The cluster of offset. */
1785 vm_offset_t newcl
; /* The new cluster allocated. */
1788 struct vs_map
*vsmap
;
1792 ASSERT(vs
->vs_dmap
);
1793 cluster
= atop(offset
) >> vs
->vs_clshift
;
1796 * Initialize cluster error value
1798 clmap
->cl_error
= 0;
1801 * If the object has grown, extend the page map.
1803 if (cluster
>= vs
->vs_size
) {
1804 if (flag
== CL_FIND
) {
1805 /* Do not allocate if just doing a lookup */
1807 return (vm_offset_t
) -1;
1809 if (ps_map_extend(vs
, cluster
+ 1)) {
1811 return (vm_offset_t
) -1;
1816 * Look for the desired cluster. If the map is indirect, then we
1817 * have a two level lookup. First find the indirect block, then
1818 * find the actual cluster. If the indirect block has not yet
1819 * been allocated, then do so. If the cluster has not yet been
1820 * allocated, then do so.
1822 * If any of the allocations fail, then return an error.
1823 * Don't allocate if just doing a lookup.
1825 if (vs
->vs_indirect
) {
1826 long ind_block
= cluster
/CLMAP_ENTRIES
;
1828 /* Is the indirect block allocated? */
1829 vsmap
= vs
->vs_imap
[ind_block
];
1830 if (vsmap
== NULL
) {
1831 if (flag
== CL_FIND
) {
1833 return (vm_offset_t
) -1;
1836 /* Allocate the indirect block */
1837 vsmap
= (struct vs_map
*) kalloc(CLMAP_THRESHOLD
);
1838 if (vsmap
== NULL
) {
1840 return (vm_offset_t
) -1;
1842 /* Initialize the cluster offsets */
1843 for (i
= 0; i
< CLMAP_ENTRIES
; i
++)
1845 vs
->vs_imap
[ind_block
] = vsmap
;
1848 vsmap
= vs
->vs_dmap
;
1851 vsmap
+= cluster%CLMAP_ENTRIES
;
1854 * At this point, vsmap points to the struct vs_map desired.
1856 * Look in the map for the cluster, if there was an error on a
1857 * previous write, flag it and return. If it is not yet
1858 * allocated, then allocate it, if we're writing; if we're
1859 * doing a lookup and the cluster's not allocated, return error.
1861 if (VSM_ISERR(*vsmap
)) {
1862 clmap
->cl_error
= VSM_GETERR(*vsmap
);
1864 return (vm_offset_t
) -1;
1865 } else if (VSM_ISCLR(*vsmap
)) {
1868 if (flag
== CL_FIND
) {
1870 * If there's an error and the entry is clear, then
1871 * we've run out of swap space. Record the error
1875 VSM_SETERR(*vsmap
, error
);
1878 return (vm_offset_t
) -1;
1881 * Attempt to allocate a cluster from the paging segment
1883 newcl
= ps_allocate_cluster(vs
, &psindex
,
1884 PAGING_SEGMENT_NULL
);
1887 return (vm_offset_t
) -1;
1890 VSM_SETCLOFF(*vsmap
, newcl
);
1891 VSM_SETPS(*vsmap
, psindex
);
1894 newcl
= VSM_CLOFF(*vsmap
);
1897 * Fill in pertinent fields of the clmap
1899 clmap
->cl_ps
= VSM_PS(*vsmap
);
1900 clmap
->cl_numpages
= VSCLSIZE(vs
);
1901 clmap
->cl_bmap
.clb_map
= (unsigned int) VSM_BMAP(*vsmap
);
1904 * Byte offset in paging segment is byte offset to cluster plus
1905 * byte offset within cluster. It looks ugly, but should be
1908 ASSERT(trunc_page(offset
) == offset
);
1909 newcl
= ptoa(newcl
) << vs
->vs_clshift
;
1910 newoff
= offset
& ((1<<(vm_page_shift
+ vs
->vs_clshift
)) - 1);
1911 if (flag
== CL_ALLOC
) {
1913 * set bits in the allocation bitmap according to which
1914 * pages were requested. size is in bytes.
1917 while ((size
> 0) && (i
< VSCLSIZE(vs
))) {
1918 VSM_SETALLOC(*vsmap
, i
);
1920 size
-= vm_page_size
;
1923 clmap
->cl_alloc
.clb_map
= (unsigned int) VSM_ALLOC(*vsmap
);
1926 * Offset is not cluster aligned, so number of pages
1927 * and bitmaps must be adjusted
1929 clmap
->cl_numpages
-= atop(newoff
);
1930 CLMAP_SHIFT(clmap
, vs
);
1931 CLMAP_SHIFTALLOC(clmap
, vs
);
1936 * The setting of valid bits and handling of write errors
1937 * must be done here, while we hold the lock on the map.
1938 * It logically should be done in ps_vs_write_complete().
1939 * The size and error information has been passed from
1940 * ps_vs_write_complete(). If the size parameter is non-zero,
1941 * then there is work to be done. If error is also non-zero,
1942 * then the error number is recorded in the cluster and the
1943 * entire cluster is in error.
1945 if (size
&& flag
== CL_FIND
) {
1946 vm_offset_t off
= (vm_offset_t
) 0;
1949 for (i
= VSCLSIZE(vs
) - clmap
->cl_numpages
; size
> 0;
1951 VSM_SETPG(*vsmap
, i
);
1952 size
-= vm_page_size
;
1954 ASSERT(i
<= VSCLSIZE(vs
));
1956 BS_STAT(clmap
->cl_ps
->ps_bs
,
1957 clmap
->cl_ps
->ps_bs
->bs_pages_out_fail
+=
1959 off
= VSM_CLOFF(*vsmap
);
1960 VSM_SETERR(*vsmap
, error
);
1963 * Deallocate cluster if error, and no valid pages
1966 if (off
!= (vm_offset_t
) 0)
1967 ps_deallocate_cluster(clmap
->cl_ps
, off
);
1969 return (vm_offset_t
) 0;
1973 DEBUG(DEBUG_VS_INTERNAL
,
1974 ("returning 0x%X,vs=0x%X,vsmap=0x%X,flag=%d\n",
1975 newcl
+newoff
, (int) vs
, (int) vsmap
, flag
));
1976 DEBUG(DEBUG_VS_INTERNAL
,
1977 (" clmap->cl_ps=0x%X,cl_numpages=%d,clbmap=0x%x,cl_alloc=%x\n",
1978 (int) clmap
->cl_ps
, clmap
->cl_numpages
,
1979 (int) clmap
->cl_bmap
.clb_map
, (int) clmap
->cl_alloc
.clb_map
));
1981 return (newcl
+ newoff
);
1984 void ps_clunmap(vstruct_t
, vm_offset_t
, vm_size_t
); /* forward */
1992 vm_offset_t cluster
; /* The cluster number of offset */
1993 struct vs_map
*vsmap
;
1998 * Loop through all clusters in this range, freeing paging segment
1999 * clusters and map entries as encountered.
2001 while (length
> 0) {
2005 cluster
= atop(offset
) >> vs
->vs_clshift
;
2006 if (vs
->vs_indirect
) /* indirect map */
2007 vsmap
= vs
->vs_imap
[cluster
/CLMAP_ENTRIES
];
2009 vsmap
= vs
->vs_dmap
;
2010 if (vsmap
== NULL
) {
2014 vsmap
+= cluster%CLMAP_ENTRIES
;
2015 if (VSM_ISCLR(*vsmap
)) {
2016 length
-= vm_page_size
;
2017 offset
+= vm_page_size
;
2021 * We've got a valid mapping. Clear it and deallocate
2022 * paging segment cluster pages.
2023 * Optimize for entire cluster cleraing.
2025 if (newoff
= (offset
&((1<<(vm_page_shift
+vs
->vs_clshift
))-1))) {
2027 * Not cluster aligned.
2029 ASSERT(trunc_page(newoff
) == newoff
);
2033 while ((i
< VSCLSIZE(vs
)) && (length
> 0)) {
2034 VSM_CLRPG(*vsmap
, i
);
2035 VSM_CLRALLOC(*vsmap
, i
);
2036 length
-= vm_page_size
;
2037 offset
+= vm_page_size
;
2042 * If map entry is empty, clear and deallocate cluster.
2044 if (!VSM_ALLOC(*vsmap
)) {
2045 ps_deallocate_cluster(VSM_PS(*vsmap
),
2054 void ps_vs_write_complete(vstruct_t
, vm_offset_t
, vm_size_t
, int); /* forward */
2057 ps_vs_write_complete(
2066 * Get the struct vsmap for this cluster.
2067 * Use READ, even though it was written, because the
2068 * cluster MUST be present, unless there was an error
2069 * in the original ps_clmap (e.g. no space), in which
2070 * case, nothing happens.
2072 * Must pass enough information to ps_clmap to allow it
2073 * to set the vs_map structure bitmap under lock.
2075 (void) ps_clmap(vs
, offset
, &clmap
, CL_FIND
, size
, error
);
2078 void vs_cl_write_complete(vstruct_t
, paging_segment_t
, vm_offset_t
, vm_offset_t
, vm_size_t
, boolean_t
, int); /* forward */
2081 vs_cl_write_complete(
2083 paging_segment_t ps
,
2094 * For internal objects, the error is recorded on a
2095 * per-cluster basis by ps_clmap() which is called
2096 * by ps_vs_write_complete() below.
2098 dprintf(("write failed error = 0x%x\n", error
));
2099 /* add upl_abort code here */
2101 GSTAT(global_stats
.gs_pages_out
+= atop(size
));
2103 * Notify the vstruct mapping code, so it can do its accounting.
2105 ps_vs_write_complete(vs
, offset
, size
, error
);
2109 ASSERT(vs
->vs_async_pending
> 0);
2110 vs
->vs_async_pending
-= size
;
2111 if (vs
->vs_async_pending
== 0 && vs
->vs_waiting_async
) {
2112 vs
->vs_waiting_async
= FALSE
;
2114 /* mutex_unlock(&vs->vs_waiting_async); */
2115 thread_wakeup(&vs
->vs_async_pending
);
2122 #ifdef DEVICE_PAGING
2123 kern_return_t
device_write_reply(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2127 MACH_PORT_FACE reply_port
,
2128 kern_return_t device_code
,
2129 io_buf_len_t bytes_written
)
2131 struct vs_async
*vsa
;
2133 vsa
= (struct vs_async
*)
2134 ((struct vstruct_alias
*)(reply_port
->alias
))->vs
;
2136 if (device_code
== KERN_SUCCESS
&& bytes_written
!= vsa
->vsa_size
) {
2137 device_code
= KERN_FAILURE
;
2140 vsa
->vsa_error
= device_code
;
2143 ASSERT(vsa
->vsa_vs
!= VSTRUCT_NULL
);
2144 if(vsa
->vsa_flags
& VSA_TRANSFER
) {
2145 /* revisit when async disk segments redone */
2146 if(vsa
->vsa_error
) {
2147 /* need to consider error condition. re-write data or */
2148 /* throw it away here. */
2150 if(vm_map_copyout(kernel_map
, &ioaddr
,
2151 (vm_map_copy_t
)vsa
->vsa_addr
) != KERN_SUCCESS
)
2152 panic("vs_cluster_write: unable to copy source list\n");
2153 vm_deallocate(kernel_map
, ioaddr
, vsa
->vsa_size
);
2155 ps_vs_write_complete(vsa
->vsa_vs
, vsa
->vsa_offset
,
2156 vsa
->vsa_size
, vsa
->vsa_error
);
2158 vs_cl_write_complete(vsa
->vsa_vs
, vsa
->vsa_ps
, vsa
->vsa_offset
,
2159 vsa
->vsa_addr
, vsa
->vsa_size
, TRUE
,
2164 return KERN_SUCCESS
;
2167 kern_return_t
device_write_reply_inband(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2169 device_write_reply_inband(
2170 MACH_PORT_FACE reply_port
,
2171 kern_return_t return_code
,
2172 io_buf_len_t bytes_written
)
2174 panic("device_write_reply_inband: illegal");
2175 return KERN_SUCCESS
;
2178 kern_return_t
device_read_reply(MACH_PORT_FACE
, kern_return_t
, io_buf_ptr_t
, mach_msg_type_number_t
);
2181 MACH_PORT_FACE reply_port
,
2182 kern_return_t return_code
,
2184 mach_msg_type_number_t dataCnt
)
2186 struct vs_async
*vsa
;
2187 vsa
= (struct vs_async
*)
2188 ((struct vstruct_alias
*)(reply_port
->alias
))->vs
;
2189 vsa
->vsa_addr
= (vm_offset_t
)data
;
2190 vsa
->vsa_size
= (vm_size_t
)dataCnt
;
2191 vsa
->vsa_error
= return_code
;
2192 thread_wakeup(&vsa
->vsa_lock
);
2193 return KERN_SUCCESS
;
2196 kern_return_t
device_read_reply_inband(MACH_PORT_FACE
, kern_return_t
, io_buf_ptr_inband_t
, mach_msg_type_number_t
);
2198 device_read_reply_inband(
2199 MACH_PORT_FACE reply_port
,
2200 kern_return_t return_code
,
2201 io_buf_ptr_inband_t data
,
2202 mach_msg_type_number_t dataCnt
)
2204 panic("device_read_reply_inband: illegal");
2205 return KERN_SUCCESS
;
2208 kern_return_t
device_read_reply_overwrite(MACH_PORT_FACE
, kern_return_t
, io_buf_len_t
);
2210 device_read_reply_overwrite(
2211 MACH_PORT_FACE reply_port
,
2212 kern_return_t return_code
,
2213 io_buf_len_t bytes_read
)
2215 panic("device_read_reply_overwrite: illegal\n");
2216 return KERN_SUCCESS
;
2219 kern_return_t
device_open_reply(MACH_PORT_FACE
, kern_return_t
, MACH_PORT_FACE
);
2222 MACH_PORT_FACE reply_port
,
2223 kern_return_t return_code
,
2224 MACH_PORT_FACE device_port
)
2226 panic("device_open_reply: illegal\n");
2227 return KERN_SUCCESS
;
2230 kern_return_t
ps_read_device(paging_segment_t
, vm_offset_t
, vm_offset_t
*, unsigned int, unsigned int *, int); /* forward */
2234 paging_segment_t ps
,
2236 vm_offset_t
*bufferp
,
2238 unsigned int *residualp
,
2242 recnum_t dev_offset
;
2243 unsigned int bytes_wanted
;
2244 unsigned int bytes_read
;
2245 unsigned int total_read
;
2246 vm_offset_t dev_buffer
;
2247 vm_offset_t buf_ptr
;
2248 unsigned int records_read
;
2249 struct vs_async
*vsa
;
2250 mutex_t vs_waiting_read_reply
;
2253 vm_map_copy_t device_data
= NULL
;
2254 default_pager_thread_t
*dpt
= NULL
;
2256 device
= dev_port_lookup(ps
->ps_device
);
2257 clustered_reads
[atop(size
)]++;
2259 dev_offset
= (ps
->ps_offset
+
2260 (offset
>> (vm_page_shift
- ps
->ps_record_shift
)));
2261 bytes_wanted
= size
;
2263 *bufferp
= (vm_offset_t
)NULL
;
2266 vsa
= VS_ALLOC_ASYNC();
2270 vsa
->vsa_offset
= 0;
2274 mutex_init(&vsa
->vsa_lock
, ETAP_DPAGE_VSSEQNO
);
2275 ip_lock(vsa
->reply_port
);
2276 vsa
->reply_port
->ip_sorights
++;
2277 ip_reference(vsa
->reply_port
);
2278 ip_unlock(vsa
->reply_port
);
2279 kr
= ds_device_read_common(device
,
2281 (mach_msg_type_name_t
)
2282 MACH_MSG_TYPE_MOVE_SEND_ONCE
,
2286 (IO_READ
| IO_CALL
),
2287 (io_buf_ptr_t
*) &dev_buffer
,
2288 (mach_msg_type_number_t
*) &bytes_read
);
2289 if(kr
== MIG_NO_REPLY
) {
2290 assert_wait(&vsa
->vsa_lock
, THREAD_UNINT
);
2291 thread_block((void(*)(void))0);
2293 dev_buffer
= vsa
->vsa_addr
;
2294 bytes_read
= (unsigned int)vsa
->vsa_size
;
2295 kr
= vsa
->vsa_error
;
2298 if (kr
!= KERN_SUCCESS
|| bytes_read
== 0) {
2301 total_read
+= bytes_read
;
2304 * If we got the entire range, use the returned dev_buffer.
2306 if (bytes_read
== size
) {
2307 *bufferp
= (vm_offset_t
)dev_buffer
;
2312 dprintf(("read only %d bytes out of %d\n",
2313 bytes_read
, bytes_wanted
));
2316 dpt
= get_read_buffer();
2317 buf_ptr
= dpt
->dpt_buffer
;
2318 *bufferp
= (vm_offset_t
)buf_ptr
;
2321 * Otherwise, copy the data into the provided buffer (*bufferp)
2322 * and append the rest of the range as it comes in.
2324 memcpy((void *) buf_ptr
, (void *) dev_buffer
, bytes_read
);
2325 buf_ptr
+= bytes_read
;
2326 bytes_wanted
-= bytes_read
;
2327 records_read
= (bytes_read
>>
2328 (vm_page_shift
- ps
->ps_record_shift
));
2329 dev_offset
+= records_read
;
2330 DEBUG(DEBUG_VS_INTERNAL
,
2331 ("calling vm_deallocate(addr=0x%X,size=0x%X)\n",
2332 dev_buffer
, bytes_read
));
2333 if (vm_deallocate(kernel_map
, dev_buffer
, bytes_read
)
2335 Panic("dealloc buf");
2336 } while (bytes_wanted
);
2338 *residualp
= size
- total_read
;
2339 if((dev_buffer
!= *bufferp
) && (total_read
!= 0)) {
2340 vm_offset_t temp_buffer
;
2341 vm_allocate(kernel_map
, &temp_buffer
, total_read
, TRUE
);
2342 memcpy((void *) temp_buffer
, (void *) *bufferp
, total_read
);
2343 if(vm_map_copyin_page_list(kernel_map
, temp_buffer
, total_read
,
2344 VM_MAP_COPYIN_OPT_SRC_DESTROY
|
2345 VM_MAP_COPYIN_OPT_STEAL_PAGES
|
2346 VM_MAP_COPYIN_OPT_PMAP_ENTER
,
2347 (vm_map_copy_t
*)&device_data
, FALSE
))
2348 panic("ps_read_device: cannot copyin locally provided buffer\n");
2350 else if((kr
== KERN_SUCCESS
) && (total_read
!= 0) && (dev_buffer
!= 0)){
2351 if(vm_map_copyin_page_list(kernel_map
, dev_buffer
, bytes_read
,
2352 VM_MAP_COPYIN_OPT_SRC_DESTROY
|
2353 VM_MAP_COPYIN_OPT_STEAL_PAGES
|
2354 VM_MAP_COPYIN_OPT_PMAP_ENTER
,
2355 (vm_map_copy_t
*)&device_data
, FALSE
))
2356 panic("ps_read_device: cannot copyin backing store provided buffer\n");
2361 *bufferp
= (vm_offset_t
)device_data
;
2364 /* Free the receive buffer */
2365 dpt
->checked_out
= 0;
2366 thread_wakeup(&dpt_array
);
2368 return KERN_SUCCESS
;
2371 kern_return_t
ps_write_device(paging_segment_t
, vm_offset_t
, vm_offset_t
, unsigned int, struct vs_async
*); /* forward */
2375 paging_segment_t ps
,
2379 struct vs_async
*vsa
)
2381 recnum_t dev_offset
;
2382 io_buf_len_t bytes_to_write
, bytes_written
;
2383 recnum_t records_written
;
2385 MACH_PORT_FACE reply_port
;
2389 clustered_writes
[atop(size
)]++;
2391 dev_offset
= (ps
->ps_offset
+
2392 (offset
>> (vm_page_shift
- ps
->ps_record_shift
)));
2393 bytes_to_write
= size
;
2397 * Asynchronous write.
2399 reply_port
= vsa
->reply_port
;
2400 ip_lock(reply_port
);
2401 reply_port
->ip_sorights
++;
2402 ip_reference(reply_port
);
2403 ip_unlock(reply_port
);
2406 device
= dev_port_lookup(ps
->ps_device
);
2408 vsa
->vsa_addr
= addr
;
2409 kr
=ds_device_write_common(device
,
2411 (mach_msg_type_name_t
) MACH_MSG_TYPE_MOVE_SEND_ONCE
,
2414 (io_buf_ptr_t
) addr
,
2416 (IO_WRITE
| IO_CALL
),
2419 if ((kr
!= KERN_SUCCESS
) && (kr
!= MIG_NO_REPLY
)) {
2421 dprintf(("%s0x%x, addr=0x%x,"
2422 "size=0x%x,offset=0x%x\n",
2423 "device_write_request returned ",
2424 kr
, addr
, size
, offset
));
2426 ps
->ps_bs
->bs_pages_out_fail
+= atop(size
));
2427 /* do the completion notification to free resources */
2428 device_write_reply(reply_port
, kr
, 0);
2433 * Synchronous write.
2437 device
= dev_port_lookup(ps
->ps_device
);
2438 kr
=ds_device_write_common(device
,
2442 (io_buf_ptr_t
) addr
,
2444 (IO_WRITE
| IO_SYNC
| IO_KERNEL_BUF
),
2447 if (kr
!= KERN_SUCCESS
) {
2448 dprintf(("%s0x%x, addr=0x%x,size=0x%x,offset=0x%x\n",
2449 "device_write returned ",
2450 kr
, addr
, size
, offset
));
2452 ps
->ps_bs
->bs_pages_out_fail
+= atop(size
));
2455 if (bytes_written
& ((vm_page_size
>> ps
->ps_record_shift
) - 1))
2456 Panic("fragmented write");
2457 records_written
= (bytes_written
>>
2458 (vm_page_shift
- ps
->ps_record_shift
));
2459 dev_offset
+= records_written
;
2461 if (bytes_written
!= bytes_to_write
) {
2462 dprintf(("wrote only %d bytes out of %d\n",
2463 bytes_written
, bytes_to_write
));
2466 bytes_to_write
-= bytes_written
;
2467 addr
+= bytes_written
;
2468 } while (bytes_to_write
> 0);
2470 return PAGER_SUCCESS
;
2474 #else /* !DEVICE_PAGING */
2478 paging_segment_t ps
,
2480 vm_offset_t
*bufferp
,
2482 unsigned int *residualp
,
2485 panic("ps_read_device not supported");
2489 paging_segment_t ps
,
2493 struct vs_async
*vsa
)
2495 panic("ps_write_device not supported");
2498 #endif /* DEVICE_PAGING */
2499 void pvs_object_data_provided(vstruct_t
, upl_t
, vm_offset_t
, vm_size_t
); /* forward */
2502 pvs_object_data_provided(
2509 DEBUG(DEBUG_VS_INTERNAL
,
2510 ("buffer=0x%x,offset=0x%x,size=0x%x\n",
2511 upl
, offset
, size
));
2514 GSTAT(global_stats
.gs_pages_in
+= atop(size
));
2518 ps_clunmap(vs
, offset
, size
);
2519 #endif /* USE_PRECIOUS */
2526 vm_offset_t vs_offset
,
2530 kern_return_t error
= KERN_SUCCESS
;
2532 unsigned int residual
;
2533 unsigned int request_flags
;
2540 vm_offset_t ps_offset
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
];
2541 paging_segment_t psp
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
];
2544 pages_in_cl
= 1 << vs
->vs_clshift
;
2545 cl_size
= pages_in_cl
* vm_page_size
;
2546 cl_mask
= cl_size
- 1;
2549 * This loop will be executed multiple times until the entire
2550 * request has been satisfied... if the request spans cluster
2551 * boundaries, the clusters will be checked for logical continunity,
2552 * if contiguous the I/O request will span multiple clusters, otherwise
2553 * it will be broken up into the minimal set of I/O's
2555 * If there are holes in a request (either unallocated pages in a paging
2556 * segment or an unallocated paging segment), we stop
2557 * reading at the hole, inform the VM of any data read, inform
2558 * the VM of an unavailable range, then loop again, hoping to
2559 * find valid pages later in the requested range. This continues until
2560 * the entire range has been examined, and read, if present.
2564 request_flags
= UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_PRECIOUS
;
2566 request_flags
= UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
;
2568 while (cnt
&& (error
== KERN_SUCCESS
)) {
2570 int page_list_count
;
2572 if (cnt
> VM_SUPER_CLUSTER
)
2573 size
= VM_SUPER_CLUSTER
;
2581 while (size
> 0 && error
== KERN_SUCCESS
) {
2586 vm_offset_t cur_offset
;
2589 if ( !ps_info_valid
) {
2590 ps_offset
[seg_index
] = ps_clmap(vs
, vs_offset
& ~cl_mask
, &clmap
, CL_FIND
, 0, 0);
2591 psp
[seg_index
] = CLMAP_PS(clmap
);
2595 * skip over unallocated physical segments
2597 if (ps_offset
[seg_index
] == (vm_offset_t
) -1) {
2598 abort_size
= cl_size
- (vs_offset
& cl_mask
);
2599 abort_size
= MIN(abort_size
, size
);
2601 page_list_count
= 0;
2602 memory_object_super_upl_request(
2604 (memory_object_offset_t
)vs_offset
,
2605 abort_size
, abort_size
,
2606 &upl
, NULL
, &page_list_count
,
2609 if (clmap
.cl_error
) {
2610 upl_abort(upl
, UPL_ABORT_ERROR
);
2612 upl_abort(upl
, UPL_ABORT_UNAVAILABLE
);
2614 upl_deallocate(upl
);
2617 vs_offset
+= abort_size
;
2623 cl_index
= (vs_offset
& cl_mask
) / vm_page_size
;
2625 for (abort_size
= 0; cl_index
< pages_in_cl
&& abort_size
< size
; cl_index
++) {
2627 * skip over unallocated pages
2629 if (CLMAP_ISSET(clmap
, cl_index
))
2631 abort_size
+= vm_page_size
;
2635 * Let VM system know about holes in clusters.
2637 GSTAT(global_stats
.gs_pages_unavail
+= atop(abort_size
));
2639 page_list_count
= 0;
2640 memory_object_super_upl_request(
2642 (memory_object_offset_t
)vs_offset
,
2643 abort_size
, abort_size
,
2644 &upl
, NULL
, &page_list_count
,
2647 upl_abort(upl
, UPL_ABORT_UNAVAILABLE
);
2648 upl_deallocate(upl
);
2651 vs_offset
+= abort_size
;
2653 if (cl_index
== pages_in_cl
) {
2655 * if we're at the end of this physical cluster
2656 * then bump to the next one and continue looking
2666 * remember the starting point of the first allocated page
2667 * for the I/O we're about to issue
2669 beg_pseg
= seg_index
;
2670 beg_indx
= cl_index
;
2671 cur_offset
= vs_offset
;
2674 * calculate the size of the I/O that we can do...
2675 * this may span multiple physical segments if
2676 * they are contiguous
2678 for (xfer_size
= 0; xfer_size
< size
; ) {
2680 while (cl_index
< pages_in_cl
&& xfer_size
< size
) {
2682 * accumulate allocated pages within a physical segment
2684 if (CLMAP_ISSET(clmap
, cl_index
)) {
2685 xfer_size
+= vm_page_size
;
2686 cur_offset
+= vm_page_size
;
2689 BS_STAT(psp
[seg_index
]->ps_bs
,
2690 psp
[seg_index
]->ps_bs
->bs_pages_in
++);
2694 if (cl_index
< pages_in_cl
|| xfer_size
>= size
) {
2696 * we've hit an unallocated page or the
2697 * end of this request... go fire the I/O
2702 * we've hit the end of the current physical segment
2703 * and there's more to do, so try moving to the next one
2707 ps_offset
[seg_index
] = ps_clmap(vs
, cur_offset
& ~cl_mask
, &clmap
, CL_FIND
, 0, 0);
2708 psp
[seg_index
] = CLMAP_PS(clmap
);
2711 if ((ps_offset
[seg_index
- 1] != (ps_offset
[seg_index
] - cl_size
)) || (psp
[seg_index
- 1] != psp
[seg_index
])) {
2713 * if the physical segment we're about to step into
2714 * is not contiguous to the one we're currently
2715 * in, or it's in a different paging file, or
2716 * it hasn't been allocated....
2717 * we stop here and generate the I/O
2722 * start with first page of the next physical segment
2728 * we have a contiguous range of allocated pages
2731 page_list_count
= 0;
2732 memory_object_super_upl_request(vs
->vs_control
,
2733 (memory_object_offset_t
)vs_offset
,
2734 xfer_size
, xfer_size
,
2735 &upl
, NULL
, &page_list_count
,
2736 request_flags
| UPL_SET_INTERNAL
);
2738 error
= ps_read_file(psp
[beg_pseg
], upl
, (vm_offset_t
) 0,
2739 ps_offset
[beg_pseg
] + (beg_indx
* vm_page_size
), xfer_size
, &residual
, 0);
2746 * Adjust counts and send response to VM. Optimize for the
2747 * common case, i.e. no error and/or partial data.
2748 * If there was an error, then we need to error the entire
2749 * range, even if some data was successfully read.
2750 * If there was a partial read we may supply some
2751 * data and may error some as well. In all cases the
2752 * VM must receive some notification for every page in the
2755 if ((error
== KERN_SUCCESS
) && (residual
== 0)) {
2757 * Got everything we asked for, supply the data to
2758 * the VM. Note that as a side effect of supplying
2759 * the data, the buffer holding the supplied data is
2760 * deallocated from the pager's address space.
2762 pvs_object_data_provided(vs
, upl
, vs_offset
, xfer_size
);
2764 failed_size
= xfer_size
;
2766 if (error
== KERN_SUCCESS
) {
2767 if (residual
== xfer_size
) {
2769 * If a read operation returns no error
2770 * and no data moved, we turn it into
2771 * an error, assuming we're reading at
2773 * Fall through and error the entire
2776 error
= KERN_FAILURE
;
2779 * Otherwise, we have partial read. If
2780 * the part read is a integral number
2781 * of pages supply it. Otherwise round
2782 * it up to a page boundary, zero fill
2783 * the unread part, and supply it.
2784 * Fall through and error the remainder
2785 * of the range, if any.
2789 fill
= residual
& ~vm_page_size
;
2790 lsize
= (xfer_size
- residual
) + fill
;
2791 pvs_object_data_provided(vs
, upl
, vs_offset
, lsize
);
2793 if (lsize
< xfer_size
) {
2794 failed_size
= xfer_size
- lsize
;
2795 error
= KERN_FAILURE
;
2801 * If there was an error in any part of the range, tell
2802 * the VM. Note that error is explicitly checked again since
2803 * it can be modified above.
2805 if (error
!= KERN_SUCCESS
) {
2806 BS_STAT(psp
[beg_pseg
]->ps_bs
,
2807 psp
[beg_pseg
]->ps_bs
->bs_pages_in_fail
+= atop(failed_size
));
2810 vs_offset
+= xfer_size
;
2813 } /* END while (cnt && (error == 0)) */
2817 int vs_do_async_write
= 1;
2825 boolean_t dp_internal
,
2829 vm_offset_t transfer_size
;
2833 vm_offset_t actual_offset
; /* Offset within paging segment */
2834 paging_segment_t ps
;
2835 vm_offset_t subx_size
;
2836 vm_offset_t mobj_base_addr
;
2837 vm_offset_t mobj_target_addr
;
2840 struct vs_async
*vsa
;
2844 upl_page_info_t
*pl
;
2850 int page_list_count
;
2859 vm_offset_t upl_offset
;
2860 vm_offset_t seg_offset
;
2861 vm_offset_t ps_offset
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
];
2862 paging_segment_t psp
[(VM_SUPER_CLUSTER
/ PAGE_SIZE
) >> VSTRUCT_DEF_CLSHIFT
];
2865 pages_in_cl
= 1 << vs
->vs_clshift
;
2866 cl_size
= pages_in_cl
* vm_page_size
;
2869 super_size
= cl_size
;
2871 request_flags
= UPL_NOBLOCK
|
2872 UPL_RET_ONLY_DIRTY
| UPL_COPYOUT_FROM
|
2873 UPL_NO_SYNC
| UPL_SET_INTERNAL
;
2875 super_size
= VM_SUPER_CLUSTER
;
2877 request_flags
= UPL_NOBLOCK
| UPL_CLEAN_IN_PLACE
|
2878 UPL_RET_ONLY_DIRTY
| UPL_COPYOUT_FROM
|
2879 UPL_NO_SYNC
| UPL_SET_INTERNAL
;
2882 page_list_count
= 0;
2883 memory_object_super_upl_request(vs
->vs_control
,
2884 (memory_object_offset_t
)offset
,
2886 &upl
, NULL
, &page_list_count
,
2887 request_flags
| UPL_PAGEOUT
);
2889 pl
= UPL_GET_INTERNAL_PAGE_LIST(upl
);
2891 for (seg_index
= 0, transfer_size
= upl
->size
; transfer_size
> 0; ) {
2893 ps_offset
[seg_index
] = ps_clmap(vs
, upl
->offset
+ (seg_index
* cl_size
),
2895 transfer_size
< cl_size
?
2896 transfer_size
: cl_size
, 0);
2898 if (ps_offset
[seg_index
] == (vm_offset_t
) -1) {
2900 upl_deallocate(upl
);
2902 return KERN_FAILURE
;
2905 psp
[seg_index
] = CLMAP_PS(clmap
);
2907 if (transfer_size
> cl_size
) {
2908 transfer_size
-= cl_size
;
2913 for (page_index
= 0, num_of_pages
= upl
->size
/ vm_page_size
; page_index
< num_of_pages
; ) {
2915 * skip over non-dirty pages
2917 for ( ; page_index
< num_of_pages
; page_index
++) {
2918 if (UPL_DIRTY_PAGE(pl
, page_index
) || UPL_PRECIOUS_PAGE(pl
, page_index
))
2920 * this is a page we need to write
2921 * go see if we can buddy it up with others
2922 * that are contiguous to it
2926 * if the page is not-dirty, but present we need to commit it...
2927 * this is an unusual case since we only asked for dirty pages
2929 if (UPL_PAGE_PRESENT(pl
, page_index
)) {
2930 boolean_t empty
= FALSE
;
2931 upl_commit_range(upl
,
2932 page_index
* vm_page_size
,
2934 UPL_COMMIT_NOTIFY_EMPTY
,
2939 upl_deallocate(upl
);
2942 if (page_index
== num_of_pages
)
2944 * no more pages to look at, we're out of here
2949 * gather up contiguous dirty pages... we have at least 1
2950 * otherwise we would have bailed above
2951 * make sure that each physical segment that we step
2952 * into is contiguous to the one we're currently in
2953 * if it's not, we have to stop and write what we have
2955 for (first_dirty
= page_index
; page_index
< num_of_pages
; ) {
2956 if ( !UPL_DIRTY_PAGE(pl
, page_index
) && !UPL_PRECIOUS_PAGE(pl
, page_index
))
2960 * if we just looked at the last page in the UPL
2961 * we don't need to check for physical segment
2964 if (page_index
< num_of_pages
) {
2968 cur_seg
= (page_index
- 1) / pages_in_cl
;
2969 nxt_seg
= page_index
/ pages_in_cl
;
2971 if (cur_seg
!= nxt_seg
) {
2972 if ((ps_offset
[cur_seg
] != (ps_offset
[nxt_seg
] - cl_size
)) || (psp
[cur_seg
] != psp
[nxt_seg
]))
2974 * if the segment we're about to step into
2975 * is not contiguous to the one we're currently
2976 * in, or it's in a different paging file....
2977 * we stop here and generate the I/O
2983 num_dirty
= page_index
- first_dirty
;
2987 upl_offset
= first_dirty
* vm_page_size
;
2988 seg_index
= first_dirty
/ pages_in_cl
;
2989 seg_offset
= upl_offset
- (seg_index
* cl_size
);
2990 transfer_size
= num_dirty
* vm_page_size
;
2992 error
= ps_write_file(psp
[seg_index
], upl
, upl_offset
,
2993 ps_offset
[seg_index
] + seg_offset
, transfer_size
, flags
);
2996 while (transfer_size
) {
2999 if ((seg_size
= cl_size
- (upl_offset
% cl_size
)) > transfer_size
)
3000 seg_size
= transfer_size
;
3002 ps_vs_write_complete(vs
, upl
->offset
+ upl_offset
, seg_size
, error
);
3004 transfer_size
-= seg_size
;
3005 upl_offset
+= seg_size
;
3011 boolean_t empty
= FALSE
;
3012 upl_abort_range(upl
,
3013 first_dirty
* vm_page_size
,
3014 num_dirty
* vm_page_size
,
3015 UPL_ABORT_NOTIFY_EMPTY
,
3018 upl_deallocate(upl
);
3023 assert(cnt
<= (vm_page_size
<< vs
->vs_clshift
));
3027 /* The caller provides a mapped_data which is derived */
3028 /* from a temporary object. The targeted pages are */
3029 /* guaranteed to be set at offset 0 in the mapped_data */
3030 /* The actual offset however must still be derived */
3031 /* from the offset in the vs in question */
3032 mobj_base_addr
= offset
;
3033 mobj_target_addr
= mobj_base_addr
;
3035 for (transfer_size
= list_size
; transfer_size
!= 0;) {
3036 actual_offset
= ps_clmap(vs
, mobj_target_addr
,
3038 transfer_size
< cl_size
?
3039 transfer_size
: cl_size
, 0);
3040 if(actual_offset
== (vm_offset_t
) -1) {
3044 cnt
= MIN(transfer_size
,
3045 CLMAP_NPGS(clmap
) * vm_page_size
);
3046 ps
= CLMAP_PS(clmap
);
3047 /* Assume that the caller has given us contiguous */
3050 error
= ps_write_file(ps
, internal_upl
,
3055 ps_vs_write_complete(vs
, mobj_target_addr
,
3060 actual_offset
+= cnt
;
3061 mobj_target_addr
+= cnt
;
3062 transfer_size
-= cnt
;
3070 return KERN_FAILURE
;
3072 return KERN_SUCCESS
;
3076 ps_vstruct_allocated_size(
3080 struct vs_map
*vsmap
;
3084 if (vs
->vs_indirect
) {
3085 /* loop on indirect maps */
3086 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3087 vsmap
= vs
->vs_imap
[i
];
3090 /* loop on clusters in this indirect map */
3091 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3092 if (VSM_ISCLR(vsmap
[j
]) ||
3093 VSM_ISERR(vsmap
[j
]))
3095 /* loop on pages in this cluster */
3096 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3097 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
))
3103 vsmap
= vs
->vs_dmap
;
3106 /* loop on clusters in the direct map */
3107 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3108 if (VSM_ISCLR(vsmap
[j
]) ||
3109 VSM_ISERR(vsmap
[j
]))
3111 /* loop on pages in this cluster */
3112 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3113 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
))
3119 return ptoa(num_pages
);
3123 ps_vstruct_allocated_pages(
3125 default_pager_page_t
*pages
,
3129 struct vs_map
*vsmap
;
3135 if (vs
->vs_indirect
) {
3136 /* loop on indirect maps */
3137 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3138 vsmap
= vs
->vs_imap
[i
];
3139 if (vsmap
== NULL
) {
3140 offset
+= (vm_page_size
* CLMAP_ENTRIES
*
3144 /* loop on clusters in this indirect map */
3145 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3146 if (VSM_ISCLR(vsmap
[j
]) ||
3147 VSM_ISERR(vsmap
[j
])) {
3148 offset
+= vm_page_size
* VSCLSIZE(vs
);
3151 /* loop on pages in this cluster */
3152 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3153 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
)) {
3155 if (num_pages
< pages_size
)
3156 pages
++->dpp_offset
=
3159 offset
+= vm_page_size
;
3164 vsmap
= vs
->vs_dmap
;
3167 /* loop on clusters in the direct map */
3168 for (j
= 0; j
< CLMAP_ENTRIES
; j
++) {
3169 if (VSM_ISCLR(vsmap
[j
]) ||
3170 VSM_ISERR(vsmap
[j
])) {
3171 offset
+= vm_page_size
* VSCLSIZE(vs
);
3174 /* loop on pages in this cluster */
3175 for (k
= 0; k
< VSCLSIZE(vs
); k
++) {
3176 if ((VSM_BMAP(vsmap
[j
])) & (1 << k
)) {
3178 if (num_pages
< pages_size
)
3179 pages
++->dpp_offset
= offset
;
3181 offset
+= vm_page_size
;
3191 ps_vstruct_transfer_from_segment(
3193 paging_segment_t segment
,
3194 #ifndef ubc_sync_working
3195 vm_object_t transfer_object
)
3200 struct vs_map
*vsmap
;
3201 struct vs_map old_vsmap
;
3202 struct vs_map new_vsmap
;
3205 VS_LOCK(vs
); /* block all work on this vstruct */
3206 /* can't allow the normal multiple write */
3207 /* semantic because writes may conflict */
3208 vs
->vs_xfer_pending
= TRUE
;
3209 vs_wait_for_sync_writers(vs
);
3211 vs_wait_for_readers(vs
);
3212 /* we will unlock the vs to allow other writes while transferring */
3213 /* and will be guaranteed of the persistance of the vs struct */
3214 /* because the caller of ps_vstruct_transfer_from_segment bumped */
3215 /* vs_async_pending */
3216 /* OK we now have guaranteed no other parties are accessing this */
3217 /* vs. Now that we are also supporting simple lock versions of */
3218 /* vs_lock we cannot hold onto VS_LOCK as we may block below. */
3219 /* our purpose in holding it before was the multiple write case */
3220 /* we now use the boolean xfer_pending to do that. We can use */
3221 /* a boolean instead of a count because we have guaranteed single */
3222 /* file access to this code in its caller */
3225 if (vs
->vs_indirect
) {
3228 /* loop on indirect maps */
3229 for (i
= 0; i
< INDIRECT_CLMAP_ENTRIES(vs
->vs_size
); i
++) {
3230 vsmap
= vs
->vs_imap
[i
];
3233 /* loop on clusters in this indirect map */
3234 clmap_off
= (vm_page_size
* CLMAP_ENTRIES
*
3236 if(i
+1 == INDIRECT_CLMAP_ENTRIES(vs
->vs_size
))
3237 vsmap_size
= vs
->vs_size
- (CLMAP_ENTRIES
* i
);
3239 vsmap_size
= CLMAP_ENTRIES
;
3240 for (j
= 0; j
< vsmap_size
; j
++) {
3241 if (VSM_ISCLR(vsmap
[j
]) ||
3242 VSM_ISERR(vsmap
[j
]) ||
3243 (VSM_PS(vsmap
[j
]) != segment
))
3245 if(vs_cluster_transfer(vs
,
3246 (vm_page_size
* (j
<< vs
->vs_clshift
))
3248 vm_page_size
<< vs
->vs_clshift
,
3249 #ifndef ubc_sync_working
3256 vs
->vs_xfer_pending
= FALSE
;
3258 vs_finish_write(vs
);
3259 return KERN_FAILURE
;
3261 /* allow other readers/writers during transfer*/
3263 vs
->vs_xfer_pending
= FALSE
;
3265 vs_finish_write(vs
);
3267 vs
->vs_xfer_pending
= TRUE
;
3268 vs_wait_for_sync_writers(vs
);
3270 vs_wait_for_readers(vs
);
3272 if (!(vs
->vs_indirect
)) {
3278 vsmap
= vs
->vs_dmap
;
3279 if (vsmap
== NULL
) {
3281 vs
->vs_xfer_pending
= FALSE
;
3283 vs_finish_write(vs
);
3284 return KERN_SUCCESS
;
3286 /* loop on clusters in the direct map */
3287 for (j
= 0; j
< vs
->vs_size
; j
++) {
3288 if (VSM_ISCLR(vsmap
[j
]) ||
3289 VSM_ISERR(vsmap
[j
]) ||
3290 (VSM_PS(vsmap
[j
]) != segment
))
3292 if(vs_cluster_transfer(vs
,
3293 vm_page_size
* (j
<< vs
->vs_clshift
),
3294 vm_page_size
<< vs
->vs_clshift
,
3295 #ifndef ubc_sync_working
3296 transfer_object
) != KERN_SUCCESS
) {
3298 upl
) != KERN_SUCCESS
) {
3301 vs
->vs_xfer_pending
= FALSE
;
3303 vs_finish_write(vs
);
3304 return KERN_FAILURE
;
3306 /* allow other readers/writers during transfer*/
3308 vs
->vs_xfer_pending
= FALSE
;
3310 vs_finish_write(vs
);
3312 vs
->vs_xfer_pending
= TRUE
;
3314 vs_wait_for_sync_writers(vs
);
3316 vs_wait_for_readers(vs
);
3317 if (vs
->vs_indirect
) {
3324 vs
->vs_xfer_pending
= FALSE
;
3326 vs_finish_write(vs
);
3327 return KERN_SUCCESS
;
3337 struct vs_map
*vsmap
;
3338 vm_offset_t cluster
;
3340 cluster
= atop(offset
) >> vs
->vs_clshift
;
3341 if (vs
->vs_indirect
) {
3342 long ind_block
= cluster
/CLMAP_ENTRIES
;
3344 /* Is the indirect block allocated? */
3345 vsmap
= vs
->vs_imap
[ind_block
];
3346 if(vsmap
== (vs_map_t
) NULL
)
3349 vsmap
= vs
->vs_dmap
;
3350 vsmap
+= cluster%CLMAP_ENTRIES
;
3355 vs_cluster_transfer(
3359 #ifndef ubc_sync_working
3360 vm_object_t transfer_object
)
3365 vm_offset_t actual_offset
;
3366 paging_segment_t ps
;
3368 kern_return_t error
= KERN_SUCCESS
;
3369 int size
, size_wanted
, i
;
3370 unsigned int residual
;
3372 default_pager_thread_t
*dpt
;
3374 struct vs_map
*vsmap_ptr
;
3375 struct vs_map read_vsmap
;
3376 struct vs_map original_read_vsmap
;
3377 struct vs_map write_vsmap
;
3379 #ifndef ubc_sync_working
3385 /* vs_cluster_transfer reads in the pages of a cluster and
3386 * then writes these pages back to new backing store. The
3387 * segment the pages are being read from is assumed to have
3388 * been taken off-line and is no longer considered for new
3393 * This loop will be executed once per cluster referenced.
3394 * Typically this means once, since it's unlikely that the
3395 * VM system will ask for anything spanning cluster boundaries.
3397 * If there are holes in a cluster (in a paging segment), we stop
3398 * reading at the hole, then loop again, hoping to
3399 * find valid pages later in the cluster. This continues until
3400 * the entire range has been examined, and read, if present. The
3401 * pages are written as they are read. If a failure occurs after
3402 * some pages are written the unmap call at the bottom of the loop
3403 * recovers the backing store and the old backing store remains
3407 VSM_CLR(write_vsmap
);
3408 VSM_CLR(original_read_vsmap
);
3409 /* grab the actual object's pages to sync with I/O */
3410 while (cnt
&& (error
== KERN_SUCCESS
)) {
3411 vsmap_ptr
= vs_get_map_entry(vs
, offset
);
3412 actual_offset
= ps_clmap(vs
, offset
, &clmap
, CL_FIND
, 0, 0);
3414 if (actual_offset
== (vm_offset_t
) -1) {
3417 * Nothing left to write in this cluster at least
3418 * set write cluster information for any previous
3419 * write, clear for next cluster, if there is one
3421 unsigned int local_size
, clmask
, clsize
;
3423 clsize
= vm_page_size
<< vs
->vs_clshift
;
3424 clmask
= clsize
- 1;
3425 local_size
= clsize
- (offset
& clmask
);
3427 local_size
= MIN(local_size
, cnt
);
3429 /* This cluster has no data in it beyond what may */
3430 /* have been found on a previous iteration through */
3431 /* the loop "write_vsmap" */
3432 *vsmap_ptr
= write_vsmap
;
3433 VSM_CLR(write_vsmap
);
3434 VSM_CLR(original_read_vsmap
);
3437 offset
+= local_size
;
3442 * Count up contiguous available or unavailable
3445 ps
= CLMAP_PS(clmap
);
3450 (size
< cnt
) && (unavail_size
< cnt
) &&
3451 (i
< CLMAP_NPGS(clmap
)); i
++) {
3452 if (CLMAP_ISSET(clmap
, i
)) {
3453 if (unavail_size
!= 0)
3455 size
+= vm_page_size
;
3457 ps
->ps_bs
->bs_pages_in
++);
3461 unavail_size
+= vm_page_size
;
3466 ASSERT(unavail_size
);
3467 cnt
-= unavail_size
;
3468 offset
+= unavail_size
;
3469 if((offset
& ((vm_page_size
<< vs
->vs_clshift
) - 1))
3471 /* There is no more to transfer in this
3474 *vsmap_ptr
= write_vsmap
;
3475 VSM_CLR(write_vsmap
);
3476 VSM_CLR(original_read_vsmap
);
3481 if(VSM_ISCLR(original_read_vsmap
))
3482 original_read_vsmap
= *vsmap_ptr
;
3484 if(ps
->ps_segtype
== PS_PARTITION
) {
3486 NEED TO BE WITH SYNC & NO COMMIT
3487 error = ps_read_device(ps, actual_offset, &buffer,
3488 size, &residual, flags);
3491 #ifndef ubc_sync_working
3492 int page_list_count
= 0;
3494 error
= vm_object_upl_request(transfer_object
,
3495 (vm_object_offset_t
) (actual_offset
& ((vm_page_size
<< vs
->vs_clshift
) - 1)),
3496 size
, &upl
, NULL
, &page_list_count
,
3497 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
3498 | UPL_SET_INTERNAL
);
3499 if (error
== KERN_SUCCESS
) {
3500 error
= ps_read_file(ps
, upl
, (vm_offset_t
) 0, actual_offset
,
3501 size
, &residual
, 0);
3503 upl_commit(upl
, NULL
);
3504 upl_deallocate(upl
);
3508 /* NEED TO BE WITH SYNC & NO COMMIT & NO RDAHEAD*/
3509 error
= ps_read_file(ps
, upl
, (vm_offset_t
) 0, actual_offset
,
3511 (UPL_IOSYNC
| UPL_NOCOMMIT
| UPL_NORDAHEAD
));
3515 read_vsmap
= *vsmap_ptr
;
3519 * Adjust counts and put data in new BS. Optimize for the
3520 * common case, i.e. no error and/or partial data.
3521 * If there was an error, then we need to error the entire
3522 * range, even if some data was successfully read.
3525 if ((error
== KERN_SUCCESS
) && (residual
== 0)) {
3526 int page_list_count
= 0;
3529 * Got everything we asked for, supply the data to
3530 * the new BS. Note that as a side effect of supplying
3531 * the data, the buffer holding the supplied data is
3532 * deallocated from the pager's address space unless
3533 * the write is unsuccessful.
3536 /* note buffer will be cleaned up in all cases by */
3537 /* internal_cluster_write or if an error on write */
3538 /* the vm_map_copy_page_discard call */
3539 *vsmap_ptr
= write_vsmap
;
3541 #ifndef ubc_sync_working
3542 error
= vm_object_upl_request(transfer_object
,
3543 (vm_object_offset_t
)
3544 (actual_offset
& ((vm_page_size
<< vs
->vs_clshift
) - 1)),
3545 size
, &upl
, NULL
, &page_list_count
,
3546 UPL_NO_SYNC
| UPL_CLEAN_IN_PLACE
| UPL_SET_INTERNAL
);
3547 if(vs_cluster_write(vs
, upl
, offset
,
3548 size
, TRUE
, 0) != KERN_SUCCESS
) {
3549 upl_commit(upl
, NULL
);
3550 upl_deallocate(upl
);
3552 if(vs_cluster_write(vs
, upl
, offset
,
3553 size
, TRUE
, UPL_IOSYNC
| UPL_NOCOMMIT
) != KERN_SUCCESS
) {
3555 error
= KERN_FAILURE
;
3556 if(!(VSM_ISCLR(*vsmap_ptr
))) {
3557 /* unmap the new backing store object */
3558 ps_clunmap(vs
, offset
, size
);
3560 /* original vsmap */
3561 *vsmap_ptr
= original_read_vsmap
;
3562 VSM_CLR(write_vsmap
);
3564 if((offset
+ size
) &
3565 ((vm_page_size
<< vs
->vs_clshift
)
3567 /* There is more to transfer in this
3570 write_vsmap
= *vsmap_ptr
;
3571 *vsmap_ptr
= read_vsmap
;
3573 /* discard the old backing object */
3574 write_vsmap
= *vsmap_ptr
;
3575 *vsmap_ptr
= read_vsmap
;
3576 ps_clunmap(vs
, offset
, size
);
3577 *vsmap_ptr
= write_vsmap
;
3578 VSM_CLR(write_vsmap
);
3579 VSM_CLR(original_read_vsmap
);
3584 if (error
== KERN_SUCCESS
) {
3585 if (residual
== size
) {
3587 * If a read operation returns no error
3588 * and no data moved, we turn it into
3589 * an error, assuming we're reading at
3591 * Fall through and error the entire
3594 error
= KERN_FAILURE
;
3595 *vsmap_ptr
= write_vsmap
;
3596 if(!(VSM_ISCLR(*vsmap_ptr
))) {
3597 /* unmap the new backing store object */
3598 ps_clunmap(vs
, offset
, size
);
3600 *vsmap_ptr
= original_read_vsmap
;
3601 VSM_CLR(write_vsmap
);
3605 * Otherwise, we have partial read.
3606 * This is also considered an error
3607 * for the purposes of cluster transfer
3609 error
= KERN_FAILURE
;
3610 *vsmap_ptr
= write_vsmap
;
3611 if(!(VSM_ISCLR(*vsmap_ptr
))) {
3612 /* unmap the new backing store object */
3613 ps_clunmap(vs
, offset
, size
);
3615 *vsmap_ptr
= original_read_vsmap
;
3616 VSM_CLR(write_vsmap
);
3625 } /* END while (cnt && (error == 0)) */
3626 if(!VSM_ISCLR(write_vsmap
))
3627 *vsmap_ptr
= write_vsmap
;
3633 default_pager_add_file(MACH_PORT_FACE backing_store
,
3639 paging_segment_t ps
;
3643 if ((bs
= backing_store_lookup(backing_store
))
3644 == BACKING_STORE_NULL
)
3645 return KERN_INVALID_ARGUMENT
;
3648 for (i
= 0; i
<= paging_segment_max
; i
++) {
3649 ps
= paging_segments
[i
];
3650 if (ps
== PAGING_SEGMENT_NULL
)
3652 if (ps
->ps_segtype
!= PS_FILE
)
3656 * Check for overlap on same device.
3658 if (ps
->ps_vnode
== (struct vnode
*)vp
) {
3661 return KERN_INVALID_ARGUMENT
;
3667 * Set up the paging segment
3669 ps
= (paging_segment_t
) kalloc(sizeof (struct paging_segment
));
3670 if (ps
== PAGING_SEGMENT_NULL
) {
3672 return KERN_RESOURCE_SHORTAGE
;
3675 ps
->ps_segtype
= PS_FILE
;
3676 ps
->ps_vnode
= (struct vnode
*)vp
;
3678 ps
->ps_record_shift
= local_log2(vm_page_size
/ record_size
);
3679 ps
->ps_recnum
= size
;
3680 ps
->ps_pgnum
= size
>> ps
->ps_record_shift
;
3682 ps
->ps_pgcount
= ps
->ps_pgnum
;
3683 ps
->ps_clshift
= local_log2(bs
->bs_clsize
);
3684 ps
->ps_clcount
= ps
->ps_ncls
= ps
->ps_pgcount
>> ps
->ps_clshift
;
3688 ps
->ps_bmap
= (unsigned char *) kalloc(RMAPSIZE(ps
->ps_ncls
));
3690 kfree((vm_offset_t
)ps
, sizeof *ps
);
3692 return KERN_RESOURCE_SHORTAGE
;
3694 for (i
= 0; i
< ps
->ps_ncls
; i
++) {
3695 clrbit(ps
->ps_bmap
, i
);
3698 ps
->ps_going_away
= FALSE
;
3701 if ((error
= ps_enter(ps
)) != 0) {
3702 kfree((vm_offset_t
)ps
->ps_bmap
, RMAPSIZE(ps
->ps_ncls
));
3703 kfree((vm_offset_t
)ps
, sizeof *ps
);
3705 return KERN_RESOURCE_SHORTAGE
;
3708 bs
->bs_pages_free
+= ps
->ps_clcount
<< ps
->ps_clshift
;
3709 bs
->bs_pages_total
+= ps
->ps_clcount
<< ps
->ps_clshift
;
3711 dp_pages_free
+= ps
->ps_pgcount
;
3716 bs_more_space(ps
->ps_clcount
);
3718 DEBUG(DEBUG_BS_INTERNAL
,
3719 ("device=0x%x,offset=0x%x,count=0x%x,record_size=0x%x,shift=%d,total_size=0x%x\n",
3720 device
, offset
, size
, record_size
,
3721 ps
->ps_record_shift
, ps
->ps_pgnum
));
3723 return KERN_SUCCESS
;
3730 paging_segment_t ps
,
3732 vm_offset_t upl_offset
,
3735 unsigned int *residualp
,
3738 vm_object_offset_t f_offset
;
3743 clustered_reads
[atop(size
)]++;
3745 f_offset
= (vm_object_offset_t
)(ps
->ps_offset
+ offset
);
3747 /* for transfer case we need to pass uploffset and flags */
3748 error
= vnode_pagein(ps
->ps_vnode
,
3749 upl
, upl_offset
, f_offset
, (vm_size_t
)size
, flags
| UPL_NORDAHEAD
, NULL
);
3751 /* The vnode_pagein semantic is somewhat at odds with the existing */
3752 /* device_read semantic. Partial reads are not experienced at this */
3753 /* level. It is up to the bit map code and cluster read code to */
3754 /* check that requested data locations are actually backed, and the */
3755 /* pagein code to either read all of the requested data or return an */
3759 result
= KERN_FAILURE
;
3762 result
= KERN_SUCCESS
;
3769 paging_segment_t ps
,
3771 vm_offset_t upl_offset
,
3776 vm_object_offset_t f_offset
;
3777 kern_return_t result
;
3781 clustered_writes
[atop(size
)]++;
3782 f_offset
= (vm_object_offset_t
)(ps
->ps_offset
+ offset
);
3784 if (vnode_pageout(ps
->ps_vnode
,
3785 upl
, upl_offset
, f_offset
, (vm_size_t
)size
, flags
, NULL
))
3786 result
= KERN_FAILURE
;
3788 result
= KERN_SUCCESS
;
3794 default_pager_triggers(MACH_PORT_FACE default_pager
,
3798 MACH_PORT_FACE trigger_port
)
3800 MACH_PORT_FACE release
;
3804 if (flags
== HI_WAT_ALERT
) {
3805 release
= min_pages_trigger_port
;
3806 min_pages_trigger_port
= trigger_port
;
3807 minimum_pages_remaining
= hi_wat
/vm_page_size
;
3810 } else if (flags
== LO_WAT_ALERT
) {
3811 release
= max_pages_trigger_port
;
3812 max_pages_trigger_port
= trigger_port
;
3813 maximum_pages_free
= lo_wat
/vm_page_size
;
3816 release
= trigger_port
;
3817 kr
= KERN_INVALID_ARGUMENT
;
3821 if (IP_VALID(release
))
3822 ipc_port_release_send(release
);