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 | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 229 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 230 | { |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 231 | int is_std = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 232 | int flags = 0; |
| 233 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 234 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 235 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 236 | if (!strcmp(f->file_name, "-")) { |
| 237 | if (td_rw(td)) { |
| 238 | log_err("fio: can't read/write to stdin/out\n"); |
| 239 | return 1; |
| 240 | } |
| 241 | is_std = 1; |
Jens Axboe | ce98fa6 | 2007-04-17 13:27:32 +0200 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * move output logging to stderr, if we are writing to stdout |
| 245 | */ |
| 246 | if (td_write(td)) |
| 247 | f_out = stderr; |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 250 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 251 | flags |= OS_O_DIRECT; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 252 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 253 | flags |= O_SYNC; |
Jens Axboe | ad92396 | 2007-10-11 21:41:41 +0200 | [diff] [blame] | 254 | if (f->filetype != FIO_TYPE_FILE) |
| 255 | flags |= O_NOATIME; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 256 | |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 257 | open_again: |
Jens Axboe | 660a1cb | 2007-04-17 15:37:47 +0200 | [diff] [blame] | 258 | if (td_write(td)) { |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 259 | assert(!read_only); |
| 260 | |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 261 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 262 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 263 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 264 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 265 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 266 | if (is_std) |
| 267 | f->fd = dup(STDOUT_FILENO); |
| 268 | else |
| 269 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 270 | } else { |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 271 | if (f->filetype == FIO_TYPE_CHAR && !read_only) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 272 | flags |= O_RDWR; |
| 273 | else |
| 274 | flags |= O_RDONLY; |
| 275 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 276 | if (is_std) |
| 277 | f->fd = dup(STDIN_FILENO); |
| 278 | else |
| 279 | f->fd = open(f->file_name, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 283 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 284 | int __e = errno; |
| 285 | |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 286 | if (errno == EPERM && (flags & O_NOATIME)) { |
| 287 | flags &= ~O_NOATIME; |
| 288 | goto open_again; |
| 289 | } |
| 290 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 291 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
| 292 | |
| 293 | td_verror(td, __e, buf); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 294 | } |
| 295 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 296 | if (get_file_size(td, f)) |
| 297 | goto err; |
| 298 | |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame^] | 299 | add_file_hash(f); |
| 300 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 301 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 302 | err: |
| 303 | close(f->fd); |
| 304 | return 1; |
| 305 | } |
| 306 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 307 | int open_files(struct thread_data *td) |
| 308 | { |
| 309 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 310 | unsigned int i; |
| 311 | int err = 0; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 312 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 313 | dprint(FD_FILE, "open files\n"); |
| 314 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 315 | for_each_file(td, f, i) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 316 | err = td_io_open_file(td, f); |
Jens Axboe | 9bf27b4 | 2007-03-27 19:49:31 +0200 | [diff] [blame] | 317 | if (err) { |
| 318 | if (td->error == EMFILE) { |
| 319 | log_err("fio: limited open files to: %d\n", td->nr_open_files); |
| 320 | td->o.open_files = td->nr_open_files; |
| 321 | err = 0; |
| 322 | clear_error(td); |
| 323 | } |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 324 | break; |
Jens Axboe | 9bf27b4 | 2007-03-27 19:49:31 +0200 | [diff] [blame] | 325 | } |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 326 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 327 | if (td->o.open_files == td->nr_open_files) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 328 | break; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 329 | } |
| 330 | |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 331 | if (!err) |
| 332 | return 0; |
| 333 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 334 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 335 | td_io_close_file(td, f); |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 336 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 337 | return err; |
| 338 | } |
| 339 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 340 | /* |
| 341 | * open/close all files, so that ->real_file_size gets set |
| 342 | */ |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 343 | static int get_file_sizes(struct thread_data *td) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 344 | { |
| 345 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 346 | unsigned int i; |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 347 | int err = 0; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 348 | |
| 349 | for_each_file(td, f, i) { |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 350 | if (td->io_ops->open_file(td, f)) { |
Jens Axboe | 40b44f4 | 2007-04-11 12:43:35 +0200 | [diff] [blame] | 351 | if (td->error != ENOENT) { |
| 352 | log_err("%s\n", td->verror); |
| 353 | err = 1; |
| 354 | } |
Jens Axboe | 541d66d | 2007-03-27 19:50:11 +0200 | [diff] [blame] | 355 | clear_error(td); |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 356 | } else { |
| 357 | if (td->io_ops->close_file) |
| 358 | td->io_ops->close_file(td, f); |
| 359 | } |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 360 | |
| 361 | if (f->real_file_size == -1ULL && td->o.size) |
| 362 | f->real_file_size = td->o.size / td->o.nr_files; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 363 | } |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 364 | |
| 365 | return err; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Open the files and setup files sizes, creating files if necessary. |
| 370 | */ |
| 371 | int setup_files(struct thread_data *td) |
| 372 | { |
| 373 | unsigned long long total_size, extend_size; |
| 374 | struct fio_file *f; |
| 375 | unsigned int i; |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 376 | int err = 0, need_extend; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 377 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 378 | dprint(FD_FILE, "setup files\n"); |
| 379 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 380 | /* |
| 381 | * if ioengine defines a setup() method, it's responsible for |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 382 | * opening the files and setting f->real_file_size to indicate |
| 383 | * the valid range for that file. |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 384 | */ |
| 385 | if (td->io_ops->setup) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 386 | err = td->io_ops->setup(td); |
| 387 | else |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 388 | err = get_file_sizes(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 389 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 390 | if (err) |
| 391 | return err; |
| 392 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 393 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 394 | * check sizes. if the files/devices do not exist and the size |
| 395 | * isn't passed to fio, abort. |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 396 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 397 | total_size = 0; |
| 398 | for_each_file(td, f, i) { |
| 399 | if (f->real_file_size == -1ULL) |
| 400 | total_size = -1ULL; |
| 401 | else |
| 402 | total_size += f->real_file_size; |
| 403 | } |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 404 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 405 | /* |
| 406 | * device/file sizes are zero and no size given, punt |
| 407 | */ |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 408 | if ((!total_size || total_size == -1ULL) && !td->o.size && |
Shawn Lewis | aa31f1f | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 409 | !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 410 | log_err("%s: you need to specify size=\n", td->o.name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 411 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 412 | return 1; |
| 413 | } |
| 414 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 415 | /* |
| 416 | * now file sizes are known, so we can set ->io_size. if size= is |
| 417 | * not given, ->io_size is just equal to ->real_file_size. if size |
| 418 | * is given, ->io_size is size / nr_files. |
| 419 | */ |
| 420 | extend_size = total_size = 0; |
| 421 | need_extend = 0; |
| 422 | for_each_file(td, f, i) { |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 423 | f->file_offset = td->o.start_offset; |
| 424 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 425 | if (!td->o.file_size_low) { |
| 426 | /* |
| 427 | * no file size range given, file size is equal to |
| 428 | * total size divided by number of files. if that is |
| 429 | * zero, set it to the real file size. |
| 430 | */ |
| 431 | f->io_size = td->o.size / td->o.nr_files; |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 432 | if (!f->io_size) |
Jens Axboe | 273f8c9 | 2008-01-25 14:02:15 +0100 | [diff] [blame] | 433 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 434 | } else if (f->real_file_size < td->o.file_size_low || |
| 435 | f->real_file_size > td->o.file_size_high) { |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 436 | if (f->file_offset > td->o.file_size_low) |
| 437 | goto err_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 438 | /* |
| 439 | * file size given. if it's fixed, use that. if it's a |
| 440 | * range, generate a random size in-between. |
| 441 | */ |
| 442 | 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] | 443 | f->io_size = td->o.file_size_low - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 444 | else |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 445 | f->io_size = get_rand_file_size(td) - f->file_offset; |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 446 | } else |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 447 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 448 | |
| 449 | if (f->io_size == -1ULL) |
| 450 | total_size = -1ULL; |
| 451 | else |
| 452 | total_size += f->io_size; |
| 453 | |
| 454 | if (f->filetype == FIO_TYPE_FILE && |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 455 | (f->io_size + f->file_offset) > f->real_file_size && |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 456 | !(td->io_ops->flags & FIO_DISKLESSIO)) { |
| 457 | need_extend++; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 458 | extend_size += (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 459 | f->flags |= FIO_FILE_EXTEND; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 460 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 461 | } |
| 462 | |
ljzhang,Yaxin Hu,Jianchao Tang | 2298290 | 2007-07-27 13:21:28 +0200 | [diff] [blame] | 463 | if (!td->o.size || td->o.size > total_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 464 | td->o.size = total_size; |
| 465 | |
| 466 | /* |
| 467 | * See if we need to extend some files |
| 468 | */ |
| 469 | if (need_extend) { |
| 470 | temp_stall_ts = 1; |
Jens Axboe | f627d8a | 2007-08-24 10:24:09 +0200 | [diff] [blame] | 471 | 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] | 472 | td->o.name, need_extend, extend_size >> 20); |
| 473 | |
| 474 | for_each_file(td, f, i) { |
| 475 | if (!(f->flags & FIO_FILE_EXTEND)) |
| 476 | continue; |
| 477 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 478 | assert(f->filetype == FIO_TYPE_FILE); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 479 | f->flags &= ~FIO_FILE_EXTEND; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 480 | f->real_file_size = (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 481 | err = extend_file(td, f); |
| 482 | if (err) |
| 483 | break; |
| 484 | } |
| 485 | temp_stall_ts = 0; |
| 486 | } |
| 487 | |
| 488 | if (err) |
| 489 | return err; |
| 490 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 491 | if (!td->o.zone_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 492 | td->o.zone_size = td->o.size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 493 | |
Jens Axboe | ea966f8 | 2007-09-03 10:09:37 +0200 | [diff] [blame] | 494 | /* |
| 495 | * iolog already set the total io size, if we read back |
| 496 | * stored entries. |
| 497 | */ |
| 498 | if (!td->o.read_iolog_file) |
| 499 | td->total_io_size = td->o.size * td->o.loops; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 500 | return 0; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 501 | err_offset: |
| 502 | log_err("%s: you need to specify valid offset=\n", td->o.name); |
| 503 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 506 | int init_random_map(struct thread_data *td) |
| 507 | { |
Jens Axboe | 509eab1 | 2007-12-14 12:42:53 +0100 | [diff] [blame] | 508 | unsigned long long blocks, num_maps; |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 509 | struct fio_file *f; |
| 510 | unsigned int i; |
| 511 | |
Jens Axboe | de8dd11 | 2008-03-01 15:34:44 +0100 | [diff] [blame] | 512 | if (td->o.norandommap || !td_random(td)) |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 513 | return 0; |
| 514 | |
| 515 | for_each_file(td, f, i) { |
Jens Axboe | 509eab1 | 2007-12-14 12:42:53 +0100 | [diff] [blame] | 516 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / (unsigned long long) td->o.rw_min_bs; |
| 517 | 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] | 518 | f->file_map = smalloc(num_maps * sizeof(long)); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 519 | if (!f->file_map) { |
| 520 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); |
| 521 | return 1; |
| 522 | } |
| 523 | f->num_maps = num_maps; |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 529 | void close_files(struct thread_data *td) |
| 530 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 531 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 532 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 533 | |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 534 | for_each_file(td, f, i) |
| 535 | td_io_close_file(td, f); |
| 536 | } |
| 537 | |
| 538 | void close_and_free_files(struct thread_data *td) |
| 539 | { |
| 540 | struct fio_file *f; |
| 541 | unsigned int i; |
| 542 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 543 | dprint(FD_FILE, "close files\n"); |
| 544 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 545 | for_each_file(td, f, i) { |
Jens Axboe | a9b7b30 | 2007-04-19 16:31:16 +0200 | [diff] [blame] | 546 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 547 | unlink(f->file_name); |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 548 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 549 | td_io_close_file(td, f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 550 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 551 | sfree(f->file_name); |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 552 | f->file_name = NULL; |
| 553 | |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 554 | if (f->file_map) { |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 555 | sfree(f->file_map); |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 556 | f->file_map = NULL; |
| 557 | } |
Jens Axboe | 78d99e6 | 2008-03-01 18:41:50 +0100 | [diff] [blame] | 558 | sfree(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 559 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 560 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 561 | td->o.filename = NULL; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 562 | free(td->files); |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 563 | td->files = NULL; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 564 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 565 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 566 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 567 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 568 | { |
| 569 | struct stat sb; |
| 570 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 571 | if (!strcmp(f->file_name, "-")) |
| 572 | f->filetype = FIO_TYPE_PIPE; |
| 573 | else |
| 574 | f->filetype = FIO_TYPE_FILE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 575 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 576 | if (!lstat(f->file_name, &sb)) { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 577 | if (S_ISBLK(sb.st_mode)) |
| 578 | f->filetype = FIO_TYPE_BD; |
| 579 | else if (S_ISCHR(sb.st_mode)) |
| 580 | f->filetype = FIO_TYPE_CHAR; |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 581 | else if (S_ISFIFO(sb.st_mode)) |
| 582 | f->filetype = FIO_TYPE_PIPE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 583 | } |
| 584 | } |
| 585 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 586 | int add_file(struct thread_data *td, const char *fname) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 587 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 588 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 589 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 590 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 591 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 592 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 593 | dprint(FD_FILE, "add file %s\n", fname); |
| 594 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 595 | f = smalloc(sizeof(*f)); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 596 | f->fd = -1; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 597 | |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 598 | td->files = realloc(td->files, (cur_files + 1) * sizeof(f)); |
| 599 | |
| 600 | td->files[cur_files] = f; |
| 601 | |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 602 | /* |
| 603 | * init function, io engine may not be loaded yet |
| 604 | */ |
| 605 | if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) |
| 606 | f->real_file_size = -1ULL; |
| 607 | |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 608 | if (td->o.directory) |
| 609 | len = sprintf(file_name, "%s/", td->o.directory); |
| 610 | |
| 611 | sprintf(file_name + len, "%s", fname); |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 612 | f->file_name = smalloc_strdup(file_name); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 613 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 614 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 615 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 616 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 617 | if (f->filetype == FIO_TYPE_FILE) |
| 618 | td->nr_normal_files++; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 619 | |
| 620 | return cur_files; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 621 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 622 | |
| 623 | void get_file(struct fio_file *f) |
| 624 | { |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 625 | 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] | 626 | assert(f->flags & FIO_FILE_OPEN); |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 627 | f->references++; |
| 628 | } |
| 629 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 630 | int put_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 631 | { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 632 | int ret = 0; |
| 633 | |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 634 | 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] | 635 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 636 | if (!(f->flags & FIO_FILE_OPEN)) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 637 | return 0; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 638 | |
| 639 | assert(f->references); |
| 640 | if (--f->references) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 641 | return 0; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 642 | |
Jens Axboe | d424d4d | 2007-04-17 09:05:10 +0200 | [diff] [blame] | 643 | if (should_fsync(td) && td->o.fsync_on_close) |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 644 | fsync(f->fd); |
| 645 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 646 | if (td->io_ops->close_file) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 647 | ret = td->io_ops->close_file(td, f); |
Jens Axboe | 1020a13 | 2007-03-29 11:15:06 +0200 | [diff] [blame] | 648 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 649 | td->nr_open_files--; |
| 650 | f->flags &= ~FIO_FILE_OPEN; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 651 | return ret; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 652 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 653 | |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 654 | void lock_file(struct thread_data *td, struct fio_file *f) |
| 655 | { |
| 656 | } |
| 657 | |
| 658 | void unlock_file(struct fio_file *f) |
| 659 | { |
| 660 | } |
| 661 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 662 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 663 | { |
| 664 | struct dirent *dir; |
| 665 | int ret = 0; |
| 666 | DIR *D; |
| 667 | |
| 668 | D = opendir(dirname); |
| 669 | if (!D) { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 670 | char buf[FIO_VERROR_SIZE]; |
| 671 | |
| 672 | snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname); |
| 673 | td_verror(td, errno, buf); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 674 | return 1; |
| 675 | } |
| 676 | |
| 677 | while ((dir = readdir(D)) != NULL) { |
| 678 | char full_path[PATH_MAX]; |
| 679 | struct stat sb; |
| 680 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 681 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 682 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 683 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 684 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
| 685 | |
| 686 | if (lstat(full_path, &sb) == -1) { |
| 687 | if (errno != ENOENT) { |
| 688 | td_verror(td, errno, "stat"); |
| 689 | return 1; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (S_ISREG(sb.st_mode)) { |
| 694 | add_file(td, full_path); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 695 | td->o.nr_files++; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 696 | continue; |
| 697 | } |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 698 | if (!S_ISDIR(sb.st_mode)) |
| 699 | continue; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 700 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 701 | if ((ret = recurse_dir(td, full_path)) != 0) |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | closedir(D); |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | int add_dir_files(struct thread_data *td, const char *path) |
| 710 | { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 711 | int ret = recurse_dir(td, path); |
| 712 | |
| 713 | if (!ret) |
| 714 | log_info("fio: opendir added %d files\n", td->o.nr_files); |
| 715 | |
| 716 | return ret; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 717 | } |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 718 | |
| 719 | void dup_files(struct thread_data *td, struct thread_data *org) |
| 720 | { |
| 721 | struct fio_file *f; |
| 722 | unsigned int i; |
| 723 | size_t bytes; |
| 724 | |
| 725 | if (!org->files) |
| 726 | return; |
| 727 | |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 728 | bytes = org->files_index * sizeof(f); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 729 | td->files = malloc(bytes); |
| 730 | memcpy(td->files, org->files, bytes); |
| 731 | |
| 732 | for_each_file(td, f, i) { |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 733 | struct fio_file *__f; |
| 734 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 735 | __f = smalloc(sizeof(*__f)); |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 736 | |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 737 | if (f->file_name) |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 738 | __f->file_name = smalloc_strdup(f->file_name); |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 739 | |
| 740 | td->files[i] = __f; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 743 | |
| 744 | /* |
| 745 | * Returns the index that matches the filename, or -1 if not there |
| 746 | */ |
| 747 | int get_fileno(struct thread_data *td, const char *fname) |
| 748 | { |
| 749 | struct fio_file *f; |
| 750 | unsigned int i; |
| 751 | |
| 752 | for_each_file(td, f, i) |
| 753 | if (!strcmp(f->file_name, fname)) |
| 754 | return i; |
| 755 | |
| 756 | return -1; |
| 757 | } |
| 758 | |
| 759 | /* |
| 760 | * For log usage, where we add/open/close files automatically |
| 761 | */ |
| 762 | void free_release_files(struct thread_data *td) |
| 763 | { |
| 764 | close_files(td); |
| 765 | td->files_index = 0; |
| 766 | td->nr_normal_files = 0; |
| 767 | } |