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 | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 61 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
| 62 | memset(b, 0, td->o.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 | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 66 | bs = td->o.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); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 87 | else if (td->o.create_fsync) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 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 | |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 100 | static unsigned long long set_rand_file_size(struct thread_data *td, |
| 101 | unsigned long long total_size) |
| 102 | { |
| 103 | unsigned long long upper = total_size; |
| 104 | unsigned long long ret; |
| 105 | long r; |
| 106 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 107 | if (upper > td->o.file_size_high) |
| 108 | upper = td->o.file_size_high; |
| 109 | else if (upper < td->o.file_size_low) |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 110 | return 0; |
| 111 | else if (!upper) |
| 112 | return 0; |
| 113 | |
| 114 | r = os_random_long(&td->file_size_state); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 115 | ret = td->o.file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0))); |
| 116 | ret -= (ret % td->o.rw_min_bs); |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 117 | if (ret > upper) |
| 118 | ret = upper; |
| 119 | return ret; |
| 120 | } |
| 121 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 122 | static int fill_file_size(struct thread_data *td, struct fio_file *f, |
| 123 | unsigned long long *file_size, int new_files) |
| 124 | { |
| 125 | if (!td->o.file_size_low) { |
| 126 | f->file_size = *file_size / new_files; |
| 127 | f->real_file_size = f->file_size; |
| 128 | } else { |
| 129 | /* |
| 130 | * If we don't have enough space left for a file |
| 131 | * of the minimum size, bail. |
| 132 | */ |
| 133 | if (*file_size < td->o.file_size_low) |
| 134 | return 1; |
| 135 | |
| 136 | f->file_size = set_rand_file_size(td, *file_size); |
| 137 | f->real_file_size = f->file_size; |
| 138 | *file_size -= f->file_size; |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 144 | static int create_files(struct thread_data *td) |
| 145 | { |
| 146 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 147 | int err, need_create, can_extend; |
Jens Axboe | e8be395 | 2007-03-15 09:35:56 +0100 | [diff] [blame] | 148 | unsigned long long total_file_size, local_file_size, create_size; |
Jens Axboe | e407bec | 2007-03-14 11:21:27 +0100 | [diff] [blame] | 149 | unsigned int i, new_files; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 150 | |
Jens Axboe | e407bec | 2007-03-14 11:21:27 +0100 | [diff] [blame] | 151 | new_files = 0; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 152 | total_file_size = td->o.size; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 153 | for_each_file(td, f, i) { |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 154 | unsigned long long s; |
| 155 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 156 | f->file_offset = td->o.start_offset; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 157 | |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 158 | if (f->filetype != FIO_TYPE_FILE) |
| 159 | continue; |
Jens Axboe | ee3dcd9 | 2007-03-20 10:11:07 +0100 | [diff] [blame] | 160 | if (!total_file_size) |
| 161 | continue; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 162 | |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 163 | if (f->flags & FIO_FILE_EXISTS) { |
Jens Axboe | ee3dcd9 | 2007-03-20 10:11:07 +0100 | [diff] [blame] | 164 | if ((f->file_size > td->o.size / td->o.nr_files) || |
| 165 | !f->file_size) |
| 166 | f->file_size = td->o.size / td->o.nr_files; |
| 167 | |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 168 | s = f->file_size; |
| 169 | if (s > total_file_size) |
| 170 | s = total_file_size; |
| 171 | |
| 172 | total_file_size -= s; |
Jens Axboe | e407bec | 2007-03-14 11:21:27 +0100 | [diff] [blame] | 173 | } else |
| 174 | new_files++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 175 | } |
Jens Axboe | f697125 | 2006-11-15 10:00:31 +0100 | [diff] [blame] | 176 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 177 | /* |
| 178 | * unless specifically asked for overwrite, let normal io extend it |
| 179 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 180 | can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
Jens Axboe | 3f34420 | 2007-03-20 14:38:11 +0100 | [diff] [blame] | 181 | if (can_extend && new_files) { |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 182 | for_each_file(td, f, i) { |
| 183 | if (fill_file_size(td, f, &total_file_size, new_files)) { |
| 184 | log_info("fio: limited to %d files\n", i); |
| 185 | td->o.nr_files = i; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 190 | return 0; |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 191 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 192 | |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 193 | local_file_size = total_file_size; |
| 194 | if (!local_file_size) |
| 195 | local_file_size = -1; |
| 196 | |
Jens Axboe | f4c4f4d | 2007-03-15 09:33:15 +0100 | [diff] [blame] | 197 | total_file_size = 0; |
| 198 | need_create = 0; |
Jens Axboe | e8be395 | 2007-03-15 09:35:56 +0100 | [diff] [blame] | 199 | create_size = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 200 | for_each_file(td, f, i) { |
| 201 | int file_there; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 202 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 203 | if (f->filetype != FIO_TYPE_FILE) |
| 204 | continue; |
Jens Axboe | f4c4f4d | 2007-03-15 09:33:15 +0100 | [diff] [blame] | 205 | if (f->flags & FIO_FILE_EXISTS) { |
| 206 | total_file_size += f->file_size; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 207 | continue; |
Jens Axboe | f4c4f4d | 2007-03-15 09:33:15 +0100 | [diff] [blame] | 208 | } |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 209 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 210 | if (fill_file_size(td, f, &local_file_size, new_files)) { |
| 211 | log_info("fio: limited to %d files\n", i); |
| 212 | new_files -= (td->o.nr_files - i); |
| 213 | td->o.nr_files = i; |
| 214 | break; |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 215 | } |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 216 | |
Jens Axboe | f4c4f4d | 2007-03-15 09:33:15 +0100 | [diff] [blame] | 217 | total_file_size += f->file_size; |
Jens Axboe | e8be395 | 2007-03-15 09:35:56 +0100 | [diff] [blame] | 218 | create_size += f->file_size; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 219 | file_there = !file_ok(td, f); |
| 220 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 221 | if (file_there && td_write(td) && !td->o.overwrite) { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 222 | unlink(f->file_name); |
| 223 | file_there = 0; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 224 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 225 | |
| 226 | need_create += !file_there; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 227 | } |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 228 | |
| 229 | if (!need_create) |
| 230 | return 0; |
| 231 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 232 | if (!td->o.size && !total_file_size) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 233 | log_err("Need size for create\n"); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 234 | td_verror(td, EINVAL, "file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 235 | return 1; |
| 236 | } |
| 237 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 238 | temp_stall_ts = 1; |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 239 | log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n", |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 240 | td->o.name, new_files, create_size >> 20); |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 241 | |
| 242 | err = 0; |
| 243 | for_each_file(td, f, i) { |
Jens Axboe | 9b031dc | 2006-12-15 16:01:54 +0100 | [diff] [blame] | 244 | /* |
| 245 | * Only unlink files that we created. |
| 246 | */ |
Jens Axboe | f11bd94 | 2007-03-13 10:16:34 +0100 | [diff] [blame] | 247 | f->flags &= ~FIO_FILE_UNLINK; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 248 | if (file_ok(td, f)) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 249 | if (td->o.unlink) |
Jens Axboe | f11bd94 | 2007-03-13 10:16:34 +0100 | [diff] [blame] | 250 | f->flags |= FIO_FILE_UNLINK; |
| 251 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 252 | err = create_file(td, f); |
| 253 | if (err) |
| 254 | break; |
| 255 | } |
| 256 | } |
| 257 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 258 | temp_stall_ts = 0; |
| 259 | return err; |
| 260 | } |
| 261 | |
| 262 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 263 | { |
| 264 | struct stat st; |
| 265 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 266 | if (td->o.overwrite) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 267 | if (fstat(f->fd, &st) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 268 | td_verror(td, errno, "fstat"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 269 | return 1; |
| 270 | } |
| 271 | |
| 272 | f->real_file_size = st.st_size; |
| 273 | |
| 274 | if (!f->file_size || f->file_size > f->real_file_size) |
| 275 | f->file_size = f->real_file_size; |
Jens Axboe | 745508d | 2007-01-08 10:25:51 +0100 | [diff] [blame] | 276 | } else |
| 277 | f->real_file_size = f->file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 278 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 283 | { |
| 284 | unsigned long long bytes; |
| 285 | int r; |
| 286 | |
| 287 | r = blockdev_size(f->fd, &bytes); |
| 288 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 289 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 290 | return 1; |
| 291 | } |
| 292 | |
| 293 | f->real_file_size = bytes; |
| 294 | |
| 295 | /* |
| 296 | * no extend possibilities, so limit size to device size if too large |
| 297 | */ |
| 298 | if (!f->file_size || f->file_size > f->real_file_size) |
| 299 | f->file_size = f->real_file_size; |
| 300 | |
| 301 | f->file_size -= f->file_offset; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 306 | { |
| 307 | int ret = 0; |
| 308 | |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 309 | if (f->filetype == FIO_TYPE_FILE) { |
| 310 | if (!(f->flags & FIO_FILE_EXISTS)) |
| 311 | ret = file_size(td, f); |
| 312 | } else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 313 | ret = bdev_size(td, f); |
| 314 | else |
| 315 | f->real_file_size = -1; |
| 316 | |
| 317 | if (ret) |
| 318 | return ret; |
| 319 | |
| 320 | if (f->file_offset > f->real_file_size) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 321 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 328 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 329 | { |
| 330 | int ret = 0; |
| 331 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 332 | if (td->o.odirect) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 333 | return 0; |
| 334 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 335 | /* |
| 336 | * FIXME: add blockdev flushing too |
| 337 | */ |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 338 | if (f->mmap) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 339 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
Jens Axboe | 467d1b6 | 2007-03-15 14:11:10 +0100 | [diff] [blame] | 340 | else if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 341 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
Jens Axboe | 467d1b6 | 2007-03-15 14:11:10 +0100 | [diff] [blame] | 342 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 343 | ret = blockdev_invalidate_cache(f->fd); |
Jens Axboe | 467d1b6 | 2007-03-15 14:11:10 +0100 | [diff] [blame] | 344 | else if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 345 | ret = 0; |
| 346 | |
| 347 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 348 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 349 | return 1; |
| 350 | } |
| 351 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 352 | return ret; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 353 | } |
| 354 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 355 | 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] | 356 | { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 357 | close(f->fd); |
| 358 | f->fd = -1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 361 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 362 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 363 | int flags = 0; |
| 364 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 365 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 366 | flags |= OS_O_DIRECT; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 367 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 368 | flags |= O_SYNC; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 369 | |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 370 | if (td_write(td) || td_rw(td)) { |
| 371 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 372 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 373 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 374 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 375 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 376 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 377 | } else { |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 378 | if (f->filetype == FIO_TYPE_CHAR) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 379 | flags |= O_RDWR; |
| 380 | else |
| 381 | flags |= O_RDONLY; |
| 382 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 383 | f->fd = open(f->file_name, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 387 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 388 | int __e = errno; |
| 389 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 390 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
| 391 | |
| 392 | td_verror(td, __e, buf); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 393 | if (__e == EINVAL && td->o.odirect) |
Jens Axboe | 9d80e11 | 2007-02-22 19:37:53 +0100 | [diff] [blame] | 394 | log_err("fio: destination does not support O_DIRECT\n"); |
Jens Axboe | 8ab5387 | 2007-03-13 21:41:38 +0100 | [diff] [blame] | 395 | if (__e == EMFILE) |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 396 | log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 397 | return 1; |
| 398 | } |
| 399 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 400 | if (get_file_size(td, f)) |
| 401 | goto err; |
| 402 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 403 | if (td->o.invalidate_cache && file_invalidate_cache(td, f)) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 404 | goto err; |
| 405 | |
Jens Axboe | d2f3ac3 | 2007-03-22 19:24:09 +0100 | [diff] [blame] | 406 | if (!td->o.fadvise_hint) |
| 407 | return 0; |
| 408 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 409 | if (!td_random(td)) { |
| 410 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 411 | td_verror(td, errno, "fadvise"); |
| 412 | goto err; |
| 413 | } |
| 414 | } else { |
| 415 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { |
| 416 | td_verror(td, errno, "fadvise"); |
| 417 | goto err; |
| 418 | } |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 419 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 420 | |
| 421 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 422 | err: |
| 423 | close(f->fd); |
| 424 | return 1; |
| 425 | } |
| 426 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 427 | int open_files(struct thread_data *td) |
| 428 | { |
| 429 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 430 | unsigned int i; |
| 431 | int err = 0; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 432 | |
| 433 | for_each_file(td, f, i) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 434 | err = td_io_open_file(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 435 | if (err) |
| 436 | break; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 437 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 438 | if (td->o.open_files == td->nr_open_files) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 439 | break; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 442 | if (!err) |
| 443 | return 0; |
| 444 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 445 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 446 | td_io_close_file(td, f); |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 447 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 448 | return err; |
| 449 | } |
| 450 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 451 | int setup_files(struct thread_data *td) |
| 452 | { |
| 453 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 454 | unsigned int i; |
| 455 | int err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * if ioengine defines a setup() method, it's responsible for |
| 459 | * setting up everything in the td->files[] area. |
| 460 | */ |
| 461 | if (td->io_ops->setup) |
| 462 | return td->io_ops->setup(td); |
| 463 | |
| 464 | if (create_files(td)) |
| 465 | return 1; |
| 466 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 467 | err = open_files(td); |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 468 | if (err) |
| 469 | return err; |
| 470 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 471 | /* |
| 472 | * Recalculate the total file size now that files are set up. |
| 473 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 474 | td->o.size = 0; |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 475 | for_each_file(td, f, i) |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 476 | td->o.size += f->file_size; |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 477 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 478 | td->io_size = td->o.size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 479 | if (td->io_size == 0) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 480 | log_err("%s: no io blocks\n", td->o.name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 481 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 482 | return 1; |
| 483 | } |
| 484 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 485 | if (!td->o.zone_size) |
| 486 | td->o.zone_size = td->io_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 487 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 488 | td->total_io_size = td->io_size * td->o.loops; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 489 | |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 490 | for_each_file(td, f, i) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 491 | td_io_close_file(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 492 | |
| 493 | return err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 494 | } |
| 495 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 496 | int init_random_map(struct thread_data *td) |
| 497 | { |
| 498 | int num_maps, blocks; |
| 499 | struct fio_file *f; |
| 500 | unsigned int i; |
| 501 | |
| 502 | if (td->o.norandommap) |
| 503 | return 0; |
| 504 | |
| 505 | for_each_file(td, f, i) { |
| 506 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs; |
| 507 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; |
| 508 | f->file_map = malloc(num_maps * sizeof(long)); |
| 509 | if (!f->file_map) { |
| 510 | log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n"); |
| 511 | return 1; |
| 512 | } |
| 513 | f->num_maps = num_maps; |
| 514 | memset(f->file_map, 0, num_maps * sizeof(long)); |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 520 | void close_files(struct thread_data *td) |
| 521 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 522 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 523 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 524 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 525 | for_each_file(td, f, i) { |
Jens Axboe | 1315a68 | 2007-03-13 11:25:07 +0100 | [diff] [blame] | 526 | if (!f->file_name && (f->flags & FIO_FILE_UNLINK) && |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 527 | f->filetype == FIO_TYPE_FILE) { |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 528 | unlink(f->file_name); |
Jens Axboe | 1315a68 | 2007-03-13 11:25:07 +0100 | [diff] [blame] | 529 | free(f->file_name); |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 530 | f->file_name = NULL; |
| 531 | } |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 532 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 533 | td_io_close_file(td, f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 534 | |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 535 | if (f->file_map) { |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 536 | free(f->file_map); |
Jens Axboe | c343981 | 2007-03-20 13:54:53 +0100 | [diff] [blame] | 537 | f->file_map = NULL; |
| 538 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 539 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 540 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 541 | td->o.filename = NULL; |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 542 | td->files = NULL; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 543 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 544 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 545 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 546 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 547 | { |
| 548 | struct stat sb; |
| 549 | |
| 550 | f->filetype = FIO_TYPE_FILE; |
| 551 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 552 | if (!lstat(f->file_name, &sb)) { |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 553 | f->flags |= FIO_FILE_EXISTS; |
| 554 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 555 | if (S_ISBLK(sb.st_mode)) |
| 556 | f->filetype = FIO_TYPE_BD; |
| 557 | else if (S_ISCHR(sb.st_mode)) |
| 558 | f->filetype = FIO_TYPE_CHAR; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 559 | else { |
| 560 | /* |
| 561 | * might as well do this here, and save a stat later on |
| 562 | */ |
| 563 | f->real_file_size = sb.st_size; |
| 564 | f->file_size = f->real_file_size; |
| 565 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | void add_file(struct thread_data *td, const char *fname) |
| 570 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 571 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame^] | 572 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 573 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame^] | 574 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 575 | |
| 576 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); |
| 577 | |
| 578 | f = &td->files[cur_files]; |
| 579 | memset(f, 0, sizeof(*f)); |
| 580 | f->fd = -1; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame^] | 581 | |
| 582 | if (td->o.directory) |
| 583 | len = sprintf(file_name, "%s/", td->o.directory); |
| 584 | |
| 585 | sprintf(file_name + len, "%s", fname); |
| 586 | f->file_name = strdup(file_name); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 587 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 588 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 589 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 590 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 591 | if (f->filetype == FIO_TYPE_FILE) |
| 592 | td->nr_normal_files++; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 593 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 594 | |
| 595 | void get_file(struct fio_file *f) |
| 596 | { |
| 597 | f->references++; |
| 598 | } |
| 599 | |
| 600 | void put_file(struct thread_data *td, struct fio_file *f) |
| 601 | { |
| 602 | if (!(f->flags & FIO_FILE_OPEN)) |
| 603 | return; |
| 604 | |
| 605 | assert(f->references); |
| 606 | if (--f->references) |
| 607 | return; |
| 608 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 609 | if (should_fsync(td) && td->o.fsync_on_close) |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 610 | fsync(f->fd); |
| 611 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 612 | if (td->io_ops->close_file) |
| 613 | td->io_ops->close_file(td, f); |
| 614 | td->nr_open_files--; |
| 615 | f->flags &= ~FIO_FILE_OPEN; |
| 616 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 617 | |
| 618 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 619 | { |
| 620 | struct dirent *dir; |
| 621 | int ret = 0; |
| 622 | DIR *D; |
| 623 | |
| 624 | D = opendir(dirname); |
| 625 | if (!D) { |
| 626 | td_verror(td, errno, "opendir"); |
| 627 | return 1; |
| 628 | } |
| 629 | |
| 630 | while ((dir = readdir(D)) != NULL) { |
| 631 | char full_path[PATH_MAX]; |
| 632 | struct stat sb; |
| 633 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 634 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 635 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 636 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 637 | sprintf(full_path, "%s/%s", dirname, dir->d_name); |
| 638 | |
| 639 | if (lstat(full_path, &sb) == -1) { |
| 640 | if (errno != ENOENT) { |
| 641 | td_verror(td, errno, "stat"); |
| 642 | return 1; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (S_ISREG(sb.st_mode)) { |
| 647 | add_file(td, full_path); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 648 | td->o.nr_files++; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 649 | continue; |
| 650 | } |
| 651 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 652 | if ((ret = recurse_dir(td, full_path)) != 0) |
| 653 | break; |
| 654 | } |
| 655 | |
| 656 | closedir(D); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | int add_dir_files(struct thread_data *td, const char *path) |
| 661 | { |
| 662 | return recurse_dir(td, path); |
| 663 | } |