xref: /freebsd/tools/test/stress2/testcases/dirnprename/dirnprename.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 
38 #include "stress.h"
39 
40 static char path[128];
41 static unsigned long size;
42 
43 int
44 setup(int nb)
45 {
46 	char file1[512];
47 	int64_t in;
48 	int64_t bl;
49 	int64_t reserve_in;
50 	int64_t reserve_bl;
51 	int i;
52 
53 	umask(0);
54 
55 	if (nb == 0) {
56 		getdf(&bl, &in);
57 		size = in / op->incarnations;
58 
59 		if (size > 1000)
60 			size = 1000;	/* arbitrary limit number of files pr. dir */
61 
62 		/* Resource requirements: */
63 		while (size > 0) {
64 			reserve_in =  2 * size * op->incarnations + 2 * op->incarnations;
65 			reserve_bl = 30 * size * op->incarnations;
66 			if (reserve_bl <= bl && reserve_in <= in)
67 				break;
68 			size = size / 2;
69 		}
70 		if (size == 0)
71 			reserve_bl = reserve_in = 0;
72 
73 		if (op->verbose > 1)
74 			printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
75 				size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
76 		reservedf(reserve_bl, reserve_in);
77 		putval(size);
78 	} else {
79 		size = getval();
80 	}
81 	if (size == 0)
82 		exit(0);
83 
84 	sprintf(path, "%s.%05d", getprogname(), getpid());
85 	if (mkdir(path, 0770) == -1)
86 		err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
87 
88 	/*
89 	 * The value 97 was determined by figuring out how many 255 length
90 	 * names would cause us to overflow into indirect blocks w/ the
91 	 * default UFS parameters.
92 	 */
93 	for (i = 0; i < 97; i++) {
94 		sprintf(file1, "%s/%0255d", path, i);
95 		if (mkdir(file1, 0770) == -1)
96 			err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);
97 	}
98 	return (0);
99 }
100 
101 void
102 cleanup(void)
103 {
104 	char file1[512];
105 	int i;
106 
107 	/* see comment above */
108 	for (i = 0; i < 97; i++) {
109 		sprintf(file1, "%s/%0255d", path, i);
110 		if (rmdir(file1) == -1)
111 			warn("rmdir(%s), %s:%d", file1, __FILE__, __LINE__);
112 	}
113 	if (rmdir(path) == -1)
114 		warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
115 }
116 
117 static void
118 test_rename(void)
119 {
120 	int i, j;
121 	int errnotmp;
122 	pid_t pid;
123 	char file1[128];
124 	char file2[128];
125 
126 	pid = getpid();
127 	for (i = 0; i < (int)size; i++) {
128 		sprintf(file1,"p%05d.%05d", pid, i);
129 		if (mkdir(file1, 0660) == -1) {
130 			j = i;
131 			errnotmp = errno;
132 			while (j > 0) {
133 				j--;
134 				sprintf(file1,"p%05d.%05d", pid, j);
135 				rmdir(file1);
136 			}
137 			errno = errnotmp;
138 			sprintf(file1,"p%05d.%05d", pid, i);
139 			err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);
140 		}
141 	}
142 	for (j = 0; j < 100 && done_testing == 0; j++) {
143 		for (i = 0; i < (int)size; i++) {
144 			sprintf(file1,"p%05d.%05d", pid, i);
145 			sprintf(file2,"%s/p%05d.%05d.togo", path, pid, i);
146 			if (rename(file1, file2) == -1)
147 				err(1, "rename(%s, %s). %s:%d", file1, file2,
148 						__FILE__, __LINE__);
149 		}
150 		for (i = 0; i < (int)size; i++) {
151 			sprintf(file1,"p%05d.%05d", pid, i);
152 			sprintf(file2,"%s/p%05d.%05d.togo", path, pid, i);
153 			if (rename(file2, file1) == -1)
154 				err(1, "rename(%s, %s). %s:%d", file2, file1,
155 						__FILE__, __LINE__);
156 		}
157 	}
158 
159 	for (i = 0; i < (int)size; i++) {
160 		sprintf(file1,"p%05d.%05d", pid, i);
161 		if (rmdir(file1) == -1)
162 			err(1, "rmdir(%s), %s:%d", file1, __FILE__, __LINE__);
163 	}
164 }
165 
166 int
167 test(void)
168 {
169 	test_rename();
170 
171 	return (0);
172 }
173