summaryrefslogtreecommitdiffstats
path: root/src/libserver/statfile.h
blob: f7f632703cdbf95b6c43f120013cad04d370ea79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
 * @file statfile.h
 * Describes common methods for accessing statistics files and caching them in memory
 */

#ifndef RSPAMD_STATFILE_H
#define RSPAMD_STATFILE_H

#include "config.h"
#include "mem_pool.h"
#include "hash.h"

#define CHAIN_LENGTH 128

/* Section types */
#define STATFILE_SECTION_COMMON 1
#define STATFILE_SECTION_HEADERS 2
#define STATFILE_SECTION_URLS 3
#define STATFILE_SECTION_REGEXP 4

#define DEFAULT_STATFILE_INVALIDATE_TIME 30
#define DEFAULT_STATFILE_INVALIDATE_JITTER 30

/**
 * Common statfile header
 */
struct stat_file_header {
	u_char magic[3];                        /**< magic signature ('r' 's' 'd')      */
	u_char version[2];                      /**< version of statfile				*/
	u_char padding[3];                      /**< padding							*/
	guint64 create_time;                    /**< create time (time_t->guint64)		*/
	guint64 revision;                       /**< revision number					*/
	guint64 rev_time;                       /**< revision time						*/
	guint64 used_blocks;                    /**< used blocks number					*/
	guint64 total_blocks;                   /**< total number of blocks				*/
	u_char unused[239];                     /**< some bytes that can be used in future */
};

/**
 * Section header
 */
struct stat_file_section {
	guint64 code;                           /**< section's code						*/
	guint64 length;                     /**< section's length in blocks			*/
};

/**
 * Block of data in statfile
 */
struct stat_file_block {
	guint32 hash1;                          /**< hash1 (also acts as index)			*/
	guint32 hash2;                          /**< hash2								*/
	double value;                           /**< double value                       */
};

/**
 * Statistic file
 */
struct stat_file {
	struct stat_file_header header;         /**< header								*/
	struct stat_file_section section;       /**< first section						*/
	struct stat_file_block blocks[1];       /**< first block of data				*/
};

/**
 * Common view of statfile object
 */
typedef struct stat_file_s {
#ifdef HAVE_PATH_MAX
	gchar filename[PATH_MAX];               /**< name of file						*/
#else
	gchar filename[MAXPATHLEN];             /**< name of file						*/
#endif
	gint fd;                                    /**< descriptor							*/
	void *map;                              /**< mmaped area						*/
	off_t seek_pos;                         /**< current seek position				*/
	struct stat_file_section cur_section;   /**< current section					*/
	time_t open_time;                       /**< time when file was opened			*/
	time_t access_time;                     /**< last access time					*/
	size_t len;                             /**< length of file(in bytes)			*/
	rspamd_mempool_mutex_t *lock;               /**< mutex								*/
} stat_file_t;

/**
 * Statfiles pool
 */
typedef struct statfile_pool_s {
	stat_file_t *files;                     /**< hash table of opened files indexed by name	*/
	void **maps;                            /**< shared hash table of mmaped areas indexed by name	*/
	gint opened;                                /**< number of opened files				*/
	rspamd_mempool_t *pool;                 /**< memory pool object					*/
	rspamd_mempool_mutex_t *lock;               /**< mutex								*/
	struct event *invalidate_event;         /**< event for pool invalidation        */
	struct timeval invalidate_tv;
	gboolean mlock_ok;                      /**< whether it is possible to use mlock (2) to avoid statfiles unloading */
} statfile_pool_t;

/* Forwarded declarations */
struct rspamd_classifier_config;
struct rspamd_statfile_config;

/**
 * Create new statfile pool
 * @param max_size maximum size
 * @return statfile pool object
 */
statfile_pool_t * statfile_pool_new (rspamd_mempool_t *pool,
	gboolean use_mlock);

/**
 * Open statfile and attach it to pool
 * @param pool statfile pool object
 * @param filename name of statfile to open
 * @return 0 if specified statfile is attached and -1 in case of error
 */
stat_file_t * statfile_pool_open (statfile_pool_t *pool,
	gchar *filename,
	size_t len,
	gboolean forced);

/**
 * Create new statfile but DOES NOT attach it to pool, use @see statfile_pool_open for attaching
 * @param pool statfile pool object
 * @param filename name of statfile to create
 * @param len length of new statfile
 * @return 0 if file was created and -1 in case of error
 */
gint statfile_pool_create (statfile_pool_t *pool, gchar *filename, size_t len);

/**
 * Close specified statfile
 * @param pool statfile pool object
 * @param filename name of statfile to close
 * @param remove_hash remove filename from opened files hash also
 * @return 0 if file was closed and -1 if statfile was not opened
 */
gint statfile_pool_close (statfile_pool_t *pool,
	stat_file_t *file,
	gboolean keep_sorted);

