1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
4 * Author: Alexey Kodanev <alexey.kodanev@oracle.com>
5 */
6
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <pthread.h>
12 #include <sched.h>
13
14 #include "lapi/fcntl.h"
15 #include "tst_safe_pthread.h"
16 #include "tst_test.h"
17 #include "fcntl_common.h"
18
19 static int thread_cnt;
20 static const int max_thread_cnt = 32;
21 static const char fname[] = "tst_ofd_locks";
22 const int writes_num = 100;
23 const int write_size = 4096;
24
setup(void)25 static void setup(void)
26 {
27 thread_cnt = tst_ncpus_conf() * 3;
28 if (thread_cnt > max_thread_cnt)
29 thread_cnt = max_thread_cnt;
30 }
31
spawn_threads(pthread_t * id,void * (* thread_fn)(void *))32 static void spawn_threads(pthread_t *id, void *(*thread_fn)(void *))
33 {
34 intptr_t i;
35
36 tst_res(TINFO, "spawning '%d' threads", thread_cnt);
37 for (i = 0; i < thread_cnt; ++i)
38 SAFE_PTHREAD_CREATE(id + i, NULL, thread_fn, (void *)i);
39 }
40
wait_threads(pthread_t * id)41 static void wait_threads(pthread_t *id)
42 {
43 int i;
44
45 tst_res(TINFO, "waiting for '%d' threads", thread_cnt);
46 for (i = 0; i < thread_cnt; ++i)
47 SAFE_PTHREAD_JOIN(id[i], NULL);
48 }
49
thread_fn_01(void * arg)50 void *thread_fn_01(void *arg)
51 {
52 int i;
53 unsigned char buf[write_size];
54 int fd = SAFE_OPEN(fname, O_RDWR);
55
56 memset(buf, (intptr_t)arg, write_size);
57
58 struct flock64 lck = {
59 .l_whence = SEEK_SET,
60 .l_start = 0,
61 .l_len = 1,
62 };
63
64 for (i = 0; i < writes_num; ++i) {
65 lck.l_type = F_WRLCK;
66 my_fcntl(fd, F_OFD_SETLKW, &lck);
67
68 SAFE_LSEEK(fd, 0, SEEK_END);
69 SAFE_WRITE(1, fd, buf, write_size);
70
71 lck.l_type = F_UNLCK;
72 my_fcntl(fd, F_OFD_SETLKW, &lck);
73
74 sched_yield();
75 }
76
77 SAFE_CLOSE(fd);
78
79 return NULL;
80 }
81
test01(void)82 static void test01(void)
83 {
84 intptr_t i;
85 int k;
86 pthread_t id[thread_cnt];
87 int res[thread_cnt];
88 unsigned char buf[write_size];
89
90 tst_res(TINFO, "write to a file inside threads with OFD locks");
91
92 int fd = SAFE_OPEN(fname, O_CREAT | O_TRUNC | O_RDWR, 0600);
93
94 memset(res, 0, sizeof(res));
95
96 spawn_threads(id, thread_fn_01);
97 wait_threads(id);
98
99 tst_res(TINFO, "verifying file's data");
100 SAFE_LSEEK(fd, 0, SEEK_SET);
101 for (i = 0; i < writes_num * thread_cnt; ++i) {
102 SAFE_READ(1, fd, buf, write_size);
103
104 if (buf[0] >= thread_cnt) {
105 tst_res(TFAIL, "unexpected data read");
106 return;
107 }
108
109 ++res[buf[0]];
110
111 for (k = 1; k < write_size; ++k) {
112 if (buf[0] != buf[k]) {
113 tst_res(TFAIL, "unexpected data read");
114 return;
115 }
116 }
117 }
118
119 for (i = 0; i < thread_cnt; ++i) {
120 if (res[i] != writes_num) {
121 tst_res(TFAIL, "corrupted data found");
122 return;
123 }
124 }
125 SAFE_CLOSE(fd);
126
127 tst_res(TPASS, "OFD locks synchronized access between threads");
128 }
129
130 static struct tst_test test = {
131 .min_kver = "3.15.0",
132 .needs_tmpdir = 1,
133 .test_all = test01,
134 .setup = setup
135 };
136