1 /*
2  * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it would be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  *
17  * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
18  *
19  */
20 
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <pthread.h>
26 
27 #include "lapi/fcntl.h"
28 #include "tst_test.h"
29 
30 static int thread_cnt;
31 static const int max_thread_cnt = 32;
32 static const char fname[] = "tst_ofd_locks";
33 const int writes_num = 100;
34 const int write_size = 4096;
35 
setup(void)36 static void setup(void)
37 {
38 	thread_cnt = tst_ncpus_conf() * 3;
39 	if (thread_cnt > max_thread_cnt)
40 		thread_cnt = max_thread_cnt;
41 }
42 
spawn_threads(pthread_t * id,void * (* thread_fn)(void *))43 static void spawn_threads(pthread_t *id, void *(*thread_fn)(void *))
44 {
45 	intptr_t i;
46 
47 	tst_res(TINFO, "spawning '%d' threads", thread_cnt);
48 	for (i = 0; i < thread_cnt; ++i)
49 		SAFE_PTHREAD_CREATE(id + i, NULL, thread_fn, (void *)i);
50 }
51 
wait_threads(pthread_t * id)52 static void wait_threads(pthread_t *id)
53 {
54 	int i;
55 
56 	tst_res(TINFO, "waiting for '%d' threads", thread_cnt);
57 	for (i = 0; i < thread_cnt; ++i)
58 		SAFE_PTHREAD_JOIN(id[i], NULL);
59 }
60 
thread_fn_01(void * arg)61 void *thread_fn_01(void *arg)
62 {
63 	int i;
64 	unsigned char buf[write_size];
65 	int fd = SAFE_OPEN(fname, O_RDWR);
66 
67 	memset(buf, (intptr_t)arg, write_size);
68 
69 	struct flock lck = {
70 		.l_whence = SEEK_SET,
71 		.l_start  = 0,
72 		.l_len    = 1,
73 	};
74 
75 	for (i = 0; i < writes_num; ++i) {
76 		lck.l_type = F_WRLCK;
77 		if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
78 			tst_brk(TBROK | TERRNO, "fcntl() failed");
79 
80 		SAFE_LSEEK(fd, 0, SEEK_END);
81 		SAFE_WRITE(1, fd, buf, write_size);
82 
83 		lck.l_type = F_UNLCK;
84 		if (fcntl(fd, F_OFD_SETLKW, &lck) == -1)
85 			tst_brk(TBROK | TERRNO, "fcntl() failed");
86 
87 		pthread_yield();
88 	}
89 
90 	SAFE_CLOSE(fd);
91 
92 	return NULL;
93 }
94 
test01(void)95 static void test01(void)
96 {
97 	intptr_t i;
98 	int k;
99 	pthread_t id[thread_cnt];
100 	int res[thread_cnt];
101 	unsigned char buf[write_size];
102 
103 	tst_res(TINFO, "write to a file inside threads with OFD locks");
104 
105 	int fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDWR, 0600);
106 
107 	memset(res, 0, sizeof(res));
108 
109 	spawn_threads(id, thread_fn_01);
110 	wait_threads(id);
111 
112 	tst_res(TINFO, "verifying file's data");
113 	SAFE_LSEEK(fd, 0, SEEK_SET);
114 	for (i = 0; i < writes_num * thread_cnt; ++i) {
115 		SAFE_READ(1, fd, buf, write_size);
116 
117 		if (buf[0] >= thread_cnt) {
118 			tst_res(TFAIL, "unexpected data read");
119 			return;
120 		}
121 
122 		++res[buf[0]];
123 
124 		for (k = 1; k < write_size; ++k) {
125 			if (buf[0] != buf[k]) {
126 				tst_res(TFAIL, "unexpected data read");
127 				return;
128 			}
129 		}
130 	}
131 
132 	for (i = 0; i < thread_cnt; ++i) {
133 		if (res[i] != writes_num) {
134 			tst_res(TFAIL, "corrupted data found");
135 			return;
136 		}
137 	}
138 	SAFE_CLOSE(fd);
139 
140 	tst_res(TPASS, "OFD locks synchronized access between threads");
141 }
142 
143 static struct tst_test test = {
144 	.tid = "fcntl34",
145 	.min_kver = "3.15.0",
146 	.needs_tmpdir = 1,
147 	.test_all = test01,
148 	.setup = setup
149 };
150