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 
42 int
43 setup(int nb)
44 {
45 	int pct;
46 	int64_t in;
47 	int64_t bl;
48 	int64_t reserve_in;
49 	int64_t reserve_bl;
50 
51 	if (nb == 0) {
52 		getdf(&bl, &in);
53 		size = in / op->incarnations;
54 
55 		pct = 90;
56 		if (op->hog == 0)
57 			pct = random_int(1, 90);
58 		size = size / 100 * pct + 1;
59 
60 		size = size % 10000;	/* arbitrary limit depth */
61 		size = size % 200;	/* arbitrary limit depth */ /* XXX Soft Update */
62 
63 		/* Resource requirements: */
64 		while (size > 0) {
65 			reserve_in =    1 * size * op->incarnations + 1;
66 			reserve_bl = 2048 * size * op->incarnations;
67 //			printf("---size = %lu, reserve(%jd, %jd)\n", size, reserve_bl/1024, reserve_in);
68 			if (reserve_bl <= bl && reserve_in <= in)
69 				break;
70 			size--;
71 		}
72 		if (size == 0)
73 			reserve_bl = reserve_in = 0;
74 
75 		if (op->verbose > 1)
76 			printf("mkdir(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
77 				size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
78 		reservedf(reserve_bl, reserve_in);
79 		putval(size);
80 	} else
81 		size = getval();
82 
83 	return (0);
84 }
85 
86 void
87 cleanup(void)
88 {
89 }
90 
91 void
92 mkDir(char *path, int level) {
93 	char newPath[MAXPATHLEN + 1];
94 
95 	if (mkdir(path, 0770) == -1) {
96 		warn("mkdir(%s), %s:%d", path, __FILE__, __LINE__);
97 		size = level;
98 	} else
99 		chdir(path);
100 
101 	if (done_testing == 1)
102 		size = level;
103 
104 	if (level < size) {
105 		sprintf(newPath,"d%d", level+1);
106 		mkDir(newPath, level+1);
107 	}
108 }
109 
110 void
111 rmDir(char *path, int level) {
112 	char newPath[MAXPATHLEN + 1];
113 
114 	if (level < 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
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