file hash: don't close file fd on lookup/add race
[fio.git] / file.h
1 #ifndef FIO_FILE_H
2 #define FIO_FILE_H
3
4 #include <string.h>
5 #include "compiler/compiler.h"
6 #include "io_ddir.h"
7 #include "flist.h"
8 #include "lib/zipf.h"
9 #include "lib/axmap.h"
10 #include "lib/lfsr.h"
11
12 /*
13  * The type of object we are working on
14  */
15 enum fio_filetype {
16         FIO_TYPE_FILE = 1,              /* plain file */
17         FIO_TYPE_BD,                    /* block device */
18         FIO_TYPE_CHAR,                  /* character device */
19         FIO_TYPE_PIPE,                  /* pipe */
20 };
21
22 enum fio_file_flags {
23         FIO_FILE_open           = 1 << 0,       /* file is open */
24         FIO_FILE_closing        = 1 << 1,       /* file being closed */
25         FIO_FILE_extend         = 1 << 2,       /* needs extend */
26         FIO_FILE_done           = 1 << 3,       /* io completed to this file */
27         FIO_FILE_size_known     = 1 << 4,       /* size has been set */
28         FIO_FILE_hashed         = 1 << 5,       /* file is on hash */
29         FIO_FILE_partial_mmap   = 1 << 6,       /* can't do full mmap */
30 };
31
32 enum file_lock_mode {
33         FILE_LOCK_NONE,
34         FILE_LOCK_EXCLUSIVE,
35         FILE_LOCK_READWRITE,
36 };
37
38 /*
39  * roundrobin available files, or choose one at random, or do each one
40  * serially.
41  */
42 enum {
43         FIO_FSERVICE_RANDOM     = 1,
44         FIO_FSERVICE_RR         = 2,
45         FIO_FSERVICE_SEQ        = 3,
46 };
47
48 /*
49  * No pre-allocation when laying down files, or call posix_fallocate(), or
50  * call fallocate() with FALLOC_FL_KEEP_SIZE set.
51  */
52 enum fio_fallocate_mode {
53         FIO_FALLOCATE_NONE      = 1,
54         FIO_FALLOCATE_POSIX     = 2,
55         FIO_FALLOCATE_KEEP_SIZE = 3,
56 };
57
58 /*
59  * Each thread_data structure has a number of files associated with it,
60  * this structure holds state information for a single file.
61  */
62 struct fio_file {
63         struct flist_head hash_list;
64         enum fio_filetype filetype;
65
66         void *file_data;
67         int fd;
68         int shadow_fd;
69 #ifdef WIN32
70         HANDLE hFile;
71         HANDLE ioCP;
72 #endif
73
74         /*
75          * filename and possible memory mapping
76          */
77         char *file_name;
78         unsigned int major, minor;
79         int fileno;
80
81         void *mmap_ptr;
82         size_t mmap_sz;
83         off_t mmap_off;
84
85         /*
86          * size of the file, offset into file, and io size from that offset
87          */
88         unsigned long long real_file_size;
89         unsigned long long file_offset;
90         unsigned long long io_size;
91
92         unsigned long long last_pos;
93         unsigned long long last_start;
94
95         unsigned long long first_write;
96         unsigned long long last_write;
97
98         /*
99          * For use by the io engine
100          */
101         unsigned long long file_pos;
102
103         /*
104          * if io is protected by a semaphore, this is set
105          */
106         struct fio_mutex *lock;
107         void *lock_owner;
108         unsigned int lock_batch;
109         enum fio_ddir lock_ddir;
110
111         /*
112          * block map for random io
113          */
114         struct axmap *io_axmap;
115
116         struct fio_lfsr lfsr;
117
118         /*
119          * Used for zipf random distribution
120          */
121         struct zipf_state zipf;
122
123         int references;
124         enum fio_file_flags flags;
125
126         struct disk_util *du;
127 };
128
129 #define FILE_FLAG_FNS(name)                                             \
130 static inline void fio_file_set_##name(struct fio_file *f)              \
131 {                                                                       \
132         (f)->flags |= FIO_FILE_##name;                                  \
133 }                                                                       \
134 static inline void fio_file_clear_##name(struct fio_file *f)            \
135 {                                                                       \
136         (f)->flags &= ~FIO_FILE_##name;                                 \
137 }                                                                       \
138 static inline int fio_file_##name(struct fio_file *f)                   \
139 {                                                                       \
140         return ((f)->flags & FIO_FILE_##name) != 0;                     \
141 }
142
143 FILE_FLAG_FNS(open);
144 FILE_FLAG_FNS(closing);
145 FILE_FLAG_FNS(extend);
146 FILE_FLAG_FNS(done);
147 FILE_FLAG_FNS(size_known);
148 FILE_FLAG_FNS(hashed);
149 FILE_FLAG_FNS(partial_mmap);
150 #undef FILE_FLAG_FNS
151
152 /*
153  * File setup/shutdown
154  */
155 struct thread_data;
156 extern void close_files(struct thread_data *);
157 extern void close_and_free_files(struct thread_data *);
158 extern unsigned long long get_start_offset(struct thread_data *);
159 extern int __must_check setup_files(struct thread_data *);
160 extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
161 extern int __must_check generic_open_file(struct thread_data *, struct fio_file *);
162 extern int __must_check generic_close_file(struct thread_data *, struct fio_file *);
163 extern int __must_check generic_get_file_size(struct thread_data *, struct fio_file *);
164 extern int __must_check file_lookup_open(struct fio_file *f, int flags);
165 extern int __must_check pre_read_files(struct thread_data *);
166 extern int add_file(struct thread_data *, const char *);
167 extern int add_file_exclusive(struct thread_data *, const char *);
168 extern void get_file(struct fio_file *);
169 extern int __must_check put_file(struct thread_data *, struct fio_file *);
170 extern void put_file_log(struct thread_data *, struct fio_file *);
171 extern void lock_file(struct thread_data *, struct fio_file *, enum fio_ddir);
172 extern void unlock_file(struct thread_data *, struct fio_file *);
173 extern void unlock_file_all(struct thread_data *, struct fio_file *);
174 extern int add_dir_files(struct thread_data *, const char *);
175 extern int init_random_map(struct thread_data *);
176 extern void dup_files(struct thread_data *, struct thread_data *);
177 extern int get_fileno(struct thread_data *, const char *);
178 extern void free_release_files(struct thread_data *);
179
180 static inline void fio_file_reset(struct fio_file *f)
181 {
182         f->last_pos = f->file_offset;
183         f->last_start = -1ULL;
184         f->file_pos = -1ULL;
185         if (f->io_axmap)
186                 axmap_reset(f->io_axmap);
187 }
188
189 #endif