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/types.h>
29 #include <sys/sysctl.h>
30 #include <unistd.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/stat.h>
34 #include <sys/param.h>
35 #include <err.h>
36
37 #include "stress.h"
38
39 static unsigned long size;
40
41 int
setup(int nb)42 setup(int nb)
43 {
44 int64_t in;
45 int64_t bl;
46 int64_t reserve_in;
47 int64_t reserve_bl;
48 int pct;
49
50 if (nb == 0) {
51 getdf(&bl, &in);
52 size = in / op->incarnations;
53
54 pct = 90;
55 if (op->hog == 0)
56 pct = random_int(1, 90);
57 size = size / 100 * pct + 1;
58
59 size = size % 200; /* arbitrary limit depth */
60
61 /* Resource requirements: */
62 while (size > 0) {
63 reserve_in = 1 * size * op->incarnations;
64 reserve_bl = 4096 * size * op->incarnations;
65 if (reserve_bl <= bl && reserve_in <= in)
66 break;
67 size = size / 2;
68 }
69 if (size == 0)
70 reserve_bl = reserve_in = 0;
71
72 if (op->verbose > 1)
73 printf("mkdir(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
74 size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
75 reservedf(reserve_bl, reserve_in);
76 putval(size);
77 } else
78 size = getval();
79
80 if (size == 0)
81 exit(0);
82
83 return (0);
84 }
85
86 void
cleanup(void)87 cleanup(void)
88 {
89 }
90
91 static void
mkDir(char * path,int level)92 mkDir(char *path, int level) {
93 char newPath[MAXPATHLEN + 1];
94
95 if (mkdir(path, 0770) == -1) {
96 warn("mkdir(%s), level %d. %s:%d", path, level, __FILE__, __LINE__);
97 size = level;
98 } else
99 chdir(path);
100
101 if (done_testing == 1)
102 size = level;
103
104 if (level < (int)size) {
105 sprintf(newPath,"d%d", level+1);
106 mkDir(newPath, level+1);
107 }
108 }
109
110 static void
rmDir(char * path,int level)111 rmDir(char *path, int level) {
112 char newPath[MAXPATHLEN + 1];
113
114 if (level < (int)size) {
115 sprintf(newPath,"d%d", level+1);
116 rmDir(newPath, level+1);
117 }
118 chdir ("..");
119 if (rmdir(path) == -1) {
120 err(1, "rmdir(%s), %s:%d", path, __FILE__, __LINE__);
121 }
122 }
123
124 int
test(void)125 test(void)
126 {
127 char path[MAXPATHLEN + 1];
128
129 umask(0);
130 sprintf(path,"p%05d.d%d", getpid(), 1);
131 mkDir(path, 1);
132 rmDir(path, 1);
133
134 return (0);
135 }
136