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> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/mman.h> |
| 7 | |
| 8 | #include "fio.h" |
| 9 | #include "os.h" |
| 10 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 11 | /* |
| 12 | * Check if the file exists and it's large enough. |
| 13 | */ |
| 14 | static int file_ok(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 15 | { |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 16 | struct stat st; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 17 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 18 | if (f->filetype != FIO_TYPE_FILE || |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 19 | (td->io_ops->flags & FIO_DISKLESSIO)) |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 20 | return 0; |
| 21 | |
Jens Axboe | fa01d13 | 2007-02-22 14:07:39 +0100 | [diff] [blame] | 22 | if (lstat(f->file_name, &st) == -1) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 23 | return 1; |
Jens Axboe | fa01d13 | 2007-02-22 14:07:39 +0100 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * if it's a special file, size is always ok for now |
| 27 | */ |
| 28 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) |
| 29 | return 0; |
| 30 | if (st.st_size < (off_t) f->file_size) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 31 | return 1; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int create_file(struct thread_data *td, struct fio_file *f) |
| 37 | { |
| 38 | unsigned long long left; |
| 39 | unsigned int bs; |
| 40 | char *b; |
| 41 | int r; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 42 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 43 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 44 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 45 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | if (ftruncate(f->fd, f->file_size) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 50 | td_verror(td, errno, "ftruncate"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 51 | goto err; |
| 52 | } |
| 53 | |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 54 | if (posix_fallocate(f->fd, 0, f->file_size) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 55 | td_verror(td, errno, "posix_fallocate"); |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 56 | goto err; |
| 57 | } |
| 58 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 59 | b = malloc(td->max_bs[DDIR_WRITE]); |
| 60 | memset(b, 0, td->max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 61 | |
| 62 | left = f->file_size; |
| 63 | while (left && !td->terminate) { |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 64 | bs = td->max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 65 | if (bs > left) |
| 66 | bs = left; |
| 67 | |
| 68 | r = write(f->fd, b, bs); |
| 69 | |
| 70 | if (r == (int) bs) { |
| 71 | left -= bs; |
| 72 | continue; |
| 73 | } else { |
| 74 | if (r < 0) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 75 | td_verror(td, errno, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 76 | else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 77 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 78 | |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (td->terminate) |
| 84 | unlink(f->file_name); |
| 85 | else if (td->create_fsync) |
| 86 | fsync(f->fd); |
| 87 | |
| 88 | free(b); |
| 89 | close(f->fd); |
| 90 | f->fd = -1; |
| 91 | return 0; |
| 92 | err: |
| 93 | close(f->fd); |
| 94 | f->fd = -1; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | static int create_files(struct thread_data *td) |
| 99 | { |
| 100 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 101 | int err, need_create, can_extend; |
| 102 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 103 | |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 104 | for_each_file(td, f, i) { |
| 105 | if (f->filetype != FIO_TYPE_FILE) |
| 106 | continue; |
| 107 | |
| 108 | f->file_size = td->total_file_size / td->nr_normal_files; |
| 109 | f->file_offset = td->start_offset; |
| 110 | } |
Jens Axboe | f697125 | 2006-11-15 10:00:31 +0100 | [diff] [blame] | 111 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 112 | /* |
| 113 | * unless specifically asked for overwrite, let normal io extend it |
| 114 | */ |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame] | 115 | can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
| 116 | if (can_extend) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 117 | return 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 118 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 119 | need_create = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 120 | for_each_file(td, f, i) { |
| 121 | int file_there; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 122 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 123 | if (f->filetype != FIO_TYPE_FILE) |
| 124 | continue; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 125 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 126 | file_there = !file_ok(td, f); |
| 127 | |
| 128 | if (file_there && td_write(td) && !td->overwrite) { |
| 129 | unlink(f->file_name); |
| 130 | file_there = 0; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 131 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 132 | |
| 133 | need_create += !file_there; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 134 | } |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 135 | |
| 136 | if (!need_create) |
| 137 | return 0; |
| 138 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 139 | if (!td->total_file_size) { |
| 140 | log_err("Need size for create\n"); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 141 | td_verror(td, EINVAL, "file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 142 | return 1; |
| 143 | } |
| 144 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 145 | temp_stall_ts = 1; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 146 | fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n", |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 147 | td->name, td->nr_normal_files, |
| 148 | (td->total_file_size >> 20) / td->nr_normal_files, |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 149 | td->total_file_size >> 20); |
| 150 | |
| 151 | err = 0; |
| 152 | for_each_file(td, f, i) { |
Jens Axboe | 9b031dc | 2006-12-15 16:01:54 +0100 | [diff] [blame] | 153 | /* |
| 154 | * Only unlink files that we created. |
| 155 | */ |
Jens Axboe | f11bd94 | 2007-03-13 10:16:34 +0100 | [diff] [blame] | 156 | f->flags &= ~FIO_FILE_UNLINK; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 157 | if (file_ok(td, f)) { |
Jens Axboe | f11bd94 | 2007-03-13 10:16:34 +0100 | [diff] [blame] | 158 | if (td->unlink) |
| 159 | f->flags |= FIO_FILE_UNLINK; |
| 160 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 161 | err = create_file(td, f); |
| 162 | if (err) |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 167 | temp_stall_ts = 0; |
| 168 | return err; |
| 169 | } |
| 170 | |
| 171 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 172 | { |
| 173 | struct stat st; |
| 174 | |
| 175 | if (td->overwrite) { |
| 176 | if (fstat(f->fd, &st) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 177 | td_verror(td, errno, "fstat"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | f->real_file_size = st.st_size; |
| 182 | |
| 183 | if (!f->file_size || f->file_size > f->real_file_size) |
| 184 | f->file_size = f->real_file_size; |
Jens Axboe | 745508d | 2007-01-08 10:25:51 +0100 | [diff] [blame] | 185 | } else |
| 186 | f->real_file_size = f->file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 187 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 192 | { |
| 193 | unsigned long long bytes; |
| 194 | int r; |
| 195 | |
| 196 | r = blockdev_size(f->fd, &bytes); |
| 197 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 198 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | f->real_file_size = bytes; |
| 203 | |
| 204 | /* |
| 205 | * no extend possibilities, so limit size to device size if too large |
| 206 | */ |
| 207 | if (!f->file_size || f->file_size > f->real_file_size) |
| 208 | f->file_size = f->real_file_size; |
| 209 | |
| 210 | f->file_size -= f->file_offset; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 215 | { |
| 216 | int ret = 0; |
| 217 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 218 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 219 | ret = file_size(td, f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 220 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 221 | ret = bdev_size(td, f); |
| 222 | else |
| 223 | f->real_file_size = -1; |
| 224 | |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | if (f->file_offset > f->real_file_size) { |
| 229 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); |
| 230 | return 1; |
| 231 | } |
| 232 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 236 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 237 | { |
| 238 | int ret = 0; |
| 239 | |
Jens Axboe | da86ccb | 2007-03-08 12:49:37 +0100 | [diff] [blame] | 240 | if (td->odirect) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 241 | return 0; |
| 242 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 243 | /* |
| 244 | * FIXME: add blockdev flushing too |
| 245 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 246 | if (f->mmap) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 247 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 248 | else if (f->filetype == FIO_TYPE_FILE) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 249 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 250 | } else if (f->filetype == FIO_TYPE_BD) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 251 | ret = blockdev_invalidate_cache(f->fd); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 252 | } else if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 253 | ret = 0; |
| 254 | |
| 255 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 256 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 257 | return 1; |
| 258 | } |
| 259 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 260 | return ret; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 263 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 264 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 265 | close(f->fd); |
| 266 | f->fd = -1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 269 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 270 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 271 | int flags = 0; |
| 272 | |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 273 | if (td->odirect) |
| 274 | flags |= OS_O_DIRECT; |
| 275 | if (td->sync_io) |
| 276 | flags |= O_SYNC; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 277 | |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 278 | if (td_write(td) || td_rw(td)) { |
| 279 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 280 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 281 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 282 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 283 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 284 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 285 | } else { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 286 | if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 287 | flags |= O_RDWR; |
| 288 | else |
| 289 | flags |= O_RDONLY; |
| 290 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 291 | f->fd = open(f->file_name, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (f->fd == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 295 | int __e = errno; |
| 296 | |
| 297 | td_verror(td, __e, "open"); |
| 298 | if (__e == EINVAL && td->odirect) |
Jens Axboe | 9d80e11 | 2007-02-22 19:37:53 +0100 | [diff] [blame] | 299 | log_err("fio: destination does not support O_DIRECT\n"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 300 | return 1; |
| 301 | } |
| 302 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 303 | if (get_file_size(td, f)) |
| 304 | goto err; |
| 305 | |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame] | 306 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 307 | goto err; |
| 308 | |
| 309 | if (!td_random(td)) { |
| 310 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 311 | td_verror(td, errno, "fadvise"); |
| 312 | goto err; |
| 313 | } |
| 314 | } else { |
| 315 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { |
| 316 | td_verror(td, errno, "fadvise"); |
| 317 | goto err; |
| 318 | } |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 319 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 320 | |
| 321 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 322 | err: |
| 323 | close(f->fd); |
| 324 | return 1; |
| 325 | } |
| 326 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 327 | int open_files(struct thread_data *td) |
| 328 | { |
| 329 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 330 | unsigned int i; |
| 331 | int err = 0; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 332 | |
| 333 | for_each_file(td, f, i) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 334 | err = td_io_open_file(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 335 | if (err) |
| 336 | break; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 337 | |
| 338 | if (td->open_files == td->nr_open_files) |
| 339 | break; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 340 | } |
| 341 | |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 342 | if (!err) |
| 343 | return 0; |
| 344 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 345 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 346 | td_io_close_file(td, f); |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 347 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 348 | return err; |
| 349 | } |
| 350 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 351 | int setup_files(struct thread_data *td) |
| 352 | { |
| 353 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 354 | unsigned int i; |
| 355 | int err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 356 | |
| 357 | /* |
| 358 | * if ioengine defines a setup() method, it's responsible for |
| 359 | * setting up everything in the td->files[] area. |
| 360 | */ |
| 361 | if (td->io_ops->setup) |
| 362 | return td->io_ops->setup(td); |
| 363 | |
| 364 | if (create_files(td)) |
| 365 | return 1; |
| 366 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 367 | err = open_files(td); |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 368 | if (err) |
| 369 | return err; |
| 370 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 371 | /* |
| 372 | * Recalculate the total file size now that files are set up. |
| 373 | */ |
| 374 | td->total_file_size = 0; |
| 375 | for_each_file(td, f, i) |
| 376 | td->total_file_size += f->file_size; |
| 377 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 378 | td->io_size = td->total_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 379 | if (td->io_size == 0) { |
| 380 | log_err("%s: no io blocks\n", td->name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 381 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | if (!td->zone_size) |
| 386 | td->zone_size = td->io_size; |
| 387 | |
| 388 | td->total_io_size = td->io_size * td->loops; |
| 389 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 390 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 391 | td_io_close_file(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 392 | |
| 393 | return err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | void close_files(struct thread_data *td) |
| 397 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 398 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 399 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 400 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 401 | for_each_file(td, f, i) { |
Jens Axboe | 1315a68 | 2007-03-13 11:25:07 +0100 | [diff] [blame] | 402 | if (!f->file_name && (f->flags & FIO_FILE_UNLINK) && |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 403 | f->filetype == FIO_TYPE_FILE) { |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 404 | unlink(f->file_name); |
Jens Axboe | 1315a68 | 2007-03-13 11:25:07 +0100 | [diff] [blame] | 405 | free(f->file_name); |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 406 | f->file_name = NULL; |
| 407 | } |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 408 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 409 | td_io_close_file(td, f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 410 | |
| 411 | if (f->file_map) |
| 412 | free(f->file_map); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 413 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 414 | |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 415 | td->filename = NULL; |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 416 | free(td->files); |
| 417 | td->files = NULL; |
| 418 | td->nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 419 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 420 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 421 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 422 | { |
| 423 | struct stat sb; |
| 424 | |
| 425 | f->filetype = FIO_TYPE_FILE; |
| 426 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 427 | if (!lstat(f->file_name, &sb)) { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 428 | if (S_ISBLK(sb.st_mode)) |
| 429 | f->filetype = FIO_TYPE_BD; |
| 430 | else if (S_ISCHR(sb.st_mode)) |
| 431 | f->filetype = FIO_TYPE_CHAR; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void add_file(struct thread_data *td, const char *fname) |
| 436 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 437 | int cur_files = td->files_index; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 438 | struct fio_file *f; |
| 439 | |
| 440 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); |
| 441 | |
| 442 | f = &td->files[cur_files]; |
| 443 | memset(f, 0, sizeof(*f)); |
| 444 | f->fd = -1; |
Jens Axboe | cae6195 | 2007-03-13 11:12:14 +0100 | [diff] [blame] | 445 | f->file_name = strdup(fname); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 446 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 447 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 448 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 449 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 450 | if (f->filetype == FIO_TYPE_FILE) |
| 451 | td->nr_normal_files++; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 452 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 453 | |
| 454 | void get_file(struct fio_file *f) |
| 455 | { |
| 456 | f->references++; |
| 457 | } |
| 458 | |
| 459 | void put_file(struct thread_data *td, struct fio_file *f) |
| 460 | { |
| 461 | if (!(f->flags & FIO_FILE_OPEN)) |
| 462 | return; |
| 463 | |
| 464 | assert(f->references); |
| 465 | if (--f->references) |
| 466 | return; |
| 467 | |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame^] | 468 | if (should_fsync(td) && td->fsync_on_close) |
| 469 | fsync(f->fd); |
| 470 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 471 | if (td->io_ops->close_file) |
| 472 | td->io_ops->close_file(td, f); |
| 473 | td->nr_open_files--; |
| 474 | f->flags &= ~FIO_FILE_OPEN; |
| 475 | } |