1*86d7f5d3SJohn Marino /*-
2*86d7f5d3SJohn Marino * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3*86d7f5d3SJohn Marino * All rights reserved.
4*86d7f5d3SJohn Marino *
5*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
6*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
7*86d7f5d3SJohn Marino * are met:
8*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
9*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
10*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
11*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
12*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
13*86d7f5d3SJohn Marino *
14*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*86d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*86d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*86d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*86d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*86d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*86d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*86d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*86d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*86d7f5d3SJohn Marino * SUCH DAMAGE.
25*86d7f5d3SJohn Marino *
26*86d7f5d3SJohn Marino */
27*86d7f5d3SJohn Marino
28*86d7f5d3SJohn Marino #include <unistd.h>
29*86d7f5d3SJohn Marino #include <stdio.h>
30*86d7f5d3SJohn Marino #include <stdlib.h>
31*86d7f5d3SJohn Marino #include <fcntl.h>
32*86d7f5d3SJohn Marino #include <string.h>
33*86d7f5d3SJohn Marino #include <sys/stat.h>
34*86d7f5d3SJohn Marino #include <sys/param.h>
35*86d7f5d3SJohn Marino #include <sys/syslimits.h>
36*86d7f5d3SJohn Marino #include <err.h>
37*86d7f5d3SJohn Marino #include <errno.h>
38*86d7f5d3SJohn Marino
39*86d7f5d3SJohn Marino #include "stress.h"
40*86d7f5d3SJohn Marino
41*86d7f5d3SJohn Marino static char path[128];
42*86d7f5d3SJohn Marino static unsigned long size;
43*86d7f5d3SJohn Marino
44*86d7f5d3SJohn Marino int
setup(int nb)45*86d7f5d3SJohn Marino setup(int nb)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino int pct;
48*86d7f5d3SJohn Marino int64_t in;
49*86d7f5d3SJohn Marino int64_t bl;
50*86d7f5d3SJohn Marino int64_t reserve_in;
51*86d7f5d3SJohn Marino int64_t reserve_bl;
52*86d7f5d3SJohn Marino
53*86d7f5d3SJohn Marino umask(0);
54*86d7f5d3SJohn Marino path[0] = 0;
55*86d7f5d3SJohn Marino if (nb == 0) {
56*86d7f5d3SJohn Marino getdf(&bl, &in);
57*86d7f5d3SJohn Marino size = in / op->incarnations;
58*86d7f5d3SJohn Marino
59*86d7f5d3SJohn Marino pct = 90;
60*86d7f5d3SJohn Marino if (op->hog == 0)
61*86d7f5d3SJohn Marino pct = random_int(1, 90);
62*86d7f5d3SJohn Marino size = size / 100 * pct + 1;
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino if (size > 20000 && op->hog == 0)
65*86d7f5d3SJohn Marino size = 20000; /* arbitrary limit number of files pr. dir */
66*86d7f5d3SJohn Marino if (size > LINK_MAX)
67*86d7f5d3SJohn Marino size = LINK_MAX;
68*86d7f5d3SJohn Marino
69*86d7f5d3SJohn Marino
70*86d7f5d3SJohn Marino /* Resource requirements: */
71*86d7f5d3SJohn Marino reserve_in = 2 * op->incarnations + 1;
72*86d7f5d3SJohn Marino reserve_bl = 26 * size * op->incarnations;
73*86d7f5d3SJohn Marino if (reserve_bl > bl)
74*86d7f5d3SJohn Marino size = bl / 26 * op->incarnations;
75*86d7f5d3SJohn Marino if (reserve_in > in)
76*86d7f5d3SJohn Marino size = reserve_in = reserve_bl = 0;
77*86d7f5d3SJohn Marino
78*86d7f5d3SJohn Marino if (op->verbose > 1)
79*86d7f5d3SJohn Marino printf("link(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
80*86d7f5d3SJohn Marino size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
81*86d7f5d3SJohn Marino reservedf(reserve_bl, reserve_in);
82*86d7f5d3SJohn Marino putval(size);
83*86d7f5d3SJohn Marino } else {
84*86d7f5d3SJohn Marino size = getval();
85*86d7f5d3SJohn Marino }
86*86d7f5d3SJohn Marino if (size == 0)
87*86d7f5d3SJohn Marino exit (1);
88*86d7f5d3SJohn Marino
89*86d7f5d3SJohn Marino sprintf(path,"%s.%05d", getprogname(), getpid());
90*86d7f5d3SJohn Marino if (mkdir(path, 0770) < 0)
91*86d7f5d3SJohn Marino err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
92*86d7f5d3SJohn Marino
93*86d7f5d3SJohn Marino if (chdir(path) == -1)
94*86d7f5d3SJohn Marino err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
95*86d7f5d3SJohn Marino
96*86d7f5d3SJohn Marino return (0);
97*86d7f5d3SJohn Marino }
98*86d7f5d3SJohn Marino
99*86d7f5d3SJohn Marino void
cleanup(void)100*86d7f5d3SJohn Marino cleanup(void)
101*86d7f5d3SJohn Marino {
102*86d7f5d3SJohn Marino (void)chdir("..");
103*86d7f5d3SJohn Marino if (path[0] != 0 && rmdir(path) == -1)
104*86d7f5d3SJohn Marino warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
105*86d7f5d3SJohn Marino }
106*86d7f5d3SJohn Marino
107*86d7f5d3SJohn Marino int
test(void)108*86d7f5d3SJohn Marino test(void)
109*86d7f5d3SJohn Marino {
110*86d7f5d3SJohn Marino int fd, i, j;
111*86d7f5d3SJohn Marino pid_t pid;
112*86d7f5d3SJohn Marino char file[128];
113*86d7f5d3SJohn Marino char lfile[128];
114*86d7f5d3SJohn Marino
115*86d7f5d3SJohn Marino pid = getpid();
116*86d7f5d3SJohn Marino for (j = 0; j < size && done_testing == 0; j++) {
117*86d7f5d3SJohn Marino sprintf(file,"p%05d.%05d", pid, j);
118*86d7f5d3SJohn Marino if (j == 0) {
119*86d7f5d3SJohn Marino if ((fd = creat(file, 0660)) == -1) {
120*86d7f5d3SJohn Marino if (errno != EINTR) {
121*86d7f5d3SJohn Marino warn("creat(%s)", file);
122*86d7f5d3SJohn Marino break;
123*86d7f5d3SJohn Marino }
124*86d7f5d3SJohn Marino }
125*86d7f5d3SJohn Marino if (fd != -1 && close(fd) == -1)
126*86d7f5d3SJohn Marino err(2, "close(%d)", j);
127*86d7f5d3SJohn Marino strcpy(lfile, file);
128*86d7f5d3SJohn Marino } else {
129*86d7f5d3SJohn Marino if (link(lfile, file) == -1) {
130*86d7f5d3SJohn Marino if (errno != EINTR) {
131*86d7f5d3SJohn Marino warn("link(%s, %s)", lfile, file);
132*86d7f5d3SJohn Marino break;
133*86d7f5d3SJohn Marino }
134*86d7f5d3SJohn Marino }
135*86d7f5d3SJohn Marino }
136*86d7f5d3SJohn Marino
137*86d7f5d3SJohn Marino }
138*86d7f5d3SJohn Marino
139*86d7f5d3SJohn Marino for (i = --j; i >= 0; i--) {
140*86d7f5d3SJohn Marino sprintf(file,"p%05d.%05d", pid, i);
141*86d7f5d3SJohn Marino if (unlink(file) == -1)
142*86d7f5d3SJohn Marino err(3, "unlink(%s)", file);
143*86d7f5d3SJohn Marino
144*86d7f5d3SJohn Marino }
145*86d7f5d3SJohn Marino
146*86d7f5d3SJohn Marino return (0);
147*86d7f5d3SJohn Marino }
148