xref: /freebsd/tests/sys/cddl/zfs/bin/dir_rd_update.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 #pragma ident	"@(#)dir_rd_update.c	1.2	07/01/09 SMI"
28 
29 /*
30  * Assertion:
31  *
32  *	A read operation and directory update operation performed
33  *      concurrently on the same directory can lead to deadlock
34  *	on a UFS logging file system, but not on a ZFS file system.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #define	TMP_DIR /tmp
46 
47 static char dirpath[256];
48 
49 int
50 main(int argc, char **argv)
51 {
52 	char *cp1 = "";
53 	int i = 0;
54 	int ret = 0;
55 	int testdd = 0;
56 	pid_t pid;
57 	static const int op_num = 5;
58 
59 	if (argc == 1) {
60 		(void) printf("Usage: %s <mount point>\n", argv[0]);
61 		exit(-1);
62 	}
63 	for (i = 0; i < 256; i++) {
64 		dirpath[i] = 0;
65 	}
66 
67 	cp1 = argv[1];
68 	(void) strcpy(&dirpath[0], (const char *)cp1);
69 	(void) strcat(&dirpath[strlen(dirpath)], "TMP_DIR");
70 
71 	ret = mkdir(dirpath, 0777);
72 	if (ret != 0) {
73 		if (errno != EEXIST) {
74 			(void) printf(
75 			"%s: mkdir(<%s>, 0777) failed: errno (decimal)=%d\n",
76 				argv[0], dirpath, errno);
77 			exit(-1);
78 		}
79 	}
80 	testdd = open(dirpath, O_RDONLY|O_SYNC);
81 	if (testdd < 0) {
82 		(void) printf(
83 "%s: open(<%s>, O_RDONLY|O_SYNC) failed: errno (decimal)=%d\n",
84 			argv[0], dirpath, errno);
85 		exit(-1);
86 	} else {
87 		(void) close(testdd);
88 	}
89 	pid = fork();
90 	if (pid > 0) {
91 		int fd = open(dirpath, O_RDONLY|O_SYNC);
92 		char buf[16];
93 		int rdret;
94 		int j = 0;
95 
96 		while (j < op_num) {
97 			(void) sleep(1);
98 			rdret = read(fd, buf, 16);
99 			if (rdret == -1) {
100 				(void) printf("readdir failed");
101 			}
102 			j++;
103 		}
104 	} else if (pid == 0) {
105 		int fd = open(dirpath, O_RDONLY);
106 		int chownret;
107 		int k = 0;
108 
109 		while (k < op_num) {
110 			(void) sleep(1);
111 			chownret = fchown(fd, 0, 0);
112 			if (chownret == -1) {
113 				(void) printf("chown failed");
114 			}
115 
116 			k++;
117 		}
118 	}
119 
120 	return (0);
121 }
122