• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4  * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5  */
6 
7 /*
8  * Description:
9  * Check the basic functionality of the preadv(2) for the file
10  * opened with O_DIRECT in all filesystem.
11  * preadv(2) should succeed to read the expected content of data
12  * and after reading the file, the file offset is not changed.
13  */
14 
15 #define _GNU_SOURCE
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/uio.h>
19 #include <sys/ioctl.h>
20 #include <sys/mount.h>
21 #include "tst_test.h"
22 #include "preadv.h"
23 
24 #define MNTPOINT	"mntpoint"
25 #define FNAME	MNTPOINT"/file"
26 
27 static int fd;
28 static off_t blk_off, zero_off;
29 static ssize_t blksz;
30 static char *pop_buf;
31 
32 static struct iovec rd_iovec[] = {
33 	{NULL, 0},
34 	{NULL, 0},
35 };
36 
37 static struct tcase {
38 	int count;
39 	off_t *offset;
40 	ssize_t *size;
41 	char content;
42 } tcases[] = {
43 	{1, &zero_off, &blksz, 0x61},
44 	{2, &zero_off, &blksz, 0x61},
45 	{1, &blk_off, &blksz, 0x62},
46 };
47 
verify_direct_preadv(unsigned int n)48 static void verify_direct_preadv(unsigned int n)
49 {
50 	int i;
51 	char *vec;
52 	struct tcase *tc = &tcases[n];
53 
54 	vec = rd_iovec[0].iov_base;
55 	memset(vec, 0x00, blksz);
56 
57 	SAFE_LSEEK(fd, 0, SEEK_SET);
58 
59 	TEST(preadv(fd, rd_iovec, tc->count, *tc->offset));
60 	if (TST_RET < 0) {
61 		tst_res(TFAIL | TTERRNO, "preadv(O_DIRECT) fails");
62 		return;
63 	}
64 
65 	if (TST_RET != *tc->size) {
66 		tst_res(TFAIL, "preadv(O_DIRECT) read %li bytes, expected %zi",
67 			 TST_RET, *tc->size);
68 		return;
69 	}
70 
71 	for (i = 0; i < *tc->size; i++) {
72 		if (vec[i] != tc->content)
73 			break;
74 	}
75 
76 	if (i < *tc->size) {
77 		tst_res(TFAIL, "Buffer wrong at %i have %02x expected %02x",
78 			 i, vec[i], tc->content);
79 		return;
80 	}
81 
82 	if (SAFE_LSEEK(fd, 0, SEEK_CUR) != 0) {
83 		tst_res(TFAIL, "preadv(O_DIRECT) has changed file offset");
84 		return;
85 	}
86 
87 	tst_res(TPASS, "preadv(O_DIRECT) read %zi bytes successfully "
88 		 "with content '%c' expectedly", *tc->size, tc->content);
89 }
90 
setup(void)91 static void setup(void)
92 {
93 	int dev_fd, ret;
94 
95 	dev_fd = SAFE_OPEN(tst_device->dev, O_RDWR);
96 	SAFE_IOCTL(dev_fd, BLKSSZGET, &ret);
97 	SAFE_CLOSE(dev_fd);
98 
99 	if (ret <= 0)
100 		tst_brk(TBROK, "BLKSSZGET returned invalid block size %i", ret);
101 
102 	tst_res(TINFO, "Using block size %i", ret);
103 
104 	blksz = ret;
105 	blk_off = ret;
106 
107 	fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT | O_DIRECT, 0644);
108 
109 	pop_buf = SAFE_MEMALIGN(blksz, blksz);
110 	memset(pop_buf, 0x61, blksz);
111 	SAFE_WRITE(1, fd, pop_buf, blksz);
112 
113 	memset(pop_buf, 0x62, blksz);
114 	SAFE_WRITE(1, fd, pop_buf, blksz);
115 
116 	rd_iovec[0].iov_base = SAFE_MEMALIGN(blksz, blksz);
117 	rd_iovec[0].iov_len = blksz;
118 }
119 
cleanup(void)120 static void cleanup(void)
121 {
122 	free(pop_buf);
123 	free(rd_iovec[0].iov_base);
124 
125 	if (fd > 0)
126 		SAFE_CLOSE(fd);
127 }
128 
129 static struct tst_test test = {
130 	.tcnt = ARRAY_SIZE(tcases),
131 	.setup = setup,
132 	.cleanup = cleanup,
133 	.test = verify_direct_preadv,
134 	.min_kver = "2.6.30",
135 	.mntpoint = MNTPOINT,
136 	.mount_device = 1,
137 	.all_filesystems = 1,
138 	.skip_filesystems = (const char *[]) {
139 		"tmpfs",
140 		NULL
141 	}
142 };
143