xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/dir_rd_update.c (revision fa38579f317d5c2ff2926fab9b12ee6d429bd155)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*
29  * Assertion:
30  *
31  *	A read operation and directory update operation performed
32  *      concurrently on the same directory can lead to deadlock
33  *	on a UFS logging file system, but not on a ZFS file system.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #define	TMP_DIR /tmp
45 
46 static char dirpath[256];
47 
48 int
49 main(int argc, char **argv)
50 {
51 	const char *cp1 = "";
52 	int i = 0;
53 	int ret = 0;
54 	int testdd = 0;
55 	pid_t pid;
56 	static const int op_num = 5;
57 
58 	if (argc == 1) {
59 		(void) printf("Usage: %s <mount point>\n", argv[0]);
60 		exit(-1);
61 	}
62 	for (i = 0; i < 256; i++) {
63 		dirpath[i] = 0;
64 	}
65 
66 	cp1 = argv[1];
67 	if (strlen(cp1) >= (sizeof (dirpath) - strlen("/TMP_DIR"))) {
68 		(void) printf("The string length of mount point is "
69 		    "too large\n");
70 		exit(-1);
71 	}
72 	(void) snprintf(dirpath, sizeof (dirpath), "%s/TMP_DIR", cp1);
73 
74 	ret = mkdir(dirpath, 0777);
75 	if (ret != 0) {
76 		if (errno != EEXIST) {
77 			(void) printf("%s: mkdir(<%s>, 0777) failed: errno "
78 			    "(decimal)=%d\n", argv[0], dirpath, errno);
79 			exit(-1);
80 		}
81 	}
82 	testdd = open(dirpath, O_RDONLY|O_RSYNC|O_SYNC|O_DSYNC);
83 	if (testdd < 0) {
84 		(void) printf("%s: open(<%s>, O_RDONLY|O_RSYNC|O_SYNC|O_DSYNC)"
85 		    " failed: errno (decimal)=%d\n", argv[0], dirpath, errno);
86 		exit(-1);
87 	} else {
88 		(void) close(testdd);
89 	}
90 	pid = fork();
91 	if (pid > 0) {
92 		int fd = open(dirpath, O_RDONLY|O_RSYNC|O_SYNC|O_DSYNC);
93 		char buf[16];
94 		int rdret;
95 		int j = 0;
96 
97 		if (fd < 0) {
98 			(void) printf("%s: open <%s> again failed:"
99 			    " errno = %d\n", argv[0], dirpath, errno);
100 			exit(-1);
101 		}
102 
103 		while (j < op_num) {
104 			(void) sleep(1);
105 			rdret = read(fd, buf, 16);
106 			if (rdret == -1) {
107 				(void) printf("readdir failed");
108 			}
109 			j++;
110 		}
111 		(void) close(fd);
112 	} else if (pid == 0) {
113 		int fd = open(dirpath, O_RDONLY);
114 		int chownret;
115 		int k = 0;
116 
117 		if (fd < 0) {
118 			(void) printf("%s: open(<%s>, O_RDONLY) again failed:"
119 			    " errno (decimal)=%d\n", argv[0], dirpath, errno);
120 			exit(-1);
121 		}
122 
123 		while (k < op_num) {
124 			(void) sleep(1);
125 			chownret = fchown(fd, 0, 0);
126 			if (chownret == -1) {
127 				(void) printf("chown failed");
128 			}
129 
130 			k++;
131 		}
132 		(void) close(fd);
133 	}
134 
135 	return (0);
136 }
137