2 * Copyright (c) 2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
39 * This regression test attempts to exercise two instances of uprintf(9) in
40 * UFS: (1) when blocks are exhausted, and (2) when inodes are exhausted, in
41 * order to attempt to trigger races in the uprintf(9) code. The test
42 * accepts a pointer to a path -- ideally, a very small UFS partition -- and
43 * then proceeds to fill it in various ways.
45 * This tool assumes that it is alright to create, and delete, entries in the
46 * directory with names of integer values. Don't run this tool against a
47 * directory that has files with names along those lines if you want to keep
52 * mdconfig -a -t malloc -s 512
63 * Fill up the disk, then generate NUMTRIES additional ENOSPC errors.
65 #define BLOCKSIZE 1024
66 #define BLOCKS_FILENAME "0"
70 char block
[BLOCKSIZE
];
74 fd
= open(BLOCKS_FILENAME
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
76 err(-1, "fill_blocks: open(%s)", BLOCKS_FILENAME
);
79 * First step: fill the disk device. Keep extending the file until
80 * we hit our first error, and hope it is ENOSPC.
82 bzero(block
, BLOCKSIZE
);
85 len
= write(fd
, block
, BLOCKSIZE
);
88 if (len
!= BLOCKSIZE
) {
89 warnx("fill_blocks: write(%d) returned %d",
92 (void)unlink(BLOCKS_FILENAME
);
97 if (errno
!= ENOSPC
) {
98 warn("fill_blocks: write");
100 (void)unlink(BLOCKS_FILENAME
);
105 * Second step: generate NUMTRIES instances of the error by retrying
108 for (i
= 0; i
< NUMTRIES
; i
++) {
109 len
= write(fd
, block
, BLOCKSIZE
);
110 if (len
< 0 && errno
!= ENOSPC
) {
111 warn("fill_blocks: write after ENOSPC");
113 (void)unlink(BLOCKS_FILENAME
);
119 (void)unlink(BLOCKS_FILENAME
);
123 * Create as many entries in the directory as we can, then once we start
124 * hitting ENOSPC, try NUMTRIES additional times. Note that we don't be able
125 * to tell the difference between running out of inodes and running out of
126 * room to extend the directory, so this is just a best effort.
135 * First step, fill the directory.
139 snprintf(path
, PATH_MAX
, "%d", i
);
140 fd
= open(path
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
147 if (errno
!= ENOSPC
) {
148 warn("fill_inodes: open(%s)", path
);
152 for (i
= 0; i
< NUMTRIES
; i
++) {
153 fd
= open(path
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
154 if (fd
< 0 && errno
!= ENOSPC
) {
155 warn("fill_inodes: open(%s) after ENOSPC", path
);
159 warnx("fill_inodes: open(%s) after ENOSPC returned "
167 for (i
= 0; i
< max
; i
++) {
168 snprintf(path
, PATH_MAX
, "%d", i
);
174 main(int argc
, char *argv
[])
178 err(-1, "usage: ufs_uprintf /non_optional_path");
180 if (chdir(argv
[1]) < 0)
181 err(-1, "chdir(%s)", argv
[1]);