• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sched.h>
27 
28 #include "lapi/fcntl.h"
29 #include "tst_safe_pthread.h"
30 #include "tst_test.h"
31 
32 static int thread_cnt;
33 static const int max_thread_cnt = 32;
34 static const char fname[] = "tst_ofd_locks";
35 const int writes_num = 100;
36 const int write_size = 4096;
37 
setup(void)38 static void setup(void)
39 {
40 	thread_cnt = tst_ncpus_conf() * 3;
41 	if (thread_cnt > max_thread_cnt)
42 		thread_cnt = max_thread_cnt;
43 }
44 
spawn_threads(pthread_t * id,void * (* thread_fn)(void *))45 static void spawn_threads(pthread_t *id, void *(*thread_fn)(void *))
46 {
47 	intptr_t i;
48 
49 	tst_res(TINFO, "spawning '%d' threads", thread_cnt);
50 	for (i = 0; i < thread_cnt; ++i)
51 		SAFE_PTHREAD_CREATE(id + i, NULL, thread_fn, (void *)i);
52 }
53 
wait_threads(pthread_t * id)54 static void wait_threads(pthread_t *id)
55 {
56 	int i;
57 
58 	tst_res(TINFO, "waiting for '%d' threads", thread_cnt);
59 	for (i = 0; i < thread_cnt; ++i)
60 		SAFE_PTHREAD_JOIN(id[i], NULL);
61 }
62 
thread_fn_01(void * arg)63 void *thread_fn_01(void *arg)
64 {
65 	int i;
66 	unsigned char buf[write_size];
67 	int fd = SAFE_OPEN(fname, O_RDWR);
68 
69 	memset(buf, (intptr_t)arg, write_size);
70 
71 	struct flock64 lck = {
72 		.l_whence = SEEK_SET,
73 		.l_start  = 0,
74 		.l_len    = 1,
75 	};
76 
77 	for (i = 0; i < writes_num; ++i) {
78 		lck.l_type = F_WRLCK;
79 		SAFE_FCNTL(fd, F_OFD_SETLKW, &lck);
80 
81 		SAFE_LSEEK(fd, 0, SEEK_END);
82 		SAFE_WRITE(1, fd, buf, write_size);
83 
84 		lck.l_type = F_UNLCK;
85 		SAFE_FCNTL(fd, F_OFD_SETLKW, &lck);
86 
87 		sched_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 	.min_kver = "3.15.0",
145 	.needs_tmpdir = 1,
146 	.test_all = test01,
147 	.setup = setup
148 };
149