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 <unistd.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <err.h>
36 
37 #include "stress.h"
38 
39 static char path[128];
40 static unsigned long size;
41 
42 int
43 setup(int nb)
44 {
45 	int64_t in;
46 	int64_t bl;
47 	int64_t reserve_in;
48 	int64_t reserve_bl;
49 
50 	umask(0);
51 
52 	if (nb == 0) {
53 		getdf(&bl, &in);
54 		size = in / op->incarnations;
55 
56 		if (size > 100)
57 			size = 100;	/* arbitrary limit number of files pr. dir */
58 
59 		/* Resource requirements: */
60 		while (size > 0) {
61 			reserve_in =  1 * size * op->incarnations + 2 * op->incarnations;
62 			reserve_bl = 100 * size * op->incarnations;
63 //			printf("size = %lu, reserve(%jd, %jd)\n", size, reserve_bl/1024, reserve_in);
64 			if (reserve_bl <= bl && reserve_in <= in)
65 				break;
66 			size--;
67 		}
68 		if (size == 0)
69 			reserve_bl = reserve_in = 0;
70 
71 		if (op->verbose > 1)
72 			printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
73 				size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
74 		reservedf(reserve_bl, reserve_in);
75 		putval(size);
76 	} else {
77 		size = getval();
78 	}
79 
80 	sprintf(path,"%s.%05d", getprogname(), getpid());
81 	if (mkdir(path, 0770) < 0)
82 		err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
83 	if (chdir(path) == -1)
84 		err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
85 
86 	return (0);
87 }
88 
89 void
90 cleanup(void)
91 {
92 	(void)system("rm -f p*");
93 	(void)chdir("..");
94 	if (rmdir(path) == -1)
95 		warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
96 }
97 
98 static void
99 test_rename(void)
100 {
101 	int i, j;
102 	pid_t pid;
103 	char file1[128];
104 	char file2[128];
105 	int tfd;
106 
107 	pid = getpid();
108 	for (i = 0; i < size; i++) {
109 		sprintf(file1,"p%05d.%05d", pid, i);
110 		if ((tfd = open(file1, O_RDONLY|O_CREAT, 0660)) == -1)
111 			err(1, "openat(%s), %s:%d", file1, __FILE__, __LINE__);
112 		close(tfd);
113 	}
114 	for (j = 0; j < 100; j++) {
115 		for (i = 0; i < size; i++) {
116 			sprintf(file1,"p%05d.%05d", pid, i);
117 			sprintf(file2,"p%05d.%05d.togo", pid, i);
118 			if (rename(file1, file2) == -1)
119 				err(1, "rename(%s, %s). %s:%d", file1, file2,
120 						__FILE__, __LINE__);
121 		}
122 		for (i = 0; i < size; i++) {
123 			sprintf(file1,"p%05d.%05d", pid, i);
124 			sprintf(file2,"p%05d.%05d.togo", pid, i);
125 			if (rename(file2, file1) == -1)
126 				err(1, "rename(%s, %s). %s:%d", file2, file1,
127 						__FILE__, __LINE__);
128 		}
129 	}
130 
131 	for (i = 0; i < size; i++) {
132 		sprintf(file1,"p%05d.%05d", pid, i);
133 		if (unlink(file1) == -1)
134 			err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);
135 	}
136 }
137 
138 int
139 test(void)
140 {
141 	test_rename();
142 
143 	return (0);
144 }
145