filesetup.c revision 0263882a95b9213d3afe285ae8adb5d0310d5039
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
11/*
12 * Check if the file exists and it's large enough.
13 */
14static int file_ok(struct thread_data *td, struct fio_file *f)
15{
16	struct stat st;
17
18	if (td->filetype != FIO_TYPE_FILE ||
19	    (td->io_ops->flags & FIO_DISKLESSIO))
20		return 0;
21
22	if (lstat(f->file_name, &st) == -1)
23		return 1;
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)
31		return 1;
32
33	return 0;
34}
35
36static 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;
42
43	f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
44	if (f->fd < 0) {
45		td_verror(td, errno, "open");
46		return 1;
47	}
48
49	if (ftruncate(f->fd, f->file_size) == -1) {
50		td_verror(td, errno, "ftruncate");
51		goto err;
52	}
53
54	if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
55		td_verror(td, errno, "posix_fallocate");
56		goto err;
57	}
58
59	b = malloc(td->max_bs[DDIR_WRITE]);
60	memset(b, 0, td->max_bs[DDIR_WRITE]);
61
62	left = f->file_size;
63	while (left && !td->terminate) {
64		bs = td->max_bs[DDIR_WRITE];
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)
75				td_verror(td, errno, "write");
76			else
77				td_verror(td, EIO, "write");
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;
92err:
93	close(f->fd);
94	f->fd = -1;
95	return 1;
96}
97
98static int create_files(struct thread_data *td)
99{
100	struct fio_file *f;
101	int i, err, need_create, can_extend;
102
103	for_each_file(td, f, i)
104		f->file_size = td->total_file_size / td->nr_files;
105
106	/*
107	 * unless specifically asked for overwrite, let normal io extend it
108	 */
109	can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
110	if (can_extend)
111		return 0;
112
113	need_create = 0;
114	if (td->filetype == FIO_TYPE_FILE) {
115		for_each_file(td, f, i) {
116			int file_there = !file_ok(td, f);
117
118			if (file_there && td_write(td) && !can_extend) {
119				unlink(f->file_name);
120				file_there = 0;
121			}
122
123			need_create += !file_there;
124		}
125	}
126
127	if (!need_create)
128		return 0;
129
130	if (!td->total_file_size) {
131		log_err("Need size for create\n");
132		td_verror(td, EINVAL, "file_size");
133		return 1;
134	}
135
136	temp_stall_ts = 1;
137	fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
138				td->name, td->nr_uniq_files,
139				(td->total_file_size >> 20) / td->nr_uniq_files,
140				td->total_file_size >> 20);
141
142	err = 0;
143	for_each_file(td, f, i) {
144		/*
145		 * Only unlink files that we created.
146		 */
147		f->unlink = 0;
148		if (file_ok(td, f)) {
149			f->unlink = td->unlink;
150			err = create_file(td, f);
151			if (err)
152				break;
153		}
154	}
155
156	temp_stall_ts = 0;
157	return err;
158}
159
160static int file_size(struct thread_data *td, struct fio_file *f)
161{
162	struct stat st;
163
164	if (td->overwrite) {
165		if (fstat(f->fd, &st) == -1) {
166			td_verror(td, errno, "fstat");
167			return 1;
168		}
169
170		f->real_file_size = st.st_size;
171
172		if (!f->file_size || f->file_size > f->real_file_size)
173			f->file_size = f->real_file_size;
174	} else
175		f->real_file_size = f->file_size;
176
177	return 0;
178}
179
180static int bdev_size(struct thread_data *td, struct fio_file *f)
181{
182	unsigned long long bytes;
183	int r;
184
185	r = blockdev_size(f->fd, &bytes);
186	if (r) {
187		td_verror(td, r, "blockdev_size");
188		return 1;
189	}
190
191	f->real_file_size = bytes;
192
193	/*
194	 * no extend possibilities, so limit size to device size if too large
195	 */
196	if (!f->file_size || f->file_size > f->real_file_size)
197		f->file_size = f->real_file_size;
198
199	f->file_size -= f->file_offset;
200	return 0;
201}
202
203static int get_file_size(struct thread_data *td, struct fio_file *f)
204{
205	int ret = 0;
206
207	if (td->filetype == FIO_TYPE_FILE)
208		ret = file_size(td, f);
209	else if (td->filetype == FIO_TYPE_BD)
210		ret = bdev_size(td, f);
211	else
212		f->real_file_size = -1;
213
214	if (ret)
215		return ret;
216
217	if (f->file_offset > f->real_file_size) {
218		log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
219		return 1;
220	}
221
222	return 0;
223}
224
225int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
226{
227	int ret = 0;
228
229	if (td->odirect)
230		return 0;
231
232	/*
233	 * FIXME: add blockdev flushing too
234	 */
235	if (f->mmap)
236		ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
237	else if (td->filetype == FIO_TYPE_FILE) {
238		ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
239	} else if (td->filetype == FIO_TYPE_BD) {
240		ret = blockdev_invalidate_cache(f->fd);
241	} else if (td->filetype == FIO_TYPE_CHAR)
242		ret = 0;
243
244	if (ret < 0) {
245		td_verror(td, errno, "invalidate_cache");
246		return 1;
247	}
248
249	return ret;
250}
251
252void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
253{
254	close(f->fd);
255	f->fd = -1;
256}
257
258int generic_open_file(struct thread_data *td, struct fio_file *f)
259{
260	int flags = 0;
261
262	if (td->odirect)
263		flags |= OS_O_DIRECT;
264	if (td->sync_io)
265		flags |= O_SYNC;
266
267	if (td_write(td) || td_rw(td)) {
268		flags |= O_RDWR;
269
270		if (td->filetype == FIO_TYPE_FILE)
271			flags |= O_CREAT;
272
273		f->fd = open(f->file_name, flags, 0600);
274	} else {
275		if (td->filetype == FIO_TYPE_CHAR)
276			flags |= O_RDWR;
277		else
278			flags |= O_RDONLY;
279
280		f->fd = open(f->file_name, flags);
281	}
282
283	if (f->fd == -1) {
284		int __e = errno;
285
286		td_verror(td, __e, "open");
287		if (__e == EINVAL && td->odirect)
288			log_err("fio: destination does not support O_DIRECT\n");
289		return 1;
290	}
291
292	if (get_file_size(td, f))
293		goto err;
294
295	if (td->invalidate_cache && file_invalidate_cache(td, f))
296		goto err;
297
298	if (!td_random(td)) {
299		if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
300			td_verror(td, errno, "fadvise");
301			goto err;
302		}
303	} else {
304		if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
305			td_verror(td, errno, "fadvise");
306			goto err;
307		}
308	}
309
310	return 0;
311err:
312	close(f->fd);
313	return 1;
314}
315
316int open_files(struct thread_data *td)
317{
318	struct fio_file *f;
319	int i, err = 0;
320
321	for_each_file(td, f, i) {
322		err = td_io_open_file(td, f);
323		if (err)
324			break;
325
326		if (td->open_files == td->nr_open_files)
327			break;
328	}
329
330	if (!err)
331		return 0;
332
333	for_each_file(td, f, i)
334		td_io_close_file(td, f);
335
336	return err;
337}
338
339int setup_files(struct thread_data *td)
340{
341	struct fio_file *f;
342	int err, i;
343
344	/*
345	 * if ioengine defines a setup() method, it's responsible for
346	 * setting up everything in the td->files[] area.
347	 */
348	if (td->io_ops->setup)
349		return td->io_ops->setup(td);
350
351	if (create_files(td))
352		return 1;
353
354	err = open_files(td);
355	if (err)
356		return err;
357
358	/*
359	 * Recalculate the total file size now that files are set up.
360	 */
361	td->total_file_size = 0;
362	for_each_file(td, f, i)
363		td->total_file_size += f->file_size;
364
365	td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files;
366
367	td->io_size = td->total_file_size;
368	if (td->io_size == 0) {
369		log_err("%s: no io blocks\n", td->name);
370		td_verror(td, EINVAL, "total_file_size");
371		return 1;
372	}
373
374	if (!td->zone_size)
375		td->zone_size = td->io_size;
376
377	td->total_io_size = td->io_size * td->loops;
378
379	for_each_file(td, f, i)
380		td_io_close_file(td, f);
381
382	return err;
383}
384
385void close_files(struct thread_data *td)
386{
387	struct fio_file *f;
388	int i;
389
390	for_each_file(td, f, i) {
391		if (!td->filename && f->unlink &&
392		    td->filetype == FIO_TYPE_FILE) {
393			unlink(f->file_name);
394			free(f->file_name);
395			f->file_name = NULL;
396		}
397
398		td_io_close_file(td, f);
399
400		if (f->file_map)
401			free(f->file_map);
402	}
403
404	td->filename = NULL;
405	free(td->files);
406	td->files = NULL;
407	td->nr_files = 0;
408}
409