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