• 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 pwritev(2) for the file
10  * opened with O_DIRECT in all filesystem.
11  * pwritev(2) should succeed to write the expected content of data
12  * and after writing 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 "pwritev.h"
23 #include "tst_safe_prw.h"
24 
25 #define MNTPOINT	"mntpoint"
26 #define FNAME	MNTPOINT"/file"
27 
28 static char *initbuf, *preadbuf;
29 static int fd;
30 static off_t blk_off, zero_off;
31 static ssize_t blksz;
32 
33 static struct iovec wr_iovec[] = {
34 	{NULL, 0},
35 	{NULL, 0},
36 };
37 
38 static struct tcase {
39 	int count;
40 	off_t *offset;
41 	ssize_t *size;
42 } tcases[] = {
43 	{1, &zero_off, &blksz},
44 	{2, &zero_off, &blksz},
45 	{1, &blk_off, &blksz},
46 };
47 
verify_direct_pwritev(unsigned int n)48 static void verify_direct_pwritev(unsigned int n)
49 {
50 	int i;
51 	struct tcase *tc = &tcases[n];
52 
53 	SAFE_PWRITE(1, fd, initbuf, blksz * 2, 0);
54 
55 	TEST(pwritev(fd, wr_iovec, tc->count, *tc->offset));
56 	if (TST_RET < 0) {
57 		tst_res(TFAIL | TTERRNO, "pwritev(O_DIRECT) fails");
58 		return;
59 	}
60 
61 	if (TST_RET != *tc->size) {
62 		tst_res(TFAIL, "pwritev(O_DIRECT) wrote %li bytes, expected %zi",
63 			 TST_RET, *tc->size);
64 		return;
65 	}
66 
67 	if (SAFE_LSEEK(fd, 0, SEEK_CUR) != 0) {
68 		tst_res(TFAIL, "pwritev(O_DIRECT) had changed file offset");
69 		return;
70 	}
71 
72 	memset(preadbuf, 0x00, blksz);
73 	SAFE_PREAD(1, fd, preadbuf, *tc->size, *tc->offset);
74 
75 	for (i = 0; i < *tc->size; i++) {
76 		if (preadbuf[i] != 0x61)
77 			break;
78 	}
79 
80 	if (i != *tc->size) {
81 		tst_res(TFAIL, "Buffer wrong at %i have %02x expected 0x61",
82 			 i, preadbuf[i]);
83 		return;
84 	}
85 
86 	tst_res(TPASS, "pwritev(O_DIRECT) wrote %zi bytes successfully "
87 		 "with content 'a' expectedly ", *tc->size);
88 }
89 
setup(void)90 static void setup(void)
91 {
92 	int dev_fd, ret;
93 
94 	dev_fd = SAFE_OPEN(tst_device->dev, O_RDWR);
95 	SAFE_IOCTL(dev_fd, BLKSSZGET, &ret);
96 	SAFE_CLOSE(dev_fd);
97 
98 	if (ret <= 0)
99 		tst_brk(TBROK, "BLKSSZGET returned invalid block size %i", ret);
100 
101 	tst_res(TINFO, "Using block size %i", ret);
102 
103 	blksz = ret;
104 	blk_off = ret;
105 
106 	fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT | O_DIRECT, 0644);
107 
108 	initbuf = SAFE_MEMALIGN(blksz, blksz * 2);
109 	memset(initbuf, 0x00, blksz * 2);
110 
111 	preadbuf = SAFE_MEMALIGN(blksz, blksz);
112 
113 	wr_iovec[0].iov_base = SAFE_MEMALIGN(blksz, blksz);
114 	wr_iovec[0].iov_len = blksz;
115 	memset(wr_iovec[0].iov_base, 0x61, blksz);
116 }
117 
cleanup(void)118 static void cleanup(void)
119 {
120 	free(initbuf);
121 	free(preadbuf);
122 	free(wr_iovec[0].iov_base);
123 
124 	if (fd > 0)
125 		SAFE_CLOSE(fd);
126 }
127 
128 static struct tst_test test = {
129 	.tcnt = ARRAY_SIZE(tcases),
130 	.setup = setup,
131 	.cleanup = cleanup,
132 	.test = verify_direct_pwritev,
133 	.min_kver = "2.6.30",
134 	.mntpoint = MNTPOINT,
135 	.mount_device = 1,
136 	.all_filesystems = 1,
137 	.skip_filesystems = (const char *[]){
138 		"tmpfs",
139 		NULL
140 	}
141 };
142