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