• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write the Free Software Foundation,
16  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
19  *
20  */
21 
22 #include <errno.h>
23 #include "lapi/syscalls.h"
24 #include "test.h"
25 #include "libswapon.h"
26 
27 /*
28  * Make a swap file
29  */
make_swapfile(void (cleanup)(void),const char * swapfile,int safe)30 int make_swapfile(void (cleanup)(void), const char *swapfile, int safe)
31 {
32 	if (!tst_fs_has_free(NULL, ".", sysconf(_SC_PAGESIZE) * 10,
33 	    TST_BYTES)) {
34 		tst_brkm(TBROK, cleanup,
35 			"Insufficient disk space to create swap file");
36 	}
37 
38 	/* create file */
39 	if (tst_fill_file(swapfile, 0,
40 			sysconf(_SC_PAGESIZE), 10) != 0) {
41 		tst_brkm(TBROK, cleanup, "Failed to create swapfile");
42 	}
43 
44 	/* make the file swapfile */
45 	const char *argv[2 + 1];
46 	argv[0] = "mkswap";
47 	argv[1] = swapfile;
48 	argv[2] = NULL;
49 
50 	return tst_run_cmd(cleanup, argv, "/dev/null", "/dev/null", safe);
51 }
52 
53 /*
54  * Check swapon/swapoff support status of filesystems or files
55  * we are testing on.
56  */
is_swap_supported(void (cleanup)(void),const char * filename)57 void is_swap_supported(void (cleanup)(void), const char *filename)
58 {
59 	int fibmap = tst_fibmap(filename);
60 	long fs_type = tst_fs_type(cleanup, filename);
61 	const char *fstype = tst_fs_type_name(fs_type);
62 
63 	int ret = make_swapfile(NULL, filename, 1);
64 	if (ret != 0) {
65 		if (fibmap == 1) {
66 			tst_brkm(TCONF, cleanup,
67 				"mkswap on %s not supported", fstype);
68 		} else {
69 			tst_brkm(TFAIL, cleanup,
70 				"mkswap on %s failed", fstype);
71 		}
72 	}
73 
74 	TEST(ltp_syscall(__NR_swapon, filename, 0));
75 	if (TEST_RETURN == -1) {
76 		if (fibmap == 1 && errno == EINVAL) {
77 			tst_brkm(TCONF, cleanup,
78 				"Swapfile on %s not implemented", fstype);
79 		} else {
80 			tst_brkm(TFAIL | TERRNO, cleanup,
81 				 "swapon on %s failed", fstype);
82 		}
83 	}
84 
85 	TEST(ltp_syscall(__NR_swapoff, filename, 0));
86 	if (TEST_RETURN == -1) {
87 		tst_brkm(TFAIL | TERRNO, cleanup,
88 			"swapoff on %s failed", fstype);
89 	}
90 }
91