xref: /illumos-gate/usr/src/uts/common/sys/swap.h (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #ifndef	_SYS_SWAP_H
41 #define	_SYS_SWAP_H
42 
43 #pragma ident	"%Z%%M%	%I%	%E% SMI"
44 
45 #include <sys/isa_defs.h>
46 #include <sys/feature_tests.h>
47 #include <vm/anon.h>
48 #include <sys/fs/swapnode.h>
49 
50 #ifdef	__cplusplus
51 extern "C" {
52 #endif
53 
54 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
55 #error  "Cannot use swapctl in the large files compilation environment"
56 #endif
57 
58 /* The following are for the swapctl system call */
59 
60 #define	SC_ADD		1	/* add a specified resource for swapping */
61 #define	SC_LIST		2	/* list all the swapping resources */
62 #define	SC_REMOVE	3	/* remove the specified swapping resource */
63 #define	SC_GETNSWP	4	/* get number of swap resources configured */
64 #define	SC_AINFO	5	/* get anonymous memory resource information */
65 
66 typedef struct swapres {
67 	char	*sr_name;	/* pathname of the resource specified */
68 	off_t	sr_start;	/* starting offset of the swapping resource */
69 	off_t 	sr_length;	/* length of the swap area */
70 } swapres_t;
71 
72 typedef struct swapent {
73 	char 	*ste_path;	/* get the name of the swap file */
74 	off_t	ste_start;	/* starting block for swapping */
75 	off_t	ste_length;	/* length of swap area */
76 	long	ste_pages;	/* numbers of pages for swapping */
77 	long	ste_free;	/* numbers of ste_pages free */
78 	int	ste_flags;	/* see below */
79 } swapent_t;
80 
81 typedef struct swaptable {
82 	int	swt_n;			/* number of swapents following */
83 	struct	swapent	swt_ent[1];	/* array of swt_n swapents */
84 } swaptbl_t;
85 
86 
87 #if defined(_SYSCALL32)
88 
89 /* Kernel's view of user ILP32 swapres and swapent structures */
90 
91 typedef struct swapres32 {
92 	caddr32_t sr_name;	/* pathname of the resource specified */
93 	off32_t	sr_start;	/* starting offset of the swapping resource */
94 	off32_t	sr_length;	/* length of the swap area */
95 } swapres32_t;
96 
97 typedef struct swapent32 {
98 	caddr32_t ste_path;	/* get the name of the swap file */
99 	off32_t	ste_start;	/* starting block for swapping */
100 	off32_t	ste_length;	/* length of swap area */
101 	int32_t	ste_pages;	/* numbers of pages for swapping */
102 	int32_t	ste_free;	/* numbers of ste_pages free */
103 	int32_t	ste_flags;	/* see below */
104 } swapent32_t;
105 
106 typedef struct	swaptable32 {
107 	int32_t	swt_n;			/* number of swapents following */
108 	struct	swapent32 swt_ent[1];	/* array of swt_n swapents */
109 } swaptbl32_t;
110 
111 #endif	/* _SYSCALL32 */
112 
113 #if defined(_KERNEL)
114 extern int swapctl(int, void *, int *);
115 #if defined(_LP64) && defined(_SYSCALL32)
116 extern int swapctl32(int, void *, int *);
117 #endif /* _LP64 && _SYSCALL32 */
118 #else /* !_KERNEL */
119 #if defined(__STDC__)
120 extern int swapctl(int, void *);
121 #else
122 extern int swapctl();
123 #endif
124 #endif /* _KERNEL */
125 
126 
127 /* ste_flags values */
128 
129 #define	ST_INDEL	0x01		/* Deletion of file is in progress. */
130 					/* Prevents others from deleting or */
131 					/* allocating from it */
132 #define	ST_DOINGDEL	0x02		/* Set during deletion of file */
133 					/* Clearing during deletion signals */
134 					/* that you want to add the file back */
135 					/* again, and will eventually cause */
136 					/* it to be added back */
137 
138 /*
139  * VM - virtual swap device.
140  */
141 struct	swapinfo {
142 	ulong_t si_soff;		/* starting offset (bytes) of file */
143 	ulong_t si_eoff;		/* ending offset (bytes) of file */
144 	struct	vnode *si_vp;		/* vnode (commonvp if device) */
145 	struct	swapinfo *si_next;	/* next swap area */
146 	int	si_allocs;		/* # of conseq. allocs from this area */
147 	short	si_flags;		/* flags defined below */
148 	pgcnt_t	si_npgs;		/* number of pages of swap space */
149 	pgcnt_t	si_nfpgs;		/* number of free pages of swap space */
150 	int 	si_pnamelen;		/* swap file name length + 1 */
151 	char 	*si_pname;		/* swap file name */
152 	ssize_t	si_mapsize;		/* # bytes allocated for bitmap */
153 	uint_t 	*si_swapslots;		/* bitmap of slots, unset == free */
154 	pgcnt_t	si_hint;		/* first page to check if free */
155 	ssize_t	si_checkcnt;		/* # of checks to find freeslot */
156 	ssize_t	si_alloccnt;		/* used to find ave checks */
157 };
158 
159 /*
160  * Stuff to convert an anon slot pointer to a page name.
161  * Because the address of the slot (ap) is a unique identifier, we
162  * use it to generate a unique (vp,off), as shown below.
163  *
164  *  	|<-- 11 bits -->|<------32 - 11 --------->|
165  *	   vp index bits	off bits
166  *
167  * The off bits are shifted PAGESHIFT to directly form a page aligned
168  * offset; the vp index bits map 1-1 to a vnode.
169  *
170  * Note: if we go to 64 bit offsets, we could use all the bits as the
171  * unique offset and just have one vnode.
172  */
173 #define	AN_OFFSHIFT	11			/* vnum # bits */
174 #define	AN_VPSHIFT	21 			/* 32 - 11 */
175 #define	AN_VPSIZEMASK	0x7FF			/* vp index mask */
176 #define	MAX_SWAP_VNODES	2048			/* 1 << AN_OFFSHIFT */
177 #define	AN_CACHE_ALIGN	16			/* anon address aligned */
178 						/* 16 bytes */
179 /*
180  * Convert from an anon slot to associated vnode and offset.
181  */
182 #define	swap_xlate(AP, VPP, OFFP)					\
183 {									\
184 	*(VPP) = (AP)->an_vp;						\
185 	*(OFFP) = (AP)->an_off;						\
186 }
187 #define	swap_xlate_nopanic	swap_xlate
188 
189 /*
190  * Get a vnode name for an anon slot.
191  * The vnum, offset are derived from anon struct address which is
192  * 16 bytes aligned. To get swap offset the anon address is shifted
193  * by additional 11 bits which yields 32K aligned swap offset
194  * (11 bits plus 4 bits alignment).
195  * The vnum (vnode index) is created from bits 31-21.
196  * The 64 bit swap offset is created from bits 63-32 and 20-4.
197  * The 32 bit offset is created from bits 20-4.
198  *
199  * +-----------...----------+--------+-----------------------+----+
200  * |        swap offset     |  vnum  |       swap offset     |0000|
201  * +-----------...----------+--------+-----------------------+----+
202  *  63	                  32 31    21 20	            4 3  0
203  */
204 #define	swap_alloc(AP)							\
205 {									\
206 	(AP)->an_vp = swapfs_getvp(((uintptr_t)(AP) >> AN_VPSHIFT)	\
207 	    & AN_VPSIZEMASK); 						\
208 	(AP)->an_off = (anoff_t)(((uintptr_t)(AP) & ~(uintptr_t)0xFFFFFFFF) \
209 	    | (((uintptr_t)(AP) << AN_OFFSHIFT) & (uintptr_t)0xFFFFFFFF)); \
210 }
211 
212 /*
213  * Free the page name for the specified anon slot.
214  * For now there's nothing to do.
215  */
216 #define	swap_free(AP)
217 
218 /* Flags for swap_phys_alloc */
219 #define	SA_NOT 	0x01 	/* Must have slot from swap dev other than input one */
220 
221 /* Special error codes for swap_newphysname() */
222 #define	SE_NOSWAP	-1	/* No physical swap slots available */
223 #define	SE_NOANON	-2	/* No anon slot for swap slot */
224 
225 #ifdef _KERNEL
226 extern struct anon *swap_anon(struct vnode *vp, u_offset_t off);
227 extern int swap_phys_alloc(struct vnode **vpp, u_offset_t *offp, size_t *lenp,
228     uint_t flags);
229 extern void swap_phys_free(struct vnode *vp, u_offset_t off, size_t len);
230 extern int swap_getphysname(struct vnode *vp, u_offset_t off,
231     struct vnode **pvpp, u_offset_t *poffp);
232 extern int swap_newphysname(struct vnode *vp, u_offset_t offset,
233     u_offset_t *offp, size_t *lenp, struct vnode **pvpp, u_offset_t *poffp);
234 
235 extern struct swapinfo *swapinfo;
236 extern int swap_debug;
237 #endif	/* _KERNEL */
238 
239 #ifdef SWAP_DEBUG
240 #define	SW_RENAME	0x01
241 #define	SW_RESV		0x02
242 #define	SW_ALLOC	0x04
243 #define	SW_CTL		0x08
244 #define	SWAP_PRINT(f, s, x1, x2, x3, x4, x5) \
245 		if (swap_debug & f) \
246 			printf(s, x1, x2, x3, x4, x5);
247 #else	/* SWAP_DEBUG */
248 #define	SWAP_PRINT(f, s, x1, x2, x3, x4, x5)
249 #endif	/* SWAP_DEBUG */
250 
251 #ifdef	__cplusplus
252 }
253 #endif
254 
255 #endif	/* _SYS_SWAP_H */
256