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