1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved. 4 * Copyright (c) Linux Test Project, 2021-2024 5 * Author: Stanislav Kholmanskikh <[email protected]> 6 */ 7 8 /* 9 * Contains common content for all swapon/swapoff tests. 10 */ 11 12 #ifndef __LIBSWAP_H__ 13 #define __LIBSWAP_H__ 14 15 enum swapfile_method { 16 SWAPFILE_BY_SIZE, 17 SWAPFILE_BY_BLKS 18 }; 19 20 /* 21 * Create a swapfile of a specified size or number of blocks. 22 */ 23 int make_swapfile(const char *file, const int lineno, 24 const char *swapfile, unsigned int num, 25 int safe, enum swapfile_method method); 26 27 /** 65536 bytes is minimum for 64kb page size, let's use 1 MB */ 28 #define MINIMAL_SWAP_SIZE_MB 1 29 30 /** 31 * MAKE_SMALL_SWAPFILE - create small swap file. 32 * 33 * Macro to create small swap file. Size defined with MINIMAL_SWAP_SIZE_MB. 34 * 35 * @swapfile: swap filename. 36 */ 37 #define MAKE_SMALL_SWAPFILE(swapfile) \ 38 make_swapfile(__FILE__, __LINE__, swapfile, MINIMAL_SWAP_SIZE_MB, 0, \ 39 SWAPFILE_BY_SIZE) 40 41 /** 42 * SAFE_MAKE_SMALL_SWAPFILE - create small swap file (safe version). 43 * 44 * Macro to create small swap file. Size defined with MINIMAL_SWAP_SIZE_MB. 45 * Includes safety checks to handle potential errors. 46 * 47 * @swapfile: swap filename. 48 */ 49 #define SAFE_MAKE_SMALL_SWAPFILE(swapfile) \ 50 make_swapfile(__FILE__, __LINE__, swapfile, MINIMAL_SWAP_SIZE_MB, 1, \ 51 SWAPFILE_BY_SIZE) 52 53 /** 54 * MAKE_SWAPFILE_SIZE - create swap file (MB). 55 * 56 * Macro to create swap file, size specified in megabytes (MB). 57 * 58 * @swapfile: swap filename. 59 * @size: swap size in MB. 60 */ 61 #define MAKE_SWAPFILE_SIZE(swapfile, size) \ 62 make_swapfile(__FILE__, __LINE__, swapfile, size, 0, SWAPFILE_BY_SIZE) 63 64 /** 65 * MAKE_SWAPFILE_BLKS - create swap file (blocks). 66 * 67 * Macro to create swap file, size specified in block numbers. 68 * 69 * @swapfile: swap filename. 70 * @blocks: number of blocks. 71 */ 72 #define MAKE_SWAPFILE_BLKS(swapfile, blocks) \ 73 make_swapfile(__FILE__, __LINE__, swapfile, blocks, 0, SWAPFILE_BY_BLKS) 74 75 /** 76 * SAFE_MAKE_SWAPFILE_SIZE - create swap file (MB, safe version). 77 * 78 * Macro to safely create swap file, size specified in megabytes (MB). 79 * Includes safety checks to handle potential errors. 80 * 81 * @swapfile: swap file name. 82 * @size: swap size in MB. 83 */ 84 #define SAFE_MAKE_SWAPFILE_SIZE(swapfile, size) \ 85 make_swapfile(__FILE__, __LINE__, swapfile, size, 1, SWAPFILE_BY_SIZE) 86 87 /** 88 * SAFE_MAKE_SWAPFILE_BLKS - create swap file (block, safe version) 89 * 90 * Macro to safely create swap file, size specified in block numbers. 91 * Includes safety checks to handle potential errors. 92 * 93 * @swapfile: swap file name. 94 * @blocks: number of blocks. 95 */ 96 #define SAFE_MAKE_SWAPFILE_BLKS(swapfile, blocks) \ 97 make_swapfile(__FILE__, __LINE__, swapfile, blocks, 1, SWAPFILE_BY_BLKS) 98 99 /** 100 * is_swap_supported() - Check swapon/swapoff support. 101 * 102 * Check swapon/swapoff support status of filesystems or files 103 * we are testing on. 104 * 105 * @filename: swap file name. 106 * Return: true if swap is supported, false if not. 107 */ 108 bool is_swap_supported(const char *filename); 109 110 /** 111 * tst_max_swapfiles() - Get kernel constant MAX_SWAPFILES value. 112 * 113 * Return: MAX_SWAPFILES value. 114 */ 115 int tst_max_swapfiles(void); 116 117 /** 118 * tst_count_swaps() - Get the used swapfiles number. 119 * 120 * Return: used swapfiles number. 121 */ 122 int tst_count_swaps(void); 123 124 #endif /* __LIBSWAP_H__ */ 125