xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/dir_rd_update.c (revision be181ee2a28aa2b4b0e76684bce9f673ef668874)
1716fd348SMartin Matuska /*
2716fd348SMartin Matuska  * CDDL HEADER START
3716fd348SMartin Matuska  *
4716fd348SMartin Matuska  * The contents of this file are subject to the terms of the
5716fd348SMartin Matuska  * Common Development and Distribution License (the "License").
6716fd348SMartin Matuska  * You may not use this file except in compliance with the License.
7716fd348SMartin Matuska  *
8716fd348SMartin Matuska  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10716fd348SMartin Matuska  * See the License for the specific language governing permissions
11716fd348SMartin Matuska  * and limitations under the License.
12716fd348SMartin Matuska  *
13716fd348SMartin Matuska  * When distributing Covered Code, include this CDDL HEADER in each
14716fd348SMartin Matuska  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15716fd348SMartin Matuska  * If applicable, add the following below this CDDL HEADER, with the
16716fd348SMartin Matuska  * fields enclosed by brackets "[]" replaced with your own identifying
17716fd348SMartin Matuska  * information: Portions Copyright [yyyy] [name of copyright owner]
18716fd348SMartin Matuska  *
19716fd348SMartin Matuska  * CDDL HEADER END
20716fd348SMartin Matuska  */
21716fd348SMartin Matuska 
22716fd348SMartin Matuska /*
23716fd348SMartin Matuska  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24716fd348SMartin Matuska  * Use is subject to license terms.
25716fd348SMartin Matuska  */
26716fd348SMartin Matuska 
27716fd348SMartin Matuska /*
28716fd348SMartin Matuska  * Assertion:
29716fd348SMartin Matuska  *
30716fd348SMartin Matuska  *	A read operation and directory update operation performed
31716fd348SMartin Matuska  *      concurrently on the same directory can lead to deadlock
32716fd348SMartin Matuska  *	on a UFS logging file system, but not on a ZFS file system.
33716fd348SMartin Matuska  */
34716fd348SMartin Matuska 
35716fd348SMartin Matuska #include <sys/types.h>
36716fd348SMartin Matuska #include <sys/stat.h>
37716fd348SMartin Matuska #include <errno.h>
38716fd348SMartin Matuska #include <fcntl.h>
39716fd348SMartin Matuska #include <string.h>
40716fd348SMartin Matuska #include <stdio.h>
41716fd348SMartin Matuska #include <stdlib.h>
42716fd348SMartin Matuska #include <unistd.h>
43716fd348SMartin Matuska #define	TMP_DIR /tmp
44716fd348SMartin Matuska 
45716fd348SMartin Matuska static char dirpath[256];
46716fd348SMartin Matuska 
47716fd348SMartin Matuska int
main(int argc,char ** argv)48716fd348SMartin Matuska main(int argc, char **argv)
49716fd348SMartin Matuska {
50a0b956f5SMartin Matuska 	const char *cp1 = "";
51716fd348SMartin Matuska 	int i = 0;
52716fd348SMartin Matuska 	int ret = 0;
53716fd348SMartin Matuska 	int testdd = 0;
54716fd348SMartin Matuska 	pid_t pid;
55716fd348SMartin Matuska 	static const int op_num = 5;
56716fd348SMartin Matuska 
57716fd348SMartin Matuska 	if (argc == 1) {
58716fd348SMartin Matuska 		(void) printf("Usage: %s <mount point>\n", argv[0]);
59716fd348SMartin Matuska 		exit(-1);
60716fd348SMartin Matuska 	}
61716fd348SMartin Matuska 	for (i = 0; i < 256; i++) {
62716fd348SMartin Matuska 		dirpath[i] = 0;
63716fd348SMartin Matuska 	}
64716fd348SMartin Matuska 
65716fd348SMartin Matuska 	cp1 = argv[1];
66*be181ee2SMartin Matuska 	if (strlen(cp1) >= (sizeof (dirpath) - strlen("/TMP_DIR"))) {
67716fd348SMartin Matuska 		(void) printf("The string length of mount point is "
68716fd348SMartin Matuska 		    "too large\n");
69716fd348SMartin Matuska 		exit(-1);
70716fd348SMartin Matuska 	}
71*be181ee2SMartin Matuska 	(void) snprintf(dirpath, sizeof (dirpath), "%s/TMP_DIR", cp1);
72716fd348SMartin Matuska 
73716fd348SMartin Matuska 	ret = mkdir(dirpath, 0777);
74716fd348SMartin Matuska 	if (ret != 0) {
75716fd348SMartin Matuska 		if (errno != EEXIST) {
76716fd348SMartin Matuska 			(void) printf("%s: mkdir(<%s>, 0777) failed: errno "
77716fd348SMartin Matuska 			    "(decimal)=%d\n", argv[0], dirpath, errno);
78716fd348SMartin Matuska 			exit(-1);
79716fd348SMartin Matuska 		}
80716fd348SMartin Matuska 	}
81716fd348SMartin Matuska 	testdd = open(dirpath, O_RDONLY|O_RSYNC|O_SYNC|O_DSYNC);
82716fd348SMartin Matuska 	if (testdd < 0) {
83716fd348SMartin Matuska 		(void) printf("%s: open(<%s>, O_RDONLY|O_RSYNC|O_SYNC|O_DSYNC)"
84716fd348SMartin Matuska 		    " failed: errno (decimal)=%d\n", argv[0], dirpath, errno);
85716fd348SMartin Matuska 		exit(-1);
86716fd348SMartin Matuska 	} else {
87716fd348SMartin Matuska 		(void) close(testdd);
88716fd348SMartin Matuska 	}
89716fd348SMartin Matuska 	pid = fork();
90716fd348SMartin Matuska 	if (pid > 0) {
91716fd348SMartin Matuska 		int fd = open(dirpath, O_RDONLY|O_RSYNC|O_SYNC|O_DSYNC);
92716fd348SMartin Matuska 		char buf[16];
93716fd348SMartin Matuska 		int rdret;
94716fd348SMartin Matuska 		int j = 0;
95716fd348SMartin Matuska 
96716fd348SMartin Matuska 		if (fd < 0) {
97716fd348SMartin Matuska 			(void) printf("%s: open <%s> again failed:"
98716fd348SMartin Matuska 			    " errno = %d\n", argv[0], dirpath, errno);
99716fd348SMartin Matuska 			exit(-1);
100716fd348SMartin Matuska 		}
101716fd348SMartin Matuska 
102716fd348SMartin Matuska 		while (j < op_num) {
103716fd348SMartin Matuska 			(void) sleep(1);
104716fd348SMartin Matuska 			rdret = read(fd, buf, 16);
105716fd348SMartin Matuska 			if (rdret == -1) {
106716fd348SMartin Matuska 				(void) printf("readdir failed");
107716fd348SMartin Matuska 			}
108716fd348SMartin Matuska 			j++;
109716fd348SMartin Matuska 		}
110716fd348SMartin Matuska 		(void) close(fd);
111716fd348SMartin Matuska 	} else if (pid == 0) {
112716fd348SMartin Matuska 		int fd = open(dirpath, O_RDONLY);
113716fd348SMartin Matuska 		int chownret;
114716fd348SMartin Matuska 		int k = 0;
115716fd348SMartin Matuska 
116716fd348SMartin Matuska 		if (fd < 0) {
117716fd348SMartin Matuska 			(void) printf("%s: open(<%s>, O_RDONLY) again failed:"
118716fd348SMartin Matuska 			    " errno (decimal)=%d\n", argv[0], dirpath, errno);
119716fd348SMartin Matuska 			exit(-1);
120716fd348SMartin Matuska 		}
121716fd348SMartin Matuska 
122716fd348SMartin Matuska 		while (k < op_num) {
123716fd348SMartin Matuska 			(void) sleep(1);
124716fd348SMartin Matuska 			chownret = fchown(fd, 0, 0);
125716fd348SMartin Matuska 			if (chownret == -1) {
126716fd348SMartin Matuska 				(void) printf("chown failed");
127716fd348SMartin Matuska 			}
128716fd348SMartin Matuska 
129716fd348SMartin Matuska 			k++;
130716fd348SMartin Matuska 		}
131716fd348SMartin Matuska 		(void) close(fd);
132716fd348SMartin Matuska 	}
133716fd348SMartin Matuska 
134716fd348SMartin Matuska 	return (0);
135716fd348SMartin Matuska }
136