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