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 <err.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include "stress.h"
39
40 static unsigned long size;
41
42 int
setup(int nb)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 > 1000)
57 size = 1000; /* arbitrary limit number of files pr. dir */
58
59 /* Resource requirements: */
60 while (size > 0) {
61 reserve_in = 2 * size * op->incarnations + 2 * op->incarnations;
62 reserve_bl = 30 * size * op->incarnations;
63 if (reserve_bl <= bl && reserve_in <= in)
64 break;
65 size = size / 2;
66 }
67 if (size == 0)
68 reserve_bl = reserve_in = 0;
69
70 if (op->verbose > 1)
71 printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
72 size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
73 reservedf(reserve_bl, reserve_in);
74 putval(size);
75 } else {
76 size = getval();
77 }
78 if (size == 0)
79 exit(0);
80
81 return (0);
82 }
83
84 void
cleanup(void)85 cleanup(void)
86 {
87 }
88
89 static void
test_rename(void)90 test_rename(void)
91 {
92 int i, j;
93 int errnotmp;
94 pid_t pid;
95 char file1[128];
96 char file2[128];
97
98 pid = getpid();
99 for (i = 0; i < (int)size; i++) {
100 sprintf(file1,"p%05d.%05d", pid, i);
101 if (mkdir(file1, 0660) == -1) {
102 j = i;
103 errnotmp = errno;
104 while (j > 0) {
105 j--;
106 sprintf(file1,"p%05d.%05d", pid, j);
107 rmdir(file1);
108 }
109 errno = errnotmp;
110 sprintf(file1,"p%05d.%05d", pid, i);
111 err(1, "mkdir(%s), %s:%d", file1, __FILE__, __LINE__);
112 }
113 }
114 for (j = 0; j < 100 && done_testing == 0; j++) {
115 for (i = 0; i < (int)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 < (int)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 < (int)size; i++) {
132 sprintf(file1,"p%05d.%05d", pid, i);
133 if (rmdir(file1) == -1)
134 err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);
135 }
136 }
137
138 int
test(void)139 test(void)
140 {
141 test_rename();
142
143 return (0);
144 }
145