xref: /freebsd/tests/sys/cddl/zfs/bin/rm_lnkcnt_zero_file.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  * --------------------------------------------------------------------
30*2fae26bdSAlan Somers  * The purpose of this test is to see if the bug reported (#4723351) for
31*2fae26bdSAlan Somers  * UFS exists when using a ZFS file system.
32*2fae26bdSAlan Somers  * --------------------------------------------------------------------
33*2fae26bdSAlan Somers  *
34*2fae26bdSAlan Somers  */
35*2fae26bdSAlan Somers #define	_REENTRANT 1
36*2fae26bdSAlan Somers #include <stdio.h>
37*2fae26bdSAlan Somers #include <fcntl.h>
38*2fae26bdSAlan Somers #include <pthread.h>
39*2fae26bdSAlan Somers #include <errno.h>
40*2fae26bdSAlan Somers #include <sys/types.h>
41*2fae26bdSAlan Somers #include <sys/stat.h>
42*2fae26bdSAlan Somers #include <stdlib.h>
43*2fae26bdSAlan Somers #include <unistd.h>
44*2fae26bdSAlan Somers 
45*2fae26bdSAlan Somers static const int TRUE = 1;
46*2fae26bdSAlan Somers static char *filebase;
47*2fae26bdSAlan Somers 
48*2fae26bdSAlan Somers static int
pickidx()49*2fae26bdSAlan Somers pickidx()
50*2fae26bdSAlan Somers {
51*2fae26bdSAlan Somers 	return (random() % 1000);
52*2fae26bdSAlan Somers }
53*2fae26bdSAlan Somers 
54*2fae26bdSAlan Somers /* ARGSUSED */
55*2fae26bdSAlan Somers static void *
mover(void * a)56*2fae26bdSAlan Somers mover(void *a)
57*2fae26bdSAlan Somers {
58*2fae26bdSAlan Somers 	char buf[256];
59*2fae26bdSAlan Somers 	int idx, ret;
60*2fae26bdSAlan Somers 
61*2fae26bdSAlan Somers 	while (TRUE) {
62*2fae26bdSAlan Somers 		idx = pickidx();
63*2fae26bdSAlan Somers 		(void) sprintf(buf, "%s.%03d", filebase, idx);
64*2fae26bdSAlan Somers 		ret = rename(filebase, buf);
65*2fae26bdSAlan Somers 		if (ret < 0 && errno != ENOENT)
66*2fae26bdSAlan Somers 			(void) perror("renaming file");
67*2fae26bdSAlan Somers 	}
68*2fae26bdSAlan Somers 
69*2fae26bdSAlan Somers 	return (NULL);
70*2fae26bdSAlan Somers }
71*2fae26bdSAlan Somers 
72*2fae26bdSAlan Somers /* ARGSUSED */
73*2fae26bdSAlan Somers static void *
cleaner(void * a)74*2fae26bdSAlan Somers cleaner(void *a)
75*2fae26bdSAlan Somers {
76*2fae26bdSAlan Somers 	char buf[256];
77*2fae26bdSAlan Somers 	int idx, ret;
78*2fae26bdSAlan Somers 
79*2fae26bdSAlan Somers 	while (TRUE) {
80*2fae26bdSAlan Somers 		idx = pickidx();
81*2fae26bdSAlan Somers 		(void) sprintf(buf, "%s.%03d", filebase, idx);
82*2fae26bdSAlan Somers 		ret = remove(buf);
83*2fae26bdSAlan Somers 		if (ret < 0 && errno != ENOENT)
84*2fae26bdSAlan Somers 			(void) perror("removing file");
85*2fae26bdSAlan Somers 	}
86*2fae26bdSAlan Somers 
87*2fae26bdSAlan Somers 	return (NULL);
88*2fae26bdSAlan Somers }
89*2fae26bdSAlan Somers 
90*2fae26bdSAlan Somers static void *
writer(void * a)91*2fae26bdSAlan Somers writer(void *a)
92*2fae26bdSAlan Somers {
93*2fae26bdSAlan Somers 	int *fd = (int *)a;
94*2fae26bdSAlan Somers 
95*2fae26bdSAlan Somers 	while (TRUE) {
96*2fae26bdSAlan Somers 		(void) close (*fd);
97*2fae26bdSAlan Somers 		*fd = open(filebase, O_APPEND | O_RDWR | O_CREAT, 0644);
98*2fae26bdSAlan Somers 		if (*fd < 0)
99*2fae26bdSAlan Somers 			perror("refreshing file");
100*2fae26bdSAlan Somers 		(void) write(*fd, "test\n", 5);
101*2fae26bdSAlan Somers 	}
102*2fae26bdSAlan Somers 
103*2fae26bdSAlan Somers 	return (NULL);
104*2fae26bdSAlan Somers }
105*2fae26bdSAlan Somers 
106*2fae26bdSAlan Somers int
main(int argc,char ** argv)107*2fae26bdSAlan Somers main(int argc, char **argv)
108*2fae26bdSAlan Somers {
109*2fae26bdSAlan Somers 	int fd;
110*2fae26bdSAlan Somers 	pthread_t tid;
111*2fae26bdSAlan Somers 
112*2fae26bdSAlan Somers 	if (argc == 1) {
113*2fae26bdSAlan Somers 		(void) printf("Usage: %s <filebase>\n", argv[0]);
114*2fae26bdSAlan Somers 		exit(-1);
115*2fae26bdSAlan Somers 	}
116*2fae26bdSAlan Somers 
117*2fae26bdSAlan Somers 	filebase = argv[1];
118*2fae26bdSAlan Somers 	fd = open(filebase, O_APPEND | O_RDWR | O_CREAT, 0644);
119*2fae26bdSAlan Somers 	if (fd < 0) {
120*2fae26bdSAlan Somers 		perror("creating test file");
121*2fae26bdSAlan Somers 		exit(-1);
122*2fae26bdSAlan Somers 	}
123*2fae26bdSAlan Somers 
124*2fae26bdSAlan Somers 	if (pthread_setconcurrency(4)) {	/* 3 threads + main */
125*2fae26bdSAlan Somers 		fprintf(stderr, "failed to set concurrency\n");
126*2fae26bdSAlan Somers 		exit(-1);
127*2fae26bdSAlan Somers 	}
128*2fae26bdSAlan Somers 	(void) pthread_create(&tid, NULL, mover, NULL);
129*2fae26bdSAlan Somers 	(void) pthread_create(&tid, NULL, cleaner, NULL);
130*2fae26bdSAlan Somers 	(void) pthread_create(&tid, NULL, writer, (void *) &fd);
131*2fae26bdSAlan Somers 
132*2fae26bdSAlan Somers 	while (TRUE) {
133*2fae26bdSAlan Somers 		int ret;
134*2fae26bdSAlan Somers 		struct stat st;
135*2fae26bdSAlan Somers 
136*2fae26bdSAlan Somers 		ret = stat(filebase, &st);
137*2fae26bdSAlan Somers 		if (ret == 0 && (st.st_nlink > 2 || st.st_nlink < 1)) {
138*2fae26bdSAlan Somers 			(void) printf("st.st_nlink = %d, exiting\n", \
139*2fae26bdSAlan Somers 			    (int)st.st_nlink);
140*2fae26bdSAlan Somers 			exit(0);
141*2fae26bdSAlan Somers 		}
142*2fae26bdSAlan Somers 		(void) sleep(1);
143*2fae26bdSAlan Somers 	}
144*2fae26bdSAlan Somers 
145*2fae26bdSAlan Somers 	return (0);
146*2fae26bdSAlan Somers }
147