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