1 /*
2 * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
3 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * Description:
21 * Check the basic functionality of the pwritev(2) for the file
22 * opened with O_DIRECT in all filesystem.
23 * pwritev(2) should succeed to write the expected content of data
24 * and after writing the file, the file offset is not changed.
25 */
26
27 #define _GNU_SOURCE
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/uio.h>
31 #include <sys/ioctl.h>
32 #include <sys/mount.h>
33 #include "tst_test.h"
34 #include "pwritev.h"
35 #include "tst_safe_prw.h"
36
37 #define MNTPOINT "mntpoint"
38 #define FNAME MNTPOINT"/file"
39
40 static char *initbuf, *preadbuf;
41 static int fd;
42 static off_t blk_off, zero_off;
43 static ssize_t blksz;
44
45 static struct iovec wr_iovec[] = {
46 {NULL, 0},
47 {NULL, 0},
48 };
49
50 static struct tcase {
51 int count;
52 off_t *offset;
53 ssize_t *size;
54 } tcases[] = {
55 {1, &zero_off, &blksz},
56 {2, &zero_off, &blksz},
57 {1, &blk_off, &blksz},
58 };
59
verify_direct_pwritev(unsigned int n)60 static void verify_direct_pwritev(unsigned int n)
61 {
62 int i;
63 struct tcase *tc = &tcases[n];
64
65 SAFE_PWRITE(1, fd, initbuf, blksz * 2, 0);
66
67 TEST(pwritev(fd, wr_iovec, tc->count, *tc->offset));
68 if (TST_RET < 0) {
69 tst_res(TFAIL | TTERRNO, "pwritev(O_DIRECT) fails");
70 return;
71 }
72
73 if (TST_RET != *tc->size) {
74 tst_res(TFAIL, "pwritev(O_DIRECT) wrote %li bytes, expected %zi",
75 TST_RET, *tc->size);
76 return;
77 }
78
79 if (SAFE_LSEEK(fd, 0, SEEK_CUR) != 0) {
80 tst_res(TFAIL, "pwritev(O_DIRECT) had changed file offset");
81 return;
82 }
83
84 memset(preadbuf, 0x00, blksz);
85 SAFE_PREAD(1, fd, preadbuf, *tc->size, *tc->offset);
86
87 for (i = 0; i < *tc->size; i++) {
88 if (preadbuf[i] != 0x61)
89 break;
90 }
91
92 if (i != *tc->size) {
93 tst_res(TFAIL, "Buffer wrong at %i have %02x expected 0x61",
94 i, preadbuf[i]);
95 return;
96 }
97
98 tst_res(TPASS, "pwritev(O_DIRECT) wrote %zi bytes successfully "
99 "with content 'a' expectedly ", *tc->size);
100 }
101
setup(void)102 static void setup(void)
103 {
104 int dev_fd, ret;
105
106 dev_fd = SAFE_OPEN(tst_device->dev, O_RDWR);
107 SAFE_IOCTL(dev_fd, BLKSSZGET, &ret);
108 SAFE_CLOSE(dev_fd);
109
110 if (ret <= 0)
111 tst_brk(TBROK, "BLKSSZGET returned invalid block size %i", ret);
112
113 tst_res(TINFO, "Using block size %i", ret);
114
115 blksz = ret;
116 blk_off = ret;
117
118 fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT | O_DIRECT, 0644);
119
120 initbuf = SAFE_MEMALIGN(blksz, blksz * 2);
121 memset(initbuf, 0x00, blksz * 2);
122
123 preadbuf = SAFE_MEMALIGN(blksz, blksz);
124
125 wr_iovec[0].iov_base = SAFE_MEMALIGN(blksz, blksz);
126 wr_iovec[0].iov_len = blksz;
127 memset(wr_iovec[0].iov_base, 0x61, blksz);
128 }
129
cleanup(void)130 static void cleanup(void)
131 {
132 free(initbuf);
133 free(preadbuf);
134 free(wr_iovec[0].iov_base);
135
136 if (fd > 0)
137 SAFE_CLOSE(fd);
138 }
139
140 static struct tst_test test = {
141 .tcnt = ARRAY_SIZE(tcases),
142 .setup = setup,
143 .cleanup = cleanup,
144 .test = verify_direct_pwritev,
145 .min_kver = "2.6.30",
146 .mntpoint = MNTPOINT,
147 .mount_device = 1,
148 .all_filesystems = 1,
149 };
150