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