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> |
YAMAMOTO Takashi | d74ac84 | 2010-06-09 09:44:53 +0200 | [diff] [blame] | 6 | #include <libgen.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | #include <sys/mman.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 9 | #include <sys/types.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 10 | |
| 11 | #include "fio.h" |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 12 | #include "smalloc.h" |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 13 | #include "filehash.h" |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 14 | #include "options.h" |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 15 | #include "os/os.h" |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 16 | #include "hash.h" |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 17 | #include "lib/axmap.h" |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 18 | |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 19 | #ifdef CONFIG_LINUX_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 20 | #include <linux/falloc.h> |
| 21 | #endif |
| 22 | |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 23 | static int root_warn; |
| 24 | |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 25 | static FLIST_HEAD(filename_list); |
| 26 | |
Jens Axboe | c592b9f | 2009-06-03 09:29:28 +0200 | [diff] [blame] | 27 | static inline void clear_error(struct thread_data *td) |
| 28 | { |
| 29 | td->error = 0; |
| 30 | td->verror[0] = '\0'; |
| 31 | } |
| 32 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 33 | /* |
| 34 | * Leaves f->fd open on success, caller must close |
| 35 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 36 | static int extend_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 37 | { |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 38 | int r, new_layout = 0, unlink_file = 0, flags; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 39 | unsigned long long left; |
| 40 | unsigned int bs; |
Jens Axboe | f3e1eb2 | 2014-04-11 11:28:54 -0600 | [diff] [blame] | 41 | char *b = NULL; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 42 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 43 | if (read_only) { |
| 44 | log_err("fio: refusing extend of file due to read-only\n"); |
| 45 | return 0; |
| 46 | } |
| 47 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 48 | /* |
| 49 | * check if we need to lay the file out complete again. fio |
| 50 | * does that for operations involving reads, or for writes |
| 51 | * where overwrite is set |
| 52 | */ |
Jens Axboe | 1417dae | 2014-03-20 09:33:13 -0600 | [diff] [blame] | 53 | if (td_read(td) || |
| 54 | (td_write(td) && td->o.overwrite && !td->o.file_append) || |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 55 | (td_write(td) && td->io_ops->flags & FIO_NOEXTEND)) |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 56 | new_layout = 1; |
Jens Axboe | 1417dae | 2014-03-20 09:33:13 -0600 | [diff] [blame] | 57 | if (td_write(td) && !td->o.overwrite && !td->o.file_append) |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 58 | unlink_file = 1; |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 59 | |
Jens Axboe | 6ae1f57 | 2008-02-21 10:20:00 +0100 | [diff] [blame] | 60 | if (unlink_file || new_layout) { |
Jens Axboe | bd199f2 | 2008-03-26 09:57:18 +0100 | [diff] [blame] | 61 | dprint(FD_FILE, "layout unlink %s\n", f->file_name); |
Castor Fu | 9187a26 | 2014-08-19 09:28:53 -0700 | [diff] [blame] | 62 | if ((td_io_unlink_file(td, f) < 0) && (errno != ENOENT)) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 63 | td_verror(td, errno, "unlink"); |
| 64 | return 1; |
| 65 | } |
| 66 | } |
| 67 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 68 | flags = O_WRONLY | O_CREAT; |
| 69 | if (new_layout) |
| 70 | flags |= O_TRUNC; |
| 71 | |
Bruce Cran | 9c0f3f3 | 2014-04-28 15:54:43 -0600 | [diff] [blame] | 72 | #ifdef WIN32 |
| 73 | flags |= _O_BINARY; |
| 74 | #endif |
| 75 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 76 | 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] | 77 | f->fd = open(f->file_name, flags, 0644); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 78 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 79 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 80 | return 1; |
| 81 | } |
| 82 | |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_POSIX_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 84 | if (!td->o.fill_device) { |
| 85 | switch (td->o.fallocate_mode) { |
| 86 | case FIO_FALLOCATE_NONE: |
| 87 | break; |
| 88 | case FIO_FALLOCATE_POSIX: |
| 89 | dprint(FD_FILE, "posix_fallocate file %s size %llu\n", |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 90 | f->file_name, |
| 91 | (unsigned long long) f->real_file_size); |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 92 | |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 93 | r = posix_fallocate(f->fd, 0, f->real_file_size); |
| 94 | if (r > 0) { |
| 95 | log_err("fio: posix_fallocate fails: %s\n", |
| 96 | strerror(r)); |
| 97 | } |
| 98 | break; |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 99 | #ifdef CONFIG_LINUX_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 100 | case FIO_FALLOCATE_KEEP_SIZE: |
| 101 | dprint(FD_FILE, |
| 102 | "fallocate(FALLOC_FL_KEEP_SIZE) " |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 103 | "file %s size %llu\n", f->file_name, |
| 104 | (unsigned long long) f->real_file_size); |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 105 | |
| 106 | r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, |
| 107 | f->real_file_size); |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 108 | if (r != 0) |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 109 | td_verror(td, errno, "fallocate"); |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 110 | |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 111 | break; |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 112 | #endif /* CONFIG_LINUX_FALLOCATE */ |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 113 | default: |
| 114 | log_err("fio: unknown fallocate mode: %d\n", |
| 115 | td->o.fallocate_mode); |
| 116 | assert(0); |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
Jens Axboe | 97ac992 | 2013-01-24 15:00:25 -0700 | [diff] [blame] | 119 | #endif /* CONFIG_POSIX_FALLOCATE */ |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 120 | |
Shawn Lewis | fc74ac1 | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 121 | if (!new_layout) |
| 122 | goto done; |
| 123 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 124 | /* |
| 125 | * The size will be -1ULL when fill_device is used, so don't truncate |
| 126 | * or fallocate this file, just write it |
| 127 | */ |
| 128 | if (!td->o.fill_device) { |
| 129 | dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name, |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 130 | (unsigned long long) f->real_file_size); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 131 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
John | 3cd4c66 | 2013-12-29 19:20:35 -0700 | [diff] [blame] | 132 | if (errno != EFBIG) { |
| 133 | td_verror(td, errno, "ftruncate"); |
| 134 | goto err; |
| 135 | } |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 136 | } |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 137 | } |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 138 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 139 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 140 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 141 | left = f->real_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 142 | while (left && !td->terminate) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 143 | bs = td->o.max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 144 | if (bs > left) |
| 145 | bs = left; |
| 146 | |
Jens Axboe | cc86c39 | 2013-05-03 15:12:33 +0200 | [diff] [blame] | 147 | fill_io_buffer(td, b, bs, bs); |
| 148 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 149 | r = write(f->fd, b, bs); |
| 150 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 151 | if (r > 0) { |
| 152 | left -= r; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 153 | continue; |
| 154 | } else { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 155 | if (r < 0) { |
| 156 | int __e = errno; |
| 157 | |
| 158 | if (__e == ENOSPC) { |
| 159 | if (td->o.fill_device) |
| 160 | break; |
| 161 | log_info("fio: ENOSPC on laying out " |
| 162 | "file, stopping\n"); |
| 163 | break; |
| 164 | } |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 165 | td_verror(td, errno, "write"); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 166 | } else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 167 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 168 | |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 173 | if (td->terminate) { |
| 174 | dprint(FD_FILE, "terminate unlink %s\n", f->file_name); |
Castor Fu | 9187a26 | 2014-08-19 09:28:53 -0700 | [diff] [blame] | 175 | td_io_unlink_file(td, f); |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 176 | } else if (td->o.create_fsync) { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 177 | if (fsync(f->fd) < 0) { |
| 178 | td_verror(td, errno, "fsync"); |
| 179 | goto err; |
| 180 | } |
| 181 | } |
Jens Axboe | 0d1cd20 | 2009-06-05 22:11:52 +0200 | [diff] [blame] | 182 | if (td->o.fill_device && !td_write(td)) { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 183 | fio_file_clear_size_known(f); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 184 | if (td_io_get_file_size(td, f)) |
| 185 | goto err; |
| 186 | if (f->io_size > f->real_file_size) |
| 187 | f->io_size = f->real_file_size; |
| 188 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 189 | |
| 190 | free(b); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 191 | done: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 192 | return 0; |
| 193 | err: |
| 194 | close(f->fd); |
| 195 | f->fd = -1; |
Jens Axboe | f3e1eb2 | 2014-04-11 11:28:54 -0600 | [diff] [blame] | 196 | if (b) |
| 197 | free(b); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 198 | return 1; |
| 199 | } |
| 200 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 201 | static int pre_read_file(struct thread_data *td, struct fio_file *f) |
| 202 | { |
Jens Axboe | ef799a6 | 2014-04-01 17:01:30 -0600 | [diff] [blame] | 203 | int ret = 0, r, did_open = 0, old_runstate; |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 204 | unsigned long long left; |
| 205 | unsigned int bs; |
| 206 | char *b; |
| 207 | |
Jens Axboe | 9c0d224 | 2009-07-01 12:26:28 +0200 | [diff] [blame] | 208 | if (td->io_ops->flags & FIO_PIPEIO) |
| 209 | return 0; |
| 210 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 211 | if (!fio_file_open(f)) { |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 212 | if (td->io_ops->open_file(td, f)) { |
| 213 | log_err("fio: cannot pre-read, failed to open file\n"); |
| 214 | return 1; |
| 215 | } |
| 216 | did_open = 1; |
| 217 | } |
| 218 | |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 219 | old_runstate = td_bump_runstate(td, TD_PRE_READING); |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 220 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 221 | bs = td->o.max_bs[DDIR_READ]; |
| 222 | b = malloc(bs); |
| 223 | memset(b, 0, bs); |
| 224 | |
Jens Axboe | ef799a6 | 2014-04-01 17:01:30 -0600 | [diff] [blame] | 225 | if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) { |
| 226 | td_verror(td, errno, "lseek"); |
| 227 | log_err("fio: failed to lseek pre-read file\n"); |
| 228 | ret = 1; |
| 229 | goto error; |
| 230 | } |
| 231 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 232 | left = f->io_size; |
| 233 | |
| 234 | while (left && !td->terminate) { |
| 235 | if (bs > left) |
| 236 | bs = left; |
| 237 | |
| 238 | r = read(f->fd, b, bs); |
| 239 | |
| 240 | if (r == (int) bs) { |
| 241 | left -= bs; |
| 242 | continue; |
| 243 | } else { |
| 244 | td_verror(td, EIO, "pre_read"); |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
Jens Axboe | ef799a6 | 2014-04-01 17:01:30 -0600 | [diff] [blame] | 249 | error: |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 250 | td_restore_runstate(td, old_runstate); |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 251 | |
| 252 | if (did_open) |
| 253 | td->io_ops->close_file(td, f); |
Jens Axboe | ef799a6 | 2014-04-01 17:01:30 -0600 | [diff] [blame] | 254 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 255 | free(b); |
Jens Axboe | ef799a6 | 2014-04-01 17:01:30 -0600 | [diff] [blame] | 256 | return ret; |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 259 | static unsigned long long get_rand_file_size(struct thread_data *td) |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 260 | { |
Jens Axboe | dc873b6 | 2008-06-04 20:13:04 +0200 | [diff] [blame] | 261 | unsigned long long ret, sized; |
Jens Axboe | 1294c3e | 2011-05-11 08:15:18 +0200 | [diff] [blame] | 262 | unsigned long r; |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 263 | |
Jens Axboe | f678701 | 2014-11-05 18:39:23 -0700 | [diff] [blame] | 264 | r = __rand(&td->file_size_state); |
Jens Axboe | 559073f | 2014-11-05 18:34:02 -0700 | [diff] [blame] | 265 | sized = td->o.file_size_high - td->o.file_size_low; |
| 266 | ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0))); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 267 | ret += td->o.file_size_low; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 268 | ret -= (ret % td->o.rw_min_bs); |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 269 | return ret; |
| 270 | } |
| 271 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 272 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 273 | { |
| 274 | struct stat st; |
| 275 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 276 | if (stat(f->file_name, &st) == -1) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 277 | td_verror(td, errno, "fstat"); |
| 278 | return 1; |
| 279 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 280 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 281 | f->real_file_size = st.st_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 286 | { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 287 | unsigned long long bytes = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 288 | int r; |
| 289 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 290 | if (td->io_ops->open_file(td, f)) { |
| 291 | log_err("fio: failed opening blockdev %s for size check\n", |
| 292 | f->file_name); |
| 293 | return 1; |
| 294 | } |
| 295 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 296 | r = blockdev_size(f, &bytes); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 297 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 298 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 299 | goto err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Jens Axboe | 7ab077a | 2009-02-02 15:07:37 +0100 | [diff] [blame] | 302 | if (!bytes) { |
| 303 | log_err("%s: zero sized block device?\n", f->file_name); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 304 | goto err; |
Jens Axboe | 7ab077a | 2009-02-02 15:07:37 +0100 | [diff] [blame] | 305 | } |
| 306 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 307 | f->real_file_size = bytes; |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 308 | td->io_ops->close_file(td, f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 309 | return 0; |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 310 | err: |
| 311 | td->io_ops->close_file(td, f); |
| 312 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 313 | } |
| 314 | |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 315 | static int char_size(struct thread_data *td, struct fio_file *f) |
| 316 | { |
| 317 | #ifdef FIO_HAVE_CHARDEV_SIZE |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 318 | unsigned long long bytes = 0; |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 319 | int r; |
| 320 | |
| 321 | if (td->io_ops->open_file(td, f)) { |
| 322 | log_err("fio: failed opening blockdev %s for size check\n", |
| 323 | f->file_name); |
| 324 | return 1; |
| 325 | } |
| 326 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 327 | r = chardev_size(f, &bytes); |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 328 | if (r) { |
| 329 | td_verror(td, r, "chardev_size"); |
| 330 | goto err; |
| 331 | } |
| 332 | |
| 333 | if (!bytes) { |
| 334 | log_err("%s: zero sized char device?\n", f->file_name); |
| 335 | goto err; |
| 336 | } |
| 337 | |
| 338 | f->real_file_size = bytes; |
| 339 | td->io_ops->close_file(td, f); |
| 340 | return 0; |
| 341 | err: |
| 342 | td->io_ops->close_file(td, f); |
| 343 | return 1; |
| 344 | #else |
| 345 | f->real_file_size = -1ULL; |
| 346 | return 0; |
| 347 | #endif |
| 348 | } |
| 349 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 350 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 351 | { |
| 352 | int ret = 0; |
| 353 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 354 | if (fio_file_size_known(f)) |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 355 | return 0; |
| 356 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 357 | if (f->filetype == FIO_TYPE_FILE) |
| 358 | ret = file_size(td, f); |
| 359 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 360 | ret = bdev_size(td, f); |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 361 | else if (f->filetype == FIO_TYPE_CHAR) |
| 362 | ret = char_size(td, f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 363 | else |
| 364 | f->real_file_size = -1; |
| 365 | |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | |
| 369 | if (f->file_offset > f->real_file_size) { |
Bruce Cran | 0f2152c | 2010-12-15 10:33:03 +0100 | [diff] [blame] | 370 | log_err("%s: offset extends end (%llu > %llu)\n", td->o.name, |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 371 | (unsigned long long) f->file_offset, |
| 372 | (unsigned long long) f->real_file_size); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 373 | return 1; |
| 374 | } |
| 375 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 376 | fio_file_set_size_known(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 380 | static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f, |
| 381 | unsigned long long off, |
| 382 | unsigned long long len) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 383 | { |
| 384 | int ret = 0; |
| 385 | |
Jens Axboe | ef7035a | 2014-06-18 15:30:09 -0700 | [diff] [blame] | 386 | #ifdef CONFIG_ESX |
| 387 | return 0; |
| 388 | #endif |
| 389 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 390 | if (len == -1ULL) |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 391 | len = f->io_size; |
| 392 | if (off == -1ULL) |
| 393 | off = f->file_offset; |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 394 | |
Jens Axboe | 0d1cd20 | 2009-06-05 22:11:52 +0200 | [diff] [blame] | 395 | if (len == -1ULL || off == -1ULL) |
| 396 | return 0; |
| 397 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 398 | dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off, |
| 399 | len); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 400 | |
Jens Axboe | 1be9f21 | 2014-05-19 19:57:05 -0600 | [diff] [blame] | 401 | if (td->io_ops->invalidate) |
| 402 | ret = td->io_ops->invalidate(td, f); |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 403 | else if (f->filetype == FIO_TYPE_FILE) |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 404 | ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED); |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 405 | else if (f->filetype == FIO_TYPE_BD) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 406 | ret = blockdev_invalidate_cache(f); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 407 | if (ret < 0 && errno == EACCES && geteuid()) { |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 408 | if (!root_warn) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 409 | log_err("fio: only root may flush block " |
| 410 | "devices. Cache flush bypassed!\n"); |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 411 | root_warn = 1; |
| 412 | } |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 413 | ret = 0; |
| 414 | } |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 415 | } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 416 | ret = 0; |
| 417 | |
Jens Axboe | dfe11fd | 2014-04-10 08:59:43 -0600 | [diff] [blame] | 418 | /* |
| 419 | * Cache flushing isn't a fatal condition, and we know it will |
| 420 | * happen on some platforms where we don't have the proper |
| 421 | * function to flush eg block device caches. So just warn and |
| 422 | * continue on our way. |
| 423 | */ |
| 424 | if (ret) { |
| 425 | log_info("fio: cache invalidation of %s failed: %s\n", f->file_name, strerror(errno)); |
| 426 | ret = 0; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Jens Axboe | dfe11fd | 2014-04-10 08:59:43 -0600 | [diff] [blame] | 429 | return 0; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 430 | |
| 431 | } |
| 432 | |
| 433 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 434 | { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 435 | if (!fio_file_open(f)) |
Jens Axboe | a5fb461 | 2008-05-28 10:54:01 +0200 | [diff] [blame] | 436 | return 0; |
| 437 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 438 | return __file_invalidate_cache(td, f, -1ULL, -1ULL); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 439 | } |
| 440 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 441 | 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] | 442 | { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 443 | int ret = 0; |
| 444 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 445 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 446 | |
| 447 | remove_file_hash(f); |
| 448 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 449 | if (close(f->fd) < 0) |
| 450 | ret = errno; |
| 451 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 452 | f->fd = -1; |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 453 | |
| 454 | if (f->shadow_fd != -1) { |
| 455 | close(f->shadow_fd); |
| 456 | f->shadow_fd = -1; |
| 457 | } |
| 458 | |
Juan Casse | 57e54e0 | 2013-09-20 09:20:52 -0600 | [diff] [blame] | 459 | f->engine_data = 0; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 460 | return ret; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 461 | } |
| 462 | |
Dmitry Monakhov | 1ccc6dc | 2012-09-19 23:22:53 +0400 | [diff] [blame] | 463 | int file_lookup_open(struct fio_file *f, int flags) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 464 | { |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 465 | struct fio_file *__f; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 466 | int from_hash; |
| 467 | |
| 468 | __f = lookup_file_hash(f->file_name); |
| 469 | if (__f) { |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 470 | dprint(FD_FILE, "found file in hash %s\n", f->file_name); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 471 | /* |
| 472 | * racy, need the __f->lock locked |
| 473 | */ |
| 474 | f->lock = __f->lock; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 475 | from_hash = 1; |
| 476 | } else { |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 477 | 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] | 478 | from_hash = 0; |
| 479 | } |
| 480 | |
Bruce Cran | 9c0f3f3 | 2014-04-28 15:54:43 -0600 | [diff] [blame] | 481 | #ifdef WIN32 |
| 482 | flags |= _O_BINARY; |
| 483 | #endif |
| 484 | |
Jens Axboe | e8670ef | 2008-03-06 12:06:30 +0100 | [diff] [blame] | 485 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 486 | return from_hash; |
| 487 | } |
| 488 | |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 489 | static int file_close_shadow_fds(struct thread_data *td) |
| 490 | { |
| 491 | struct fio_file *f; |
| 492 | int num_closed = 0; |
| 493 | unsigned int i; |
| 494 | |
| 495 | for_each_file(td, f, i) { |
| 496 | if (f->shadow_fd == -1) |
| 497 | continue; |
| 498 | |
| 499 | close(f->shadow_fd); |
| 500 | f->shadow_fd = -1; |
| 501 | num_closed++; |
| 502 | } |
| 503 | |
| 504 | return num_closed; |
| 505 | } |
| 506 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 507 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
| 508 | { |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 509 | int is_std = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 510 | int flags = 0; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 511 | int from_hash = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 512 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 513 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 514 | |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 515 | if (td_trim(td) && f->filetype != FIO_TYPE_BD) { |
| 516 | log_err("fio: trim only applies to block device\n"); |
| 517 | return 1; |
| 518 | } |
| 519 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 520 | if (!strcmp(f->file_name, "-")) { |
| 521 | if (td_rw(td)) { |
| 522 | log_err("fio: can't read/write to stdin/out\n"); |
| 523 | return 1; |
| 524 | } |
| 525 | is_std = 1; |
Jens Axboe | ce98fa6 | 2007-04-17 13:27:32 +0200 | [diff] [blame] | 526 | |
| 527 | /* |
| 528 | * move output logging to stderr, if we are writing to stdout |
| 529 | */ |
| 530 | if (td_write(td)) |
| 531 | f_out = stderr; |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 532 | } |
| 533 | |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 534 | if (td_trim(td)) |
| 535 | goto skip_flags; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 536 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 537 | flags |= OS_O_DIRECT; |
Chris Mason | d01612f | 2013-11-15 15:52:58 -0700 | [diff] [blame] | 538 | if (td->o.oatomic) { |
| 539 | if (!FIO_O_ATOMIC) { |
| 540 | td_verror(td, EINVAL, "OS does not support atomic IO"); |
| 541 | return 1; |
| 542 | } |
| 543 | flags |= OS_O_DIRECT | FIO_O_ATOMIC; |
| 544 | } |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 545 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 546 | flags |= O_SYNC; |
Jens Axboe | 814452b | 2009-03-04 12:53:13 +0100 | [diff] [blame] | 547 | if (td->o.create_on_open) |
| 548 | flags |= O_CREAT; |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 549 | skip_flags: |
| 550 | if (f->filetype != FIO_TYPE_FILE) |
| 551 | flags |= FIO_O_NOATIME; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 552 | |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 553 | open_again: |
Jens Axboe | 660a1cb | 2007-04-17 15:37:47 +0200 | [diff] [blame] | 554 | if (td_write(td)) { |
Jens Axboe | 1730815 | 2008-03-07 13:42:31 +0100 | [diff] [blame] | 555 | if (!read_only) |
| 556 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 557 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 558 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 559 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 560 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 561 | if (is_std) |
| 562 | f->fd = dup(STDOUT_FILENO); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 563 | else |
| 564 | from_hash = file_lookup_open(f, flags); |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 565 | } else if (td_read(td)) { |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 566 | if (f->filetype == FIO_TYPE_CHAR && !read_only) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 567 | flags |= O_RDWR; |
| 568 | else |
| 569 | flags |= O_RDONLY; |
| 570 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 571 | if (is_std) |
| 572 | f->fd = dup(STDIN_FILENO); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 573 | else |
| 574 | from_hash = file_lookup_open(f, flags); |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 575 | } else { //td trim |
| 576 | flags |= O_RDWR; |
| 577 | from_hash = file_lookup_open(f, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 581 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 582 | int __e = errno; |
| 583 | |
Jens Axboe | 835d9b9 | 2009-06-09 15:23:38 +0200 | [diff] [blame] | 584 | if (__e == EPERM && (flags & FIO_O_NOATIME)) { |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 585 | flags &= ~FIO_O_NOATIME; |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 586 | goto open_again; |
| 587 | } |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 588 | if (__e == EMFILE && file_close_shadow_fds(td)) |
| 589 | goto open_again; |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 590 | |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 591 | snprintf(buf, sizeof(buf), "open(%s)", f->file_name); |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 592 | |
Jens Axboe | a93c5f0 | 2012-06-11 08:53:53 +0200 | [diff] [blame] | 593 | if (__e == EINVAL && (flags & OS_O_DIRECT)) { |
| 594 | log_err("fio: looks like your file system does not " \ |
| 595 | "support direct=1/buffered=0\n"); |
| 596 | } |
| 597 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 598 | td_verror(td, __e, buf); |
Andreas Gruenbacher | 34329ca | 2014-07-09 19:37:33 +0200 | [diff] [blame] | 599 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 600 | } |
| 601 | |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 602 | if (!from_hash && f->fd != -1) { |
| 603 | if (add_file_hash(f)) { |
Jens Axboe | 3f0ca9b | 2011-05-24 21:36:24 +0200 | [diff] [blame] | 604 | int fio_unused ret; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 605 | |
| 606 | /* |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 607 | * Stash away descriptor for later close. This is to |
| 608 | * work-around a "feature" on Linux, where a close of |
| 609 | * an fd that has been opened for write will trigger |
| 610 | * udev to call blkid to check partitions, fs id, etc. |
Anatol Pomozov | de8f6de | 2013-09-26 16:31:34 -0700 | [diff] [blame] | 611 | * That pollutes the device cache, which can slow down |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 612 | * unbuffered accesses. |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 613 | */ |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 614 | if (f->shadow_fd == -1) |
| 615 | f->shadow_fd = f->fd; |
| 616 | else { |
| 617 | /* |
| 618 | * OK to ignore, we haven't done anything |
| 619 | * with it |
| 620 | */ |
| 621 | ret = generic_close_file(td, f); |
| 622 | } |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 623 | goto open_again; |
| 624 | } |
| 625 | } |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 626 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 627 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 628 | } |
| 629 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 630 | int generic_get_file_size(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 631 | { |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 632 | return get_file_size(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 635 | /* |
| 636 | * open/close all files, so that ->real_file_size gets set |
| 637 | */ |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 638 | static int get_file_sizes(struct thread_data *td) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 639 | { |
| 640 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 641 | unsigned int i; |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 642 | int err = 0; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 643 | |
| 644 | for_each_file(td, f, i) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 645 | dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i, |
| 646 | f->file_name); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 647 | |
Jens Axboe | 99a47c6 | 2009-04-07 22:20:56 +0200 | [diff] [blame] | 648 | if (td_io_get_file_size(td, f)) { |
Jens Axboe | 40b44f4 | 2007-04-11 12:43:35 +0200 | [diff] [blame] | 649 | if (td->error != ENOENT) { |
| 650 | log_err("%s\n", td->verror); |
| 651 | err = 1; |
Andreas Gruenbacher | 34329ca | 2014-07-09 19:37:33 +0200 | [diff] [blame] | 652 | break; |
Jens Axboe | 40b44f4 | 2007-04-11 12:43:35 +0200 | [diff] [blame] | 653 | } |
Jens Axboe | 541d66d | 2007-03-27 19:50:11 +0200 | [diff] [blame] | 654 | clear_error(td); |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 655 | } |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 656 | |
| 657 | if (f->real_file_size == -1ULL && td->o.size) |
| 658 | f->real_file_size = td->o.size / td->o.nr_files; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 659 | } |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 660 | |
| 661 | return err; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 662 | } |
| 663 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 664 | struct fio_mount { |
| 665 | struct flist_head list; |
| 666 | const char *base; |
| 667 | char __base[256]; |
| 668 | unsigned int key; |
| 669 | }; |
| 670 | |
| 671 | /* |
| 672 | * Get free number of bytes for each file on each unique mount. |
| 673 | */ |
| 674 | static unsigned long long get_fs_free_counts(struct thread_data *td) |
| 675 | { |
| 676 | struct flist_head *n, *tmp; |
Jens Axboe | 68b0316 | 2010-06-24 09:22:41 +0200 | [diff] [blame] | 677 | unsigned long long ret = 0; |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 678 | struct fio_mount *fm; |
| 679 | FLIST_HEAD(list); |
| 680 | struct fio_file *f; |
| 681 | unsigned int i; |
| 682 | |
| 683 | for_each_file(td, f, i) { |
| 684 | struct stat sb; |
| 685 | char buf[256]; |
| 686 | |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 687 | if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) { |
| 688 | if (f->real_file_size != -1ULL) |
| 689 | ret += f->real_file_size; |
Jens Axboe | 68b0316 | 2010-06-24 09:22:41 +0200 | [diff] [blame] | 690 | continue; |
| 691 | } else if (f->filetype != FIO_TYPE_FILE) |
| 692 | continue; |
| 693 | |
Jens Axboe | da27a4b | 2014-04-14 08:45:13 -0600 | [diff] [blame] | 694 | buf[255] = '\0'; |
| 695 | strncpy(buf, f->file_name, 255); |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 696 | |
| 697 | if (stat(buf, &sb) < 0) { |
| 698 | if (errno != ENOENT) |
| 699 | break; |
| 700 | strcpy(buf, "."); |
| 701 | if (stat(buf, &sb) < 0) |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | fm = NULL; |
| 706 | flist_for_each(n, &list) { |
| 707 | fm = flist_entry(n, struct fio_mount, list); |
| 708 | if (fm->key == sb.st_dev) |
| 709 | break; |
| 710 | |
| 711 | fm = NULL; |
| 712 | } |
| 713 | |
| 714 | if (fm) |
| 715 | continue; |
| 716 | |
Jens Axboe | 3660cea | 2014-04-15 08:18:08 -0600 | [diff] [blame] | 717 | fm = calloc(1, sizeof(*fm)); |
| 718 | strncpy(fm->__base, buf, sizeof(fm->__base) - 1); |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 719 | fm->base = basename(fm->__base); |
| 720 | fm->key = sb.st_dev; |
| 721 | flist_add(&fm->list, &list); |
| 722 | } |
| 723 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 724 | flist_for_each_safe(n, tmp, &list) { |
| 725 | unsigned long long sz; |
| 726 | |
| 727 | fm = flist_entry(n, struct fio_mount, list); |
| 728 | flist_del(&fm->list); |
| 729 | |
| 730 | sz = get_fs_size(fm->base); |
| 731 | if (sz && sz != -1ULL) |
| 732 | ret += sz; |
| 733 | |
| 734 | free(fm); |
| 735 | } |
| 736 | |
| 737 | return ret; |
| 738 | } |
| 739 | |
Jens Axboe | bedc9dc | 2014-03-17 12:51:09 -0600 | [diff] [blame] | 740 | uint64_t get_start_offset(struct thread_data *td, struct fio_file *f) |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 741 | { |
Jens Axboe | bedc9dc | 2014-03-17 12:51:09 -0600 | [diff] [blame] | 742 | struct thread_options *o = &td->o; |
| 743 | |
| 744 | if (o->file_append && f->filetype == FIO_TYPE_FILE) |
| 745 | return f->real_file_size; |
| 746 | |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 747 | return td->o.start_offset + |
Jiri Horky | 5a65b4e | 2014-07-25 09:55:03 +0200 | [diff] [blame] | 748 | td->subjob_number * td->o.offset_increment; |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 749 | } |
| 750 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 751 | /* |
| 752 | * Open the files and setup files sizes, creating files if necessary. |
| 753 | */ |
| 754 | int setup_files(struct thread_data *td) |
| 755 | { |
| 756 | unsigned long long total_size, extend_size; |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 757 | struct thread_options *o = &td->o; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 758 | struct fio_file *f; |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 759 | unsigned int i, nr_fs_extra = 0; |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 760 | int err = 0, need_extend; |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 761 | int old_state; |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 762 | const unsigned int bs = td_min_bs(td); |
| 763 | uint64_t fs = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 764 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 765 | dprint(FD_FILE, "setup files\n"); |
| 766 | |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 767 | old_state = td_bump_runstate(td, TD_SETTING_UP); |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 768 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 769 | if (o->read_iolog_file) |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 770 | goto done; |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 771 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 772 | /* |
| 773 | * if ioengine defines a setup() method, it's responsible for |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 774 | * opening the files and setting f->real_file_size to indicate |
| 775 | * the valid range for that file. |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 776 | */ |
| 777 | if (td->io_ops->setup) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 778 | err = td->io_ops->setup(td); |
| 779 | else |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 780 | err = get_file_sizes(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 781 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 782 | if (err) |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 783 | goto err_out; |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 784 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 785 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 786 | * check sizes. if the files/devices do not exist and the size |
| 787 | * isn't passed to fio, abort. |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 788 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 789 | total_size = 0; |
| 790 | for_each_file(td, f, i) { |
| 791 | if (f->real_file_size == -1ULL) |
| 792 | total_size = -1ULL; |
| 793 | else |
| 794 | total_size += f->real_file_size; |
| 795 | } |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 796 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 797 | if (o->fill_device) |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 798 | td->fill_device_size = get_fs_free_counts(td); |
| 799 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 800 | /* |
| 801 | * device/file sizes are zero and no size given, punt |
| 802 | */ |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 803 | if ((!total_size || total_size == -1ULL) && !o->size && |
| 804 | !(td->io_ops->flags & FIO_NOIO) && !o->fill_device && |
| 805 | !(o->nr_files && (o->file_size_low || o->file_size_high))) { |
| 806 | log_err("%s: you need to specify size=\n", o->name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 807 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 808 | goto err_out; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 809 | } |
| 810 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 811 | /* |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 812 | * Calculate per-file size and potential extra size for the |
| 813 | * first files, if needed. |
| 814 | */ |
Jens Axboe | d1e78e6 | 2014-04-06 09:56:10 -0600 | [diff] [blame] | 815 | if (!o->file_size_low && o->nr_files) { |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 816 | uint64_t all_fs; |
| 817 | |
| 818 | fs = o->size / o->nr_files; |
| 819 | all_fs = fs * o->nr_files; |
| 820 | |
| 821 | if (all_fs < o->size) |
| 822 | nr_fs_extra = (o->size - all_fs) / bs; |
| 823 | } |
| 824 | |
| 825 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 826 | * now file sizes are known, so we can set ->io_size. if size= is |
| 827 | * not given, ->io_size is just equal to ->real_file_size. if size |
| 828 | * is given, ->io_size is size / nr_files. |
| 829 | */ |
| 830 | extend_size = total_size = 0; |
| 831 | need_extend = 0; |
| 832 | for_each_file(td, f, i) { |
Jens Axboe | bedc9dc | 2014-03-17 12:51:09 -0600 | [diff] [blame] | 833 | f->file_offset = get_start_offset(td, f); |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 834 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 835 | if (!o->file_size_low) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 836 | /* |
| 837 | * no file size range given, file size is equal to |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 838 | * total size divided by number of files. If that is |
| 839 | * zero, set it to the real file size. If the size |
| 840 | * doesn't divide nicely with the min blocksize, |
| 841 | * make the first files bigger. |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 842 | */ |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 843 | f->io_size = fs; |
| 844 | if (nr_fs_extra) { |
| 845 | nr_fs_extra--; |
| 846 | f->io_size += bs; |
| 847 | } |
| 848 | |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 849 | if (!f->io_size) |
Jens Axboe | 273f8c9 | 2008-01-25 14:02:15 +0100 | [diff] [blame] | 850 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 851 | } else if (f->real_file_size < o->file_size_low || |
| 852 | f->real_file_size > o->file_size_high) { |
| 853 | if (f->file_offset > o->file_size_low) |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 854 | goto err_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 855 | /* |
| 856 | * file size given. if it's fixed, use that. if it's a |
| 857 | * range, generate a random size in-between. |
| 858 | */ |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 859 | if (o->file_size_low == o->file_size_high) |
| 860 | f->io_size = o->file_size_low - f->file_offset; |
| 861 | else { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 862 | f->io_size = get_rand_file_size(td) |
| 863 | - f->file_offset; |
| 864 | } |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 865 | } else |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 866 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 867 | |
| 868 | if (f->io_size == -1ULL) |
| 869 | total_size = -1ULL; |
Shaohua Li | 4d00256 | 2012-09-21 08:32:32 +0200 | [diff] [blame] | 870 | else { |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 871 | if (o->size_percent) |
| 872 | f->io_size = (f->io_size * o->size_percent) / 100; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 873 | total_size += f->io_size; |
Shaohua Li | 4d00256 | 2012-09-21 08:32:32 +0200 | [diff] [blame] | 874 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 875 | |
| 876 | if (f->filetype == FIO_TYPE_FILE && |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 877 | (f->io_size + f->file_offset) > f->real_file_size && |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 878 | !(td->io_ops->flags & FIO_DISKLESSIO)) { |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 879 | if (!o->create_on_open) { |
Jens Axboe | 814452b | 2009-03-04 12:53:13 +0100 | [diff] [blame] | 880 | need_extend++; |
| 881 | extend_size += (f->io_size + f->file_offset); |
| 882 | } else |
| 883 | f->real_file_size = f->io_size + f->file_offset; |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 884 | fio_file_set_extend(f); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 885 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 886 | } |
| 887 | |
Jens Axboe | a85066d | 2014-09-29 21:23:51 -0600 | [diff] [blame] | 888 | if (!o->size || (total_size && o->size > total_size)) |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 889 | o->size = total_size; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 890 | |
Jens Axboe | d1faa06 | 2014-04-16 23:51:27 +0200 | [diff] [blame] | 891 | if (o->size < td_min_bs(td)) { |
| 892 | log_err("fio: blocksize too large for data set\n"); |
| 893 | goto err_out; |
| 894 | } |
| 895 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 896 | /* |
| 897 | * See if we need to extend some files |
| 898 | */ |
| 899 | if (need_extend) { |
| 900 | temp_stall_ts = 1; |
Jens Axboe | f3afa57 | 2012-09-17 13:34:16 +0200 | [diff] [blame] | 901 | if (output_format == FIO_OUTPUT_NORMAL) |
Shaozhi Shawn Ye | a7ba8c5 | 2008-09-10 09:04:18 +0200 | [diff] [blame] | 902 | log_info("%s: Laying out IO file(s) (%u file(s) /" |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 903 | " %lluMB)\n", o->name, need_extend, |
Shaozhi Shawn Ye | a7ba8c5 | 2008-09-10 09:04:18 +0200 | [diff] [blame] | 904 | extend_size >> 20); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 905 | |
| 906 | for_each_file(td, f, i) { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 907 | unsigned long long old_len = -1ULL, extend_len = -1ULL; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 908 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 909 | if (!fio_file_extend(f)) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 910 | continue; |
| 911 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 912 | assert(f->filetype == FIO_TYPE_FILE); |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 913 | fio_file_clear_extend(f); |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 914 | if (!o->fill_device) { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 915 | old_len = f->real_file_size; |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 916 | extend_len = f->io_size + f->file_offset - |
| 917 | old_len; |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 918 | } |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 919 | f->real_file_size = (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 920 | err = extend_file(td, f); |
| 921 | if (err) |
| 922 | break; |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 923 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 924 | err = __file_invalidate_cache(td, f, old_len, |
| 925 | extend_len); |
Jens Axboe | 9824f73 | 2014-04-14 12:05:22 -0600 | [diff] [blame] | 926 | |
| 927 | /* |
| 928 | * Shut up static checker |
| 929 | */ |
| 930 | if (f->fd != -1) |
| 931 | close(f->fd); |
| 932 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 933 | f->fd = -1; |
| 934 | if (err) |
| 935 | break; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 936 | } |
| 937 | temp_stall_ts = 0; |
| 938 | } |
| 939 | |
| 940 | if (err) |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 941 | goto err_out; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 942 | |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 943 | if (!o->zone_size) |
| 944 | o->zone_size = o->size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 945 | |
Jens Axboe | ea966f8 | 2007-09-03 10:09:37 +0200 | [diff] [blame] | 946 | /* |
| 947 | * iolog already set the total io size, if we read back |
| 948 | * stored entries. |
| 949 | */ |
Jens Axboe | 77731b2 | 2014-04-28 12:08:47 -0600 | [diff] [blame] | 950 | if (!o->read_iolog_file) { |
| 951 | if (o->io_limit) |
| 952 | td->total_io_size = o->io_limit * o->loops; |
| 953 | else |
| 954 | td->total_io_size = o->size * o->loops; |
| 955 | } |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 956 | |
| 957 | done: |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 958 | if (o->create_only) |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 959 | td->done = 1; |
| 960 | |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 961 | td_restore_runstate(td, old_state); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 962 | return 0; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 963 | err_offset: |
Jens Axboe | de98bd3 | 2013-04-05 11:09:20 +0200 | [diff] [blame] | 964 | log_err("%s: you need to specify valid offset=\n", o->name); |
Jens Axboe | e90391e | 2013-04-13 20:40:53 +0200 | [diff] [blame] | 965 | err_out: |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 966 | td_restore_runstate(td, old_state); |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 967 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 968 | } |
| 969 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 970 | int pre_read_files(struct thread_data *td) |
| 971 | { |
| 972 | struct fio_file *f; |
| 973 | unsigned int i; |
| 974 | |
| 975 | dprint(FD_FILE, "pre_read files\n"); |
| 976 | |
| 977 | for_each_file(td, f, i) { |
| 978 | pre_read_file(td, f); |
| 979 | } |
| 980 | |
| 981 | return 1; |
| 982 | } |
| 983 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 984 | static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) |
| 985 | { |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 986 | unsigned int range_size, seed; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 987 | unsigned long nranges; |
Jens Axboe | 4d6922b | 2014-11-12 11:11:20 -0700 | [diff] [blame] | 988 | uint64_t fsize; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 989 | |
| 990 | range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); |
Jens Axboe | 4d6922b | 2014-11-12 11:11:20 -0700 | [diff] [blame] | 991 | fsize = min(f->real_file_size, f->io_size); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 992 | |
Jens Axboe | 4d6922b | 2014-11-12 11:11:20 -0700 | [diff] [blame] | 993 | nranges = (fsize + range_size - 1) / range_size; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 994 | |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 995 | seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; |
Jens Axboe | 8425687 | 2012-11-17 10:06:36 -0700 | [diff] [blame] | 996 | if (!td->o.rand_repeatable) |
| 997 | seed = td->rand_seeds[4]; |
| 998 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 999 | if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 1000 | zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 1001 | else |
Jens Axboe | 888677a | 2013-04-10 22:19:46 +0200 | [diff] [blame] | 1002 | pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 1003 | |
| 1004 | return 1; |
| 1005 | } |
| 1006 | |
| 1007 | static int init_rand_distribution(struct thread_data *td) |
| 1008 | { |
| 1009 | struct fio_file *f; |
| 1010 | unsigned int i; |
| 1011 | int state; |
| 1012 | |
| 1013 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) |
| 1014 | return 0; |
| 1015 | |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 1016 | state = td_bump_runstate(td, TD_SETTING_UP); |
| 1017 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 1018 | for_each_file(td, f, i) |
| 1019 | __init_rand_distribution(td, f); |
Jens Axboe | 8edd973 | 2014-03-01 08:24:03 -0700 | [diff] [blame] | 1020 | |
| 1021 | td_restore_runstate(td, state); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 1022 | |
| 1023 | return 1; |
| 1024 | } |
| 1025 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1026 | int init_random_map(struct thread_data *td) |
| 1027 | { |
Jens Axboe | 51ede0b | 2012-11-22 13:50:29 +0100 | [diff] [blame] | 1028 | unsigned long long blocks; |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1029 | struct fio_file *f; |
| 1030 | unsigned int i; |
| 1031 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 1032 | if (init_rand_distribution(td)) |
| 1033 | return 0; |
Jens Axboe | 3831a84 | 2012-11-26 19:59:14 +0100 | [diff] [blame] | 1034 | if (!td_random(td)) |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1035 | return 0; |
| 1036 | |
| 1037 | for_each_file(td, f, i) { |
Jens Axboe | 4d6922b | 2014-11-12 11:11:20 -0700 | [diff] [blame] | 1038 | uint64_t fsize = min(f->real_file_size, f->io_size); |
Jens Axboe | 38f30c8 | 2013-01-11 14:03:05 +0100 | [diff] [blame] | 1039 | |
Jens Axboe | 4d6922b | 2014-11-12 11:11:20 -0700 | [diff] [blame] | 1040 | blocks = fsize / (unsigned long long) td->o.rw_min_bs; |
Jens Axboe | 53737ae | 2013-02-21 15:18:17 +0100 | [diff] [blame] | 1041 | |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 1042 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) { |
Jens Axboe | 82af46b | 2012-12-04 13:12:25 +0100 | [diff] [blame] | 1043 | unsigned long seed; |
| 1044 | |
| 1045 | seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; |
Bruce Cran | 9c0f3f3 | 2014-04-28 15:54:43 -0600 | [diff] [blame] | 1046 | |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1047 | if (!lfsr_init(&f->lfsr, blocks, seed, 0)) { |
| 1048 | fio_file_set_lfsr(f); |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 1049 | continue; |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1050 | } |
Jens Axboe | 3831a84 | 2012-11-26 19:59:14 +0100 | [diff] [blame] | 1051 | } else if (!td->o.norandommap) { |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 1052 | f->io_axmap = axmap_new(blocks); |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1053 | if (f->io_axmap) { |
| 1054 | fio_file_set_axmap(f); |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 1055 | continue; |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1056 | } |
Jens Axboe | 1cad712 | 2012-11-29 21:37:11 +0100 | [diff] [blame] | 1057 | } else if (td->o.norandommap) |
| 1058 | continue; |
Jens Axboe | ceadd59 | 2012-02-02 08:41:28 +0100 | [diff] [blame] | 1059 | |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 1060 | if (!td->o.softrandommap) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1061 | log_err("fio: failed allocating random map. If running" |
| 1062 | " a large number of jobs, try the 'norandommap'" |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 1063 | " option or set 'softrandommap'. Or give" |
| 1064 | " a larger --alloc-size to fio.\n"); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1065 | return 1; |
| 1066 | } |
Jens Axboe | 303032a | 2008-03-26 10:11:10 +0100 | [diff] [blame] | 1067 | |
| 1068 | log_info("fio: file %s failed allocating random map. Running " |
| 1069 | "job without.\n", f->file_name); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1075 | void close_files(struct thread_data *td) |
| 1076 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 1077 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1078 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1079 | |
Jens Axboe | 2be3eec | 2009-06-10 09:05:13 +0200 | [diff] [blame] | 1080 | for_each_file(td, f, i) { |
| 1081 | if (fio_file_open(f)) |
| 1082 | td_io_close_file(td, f); |
| 1083 | } |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | void close_and_free_files(struct thread_data *td) |
| 1087 | { |
| 1088 | struct fio_file *f; |
| 1089 | unsigned int i; |
| 1090 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1091 | dprint(FD_FILE, "close files\n"); |
| 1092 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 1093 | for_each_file(td, f, i) { |
Castor Fu | 9187a26 | 2014-08-19 09:28:53 -0700 | [diff] [blame] | 1094 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
| 1095 | dprint(FD_FILE, "free unlink %s\n", f->file_name); |
| 1096 | td_io_unlink_file(td, f); |
| 1097 | } |
| 1098 | |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1099 | if (fio_file_open(f)) |
| 1100 | td_io_close_file(td, f); |
| 1101 | |
Shaozhi Shawn Ye | b9fbcf2 | 2008-09-10 09:05:17 +0200 | [diff] [blame] | 1102 | remove_file_hash(f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 1103 | |
Jens Axboe | b5f4d8b | 2014-03-20 08:28:11 -0600 | [diff] [blame] | 1104 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
| 1105 | dprint(FD_FILE, "free unlink %s\n", f->file_name); |
Castor Fu | 9187a26 | 2014-08-19 09:28:53 -0700 | [diff] [blame] | 1106 | td_io_unlink_file(td, f); |
Jens Axboe | b5f4d8b | 2014-03-20 08:28:11 -0600 | [diff] [blame] | 1107 | } |
| 1108 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1109 | sfree(f->file_name); |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 1110 | f->file_name = NULL; |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1111 | if (fio_file_axmap(f)) { |
| 1112 | axmap_free(f->io_axmap); |
| 1113 | f->io_axmap = NULL; |
| 1114 | } |
Jens Axboe | 78d99e6 | 2008-03-01 18:41:50 +0100 | [diff] [blame] | 1115 | sfree(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1116 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 1117 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1118 | td->o.filename = NULL; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1119 | free(td->files); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1120 | free(td->file_locks); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1121 | td->files_index = 0; |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 1122 | td->files = NULL; |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1123 | td->file_locks = NULL; |
Jens Axboe | 27ddbfa | 2014-01-22 16:27:32 -0800 | [diff] [blame] | 1124 | td->o.file_lock_mode = FILE_LOCK_NONE; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1125 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1126 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1127 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 1128 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1129 | { |
| 1130 | struct stat sb; |
| 1131 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 1132 | if (!strcmp(f->file_name, "-")) |
| 1133 | f->filetype = FIO_TYPE_PIPE; |
| 1134 | else |
| 1135 | f->filetype = FIO_TYPE_FILE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1136 | |
Bruce Cran | 3892182 | 2012-02-22 17:55:16 +0000 | [diff] [blame] | 1137 | /* \\.\ is the device namespace in Windows, where every file is |
| 1138 | * a block device */ |
| 1139 | if (strncmp(f->file_name, "\\\\.\\", 4) == 0) |
| 1140 | f->filetype = FIO_TYPE_BD; |
| 1141 | |
Jens Axboe | b30d395 | 2010-02-06 23:36:26 +0100 | [diff] [blame] | 1142 | if (!stat(f->file_name, &sb)) { |
Bruce Cran | 3892182 | 2012-02-22 17:55:16 +0000 | [diff] [blame] | 1143 | if (S_ISBLK(sb.st_mode)) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1144 | f->filetype = FIO_TYPE_BD; |
| 1145 | else if (S_ISCHR(sb.st_mode)) |
| 1146 | f->filetype = FIO_TYPE_CHAR; |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 1147 | else if (S_ISFIFO(sb.st_mode)) |
| 1148 | f->filetype = FIO_TYPE_PIPE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1152 | static int __is_already_allocated(const char *fname) |
| 1153 | { |
| 1154 | struct flist_head *entry; |
| 1155 | char *filename; |
| 1156 | |
| 1157 | if (flist_empty(&filename_list)) |
| 1158 | return 0; |
| 1159 | |
| 1160 | flist_for_each(entry, &filename_list) { |
| 1161 | filename = flist_entry(entry, struct file_name, list)->filename; |
| 1162 | |
| 1163 | if (strcmp(filename, fname) == 0) |
| 1164 | return 1; |
| 1165 | } |
| 1166 | |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
| 1170 | static int is_already_allocated(const char *fname) |
| 1171 | { |
| 1172 | int ret; |
| 1173 | |
| 1174 | fio_file_hash_lock(); |
| 1175 | ret = __is_already_allocated(fname); |
| 1176 | fio_file_hash_unlock(); |
| 1177 | return ret; |
| 1178 | } |
| 1179 | |
Castor Fu | 190b8f0 | 2014-03-20 10:59:29 -0700 | [diff] [blame] | 1180 | static void set_already_allocated(const char *fname) |
| 1181 | { |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1182 | struct file_name *fn; |
| 1183 | |
| 1184 | fn = malloc(sizeof(struct file_name)); |
| 1185 | fn->filename = strdup(fname); |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1186 | |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1187 | fio_file_hash_lock(); |
| 1188 | if (!__is_already_allocated(fname)) { |
| 1189 | flist_add_tail(&fn->list, &filename_list); |
| 1190 | fn = NULL; |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1191 | } |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1192 | fio_file_hash_unlock(); |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1193 | |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1194 | if (fn) { |
| 1195 | free(fn->filename); |
| 1196 | free(fn); |
| 1197 | } |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1200 | |
Castor Fu | 190b8f0 | 2014-03-20 10:59:29 -0700 | [diff] [blame] | 1201 | static void free_already_allocated(void) |
| 1202 | { |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1203 | struct flist_head *entry, *tmp; |
| 1204 | struct file_name *fn; |
| 1205 | |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1206 | if (flist_empty(&filename_list)) |
| 1207 | return; |
| 1208 | |
| 1209 | fio_file_hash_lock(); |
| 1210 | flist_for_each_safe(entry, tmp, &filename_list) { |
| 1211 | fn = flist_entry(entry, struct file_name, list); |
| 1212 | free(fn->filename); |
| 1213 | flist_del(&fn->list); |
| 1214 | free(fn); |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1215 | } |
Jens Axboe | 9042623 | 2014-04-01 12:28:47 -0600 | [diff] [blame] | 1216 | |
| 1217 | fio_file_hash_unlock(); |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
Jens Axboe | 7b5cb70 | 2014-04-01 16:37:03 -0600 | [diff] [blame] | 1220 | static struct fio_file *alloc_new_file(struct thread_data *td) |
| 1221 | { |
| 1222 | struct fio_file *f; |
| 1223 | |
| 1224 | f = smalloc(sizeof(*f)); |
| 1225 | if (!f) { |
| 1226 | log_err("fio: smalloc OOM\n"); |
| 1227 | assert(0); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
| 1231 | f->fd = -1; |
| 1232 | f->shadow_fd = -1; |
| 1233 | fio_file_reset(td, f); |
| 1234 | return f; |
| 1235 | } |
| 1236 | |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 1237 | int add_file(struct thread_data *td, const char *fname, int numjob, int inc) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1238 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 1239 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1240 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1241 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1242 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1243 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1244 | dprint(FD_FILE, "add file %s\n", fname); |
| 1245 | |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1246 | if (td->o.directory) |
| 1247 | len = set_name_idx(file_name, td->o.directory, numjob); |
| 1248 | |
| 1249 | sprintf(file_name + len, "%s", fname); |
| 1250 | |
| 1251 | /* clean cloned siblings using existing files */ |
| 1252 | if (numjob && is_already_allocated(file_name)) |
| 1253 | return 0; |
| 1254 | |
Jens Axboe | 7b5cb70 | 2014-04-01 16:37:03 -0600 | [diff] [blame] | 1255 | f = alloc_new_file(td); |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1256 | |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1257 | if (td->files_size <= td->files_index) { |
Jianpeng Ma | 1983e32 | 2013-01-10 13:19:27 +0100 | [diff] [blame] | 1258 | unsigned int new_size = td->o.nr_files + 1; |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 1259 | |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1260 | dprint(FD_FILE, "resize file array to %d files\n", new_size); |
| 1261 | |
| 1262 | td->files = realloc(td->files, new_size * sizeof(f)); |
Jianpeng Ma | d537c08 | 2013-01-11 08:52:55 +0100 | [diff] [blame] | 1263 | if (td->files == NULL) { |
| 1264 | log_err("fio: realloc OOM\n"); |
| 1265 | assert(0); |
| 1266 | } |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1267 | if (td->o.file_lock_mode != FILE_LOCK_NONE) { |
| 1268 | td->file_locks = realloc(td->file_locks, new_size); |
| 1269 | if (!td->file_locks) { |
| 1270 | log_err("fio: realloc OOM\n"); |
| 1271 | assert(0); |
| 1272 | } |
| 1273 | td->file_locks[cur_files] = FILE_LOCK_NONE; |
| 1274 | } |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1275 | td->files_size = new_size; |
| 1276 | } |
Jens Axboe | 8bb7679 | 2009-03-04 15:37:52 +0100 | [diff] [blame] | 1277 | td->files[cur_files] = f; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 1278 | f->fileno = cur_files; |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 1279 | |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 1280 | /* |
| 1281 | * init function, io engine may not be loaded yet |
| 1282 | */ |
| 1283 | if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) |
| 1284 | f->real_file_size = -1ULL; |
| 1285 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1286 | f->file_name = smalloc_strdup(file_name); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1287 | if (!f->file_name) { |
| 1288 | log_err("fio: smalloc OOM\n"); |
| 1289 | assert(0); |
| 1290 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1291 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 1292 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1293 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1294 | switch (td->o.file_lock_mode) { |
| 1295 | case FILE_LOCK_NONE: |
| 1296 | break; |
| 1297 | case FILE_LOCK_READWRITE: |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1298 | f->rwlock = fio_rwlock_init(); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1299 | break; |
| 1300 | case FILE_LOCK_EXCLUSIVE: |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 1301 | f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1302 | break; |
| 1303 | default: |
| 1304 | log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); |
| 1305 | assert(0); |
| 1306 | } |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1307 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 1308 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 1309 | if (f->filetype == FIO_TYPE_FILE) |
| 1310 | td->nr_normal_files++; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1311 | |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1312 | set_already_allocated(file_name); |
| 1313 | |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 1314 | if (inc) |
| 1315 | td->o.nr_files++; |
| 1316 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1317 | dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, |
| 1318 | cur_files); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1319 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1320 | return cur_files; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1321 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1322 | |
Jens Axboe | 49ffb4a | 2010-08-24 15:01:37 +0200 | [diff] [blame] | 1323 | int add_file_exclusive(struct thread_data *td, const char *fname) |
| 1324 | { |
| 1325 | struct fio_file *f; |
| 1326 | unsigned int i; |
| 1327 | |
| 1328 | for_each_file(td, f, i) { |
| 1329 | if (!strcmp(f->file_name, fname)) |
| 1330 | return i; |
| 1331 | } |
| 1332 | |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 1333 | return add_file(td, fname, 0, 1); |
Jens Axboe | 49ffb4a | 2010-08-24 15:01:37 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1336 | void get_file(struct fio_file *f) |
| 1337 | { |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 1338 | dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 1339 | assert(fio_file_open(f)); |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1340 | f->references++; |
| 1341 | } |
| 1342 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1343 | int put_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1344 | { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1345 | int f_ret = 0, ret = 0; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1346 | |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 1347 | 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] | 1348 | |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1349 | if (!fio_file_open(f)) { |
| 1350 | assert(f->fd == -1); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1351 | return 0; |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1352 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1353 | |
| 1354 | assert(f->references); |
| 1355 | if (--f->references) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1356 | return 0; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1357 | |
Jens Axboe | 71b84ca | 2014-04-14 12:01:45 -0600 | [diff] [blame] | 1358 | if (should_fsync(td) && td->o.fsync_on_close) { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1359 | f_ret = fsync(f->fd); |
Jens Axboe | 71b84ca | 2014-04-14 12:01:45 -0600 | [diff] [blame] | 1360 | if (f_ret < 0) |
| 1361 | f_ret = errno; |
| 1362 | } |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 1363 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1364 | if (td->io_ops->close_file) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1365 | ret = td->io_ops->close_file(td, f); |
Jens Axboe | 1020a13 | 2007-03-29 11:15:06 +0200 | [diff] [blame] | 1366 | |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1367 | if (!ret) |
Jens Axboe | a5fb461 | 2008-05-28 10:54:01 +0200 | [diff] [blame] | 1368 | ret = f_ret; |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1369 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1370 | td->nr_open_files--; |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 1371 | fio_file_clear_open(f); |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1372 | assert(f->fd == -1); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1373 | return ret; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1374 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1375 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1376 | 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] | 1377 | { |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1378 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
| 1379 | return; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1380 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1381 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
| 1382 | if (ddir == DDIR_READ) |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1383 | fio_rwlock_read(f->rwlock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1384 | else |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1385 | fio_rwlock_write(f->rwlock); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1386 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
| 1387 | fio_mutex_down(f->lock); |
| 1388 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1389 | td->file_locks[f->fileno] = td->o.file_lock_mode; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | void unlock_file(struct thread_data *td, struct fio_file *f) |
| 1393 | { |
| 1394 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
| 1395 | return; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1396 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1397 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) |
| 1398 | fio_rwlock_unlock(f->rwlock); |
| 1399 | else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1400 | fio_mutex_up(f->lock); |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1401 | |
| 1402 | td->file_locks[f->fileno] = FILE_LOCK_NONE; |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1403 | } |
| 1404 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1405 | void unlock_file_all(struct thread_data *td, struct fio_file *f) |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1406 | { |
Jens Axboe | f2a2803 | 2014-02-11 09:12:06 -0700 | [diff] [blame] | 1407 | if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks) |
Jens Axboe | 27ddbfa | 2014-01-22 16:27:32 -0800 | [diff] [blame] | 1408 | return; |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1409 | if (td->file_locks[f->fileno] != FILE_LOCK_NONE) |
| 1410 | unlock_file(td, f); |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1413 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 1414 | { |
| 1415 | struct dirent *dir; |
| 1416 | int ret = 0; |
| 1417 | DIR *D; |
| 1418 | |
| 1419 | D = opendir(dirname); |
| 1420 | if (!D) { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1421 | char buf[FIO_VERROR_SIZE]; |
| 1422 | |
Ken Raeburn | 98ffb8f | 2013-01-30 22:31:09 +0100 | [diff] [blame] | 1423 | snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname); |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1424 | td_verror(td, errno, buf); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1425 | return 1; |
| 1426 | } |
| 1427 | |
| 1428 | while ((dir = readdir(D)) != NULL) { |
| 1429 | char full_path[PATH_MAX]; |
| 1430 | struct stat sb; |
| 1431 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 1432 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 1433 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 1434 | |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 1435 | sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1436 | |
| 1437 | if (lstat(full_path, &sb) == -1) { |
| 1438 | if (errno != ENOENT) { |
| 1439 | td_verror(td, errno, "stat"); |
Jens Axboe | 68ace9e | 2014-04-11 11:24:00 -0600 | [diff] [blame] | 1440 | ret = 1; |
| 1441 | break; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (S_ISREG(sb.st_mode)) { |
Jens Axboe | 5903e7b | 2014-02-26 13:42:13 -0800 | [diff] [blame] | 1446 | add_file(td, full_path, 0, 1); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1447 | continue; |
| 1448 | } |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1449 | if (!S_ISDIR(sb.st_mode)) |
| 1450 | continue; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1451 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1452 | ret = recurse_dir(td, full_path); |
| 1453 | if (ret) |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1454 | break; |
| 1455 | } |
| 1456 | |
| 1457 | closedir(D); |
| 1458 | return ret; |
| 1459 | } |
| 1460 | |
| 1461 | int add_dir_files(struct thread_data *td, const char *path) |
| 1462 | { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1463 | int ret = recurse_dir(td, path); |
| 1464 | |
| 1465 | if (!ret) |
| 1466 | log_info("fio: opendir added %d files\n", td->o.nr_files); |
| 1467 | |
| 1468 | return ret; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1469 | } |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1470 | |
| 1471 | void dup_files(struct thread_data *td, struct thread_data *org) |
| 1472 | { |
| 1473 | struct fio_file *f; |
| 1474 | unsigned int i; |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1475 | |
| 1476 | dprint(FD_FILE, "dup files: %d\n", org->files_index); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1477 | |
| 1478 | if (!org->files) |
| 1479 | return; |
| 1480 | |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1481 | td->files = malloc(org->files_index * sizeof(f)); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1482 | |
Jens Axboe | d7df1d1 | 2013-03-20 19:57:01 -0600 | [diff] [blame] | 1483 | if (td->o.file_lock_mode != FILE_LOCK_NONE) |
| 1484 | td->file_locks = malloc(org->files_index); |
| 1485 | |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1486 | for_each_file(org, f, i) { |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1487 | struct fio_file *__f; |
| 1488 | |
Jens Axboe | 7b5cb70 | 2014-04-01 16:37:03 -0600 | [diff] [blame] | 1489 | __f = alloc_new_file(td); |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1490 | |
Aaron Carroll | bc3456f | 2008-06-25 09:20:37 +0200 | [diff] [blame] | 1491 | if (f->file_name) { |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1492 | __f->file_name = smalloc_strdup(f->file_name); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1493 | if (!__f->file_name) { |
| 1494 | log_err("fio: smalloc OOM\n"); |
| 1495 | assert(0); |
| 1496 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1497 | |
Aaron Carroll | bc3456f | 2008-06-25 09:20:37 +0200 | [diff] [blame] | 1498 | __f->filetype = f->filetype; |
| 1499 | } |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1500 | |
Jens Axboe | 67ad924 | 2014-02-07 10:56:15 -0700 | [diff] [blame] | 1501 | if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
| 1502 | __f->lock = f->lock; |
| 1503 | else if (td->o.file_lock_mode == FILE_LOCK_READWRITE) |
| 1504 | __f->rwlock = f->rwlock; |
| 1505 | |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1506 | td->files[i] = __f; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1507 | } |
| 1508 | } |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1509 | |
| 1510 | /* |
| 1511 | * Returns the index that matches the filename, or -1 if not there |
| 1512 | */ |
| 1513 | int get_fileno(struct thread_data *td, const char *fname) |
| 1514 | { |
| 1515 | struct fio_file *f; |
| 1516 | unsigned int i; |
| 1517 | |
| 1518 | for_each_file(td, f, i) |
| 1519 | if (!strcmp(f->file_name, fname)) |
| 1520 | return i; |
| 1521 | |
| 1522 | return -1; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * For log usage, where we add/open/close files automatically |
| 1527 | */ |
| 1528 | void free_release_files(struct thread_data *td) |
| 1529 | { |
| 1530 | close_files(td); |
Jens Axboe | 4c8e9f3 | 2014-06-09 19:52:05 -0600 | [diff] [blame] | 1531 | td->o.nr_files = 0; |
| 1532 | td->o.open_files = 0; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1533 | td->files_index = 0; |
| 1534 | td->nr_normal_files = 0; |
| 1535 | } |
Jens Axboe | 33c4881 | 2013-01-21 09:46:06 -0700 | [diff] [blame] | 1536 | |
| 1537 | void fio_file_reset(struct thread_data *td, struct fio_file *f) |
| 1538 | { |
Jens Axboe | 08a99be | 2014-12-14 19:01:24 -0700 | [diff] [blame] | 1539 | int i; |
| 1540 | |
| 1541 | for (i = 0; i < DDIR_RWDIR_CNT; i++) { |
| 1542 | f->last_pos[i] = f->file_offset; |
| 1543 | f->last_start[i] = -1ULL; |
| 1544 | } |
| 1545 | |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1546 | if (fio_file_axmap(f)) |
Jens Axboe | 33c4881 | 2013-01-21 09:46:06 -0700 | [diff] [blame] | 1547 | axmap_reset(f->io_axmap); |
Jens Axboe | d55dd04 | 2014-12-15 09:38:43 -0700 | [diff] [blame] | 1548 | else if (fio_file_lfsr(f)) |
Jens Axboe | 33c4881 | 2013-01-21 09:46:06 -0700 | [diff] [blame] | 1549 | lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]); |
| 1550 | } |
Jens Axboe | 002fe73 | 2014-02-11 08:31:13 -0700 | [diff] [blame] | 1551 | |
| 1552 | int fio_files_done(struct thread_data *td) |
| 1553 | { |
| 1554 | struct fio_file *f; |
| 1555 | unsigned int i; |
| 1556 | |
| 1557 | for_each_file(td, f, i) |
| 1558 | if (!fio_file_done(f)) |
| 1559 | return 0; |
| 1560 | |
| 1561 | return 1; |
| 1562 | } |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1563 | |
| 1564 | /* free memory used in initialization phase only */ |
Castor Fu | 190b8f0 | 2014-03-20 10:59:29 -0700 | [diff] [blame] | 1565 | void filesetup_mem_free(void) |
| 1566 | { |
Christian Ehrhardt | bcbfeef | 2014-02-20 09:13:06 -0800 | [diff] [blame] | 1567 | free_already_allocated(); |
| 1568 | } |