Start of man-pages-5.14: renaming .Announce and .lsm files
[man-pages.git] / man2 / swapon.2
blob07d5fe86c6bb863806d88c2b1c672e505d2f191d
1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified by Michael Haardt <michael@moria.de>
26 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1995-07-22 by Michael Chastain <mec@duracef.shout.net>
28 .\" Modified 1995-07-23 by aeb
29 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 1998-09-08 by aeb
31 .\" Modified 2004-06-17 by Michael Kerrisk <mtk.manpages@gmail.com>
32 .\" Modified 2004-10-10 by aeb
33 .\" 2004-12-14 mtk, Anand Kumria: added new errors
34 .\" 2007-06-22 Ivana Varekova <varekova@redhat.com>, mtk
35 .\"     Update text describing limit on number of swap files.
36 .\"
37 .\" FIXME Linux 3.11 added SWAP_FLAG_DISCARD_ONCE and SWAP_FLAG_DISCARD_PAGES
38 .\"     commit dcf6b7ddd7df8965727746f89c59229b23180e5a
39 .\"     Author: Rafael Aquini <aquini@redhat.com>
40 .\"     Date:   Wed Jul 3 15:02:46 2013 -0700
41 .\"
42 .TH SWAPON 2 2021-03-22 "Linux" "Linux Programmer's Manual"
43 .SH NAME
44 swapon, swapoff \- start/stop swapping to file/device
45 .SH SYNOPSIS
46 .nf
47 .B #include <sys/swap.h>
48 .PP
49 .BI "int swapon(const char *" path ", int " swapflags );
50 .BI "int swapoff(const char *" path );
51 .fi
52 .SH DESCRIPTION
53 .BR swapon ()
54 sets the swap area to the file or block device specified by
55 .IR path .
56 .BR swapoff ()
57 stops swapping to the file or block device specified by
58 .IR path .
59 .PP
60 If the
61 .B SWAP_FLAG_PREFER
62 flag is specified in the
63 .BR swapon ()
64 .I swapflags
65 argument, the new swap area will have a higher priority than default.
66 The priority is encoded within
67 .I swapflags
68 as:
69 .PP
70 .in +4n
71 .EX
72 .I "(prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK"
73 .EE
74 .in
75 .PP
76 If the
77 .B SWAP_FLAG_DISCARD
78 flag is specified in the
79 .BR swapon ()
80 .I swapflags
81 argument, freed swap pages will be discarded before they are reused,
82 if the swap device supports the discard or trim operation.
83 (This may improve performance on some Solid State Devices,
84 but often it does not.)
85 See also NOTES.
86 .PP
87 These functions may be used only by a privileged process (one having the
88 .B CAP_SYS_ADMIN
89 capability).
90 .SS Priority
91 Each swap area has a priority, either high or low.
92 The default priority is low.
93 Within the low-priority areas,
94 newer areas are even lower priority than older areas.
95 .PP
96 All priorities set with
97 .I swapflags
98 are high-priority, higher than default.
99 They may have any nonnegative value chosen by the caller.
100 Higher numbers mean higher priority.
102 Swap pages are allocated from areas in priority order,
103 highest priority first.
104 For areas with different priorities,
105 a higher-priority area is exhausted before using a lower-priority area.
106 If two or more areas have the same priority,
107 and it is the highest priority available,
108 pages are allocated on a round-robin basis between them.
110 As of Linux 1.3.6, the kernel usually follows these rules,
111 but there are exceptions.
112 .SH RETURN VALUE
113 On success, zero is returned.
114 On error, \-1 is returned, and
115 .I errno
116 is set to indicate the error.
117 .SH ERRORS
119 .B EBUSY
120 (for
121 .BR swapon ())
122 The specified
123 .I path
124 is already being used as a swap area.
126 .B EINVAL
127 The file
128 .I path
129 exists, but refers neither to a regular file nor to a block device;
131 .B EINVAL
132 .RB ( swapon ())
133 The indicated path does not contain a valid swap signature or
134 resides on an in-memory filesystem such as
135 .BR tmpfs (5).
137 .BR EINVAL " (since Linux 3.4)"
138 .RB ( swapon ())
139 An invalid flag value was specified in
140 .IR swapflags .
142 .B EINVAL
143 .RB ( swapoff ())
144 .I path
145 is not currently a swap area.
147 .B ENFILE
148 The system-wide limit on the total number of open files has been reached.
150 .B ENOENT
151 The file
152 .I path
153 does not exist.
155 .B ENOMEM
156 The system has insufficient memory to start swapping.
158 .B EPERM
159 The caller does not have the
160 .B CAP_SYS_ADMIN
161 capability.
162 Alternatively, the maximum number of swap files are already in use;
163 see NOTES below.
164 .SH CONFORMING TO
165 These functions are Linux-specific and should not be used in programs
166 intended to be portable.
167 The second
168 .I swapflags
169 argument was introduced in Linux 1.3.2.
170 .SH NOTES
171 The partition or path must be prepared with
172 .BR mkswap (8).
174 There is an upper limit on the number of swap files that may be used,
175 defined by the kernel constant
176 .BR MAX_SWAPFILES .
177 Before kernel 2.4.10,
178 .B MAX_SWAPFILES
179 has the value 8;
180 since kernel 2.4.10, it has the value 32.
181 Since kernel 2.6.18, the limit is decreased by 2 (thus: 30)
182 if the kernel is built with the
183 .B CONFIG_MIGRATION
184 option
185 (which reserves two swap table entries for the page migration features of
186 .BR mbind (2)
188 .BR migrate_pages (2)).
189 Since kernel 2.6.32, the limit is further decreased by 1
190 if the kernel is built with the
191 .B CONFIG_MEMORY_FAILURE
192 option.
194 Discard of swap pages was introduced in kernel 2.6.29,
195 then made conditional
196 on the
197 .B SWAP_FLAG_DISCARD
198 flag in kernel 2.6.36,
199 .\" To be precise: 2.6.35.5
200 which still discards the
201 entire swap area when
202 .BR swapon ()
203 is called, even if that flag bit is not set.
204 .SH SEE ALSO
205 .BR mkswap (8),
206 .BR swapoff (8),
207 .BR swapon (8)