/**
 * Delete statfile pool and close all attached statfiles
 * @param pool statfile pool object
 */
void statfile_pool_delete (statfile_pool_t *pool);

/**
 * Try to lock all statfiles in memory
 * @param pool statfile pool object
 */
void statfile_pool_lockall (statfile_pool_t *pool);

/**
 * Lock specified file for exclusive use (eg. learning)
 * @param pool statfile pool object
 * @param filename name of statfile
 */
void statfile_pool_lock_file (statfile_pool_t *pool, stat_file_t *file);

/**
 * Unlock specified file
 * @param pool statfile pool object
 * @param filename name of statfile
 */
void statfile_pool_unlock_file (statfile_pool_t *pool, stat_file_t *file);

/**
 * Get block from statfile with h1 and h2 values, use time argument for current time
 * @param pool statfile pool object
 * @param filename name of statfile
 * @param h1 h1 in file
 * @param h2 h2 in file
 * @param now current time
 * @return block value or 0 if block is not found
 */
double statfile_pool_get_block (statfile_pool_t *pool,
	stat_file_t *file,
	guint32 h1,
	guint32 h2,
	time_t now);

/**
 * Set specified block in statfile
 * @param pool statfile pool object
 * @param filename name of statfile
 * @param h1 h1 in file
 * @param h2 h2 in file
 * @param now current time
 * @param value value of block
 */
void statfile_pool_set_block (statfile_pool_t *pool,
	stat_file_t *file,
	guint32 h1,
	guint32 h2,
	time_t now,
	double value);

/**
 * Check whether statfile is opened
 * @param pool statfile pool object
 * @param filename name of statfile
 * @return TRUE if specified statfile is opened and FALSE otherwise
 */
stat_file_t * statfile_pool_is_open (statfile_pool_t *pool, gchar *filename);

/**
 * Returns current statfile section
 * @param pool statfile pool object
 * @param filename name of statfile
 * @return code of section or 0 if file is not opened
 */
guint32 statfile_pool_get_section (statfile_pool_t *pool, stat_file_t *file);

/**
 * Go to other section of statfile
 * @param pool statfile pool object
 * @param filename name of statfile
 * @param code code of section to seek to
 * @param from_begin search for section from begin of file if true
 * @return TRUE if section was set and FALSE otherwise
 */
gboolean statfile_pool_set_section (statfile_pool_t *pool,
	stat_file_t *file,
	guint32 code,
	gboolean from_begin);

/**
 * Add new section to statfile
 * @param pool statfile pool object
 * @param filename name of statfile
 * @param code code of section to seek to
 * @param length length in blocks of new section
 * @return TRUE if section was successfully added and FALSE in case of error
 */
gboolean statfile_pool_add_section (statfile_pool_t *pool,
	stat_file_t *file,
	guint32 code,
	guint64 length);


/**
 * Return code of section identified by name
 * @param name name of section
 * @return code of section or 0 if name of section is unknown
 */
guint32 statfile_get_section_by_name (const gchar *name);

/**
 * Set statfile revision and revision time
 * @param filename name of statfile
 * @param revision number of revision
 * @param time time of revision
 * @return TRUE if revision was set
 */
gboolean statfile_set_revision (stat_file_t *file, guint64 rev, time_t time);

/**
 * Increment statfile revision and revision time
 * @param filename name of statfile
 * @param time time of revision
 * @return TRUE if revision was set
 */
gboolean statfile_inc_revision (stat_file_t *file);

/**
 * Set statfile revision and revision time
 * @param filename name of statfile
 * @param revision saved number of revision
 * @param time saved time of revision
 * @return TRUE if revision was saved in rev and time
 */
gboolean statfile_get_revision (stat_file_t *file, guint64 *rev, time_t *time);

/**
 * Get statfile used blocks
 * @param file file to get number of used blocks
 * @return number of used blocks or (guint64)-1 in case of error
 */
guint64 statfile_get_used_blocks (stat_file_t *file);

/**
 * Get statfile total blocks
 * @param file file to get number of used blocks
 * @return number of used blocks or (guint64)-1 in case of error
 */
guint64 statfile_get_total_blocks (stat_file_t *file);


/**
 * Plan statfile pool invalidation
 */
void statfile_pool_plan_invalidate (statfile_pool_t *pool,
	time_t seconds,
	time_t jitter);

/**
 * Get a statfile by symbol
 * @param pool pool object
 * @param ccf ccf classifier config
 * @param symbol symbol to search
 * @param st statfile to get
 * @param try_create whether we need to create statfile if it is absent
 */
stat_file_t * get_statfile_by_symbol (statfile_pool_t *pool,
	struct rspamd_classifier_config *ccf,
	const gchar *symbol,
	struct rspamd_statfile_config **st,
	gboolean try_create);

#endif