89120072a18a217c7f15b820d0246e8aa1b94812
[fio.git] / file.h
1 #ifndef FIO_FILE_H
2 #define FIO_FILE_H
3
4 #include "io_ddir.h"
5
6 /*
7  * The type of object we are working on
8  */
9 enum fio_filetype {
10         FIO_TYPE_FILE = 1,              /* plain file */
11         FIO_TYPE_BD,                    /* block device */
12         FIO_TYPE_CHAR,                  /* character device */
13         FIO_TYPE_PIPE,                  /* pipe */
14 };
15
16 enum fio_file_flags {
17         FIO_FILE_open           = 1 << 0,       /* file is open */
18         FIO_FILE_closing        = 1 << 1,       /* file being closed */
19         FIO_FILE_extend         = 1 << 2,       /* needs extend */
20         FIO_FILE_done           = 1 << 3,       /* io completed to this file */
21         FIO_FILE_size_known     = 1 << 4,       /* size has been set */
22         FIO_FILE_hashed         = 1 << 5,       /* file is on hash */
23 };
24
25 enum file_lock_mode {
26         FILE_LOCK_NONE,
27         FILE_LOCK_EXCLUSIVE,
28         FILE_LOCK_READWRITE,
29 };
30
31 /*
32  * Each thread_data structure has a number of files associated with it,
33  * this structure holds state information for a single file.
34  */
35 struct fio_file {
36         struct flist_head hash_list;
37         enum fio_filetype filetype;
38
39         /*
40          * A file may not be a file descriptor, let the io engine decide
41          */
42         union {
43                 unsigned long file_data;
44                 int fd;
45         };
46
47         /*
48          * filename and possible memory mapping
49          */
50         char *file_name;
51         unsigned int major, minor;
52
53         void *mmap_ptr;
54         size_t mmap_sz;
55         off_t mmap_off;
56
57         /*
58          * size of the file, offset into file, and io size from that offset
59          */
60         unsigned long long real_file_size;
61         unsigned long long file_offset;
62         unsigned long long io_size;
63
64         unsigned long long last_pos;
65
66         /*
67          * if io is protected by a semaphore, this is set
68          */
69         struct fio_mutex *lock;
70         void *lock_owner;
71         unsigned int lock_batch;
72         enum fio_ddir lock_ddir;
73
74         /*
75          * block map for random io
76          */
77         unsigned int *file_map;
78         unsigned int num_maps;
79         unsigned int last_free_lookup;
80
81         int references;
82         enum fio_file_flags flags;
83
84         struct disk_util *du;
85 };
86
87 #define FILE_FLAG_FNS(name)                                             \
88 static inline void fio_file_set_##name(struct fio_file *f)              \
89 {                                                                       \
90         (f)->flags |= FIO_FILE_##name;                                  \
91 }                                                                       \
92 static inline void fio_file_clear_##name(struct fio_file *f)            \
93 {                                                                       \
94         (f)->flags &= ~FIO_FILE_##name;                                 \
95 }                                                                       \
96 static inline int fio_file_##name(struct fio_file *f)                   \
97 {                                                                       \
98         return ((f)->flags & FIO_FILE_##name) != 0;                     \
99 }
100
101 FILE_FLAG_FNS(open);
102 FILE_FLAG_FNS(closing);
103 FILE_FLAG_FNS(extend);
104 FILE_FLAG_FNS(done);
105 FILE_FLAG_FNS(size_known);
106 FILE_FLAG_FNS(hashed);
107 #undef FILE_FLAG_FNS
108
109 /*
110  * File setup/shutdown
111  */
112 struct thread_data;
113 extern void close_files(struct thread_data *);
114 extern void close_and_free_files(struct thread_data *);
115 extern int __must_check setup_files(struct thread_data *);
116 extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
117 extern int __must_check generic_open_file(struct thread_data *, struct fio_file *);
118 extern int __must_check generic_close_file(struct thread_data *, struct fio_file *);
119 extern int __must_check generic_get_file_size(struct thread_data *, struct fio_file *);
120 extern int __must_check pre_read_files(struct thread_data *);
121 extern int add_file(struct thread_data *, const char *);
122 extern void get_file(struct fio_file *);
123 extern int __must_check put_file(struct thread_data *, struct fio_file *);
124 extern void lock_file(struct thread_data *, struct fio_file *, enum fio_ddir);
125 extern void unlock_file(struct thread_data *, struct fio_file *);
126 extern void unlock_file_all(struct thread_data *, struct fio_file *);
127 extern int add_dir_files(struct thread_data *, const char *);
128 extern int init_random_map(struct thread_data *);
129 extern void dup_files(struct thread_data *, struct thread_data *);
130 extern int get_fileno(struct thread_data *, const char *);
131 extern void free_release_files(struct thread_data *);
132
133 #endif