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