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