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