xref: /freebsd/tools/test/stress2/testcases/rename/rename.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/param.h>
29 #include <sys/stat.h>
30 #include <err.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include "stress.h"
38 
39 static unsigned long size;
40 
41 int
42 setup(int nb)
43 {
44 	int64_t in;
45 	int64_t bl;
46 	int64_t reserve_in;
47 	int64_t reserve_bl;
48 
49 	umask(0);
50 
51 	if (nb == 0) {
52 		getdf(&bl, &in);
53 		size = in / op->incarnations;
54 
55 		if (size > 1000)
56 			size = 1000;	/* arbitrary limit number of files pr. dir */
57 
58 		/* Resource requirements: */
59 		while (size > 0) {
60 			reserve_in =  2 * size * op->incarnations + 2 * op->incarnations;
61 			reserve_bl = 30 * size * op->incarnations;
62 			if (reserve_bl <= bl && reserve_in <= in)
63 				break;
64 			size = size / 2;
65 		}
66 		if (size == 0)
67 			reserve_bl = reserve_in = 0;
68 
69 		if (op->verbose > 1)
70 			printf("rename(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
71 				size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
72 		reservedf(reserve_bl, reserve_in);
73 		putval(size);
74 	} else {
75 		size = getval();
76 	}
77 	if (size == 0)
78 		exit(0);
79 
80 	return (0);
81 }
82 
83 void
84 cleanup(void)
85 {
86 }
87 
88 static void
89 test_rename(void)
90 {
91 	int i, j;
92 	pid_t pid;
93 	char file1[128];
94 	char file2[128];
95 	int tfd;
96 
97 	pid = getpid();
98 	for (i = 0; i < (int)size; i++) {
99 		sprintf(file1,"p%05d.%05d", pid, i);
100 		if ((tfd = open(file1, O_RDONLY|O_CREAT, 0660)) == -1)
101 			err(1, "openat(%s), %s:%d", file1, __FILE__, __LINE__);
102 		close(tfd);
103 	}
104 	for (j = 0; j < 100 && done_testing == 0; j++) {
105 		for (i = 0; i < (int)size; i++) {
106 			sprintf(file1,"p%05d.%05d", pid, i);
107 			sprintf(file2,"p%05d.%05d.togo", pid, i);
108 			if (rename(file1, file2) == -1)
109 				err(1, "rename(%s, %s). %s:%d", file1, file2,
110 						__FILE__, __LINE__);
111 		}
112 		for (i = 0; i < (int)size; i++) {
113 			sprintf(file1,"p%05d.%05d", pid, i);
114 			sprintf(file2,"p%05d.%05d.togo", pid, i);
115 			if (rename(file2, file1) == -1)
116 				err(1, "rename(%s, %s). %s:%d", file2, file1,
117 						__FILE__, __LINE__);
118 		}
119 	}
120 
121 	for (i = 0; i < (int)size; i++) {
122 		sprintf(file1,"p%05d.%05d", pid, i);
123 		if (unlink(file1) == -1)
124 			err(1, "unlink(%s), %s:%d", file1, __FILE__, __LINE__);
125 	}
126 }
127 
128 int
129 test(void)
130 {
131 	test_rename();
132 
133 	return (0);
134 }
135