1SWAPON(2) Linux Programmer's Manual SWAPON(2)
2
3
4
6 swapon, swapoff - start/stop swapping to file/device
7
9 #include <unistd.h>
10 #include <asm/page.h> /* to find PAGE_SIZE */
11 #include <sys/swap.h>
12
13 int swapon(const char *path, int swapflags);
14 int swapoff(const char *path);
15
17 swapon() sets the swap area to the file or block device specified by
18 path. swapoff() stops swapping to the file or block device specified
19 by path.
20
21 swapon() takes a swapflags argument. If swapflags has the
22 SWAP_FLAG_PREFER bit turned on, the new swap area will have a higher
23 priority than default. The priority is encoded within swapflags as:
24
25 (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK
26
27 These functions may only be used by a privileged process (one having
28 the CAP_SYS_ADMIN capability).
29
30 Priority
31 Each swap area has a priority, either high or low. The default prior‐
32 ity is low. Within the low-priority areas, newer areas are even lower
33 priority than older areas.
34
35 All priorities set with swapflags are high-priority, higher than
36 default. They may have any non-negative value chosen by the caller.
37 Higher numbers mean higher priority.
38
39 Swap pages are allocated from areas in priority order, highest priority
40 first. For areas with different priorities, a higher-priority area is
41 exhausted before using a lower-priority area. If two or more areas
42 have the same priority, and it is the highest priority available, pages
43 are allocated on a round-robin basis between them.
44
45 As of Linux 1.3.6, the kernel usually follows these rules, but there
46 are exceptions.
47
49 On success, zero is returned. On error, -1 is returned, and errno is
50 set appropriately.
51
53 EBUSY (for swapon()) The specified path is already being used as a
54 swap area.
55
56 EINVAL The file path exists, but refers neither to a regular file nor
57 to a block device; or, for swapon(), the indicated path does not
58 contain a valid swap signature or resides on an in-memory file
59 system like tmpfs; or, for swapoff(), path is not currently a
60 swap area.
61
62 ENFILE The system limit on the total number of open files has been
63 reached.
64
65 ENOENT The file path does not exist.
66
67 ENOMEM The system has insufficient memory to start swapping.
68
69 EPERM The caller does not have the CAP_SYS_ADMIN capability. Alterna‐
70 tively, the maximum number of swap files are already in use; see
71 NOTES below.
72
74 These functions are Linux-specific and should not be used in programs
75 intended to be portable. The second swapflags argument was introduced
76 in Linux 1.3.2.
77
79 The partition or path must be prepared with mkswap(8).
80
81 There is an upper limit on the number of swap files that may be used,
82 defined by the kernel constant MAX_SWAPFILES. Before kernel 2.4.10,
83 MAX_SWAPFILES has the value 8; since kernel 2.4.10, it has the value
84 32. Since kernel 2.6.18, the limit is decreased by 2 if the kernel is
85 built with the CONFIG_MIGRATION option (which reserves two swap table
86 entries for the page migration features of mbind(2) and
87 migrate_pages(2)). Since kernel 2.6.32, the limit is decreased by 1 if
88 the kernel is built with the CONFIG_MEMORY_FAILURE option.
89
91 mkswap(8), swapoff(8), swapon(8)
92
94 This page is part of release 3.22 of the Linux man-pages project. A
95 description of the project, and information about reporting bugs, can
96 be found at http://www.kernel.org/doc/man-pages/.
97
98
99
100Linux 2007-06-22 SWAPON(2)