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