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