xref: /freebsd/tests/sys/cddl/zfs/bin/dir_rd_update.c (revision c8e7f78a3d28ff6e6223ed136ada8e1e2f34965e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
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 	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 	(void) strcpy(&dirpath[0], (const char *)cp1);
68 	(void) strcat(&dirpath[strlen(dirpath)], "TMP_DIR");
69 
70 	ret = mkdir(dirpath, 0777);
71 	if (ret != 0) {
72 		if (errno != EEXIST) {
73 			(void) printf(
74 			"%s: mkdir(<%s>, 0777) failed: errno (decimal)=%d\n",
75 				argv[0], dirpath, errno);
76 			exit(-1);
77 		}
78 	}
79 	testdd = open(dirpath, O_RDONLY|O_SYNC);
80 	if (testdd < 0) {
81 		(void) printf(
82 "%s: open(<%s>, O_RDONLY|O_SYNC) failed: errno (decimal)=%d\n",
83 			argv[0], dirpath, errno);
84 		exit(-1);
85 	} else {
86 		(void) close(testdd);
87 	}
88 	pid = fork();
89 	if (pid > 0) {
90 		int fd = open(dirpath, O_RDONLY|O_SYNC);
91 		char buf[16];
92 		int rdret;
93 		int j = 0;
94 
95 		while (j < op_num) {
96 			(void) sleep(1);
97 			rdret = read(fd, buf, 16);
98 			if (rdret == -1) {
99 				(void) printf("readdir failed");
100 			}
101 			j++;
102 		}
103 	} else if (pid == 0) {
104 		int fd = open(dirpath, O_RDONLY);
105 		int chownret;
106 		int k = 0;
107 
108 		while (k < op_num) {
109 			(void) sleep(1);
110 			chownret = fchown(fd, 0, 0);
111 			if (chownret == -1) {
112 				(void) printf("chown failed");
113 			}
114 
115 			k++;
116 		}
117 	}
118 
119 	return (0);
120 }
121