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