186d7f5d3SJohn Marino /*-
286d7f5d3SJohn Marino * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
386d7f5d3SJohn Marino * All rights reserved.
486d7f5d3SJohn Marino *
586d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
686d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
786d7f5d3SJohn Marino * are met:
886d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
986d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
1086d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
1186d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
1286d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
1386d7f5d3SJohn Marino *
1486d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1586d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1686d7f5d3SJohn Marino * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1786d7f5d3SJohn Marino * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1886d7f5d3SJohn Marino * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1986d7f5d3SJohn Marino * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2086d7f5d3SJohn Marino * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2186d7f5d3SJohn Marino * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2286d7f5d3SJohn Marino * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2386d7f5d3SJohn Marino * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2486d7f5d3SJohn Marino * SUCH DAMAGE.
2586d7f5d3SJohn Marino *
2686d7f5d3SJohn Marino */
2786d7f5d3SJohn Marino
2886d7f5d3SJohn Marino #include <unistd.h>
2986d7f5d3SJohn Marino #include <stdio.h>
3086d7f5d3SJohn Marino #include <stdlib.h>
3186d7f5d3SJohn Marino #include <fcntl.h>
3286d7f5d3SJohn Marino #include <string.h>
3386d7f5d3SJohn Marino #include <sys/stat.h>
3486d7f5d3SJohn Marino #include <sys/param.h>
3586d7f5d3SJohn Marino #include <sys/syslimits.h>
3686d7f5d3SJohn Marino #include <err.h>
3786d7f5d3SJohn Marino #include <errno.h>
3886d7f5d3SJohn Marino
3986d7f5d3SJohn Marino #include "stress.h"
4086d7f5d3SJohn Marino
4186d7f5d3SJohn Marino static char path[128];
4286d7f5d3SJohn Marino static unsigned long size;
4386d7f5d3SJohn Marino
4486d7f5d3SJohn Marino int
setup(int nb)4586d7f5d3SJohn Marino setup(int nb)
4686d7f5d3SJohn Marino {
4786d7f5d3SJohn Marino int pct;
4886d7f5d3SJohn Marino int64_t in;
4986d7f5d3SJohn Marino int64_t bl;
5086d7f5d3SJohn Marino int64_t reserve_in;
5186d7f5d3SJohn Marino int64_t reserve_bl;
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino umask(0);
5486d7f5d3SJohn Marino path[0] = 0;
5586d7f5d3SJohn Marino if (nb == 0) {
5686d7f5d3SJohn Marino getdf(&bl, &in);
5786d7f5d3SJohn Marino size = in / op->incarnations;
5886d7f5d3SJohn Marino
5986d7f5d3SJohn Marino pct = 90;
6086d7f5d3SJohn Marino if (op->hog == 0)
6186d7f5d3SJohn Marino pct = random_int(1, 90);
6286d7f5d3SJohn Marino size = size / 100 * pct + 1;
6386d7f5d3SJohn Marino
6486d7f5d3SJohn Marino if (size > 20000 && op->hog == 0)
6586d7f5d3SJohn Marino size = 20000; /* arbitrary limit number of files pr. dir */
6686d7f5d3SJohn Marino if (size > LINK_MAX)
6786d7f5d3SJohn Marino size = LINK_MAX;
6886d7f5d3SJohn Marino
6986d7f5d3SJohn Marino
7086d7f5d3SJohn Marino /* Resource requirements: */
7186d7f5d3SJohn Marino reserve_in = 2 * op->incarnations + 1;
7286d7f5d3SJohn Marino reserve_bl = 26 * size * op->incarnations;
7386d7f5d3SJohn Marino if (reserve_bl > bl)
7486d7f5d3SJohn Marino size = bl / 26 * op->incarnations;
7586d7f5d3SJohn Marino if (reserve_in > in)
7686d7f5d3SJohn Marino size = reserve_in = reserve_bl = 0;
7786d7f5d3SJohn Marino
7886d7f5d3SJohn Marino if (op->verbose > 1)
7986d7f5d3SJohn Marino printf("link(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
8086d7f5d3SJohn Marino size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
8186d7f5d3SJohn Marino reservedf(reserve_bl, reserve_in);
8286d7f5d3SJohn Marino putval(size);
8386d7f5d3SJohn Marino } else {
8486d7f5d3SJohn Marino size = getval();
8586d7f5d3SJohn Marino }
8686d7f5d3SJohn Marino if (size == 0)
8786d7f5d3SJohn Marino exit (1);
8886d7f5d3SJohn Marino
8986d7f5d3SJohn Marino sprintf(path,"%s.%05d", getprogname(), getpid());
9086d7f5d3SJohn Marino if (mkdir(path, 0770) < 0)
9186d7f5d3SJohn Marino err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
9286d7f5d3SJohn Marino
9386d7f5d3SJohn Marino if (chdir(path) == -1)
9486d7f5d3SJohn Marino err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
9586d7f5d3SJohn Marino
9686d7f5d3SJohn Marino return (0);
9786d7f5d3SJohn Marino }
9886d7f5d3SJohn Marino
9986d7f5d3SJohn Marino void
cleanup(void)10086d7f5d3SJohn Marino cleanup(void)
10186d7f5d3SJohn Marino {
10286d7f5d3SJohn Marino (void)chdir("..");
10386d7f5d3SJohn Marino if (path[0] != 0 && rmdir(path) == -1)
10486d7f5d3SJohn Marino warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
10586d7f5d3SJohn Marino }
10686d7f5d3SJohn Marino
10786d7f5d3SJohn Marino int
test(void)10886d7f5d3SJohn Marino test(void)
10986d7f5d3SJohn Marino {
11086d7f5d3SJohn Marino int fd, i, j;
11186d7f5d3SJohn Marino pid_t pid;
11286d7f5d3SJohn Marino char file[128];
11386d7f5d3SJohn Marino char lfile[128];
11486d7f5d3SJohn Marino
11586d7f5d3SJohn Marino pid = getpid();
11686d7f5d3SJohn Marino for (j = 0; j < size && done_testing == 0; j++) {
11786d7f5d3SJohn Marino sprintf(file,"p%05d.%05d", pid, j);
11886d7f5d3SJohn Marino if (j == 0) {
11986d7f5d3SJohn Marino if ((fd = creat(file, 0660)) == -1) {
12086d7f5d3SJohn Marino if (errno != EINTR) {
12186d7f5d3SJohn Marino warn("creat(%s)", file);
12286d7f5d3SJohn Marino break;
12386d7f5d3SJohn Marino }
12486d7f5d3SJohn Marino }
12586d7f5d3SJohn Marino if (fd != -1 && close(fd) == -1)
12686d7f5d3SJohn Marino err(2, "close(%d)", j);
12786d7f5d3SJohn Marino strcpy(lfile, file);
12886d7f5d3SJohn Marino } else {
12986d7f5d3SJohn Marino if (link(lfile, file) == -1) {
13086d7f5d3SJohn Marino if (errno != EINTR) {
13186d7f5d3SJohn Marino warn("link(%s, %s)", lfile, file);
13286d7f5d3SJohn Marino break;
13386d7f5d3SJohn Marino }
13486d7f5d3SJohn Marino }
13586d7f5d3SJohn Marino }
13686d7f5d3SJohn Marino
13786d7f5d3SJohn Marino }
13886d7f5d3SJohn Marino
13986d7f5d3SJohn Marino for (i = --j; i >= 0; i--) {
14086d7f5d3SJohn Marino sprintf(file,"p%05d.%05d", pid, i);
14186d7f5d3SJohn Marino if (unlink(file) == -1)
14286d7f5d3SJohn Marino err(3, "unlink(%s)", file);
14386d7f5d3SJohn Marino
14486d7f5d3SJohn Marino }
14586d7f5d3SJohn Marino
14686d7f5d3SJohn Marino return (0);
14786d7f5d3SJohn Marino }
148