Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 5 | #include <dirent.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 6 | #include <sys/stat.h> |
| 7 | #include <sys/mman.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 8 | #include <sys/types.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 9 | |
| 10 | #include "fio.h" |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 11 | |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 12 | static int root_warn; |
| 13 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 14 | static int extend_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 15 | { |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 16 | int r, new_layout = 0, unlink_file = 0, flags; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 17 | unsigned long long left; |
| 18 | unsigned int bs; |
| 19 | char *b; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 20 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 21 | /* |
| 22 | * check if we need to lay the file out complete again. fio |
| 23 | * does that for operations involving reads, or for writes |
| 24 | * where overwrite is set |
| 25 | */ |
| 26 | if (td_read(td) || (td_write(td) && td->o.overwrite)) |
| 27 | new_layout = 1; |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 28 | if (td_write(td) && !td->o.overwrite) |
| 29 | unlink_file = 1; |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 30 | |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 31 | if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 32 | if (unlink(f->file_name) < 0) { |
| 33 | td_verror(td, errno, "unlink"); |
| 34 | return 1; |
| 35 | } |
| 36 | } |
| 37 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 38 | flags = O_WRONLY | O_CREAT; |
| 39 | if (new_layout) |
| 40 | flags |= O_TRUNC; |
| 41 | |
| 42 | f->fd = open(f->file_name, flags, 0644); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 43 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 44 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 45 | return 1; |
| 46 | } |
| 47 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 48 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 49 | td_verror(td, errno, "ftruncate"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 50 | goto err; |
| 51 | } |
| 52 | |
Jens Axboe | e8be2ad | 2007-03-29 10:31:51 +0200 | [diff] [blame] | 53 | if (!new_layout) |
| 54 | goto done; |
| 55 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 56 | if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 57 | td_verror(td, errno, "posix_fallocate"); |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 58 | goto err; |
| 59 | } |
| 60 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 61 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
| 62 | memset(b, 0, td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 63 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 64 | left = f->real_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 65 | while (left && !td->terminate) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 66 | bs = td->o.max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 67 | if (bs > left) |
| 68 | bs = left; |
| 69 | |
| 70 | r = write(f->fd, b, bs); |
| 71 | |
| 72 | if (r == (int) bs) { |
| 73 | left -= bs; |
| 74 | continue; |
| 75 | } else { |
| 76 | if (r < 0) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 77 | td_verror(td, errno, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 78 | else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 79 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 80 | |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (td->terminate) |
| 86 | unlink(f->file_name); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 87 | else if (td->o.create_fsync) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 88 | fsync(f->fd); |
| 89 | |
| 90 | free(b); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 91 | done: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 92 | close(f->fd); |
| 93 | f->fd = -1; |
| 94 | return 0; |
| 95 | err: |
| 96 | close(f->fd); |
| 97 | f->fd = -1; |
| 98 | return 1; |
| 99 | } |
| 100 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 101 | static unsigned long long get_rand_file_size(struct thread_data *td) |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 102 | { |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 103 | unsigned long long ret; |
| 104 | long r; |
| 105 | |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 106 | r = os_random_long(&td->file_size_state); |
ljzhang,Yaxin Hu,Jianchao Tang | d11a531 | 2007-07-27 15:54:10 +0200 | [diff] [blame] | 107 | ret = td->o.file_size_low + (unsigned long long) ((double) (td->o.file_size_high - td->o.file_size_low) * (r / (RAND_MAX + 1.0))); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 108 | ret -= (ret % td->o.rw_min_bs); |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 109 | return ret; |
| 110 | } |
| 111 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 112 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 113 | { |
| 114 | struct stat st; |
| 115 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 116 | if (fstat(f->fd, &st) == -1) { |
| 117 | td_verror(td, errno, "fstat"); |
| 118 | return 1; |
| 119 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 120 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 121 | f->real_file_size = st.st_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 126 | { |
| 127 | unsigned long long bytes; |
| 128 | int r; |
| 129 | |
| 130 | r = blockdev_size(f->fd, &bytes); |
| 131 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 132 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | f->real_file_size = bytes; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 141 | { |
| 142 | int ret = 0; |
| 143 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 144 | if (f->flags & FIO_SIZE_KNOWN) |
| 145 | return 0; |
| 146 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 147 | if (f->filetype == FIO_TYPE_FILE) |
| 148 | ret = file_size(td, f); |
| 149 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 150 | ret = bdev_size(td, f); |
| 151 | else |
| 152 | f->real_file_size = -1; |
| 153 | |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | if (f->file_offset > f->real_file_size) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 158 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 162 | f->flags |= FIO_SIZE_KNOWN; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
| 165 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 166 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 167 | { |
| 168 | int ret = 0; |
| 169 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 170 | if (td->o.odirect) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 171 | return 0; |
| 172 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 173 | /* |
| 174 | * FIXME: add blockdev flushing too |
| 175 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 176 | if (f->mmap) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 177 | ret = madvise(f->mmap, f->io_size, MADV_DONTNEED); |
Jens Axboe | 467d1b6 | 2007-03-15 14:11:10 +0100 | [diff] [blame] | 178 | else if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 179 | ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 180 | else if (f->filetype == FIO_TYPE_BD) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 181 | ret = blockdev_invalidate_cache(f->fd); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 182 | if (ret < 0 && errno == EACCES && geteuid()) { |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 183 | if (!root_warn) { |
| 184 | log_err("fio: only root may flush block devices. Cache flush bypassed!\n"); |
| 185 | root_warn = 1; |
| 186 | } |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 187 | ret = 0; |
| 188 | } |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 189 | } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 190 | ret = 0; |
| 191 | |
| 192 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 193 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 194 | return 1; |
| 195 | } |
| 196 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 197 | return ret; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 198 | } |
| 199 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 200 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 201 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 202 | close(f->fd); |
| 203 | f->fd = -1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 206 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 207 | { |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 208 | int is_std = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 209 | int flags = 0; |
| 210 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 211 | if (!strcmp(f->file_name, "-")) { |
| 212 | if (td_rw(td)) { |
| 213 | log_err("fio: can't read/write to stdin/out\n"); |
| 214 | return 1; |
| 215 | } |
| 216 | is_std = 1; |
Jens Axboe | ce98fa6 | 2007-04-17 13:27:32 +0200 | [diff] [blame] | 217 | |
| 218 | /* |
| 219 | * move output logging to stderr, if we are writing to stdout |
| 220 | */ |
| 221 | if (td_write(td)) |
| 222 | f_out = stderr; |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 225 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 226 | flags |= OS_O_DIRECT; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 227 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 228 | flags |= O_SYNC; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 229 | |
Jens Axboe | 660a1cb | 2007-04-17 15:37:47 +0200 | [diff] [blame] | 230 | if (td_write(td)) { |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 231 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 232 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 233 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 234 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 235 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 236 | if (is_std) |
| 237 | f->fd = dup(STDOUT_FILENO); |
| 238 | else |
| 239 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 240 | } else { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 241 | if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 242 | flags |= O_RDWR; |
| 243 | else |
| 244 | flags |= O_RDONLY; |
| 245 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 246 | if (is_std) |
| 247 | f->fd = dup(STDIN_FILENO); |
| 248 | else |
| 249 | f->fd = open(f->file_name, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 253 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 254 | int __e = errno; |
| 255 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 256 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
| 257 | |
| 258 | td_verror(td, __e, buf); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 259 | } |
| 260 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 261 | if (get_file_size(td, f)) |
| 262 | goto err; |
| 263 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 264 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 265 | err: |
| 266 | close(f->fd); |
| 267 | return 1; |
| 268 | } |
| 269 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 270 | int open_files(struct thread_data *td) |
| 271 | { |
| 272 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 273 | unsigned int i; |
| 274 | int err = 0; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 275 | |
| 276 | for_each_file(td, f, i) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 277 | err = td_io_open_file(td, f); |
Jens Axboe | 9bf27b4 | 2007-03-27 19:49:31 +0200 | [diff] [blame] | 278 | if (err) { |
| 279 | if (td->error == EMFILE) { |
| 280 | log_err("fio: limited open files to: %d\n", td->nr_open_files); |
| 281 | td->o.open_files = td->nr_open_files; |
| 282 | err = 0; |
| 283 | clear_error(td); |
| 284 | } |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 285 | break; |
Jens Axboe | 9bf27b4 | 2007-03-27 19:49:31 +0200 | [diff] [blame] | 286 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 287 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 288 | if (td->o.open_files == td->nr_open_files) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 289 | break; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 292 | if (!err) |
| 293 | return 0; |
| 294 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 295 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 296 | td_io_close_file(td, f); |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 297 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 298 | return err; |
| 299 | } |
| 300 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 301 | /* |
| 302 | * open/close all files, so that ->real_file_size gets set |
| 303 | */ |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 304 | static int get_file_sizes(struct thread_data *td) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 305 | { |
| 306 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 307 | unsigned int i; |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 308 | int err = 0; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 309 | |
| 310 | for_each_file(td, f, i) { |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 311 | if (td->io_ops->open_file(td, f)) { |
Jens Axboe | 40b44f4 | 2007-04-11 12:43:35 +0200 | [diff] [blame] | 312 | if (td->error != ENOENT) { |
| 313 | log_err("%s\n", td->verror); |
| 314 | err = 1; |
| 315 | } |
Jens Axboe | 541d66d | 2007-03-27 19:50:11 +0200 | [diff] [blame] | 316 | clear_error(td); |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 317 | } else { |
| 318 | if (td->io_ops->close_file) |
| 319 | td->io_ops->close_file(td, f); |
| 320 | } |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 321 | |
| 322 | if (f->real_file_size == -1ULL && td->o.size) |
| 323 | f->real_file_size = td->o.size / td->o.nr_files; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 324 | } |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 325 | |
| 326 | return err; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | /* |
| 330 | * Open the files and setup files sizes, creating files if necessary. |
| 331 | */ |
| 332 | int setup_files(struct thread_data *td) |
| 333 | { |
| 334 | unsigned long long total_size, extend_size; |
| 335 | struct fio_file *f; |
| 336 | unsigned int i; |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 337 | int err = 0, need_extend; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * if ioengine defines a setup() method, it's responsible for |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 341 | * opening the files and setting f->real_file_size to indicate |
| 342 | * the valid range for that file. |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 343 | */ |
| 344 | if (td->io_ops->setup) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 345 | err = td->io_ops->setup(td); |
| 346 | else |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 347 | err = get_file_sizes(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 348 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 349 | if (err) |
| 350 | return err; |
| 351 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 352 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 353 | * check sizes. if the files/devices do not exist and the size |
| 354 | * isn't passed to fio, abort. |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 355 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 356 | total_size = 0; |
| 357 | for_each_file(td, f, i) { |
| 358 | if (f->real_file_size == -1ULL) |
| 359 | total_size = -1ULL; |
| 360 | else |
| 361 | total_size += f->real_file_size; |
| 362 | } |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 363 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 364 | /* |
| 365 | * device/file sizes are zero and no size given, punt |
| 366 | */ |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 367 | if ((!total_size || total_size == -1ULL) && !td->o.size) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 368 | log_err("%s: you need to specify size=\n", td->o.name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 369 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 370 | return 1; |
| 371 | } |
| 372 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 373 | /* |
| 374 | * now file sizes are known, so we can set ->io_size. if size= is |
| 375 | * not given, ->io_size is just equal to ->real_file_size. if size |
| 376 | * is given, ->io_size is size / nr_files. |
| 377 | */ |
| 378 | extend_size = total_size = 0; |
| 379 | need_extend = 0; |
| 380 | for_each_file(td, f, i) { |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 381 | f->file_offset = td->o.start_offset; |
| 382 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 383 | if (!td->o.file_size_low) { |
| 384 | /* |
| 385 | * no file size range given, file size is equal to |
| 386 | * total size divided by number of files. if that is |
| 387 | * zero, set it to the real file size. |
| 388 | */ |
| 389 | f->io_size = td->o.size / td->o.nr_files; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 390 | if (!f->io_size) { |
| 391 | if (f->file_offset > f->real_file_size) |
| 392 | goto err_offset; |
| 393 | f->io_size = f->real_file_size - f->file_offset; |
| 394 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 395 | } else if (f->real_file_size < td->o.file_size_low || |
| 396 | f->real_file_size > td->o.file_size_high) { |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 397 | if (f->file_offset > td->o.file_size_low) |
| 398 | goto err_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 399 | /* |
| 400 | * file size given. if it's fixed, use that. if it's a |
| 401 | * range, generate a random size in-between. |
| 402 | */ |
| 403 | if (td->o.file_size_low == td->o.file_size_high) |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 404 | f->io_size = td->o.file_size_low - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 405 | else |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 406 | f->io_size = get_rand_file_size(td) - f->file_offset; |
| 407 | } else if (f->file_offset > f->real_file_size) |
| 408 | goto err_offset; |
| 409 | else |
| 410 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 411 | |
| 412 | if (f->io_size == -1ULL) |
| 413 | total_size = -1ULL; |
| 414 | else |
| 415 | total_size += f->io_size; |
| 416 | |
| 417 | if (f->filetype == FIO_TYPE_FILE && |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 418 | (f->io_size + f->file_offset) > f->real_file_size && |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 419 | !(td->io_ops->flags & FIO_DISKLESSIO)) { |
| 420 | need_extend++; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 421 | extend_size += (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 422 | f->flags |= FIO_FILE_EXTEND; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 423 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 424 | } |
| 425 | |
ljzhang,Yaxin Hu,Jianchao Tang | 2298290 | 2007-07-27 13:21:28 +0200 | [diff] [blame] | 426 | if (!td->o.size || td->o.size > total_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 427 | td->o.size = total_size; |
| 428 | |
| 429 | /* |
| 430 | * See if we need to extend some files |
| 431 | */ |
| 432 | if (need_extend) { |
| 433 | temp_stall_ts = 1; |
| 434 | log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n", |
| 435 | td->o.name, need_extend, extend_size >> 20); |
| 436 | |
| 437 | for_each_file(td, f, i) { |
| 438 | if (!(f->flags & FIO_FILE_EXTEND)) |
| 439 | continue; |
| 440 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 441 | assert(f->filetype == FIO_TYPE_FILE); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 442 | f->flags &= ~FIO_FILE_EXTEND; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 443 | f->real_file_size = (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 444 | err = extend_file(td, f); |
| 445 | if (err) |
| 446 | break; |
| 447 | } |
| 448 | temp_stall_ts = 0; |
| 449 | } |
| 450 | |
| 451 | if (err) |
| 452 | return err; |
| 453 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 454 | if (!td->o.zone_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 455 | td->o.zone_size = td->o.size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 456 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 457 | td->total_io_size = td->o.size * td->o.loops; |
| 458 | return 0; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 459 | err_offset: |
| 460 | log_err("%s: you need to specify valid offset=\n", td->o.name); |
| 461 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 462 | } |
| 463 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 464 | int init_random_map(struct thread_data *td) |
| 465 | { |
| 466 | int num_maps, blocks; |
| 467 | struct fio_file *f; |
| 468 | unsigned int i; |
| 469 | |
| 470 | if (td->o.norandommap) |
| 471 | return 0; |
| 472 | |
| 473 | for_each_file(td, f, i) { |
| 474 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs; |
| 475 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; |
| 476 | f->file_map = malloc(num_maps * sizeof(long)); |
| 477 | if (!f->file_map) { |
| 478 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); |
| 479 | return 1; |
| 480 | } |
| 481 | f->num_maps = num_maps; |
| 482 | memset(f->file_map, 0, num_maps * sizeof(long)); |
| 483 | } |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 488 | void close_files(struct thread_data *td) |
| 489 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 490 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 491 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 492 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 493 | for_each_file(td, f, i) { |
Jens Axboe | a9b7b30 | 2007-04-19 16:31:16 +0200 | [diff] [blame] | 494 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 495 | unlink(f->file_name); |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 496 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 497 | td_io_close_file(td, f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 498 | |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 499 | free(f->file_name); |
| 500 | f->file_name = NULL; |
| 501 | |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 502 | if (f->file_map) { |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 503 | free(f->file_map); |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 504 | f->file_map = NULL; |
| 505 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 506 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 507 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 508 | td->o.filename = NULL; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 509 | free(td->files); |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 510 | td->files = NULL; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 511 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 512 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 513 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 514 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 515 | { |
| 516 | struct stat sb; |
| 517 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 518 | if (!strcmp(f->file_name, "-")) |
| 519 | f->filetype = FIO_TYPE_PIPE; |
| 520 | else |
| 521 | f->filetype = FIO_TYPE_FILE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 522 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 523 | if (!lstat(f->file_name, &sb)) { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 524 | if (S_ISBLK(sb.st_mode)) |
| 525 | f->filetype = FIO_TYPE_BD; |
| 526 | else if (S_ISCHR(sb.st_mode)) |
| 527 | f->filetype = FIO_TYPE_CHAR; |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 528 | else if (S_ISFIFO(sb.st_mode)) |
| 529 | f->filetype = FIO_TYPE_PIPE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 533 | int add_file(struct thread_data *td, const char *fname) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 534 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 535 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 536 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 537 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 538 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 539 | |
| 540 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); |
| 541 | |
| 542 | f = &td->files[cur_files]; |
| 543 | memset(f, 0, sizeof(*f)); |
| 544 | f->fd = -1; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 545 | |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 546 | /* |
| 547 | * init function, io engine may not be loaded yet |
| 548 | */ |
| 549 | if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) |
| 550 | f->real_file_size = -1ULL; |
| 551 | |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 552 | if (td->o.directory) |
| 553 | len = sprintf(file_name, "%s/", td->o.directory); |
| 554 | |
| 555 | sprintf(file_name + len, "%s", fname); |
| 556 | f->file_name = strdup(file_name); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 557 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 558 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 559 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 560 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 561 | if (f->filetype == FIO_TYPE_FILE) |
| 562 | td->nr_normal_files++; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 563 | |
| 564 | return cur_files; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 565 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 566 | |
| 567 | void get_file(struct fio_file *f) |
| 568 | { |
Jens Axboe | 97af62c | 2007-05-22 11:12:13 +0200 | [diff] [blame] | 569 | assert(f->flags & FIO_FILE_OPEN); |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 570 | f->references++; |
| 571 | } |
| 572 | |
| 573 | void put_file(struct thread_data *td, struct fio_file *f) |
| 574 | { |
| 575 | if (!(f->flags & FIO_FILE_OPEN)) |
| 576 | return; |
| 577 | |
| 578 | assert(f->references); |
| 579 | if (--f->references) |
| 580 | return; |
| 581 | |
Jens Axboe | d424d4d | 2007-04-17 09:05:10 +0200 | [diff] [blame] | 582 | if (should_fsync(td) && td->o.fsync_on_close) |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 583 | fsync(f->fd); |
| 584 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 585 | if (td->io_ops->close_file) |
| 586 | td->io_ops->close_file(td, f); |
Jens Axboe | 1020a13 | 2007-03-29 11:15:06 +0200 | [diff] [blame] | 587 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 588 | td->nr_open_files--; |
| 589 | f->flags &= ~FIO_FILE_OPEN; |
| 590 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 591 | |
| 592 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 593 | { |
| 594 | struct dirent *dir; |
| 595 | int ret = 0; |
| 596 | DIR *D; |
| 597 | |
| 598 | D = opendir(dirname); |
| 599 | if (!D) { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 600 | char buf[FIO_VERROR_SIZE]; |
| 601 | |
| 602 | snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname); |
| 603 | td_verror(td, errno, buf); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 604 | return 1; |
| 605 | } |
| 606 | |
| 607 | while ((dir = readdir(D)) != NULL) { |
| 608 | char full_path[PATH_MAX]; |
| 609 | struct stat sb; |
| 610 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 611 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 612 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 613 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 614 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
| 615 | |
| 616 | if (lstat(full_path, &sb) == -1) { |
| 617 | if (errno != ENOENT) { |
| 618 | td_verror(td, errno, "stat"); |
| 619 | return 1; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if (S_ISREG(sb.st_mode)) { |
| 624 | add_file(td, full_path); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 625 | td->o.nr_files++; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 626 | continue; |
| 627 | } |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 628 | if (!S_ISDIR(sb.st_mode)) |
| 629 | continue; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 630 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 631 | if ((ret = recurse_dir(td, full_path)) != 0) |
| 632 | break; |
| 633 | } |
| 634 | |
| 635 | closedir(D); |
| 636 | return ret; |
| 637 | } |
| 638 | |
| 639 | int add_dir_files(struct thread_data *td, const char *path) |
| 640 | { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 641 | int ret = recurse_dir(td, path); |
| 642 | |
| 643 | if (!ret) |
| 644 | log_info("fio: opendir added %d files\n", td->o.nr_files); |
| 645 | |
| 646 | return ret; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 647 | } |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 648 | |
| 649 | void dup_files(struct thread_data *td, struct thread_data *org) |
| 650 | { |
| 651 | struct fio_file *f; |
| 652 | unsigned int i; |
| 653 | size_t bytes; |
| 654 | |
| 655 | if (!org->files) |
| 656 | return; |
| 657 | |
| 658 | bytes = org->files_index * sizeof(*f); |
| 659 | td->files = malloc(bytes); |
| 660 | memcpy(td->files, org->files, bytes); |
| 661 | |
| 662 | for_each_file(td, f, i) { |
| 663 | if (f->file_name) |
| 664 | f->file_name = strdup(f->file_name); |
| 665 | } |
| 666 | } |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 667 | |
| 668 | /* |
| 669 | * Returns the index that matches the filename, or -1 if not there |
| 670 | */ |
| 671 | int get_fileno(struct thread_data *td, const char *fname) |
| 672 | { |
| 673 | struct fio_file *f; |
| 674 | unsigned int i; |
| 675 | |
| 676 | for_each_file(td, f, i) |
| 677 | if (!strcmp(f->file_name, fname)) |
| 678 | return i; |
| 679 | |
| 680 | return -1; |
| 681 | } |
| 682 | |
| 683 | /* |
| 684 | * For log usage, where we add/open/close files automatically |
| 685 | */ |
| 686 | void free_release_files(struct thread_data *td) |
| 687 | { |
| 688 | close_files(td); |
| 689 | td->files_index = 0; |
| 690 | td->nr_normal_files = 0; |
| 691 | } |