The flags field (<#2129#> SWP_USED<#2129#> or <#2130#> SWP_WRITEOK<#2130#>) is used to
control access to the swap files. When <#2131#> SWP_WRITEOK<#2131#> is off space
will not be allocated in that file. This is used by swapoff when it
tries to unuse a file. When swapon adds a new swap file it sets <#2132#>
SWP_USED<#2132#>. A static variable <#2133#> nr_swapfiles<#2133#> stores the number of
currently active swap files. The fields <#2134#> lowest_bit<#2134#> and <#2135#>
highest_bit<#2135#> bound the free region in the swap file and are used to
speed up the search for free swap space.
The user program mkswap initializes a swap device or file. The first
page contains a signature (`<#2136#> SWAP-SPACE<#2136#>') in the last 10 bytes, and
holds a bitmap. Initially 0's in the bitmap signal bad pages. A `1' in
the bitmap means the corresponding page is free. This page is never
allocated so the initialization needs to be done just once.
The syscall <#2137#> swapon()<#2137#> is called by the user program swapon typically from
/etc/rc. A couple of pages of memory are allocated for <#2138#> swap_map<#2138#> and
<#2139#> swap_lockmap<#2139#>.
<#2140#> swap_map<#2140#> holds a byte for each page in the swap file. It is initialized
from the bitmap to contain a 0 for available pages and 128 for unusable
pages. It is used to maintain a count of swap requests on each page in
the swap file. <#2141#> swap_lockmap<#2141#> holds a bit for each page that is
used to ensure mutual exclusion when reading or writing swap files.
When a page of memory is to be swapped out an index to the swap location is
obtained by a call to <#2142#> get_swap_page()<#2142#>. This index is then stored in
bits 1--31 of the page table entry so the swapped page may be located
by the page fault handler, <#2143#> do_no_page()<#2143#> when needed.
The upper 7 bits of the index give the swap file (or device) and the
lower 24 bits give the page number on that device. That makes as many
as 128 swap files, each with room for about 64~GB, but the space overhead
due to the <#2144#> swap_map<#2144#> would be large. Instead the swap file size
is limited to 16~MB, because the <#2145#> swap_map<#2145#> then takes 1 page.
The function <#2146#> swap_duplicate()<#2146#> is used by <#2147#>
copy_page_tables()<#2147#> to let a child process inherit swapped pages
during a fork. It just increments the count maintained in <#2148#>
swap_map<#2148#> for that page. Each process will swap in a separate copy of
the page when it accesses it.
<#2149#> swap_free()<#2149#> decrements the count maintained in <#2150#> swap_map<#2150#>.
When the count drops to 0 the page can be reallocated by <#2151#>
get_swap_page()<#2151#>. It is called each time a swapped page is read into
memory (<#2152#> swap_in()<#2152#>) or when a page is to be discarded (<#2153#>
free_one_table()<#2153#>, etc.).