-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_cache_event_port.c
456 lines (364 loc) · 9.72 KB
/
file_cache_event_port.c
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
* Copyright (c) 2014-2019 Josef 'Jeff' Sipek <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stddef.h>
#include <stdbool.h>
#include <port.h>
#include <jeffpc/atomic.h>
#include <jeffpc/val.h>
#include <jeffpc/synch.h>
#include <jeffpc/thread.h>
#include <jeffpc/refcnt.h>
#include <jeffpc/io.h>
#include <jeffpc/list.h>
#include <jeffpc/mem.h>
#include <jeffpc/rbtree.h>
#include <jeffpc/file-cache.h>
static LOCK_CLASS(file_lock_lc);
static LOCK_CLASS(file_node_lc);
static struct rb_tree file_cache;
static struct lock file_lock;
static atomic64_t current_revision;
static int filemon_port;
static struct mem_cache *file_node_cache;
struct file_node {
char *name; /* the filename */
struct rb_node node;
refcnt_t refcnt;
struct lock lock;
/* everything else is protected by the lock */
struct str *contents; /* cache file contents if allowed */
struct stat stat; /* the stat info of the cached file */
bool needs_reload; /* caching stale data */
uint64_t cache_rev; /* cache version */
struct file_obj fobj; /* FEN port object */
};
static void fn_free(struct file_node *node);
static int __reload(struct file_node *node);
REFCNT_INLINE_FXNS(struct file_node, fn, refcnt, fn_free, NULL)
static void print_event(const char *fname, int event)
{
cmn_err(CE_DEBUG, "%s: %s", __func__, fname);
if (event & FILE_ACCESS)
cmn_err(CE_DEBUG, "\tFILE_ACCESS");
if (event & FILE_MODIFIED)
cmn_err(CE_DEBUG, "\tFILE_MODIFIED");
if (event & FILE_ATTRIB)
cmn_err(CE_DEBUG, "\tFILE_ATTRIB");
if (event & FILE_DELETE)
cmn_err(CE_DEBUG, "\tFILE_DELETE");
if (event & FILE_RENAME_TO)
cmn_err(CE_DEBUG, "\tFILE_RENAME_TO");
if (event & FILE_RENAME_FROM)
cmn_err(CE_DEBUG, "\tFILE_RENAME_FROM");
if (event & UNMOUNTED)
cmn_err(CE_DEBUG, "\tUNMOUNTED");
if (event & MOUNTEDOVER)
cmn_err(CE_DEBUG, "\tMOUNTEDOVER");
}
static void process_file(struct file_node *node, int events)
{
struct file_obj *fobj = &node->fobj;
struct stat statbuf;
MXLOCK(&node->lock);
if (!(events & FILE_EXCEPTION)) {
int ret;
ret = xstat(fobj->fo_name, &statbuf);
if (ret) {
cmn_err(CE_DEBUG, "failed to stat '%s' error: %s\n",
fobj->fo_name, xstrerror(ret));
goto free;
}
}
if (events) {
/* something changed, we need to reload */
node->needs_reload = true;
print_event(fobj->fo_name, events);
/*
* If the file went away, we could rely on reloading to deal
* with it. Or we can just remove the node and have the
* next caller read it from disk. We take the later
* approach.
*/
if (events & FILE_EXCEPTION)
goto free;
/*
* Because the cached data is invalid (and therefore
* useless), we can free it now and avoid having it linger
* around, ending up core files, etc.
*/
str_putref(node->contents);
node->contents = NULL;
}
/* re-register */
fobj->fo_atime = statbuf.st_atim;
fobj->fo_mtime = statbuf.st_mtim;
fobj->fo_ctime = statbuf.st_ctim;
if (port_associate(filemon_port, PORT_SOURCE_FILE, (uintptr_t) fobj,
(FILE_MODIFIED | FILE_ATTRIB), node) == -1) {
cmn_err(CE_DEBUG, "failed to register file '%s' errno %d",
fobj->fo_name, errno);
goto free;
}
MXUNLOCK(&node->lock);
return;
free:
/*
* If there was an error, remove the node from the cache. The next
* time someone tries to get this file, they'll pull it in from
* disk.
*/
MXLOCK(&file_lock);
rb_remove(&file_cache, node);
MXUNLOCK(&file_lock);
MXUNLOCK(&node->lock);
fn_putref(node); /* put the cache's reference */
}
static void *filemon(void *arg)
{
port_event_t pe;
while (!port_get(filemon_port, &pe, NULL)) {
switch (pe.portev_source) {
case PORT_SOURCE_FILE:
process_file(pe.portev_user,
pe.portev_events);
break;
default:
panic("unexpected event source");
}
}
return NULL;
}
static int filename_cmp(const void *va, const void *vb)
{
const struct file_node *a = va;
const struct file_node *b = vb;
int ret;
ret = strcmp(a->name, b->name);
if (ret < 0)
return -1;
if (ret > 0)
return 1;
return 0;
}
int file_cache_init(void)
{
int ret;
MXINIT(&file_lock, &file_lock_lc);
rb_create(&file_cache, filename_cmp, sizeof(struct file_node),
offsetof(struct file_node, node));
file_node_cache = mem_cache_create("file-node-cache",
sizeof(struct file_node), 0);
if (IS_ERR(file_node_cache)) {
ret = PTR_ERR(file_node_cache);
goto err;
}
/* start the file event monitor */
filemon_port = port_create();
if (filemon_port == -1) {
ret = -errno;
goto err_cache;
}
ret = xthr_create(NULL, filemon, NULL);
if (ret)
goto err_port;
return 0;
err_port:
xclose(filemon_port);
err_cache:
mem_cache_destroy(file_node_cache);
err:
rb_destroy(&file_cache);
return ret;
}
static struct file_node *fn_alloc(const char *name)
{
struct file_node *node;
node = mem_cache_alloc(file_node_cache);
if (!node)
return NULL;
node->name = strdup(name);
if (!node->name)
goto err;
node->fobj.fo_name = node->name;
node->contents = NULL;
node->needs_reload = true;
node->cache_rev = atomic_inc(¤t_revision);
MXINIT(&node->lock, &file_node_lc);
refcnt_init(&node->refcnt, 1);
return node;
err:
mem_cache_free(file_node_cache, node);
return NULL;
}
static void fn_free(struct file_node *node)
{
if (!node)
return;
str_putref(node->contents);
free(node->name);
mem_cache_free(file_node_cache, node);
}
static int __reload(struct file_node *node)
{
char *tmp;
if (!node->needs_reload)
return 0;
/* free the previous */
str_putref(node->contents);
node->contents = NULL;
/* read the current */
tmp = read_file_common(AT_FDCWD, node->name, &node->stat);
if (IS_ERR(tmp)) {
cmn_err(CE_DEBUG, "file (%s) read error: %s", node->name,
xstrerror(PTR_ERR(tmp)));
return PTR_ERR(tmp);
}
node->contents = str_alloc(tmp);
if (IS_ERR(node->contents)) {
int ret = PTR_ERR(node->contents);
cmn_err(CE_DEBUG, "file (%s) str_alloc error: %s",
node->name, xstrerror(ret));
free(tmp);
node->contents = NULL;
return ret;
}
node->needs_reload = false;
node->cache_rev = atomic_inc(¤t_revision);
return 0;
}
static struct file_node *load_file(const char *name)
{
struct file_node *node;
int ret;
node = fn_alloc(name);
if (!node)
return ERR_PTR(-ENOMEM);
if ((ret = __reload(node))) {
fn_free(node);
return ERR_PTR(ret);
}
return node;
}
struct str *file_cache_get(const char *name, uint64_t *rev)
{
struct file_node *out, *tmp;
struct file_node key;
struct str *str;
struct rb_cookie where;
int ret;
key.name = (char *) name;
/* do we have it? */
MXLOCK(&file_lock);
out = rb_find(&file_cache, &key, NULL);
fn_getref(out);
MXUNLOCK(&file_lock);
/* already had it, so return that */
if (out)
goto output;
/* have to load it from disk...*/
out = load_file(name);
if (IS_ERR(out))
return ERR_CAST(out);
MXLOCK(&out->lock);
/* ...and insert it into the cache */
MXLOCK(&file_lock);
tmp = rb_find(&file_cache, &key, &where);
if (tmp) {
/*
* uh oh, someone beat us to it; free our copy & return
* existing
*/
fn_getref(tmp);
MXUNLOCK(&file_lock);
/* release the node we allocated */
MXUNLOCK(&out->lock);
fn_putref(out);
/* work with the node found in the tree */
out = tmp;
goto output;
}
/* get a ref for the cache */
fn_getref(out);
rb_insert_here(&file_cache, out, &where);
MXUNLOCK(&file_lock);
MXUNLOCK(&out->lock);
/*
* Ok! The node is in the cache.
*/
/* start watching */
process_file(out, 0);
output:
/* get a reference for the string */
MXLOCK(&out->lock);
ret = __reload(out);
if (ret) {
/* there was an error reloading - bail */
MXUNLOCK(&out->lock);
fn_putref(out);
return ERR_PTR(ret);
}
str = str_getref(out->contents);
/* inform the caller about which version we're returning */
if (rev)
*rev = out->cache_rev;
MXUNLOCK(&out->lock);
/* put the reference for the file node */
fn_putref(out);
return str;
}
bool file_cache_has_newer(const char *name, uint64_t rev)
{
struct file_node *out;
struct file_node key;
bool ret;
key.name = (char *) name;
/* do we have it? */
MXLOCK(&file_lock);
out = rb_find(&file_cache, &key, NULL);
fn_getref(out);
MXUNLOCK(&file_lock);
/*
* We don't have it cached (which is weird/a bug), so let's pretend
* that there is a newer version...just in case.
*/
if (!out)
return true;
MXLOCK(&out->lock);
ret = out->needs_reload || (rev != out->cache_rev);
MXUNLOCK(&out->lock);
/* put the reference for the file node */
fn_putref(out);
return ret;
}
void uncache_all_files(void)
{
struct file_node *cur;
struct rb_cookie cookie;
MXLOCK(&file_lock);
memset(&cookie, 0, sizeof(cookie));
while ((cur = rb_destroy_nodes(&file_cache, &cookie)))
fn_putref(cur);
MXUNLOCK(&file_lock);
}