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