summaryrefslogtreecommitdiffstats
path: root/src/kernel/tests/testcases/kernel/syscalls/ioctl/ioctl_xloop04.c
blob: adf5cdfd76e7ff4dd72ceed7cb3234184068dc28 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
 * Author: Yang Xu <xuyang2018.jy@cn.jujitsu.com>
 *
 * This is a basic ioctl test about xloopdevice.
 *
 * It is designed to test XLOOP_SET_CAPACITY can update a live
 * xloop device size when we change the size of the underlying
 * backing file. Also check sys value.
 */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "lapi/xloop.h"
#include "tst_test.h"

#define OLD_SIZE 10240
#define NEW_SIZE 5120

static char dev_path[1024], sys_xloop_sizepath[1024];
static char *wrbuf;
static int dev_num, dev_fd, file_fd, attach_flag;

static void verify_ioctl_xloop(void)
{
	struct xloop_info xloopinfoget;

	memset(&xloopinfoget, 0, sizeof(xloopinfoget));
	tst_fill_file("test.img", 0, 1024, OLD_SIZE/1024);
	tst_attach_device(dev_path, "test.img");
	attach_flag = 1;

	TST_ASSERT_INT(sys_xloop_sizepath, OLD_SIZE/512);
	file_fd = SAFE_OPEN("test.img", O_RDWR);
	SAFE_IOCTL(dev_fd, XLOOP_GET_STATUS, &xloopinfoget);

	if (xloopinfoget.xlo_flags & XLO_FLAGS_READ_ONLY)
		tst_brk(TCONF, "Current environment has unexpected XLO_FLAGS_READ_ONLY flag");

	SAFE_TRUNCATE("test.img", NEW_SIZE);
	SAFE_IOCTL(dev_fd, XLOOP_SET_CAPACITY);

	SAFE_LSEEK(dev_fd, 0, SEEK_SET);

	/*check that we can't write data beyond 5K into xloop device*/
	TEST(write(dev_fd, wrbuf, OLD_SIZE));
	if (TST_RET == NEW_SIZE) {
		tst_res(TPASS, "XLOOP_SET_CAPACITY set xloop size to %d", NEW_SIZE);
	} else {
		tst_res(TFAIL, "XLOOP_SET_CAPACITY didn't set xloop size to %d, its size is %ld",
				NEW_SIZE, TST_RET);
	}

	TST_ASSERT_INT(sys_xloop_sizepath, NEW_SIZE/512);

	SAFE_CLOSE(file_fd);
	tst_detach_device_by_fd(dev_path, dev_fd);
	unlink("test.img");
	attach_flag = 0;
}

static void setup(void)
{
	dev_num = tst_find_free_xloopdev(dev_path, sizeof(dev_path));
	if (dev_num < 0)
		tst_brk(TBROK, "Failed to find free xloop device");

	wrbuf = SAFE_MALLOC(OLD_SIZE);
	memset(wrbuf, 'x', OLD_SIZE);
	sprintf(sys_xloop_sizepath, "/sys/block/xloop%d/size", dev_num);
	dev_fd = SAFE_OPEN(dev_path, O_RDWR);
}

static void cleanup(void)
{
	if (dev_fd > 0)
		SAFE_CLOSE(dev_fd);
	if (file_fd > 0)
		SAFE_CLOSE(file_fd);
	if (wrbuf)
		free(wrbuf);
	if (attach_flag)
		tst_detach_device(dev_path);
}

static struct tst_test test = {
	.setup = setup,
	.cleanup = cleanup,
	.test_all = verify_ioctl_xloop,
	.needs_root = 1,
	.needs_tmpdir = 1,
	.needs_drivers = (const char *const []) {
		"xloop",
		"xloop_file_fmt_raw",
		NULL
	}
};