• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
4  * Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
5  */
6 
7 #include <errno.h>
8 
9 #define TST_NO_DEFAULT_MAIN
10 
11 #include "lapi/syscalls.h"
12 #include "tst_test.h"
13 #include "libswap.h"
14 
15 /*
16  * Make a swap file
17  */
make_swapfile(const char * swapfile,int safe)18 int make_swapfile(const char *swapfile, int safe)
19 {
20 	if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
21 		tst_brk(TBROK, "Insufficient disk space to create swap file");
22 
23 	/* create file */
24 	if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
25 		tst_brk(TBROK, "Failed to create swapfile");
26 
27 	/* make the file swapfile */
28 	const char *argv[2 + 1];
29 	argv[0] = "mkswap";
30 	argv[1] = swapfile;
31 	argv[2] = NULL;
32 
33 	return tst_cmd(argv, "/dev/null", "/dev/null", safe);
34 }
35 
36 /*
37  * Check swapon/swapoff support status of filesystems or files
38  * we are testing on.
39  */
is_swap_supported(const char * filename)40 void is_swap_supported(const char *filename)
41 {
42 	int fibmap = tst_fibmap(filename);
43 	long fs_type = tst_fs_type(filename);
44 	const char *fstype = tst_fs_type_name(fs_type);
45 
46 	int ret = make_swapfile(filename, 1);
47 	if (ret != 0) {
48 		if (fibmap == 1)
49 			tst_brk(TCONF, "mkswap on %s not supported", fstype);
50 		else
51 			tst_brk(TFAIL, "mkswap on %s failed", fstype);
52 	}
53 
54 	TEST(tst_syscall(__NR_swapon, filename, 0));
55 	if (TST_RET == -1) {
56 		if (fibmap == 1 && errno == EINVAL)
57 			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
58 		else
59 			tst_brk(TFAIL | TTERRNO, "swapon on %s failed", fstype);
60 	}
61 
62 	TEST(tst_syscall(__NR_swapoff, filename, 0));
63 	if (TST_RET == -1)
64 		tst_brk(TFAIL | TTERRNO, "swapoff on %s failed", fstype);
65 }
66