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