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