1.\" $OpenBSD: swapctl.2,v 1.25 2021/11/21 23:44:55 jan Exp $ 2.\" $NetBSD: swapctl.2,v 1.10 1998/08/29 17:11:09 mrg Exp $ 3.\" 4.\" Copyright (c) 1997 Matthew R. Green 5.\" Copyright (c) 1980, 1991, 1993 6.\" The Regents of the University of California. All rights reserved. 7.\" 8.\" Redistribution and use in source and binary forms, with or without 9.\" modification, are permitted provided that the following conditions 10.\" are met: 11.\" 1. Redistributions of source code must retain the above copyright 12.\" notice, this list of conditions and the following disclaimer. 13.\" 2. Redistributions in binary form must reproduce the above copyright 14.\" notice, this list of conditions and the following disclaimer in the 15.\" documentation and/or other materials provided with the distribution. 16.\" 3. Neither the name of the University nor the names of its contributors 17.\" may be used to endorse or promote products derived from this software 18.\" without specific prior written permission. 19.\" 20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30.\" SUCH DAMAGE. 31.\" 32.Dd $Mdocdate: November 21 2021 $ 33.Dt SWAPCTL 2 34.Os 35.Sh NAME 36.Nm swapctl 37.Nd modify swap configuration 38.Sh SYNOPSIS 39.In sys/types.h 40.In sys/swap.h 41.In unistd.h 42.Ft int 43.Fn swapctl "int cmd" "const void *arg" "int misc" 44.Sh DESCRIPTION 45The 46.Fn swapctl 47function is used to add and delete swap devices, modify their configuration or 48query their statistics. 49.Pp 50The 51.Fa cmd 52parameter specifies the operation to be performed. 53The 54.Fa arg 55and 56.Fa misc 57parameters have different meanings, depending on the 58.Fa cmd 59parameter. 60.Bl -item -offset indent 61.It 62If 63.Fa cmd 64is 65.Dv SWAP_NSWAP , 66the current number of swap devices in the system is returned. 67The 68.Fa arg 69and 70.Fa misc 71parameters are ignored. 72.It 73If 74.Fa cmd 75is 76.Dv SWAP_STATS , 77the current statistics for swap devices are returned in the 78.Fa arg 79parameter. 80No more than 81.Fa misc 82swap devices are returned. 83The 84.Fa arg 85parameter should point to an array of at least 86.Fa misc 87struct swapent structures: 88.Bd -literal 89struct swapent { 90 dev_t se_dev; /* device id */ 91 int se_flags; /* entry flags */ 92 int se_nblks; /* total blocks */ 93 int se_inuse; /* blocks in use */ 94 int se_priority; /* priority */ 95 char se_path[PATH_MAX]; /* path to entry */ 96}; 97.Ed 98.Pp 99The flags are defined as 100.Bd -literal 101 SWF_INUSE in use: we have swapped here 102 SWF_ENABLE enabled: we can swap here 103 SWF_BUSY busy: I/O happening here 104 SWF_FAKE fake: still being built 105.Ed 106.Pp 107All but 108.Dv SWAP_NSWAP 109and 110.Dv SWAP_STATS 111require superuser privileges. 112.It 113If 114.Fa cmd 115is 116.Dv SWAP_ON , 117the 118.Fa arg 119parameter is used as a pathname of a file to enable swapping to. 120The 121.Fa misc 122parameter is used to set the priority of this swap device. 123.It 124If 125.Fa cmd 126is 127.Dv SWAP_OFF , 128the 129.Fa arg 130parameter is used as the pathname of a file to disable swapping from. 131The 132.Fa misc 133parameter is ignored. 134.It 135If 136.Fa cmd 137is 138.Dv SWAP_CTL , 139the 140.Fa arg 141and 142.Fa misc 143parameters have the same function as for the 144.Dv SWAP_ON 145case, except that they change the priority of a currently enabled swap device. 146.El 147.Pp 148When swapping is enabled on a block device, the first portion of the disk is 149left unused to prevent any disklabel present from being overwritten. 150This space is allocated from the swap device when the 151.Dv SWAP_ON 152command is used. 153.Sh RETURN VALUES 154If the 155.Fa cmd 156parameter is 157.Dv SWAP_NSWAP 158or 159.Dv SWAP_STATS , 160.Fn swapctl 161returns the number of swap devices, if successful. 162The 163.Dv SWAP_NSWAP 164command is always successful. 165Otherwise it returns 0 on success and \-1 on failure, 166setting the global variable 167.Va errno 168to indicate the error. 169.Sh ERRORS 170.Fn swapctl 171succeeds unless: 172.Bl -tag -width Er 173.It Bq Er ENOTDIR 174A component of the path prefix is not a directory. 175.It Bq Er ENAMETOOLONG 176A component of a pathname exceeded 177.Dv NAME_MAX 178characters, or an entire pathname (including the terminating NUL) 179exceeded 180.Dv PATH_MAX 181bytes. 182.It Bq Er ENOENT 183The named device does not exist. 184For the 185.Dv SWAP_CTL 186command, the named device is not currently enabled for swapping. 187.It Bq Er EACCES 188Search permission is denied for a component of the path prefix. 189.It Bq Er ELOOP 190Too many symbolic links were encountered in translating the pathname. 191.It Bq Er EPERM 192The caller is not the superuser. 193.It Bq Er EBUSY 194The device specified by 195.Fa arg 196has already been made available for swapping. 197.It Bq Er EINVAL 198The device configured by 199.Fa arg 200has no associated size, or the 201.Fa cmd 202was unknown. 203.It Bq Er ENXIO 204The major device number of 205.Fa arg 206is out of range (this indicates no device driver exists 207for the associated hardware). 208.It Bq Er EIO 209An I/O error occurred while opening the swap device. 210.It Bq Er EFAULT 211.Fa arg 212points outside the process' allocated address space. 213.It Bq Er ENOTSUP 214The device specified by 215.Fa arg 216cannot be used for swapping. 217.El 218.Sh SEE ALSO 219.Xr config 8 , 220.Xr swapctl 8 221.Sh HISTORY 222The 223.Fn swapctl 224function call appeared in 225.Nx 1.3 . 226The 227.Fa se_path 228member was added to 229.Vt struct swapent 230in 231.Nx 1.4 , 232when the header file was also moved from 233.In vm/vm_swap.h . 234.Sh AUTHORS 235.An -nosplit 236The current swap system was designed and implemented by 237.An Matthew Green Aq Mt mrg@eterna.com.au , 238with help from 239.An Paul Kranenburg Aq Mt pk@NetBSD.ORG 240and 241.An Leo Weppelman Aq Mt leo@NetBSD.ORG , 242and insights from 243.An Jason R. Thorpe Aq Mt thorpej@NetBSD.ORG . 244