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