xref: /dragonfly/test/stress/stress2/misc/statfs.sh (revision 0ca59c34)
1#!/bin/sh
2
3#
4# Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28# $FreeBSD$
29#
30
31# Regression test for statfs problems with deleting a large number of files
32
33# $ ./statfs.sh
34# Filesystem  1K-blocks    Used Avail Capacity iused  ifree %iused  Mounted on
35# /dev/ad0s1e   1982798 1782134 42042    98%    4965 254105    2%   /tmp
36# Free inodes on /tmp: 254105
37# Creating 100000 files...
38# Deleting 100000 files...
39# Filesystem  1K-blocks    Used   Avail Capacity iused  ifree %iused  Mounted on
40# /dev/ad0s1e   1982798 -284096 2108272   -16%    4965 254105    2%   /tmp
41# $ umount -f /tmp; mount /tmp
42# $ df -i /tmp
43# Filesystem  1K-blocks    Used Avail Capacity iused  ifree %iused  Mounted on
44# /dev/ad0s1e   1982798 1784528 39648    98%    4965 254105    2%   /tmp
45
46
47odir=`pwd`
48dir=/tmp
49
50cd $dir
51sed '1,/^EOF/d' < $odir/$0 > $dir/statfs.c
52cc -o statfs -Wall statfs.c
53rm -f statfs.c
54
55df -i /tmp
56./statfs
57df -i /tmp
58
59exit
60EOF
61/* $FreeBSD$ */
62
63#include <sys/types.h>
64#include <sys/sysctl.h>
65#include <unistd.h>
66#include <stdio.h>
67#include <fcntl.h>
68#include <stdlib.h>
69#include <sys/stat.h>
70#include <sys/param.h>
71#include <sys/mount.h>
72#include <errno.h>
73#include <err.h>
74
75int64_t
76inodes(void)
77{
78	char path[MAXPATHLEN+1];
79	struct statfs buf;
80
81	sync();
82	if (getcwd(path, sizeof(path)) == NULL)
83		err(1, "getcwd()");
84
85	if (statfs(path, &buf) < 0)
86		err(1, "statfs(%s)", path);
87	printf("Free inodes on %s: %jd\n", path, buf.f_ffree);
88	return (buf.f_ffree);
89}
90
91int
92main()
93{
94	int fd, i, j;
95	int64_t size;
96	pid_t pid;
97	char file[128];
98
99	size = inodes() - 1000;
100
101	if (size > 100000)
102		size = 100000;
103
104	printf("Creating %jd files...", size); fflush(stdout);
105	pid = getpid();
106	for (j = 0; j < size; j++) {
107		sprintf(file,"p%06d.%05d", pid, j);
108		if ((fd = open(file, O_CREAT | O_TRUNC | O_WRONLY, 0666)) == -1) {
109			if (errno != EINTR) {
110				warn("creat(%s)", file);
111				printf("break out at %d, errno %d\n", j, errno);
112				break;
113			}
114		}
115		if (fd != -1 && close(fd) == -1)
116			err(2, "close(%d)", j);
117
118	}
119
120	printf("\nDeleting %jd files...", size); fflush(stdout);
121	for (i = --j; i >= 0; i--) {
122		sprintf(file,"p%06d.%05d", pid, i);
123		if (unlink(file) == -1)
124			err(3, "unlink(%s)", file);
125
126	}
127	printf("\n");
128	return (0);
129}
130