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