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