libfuse
notify_inval_entry.c
Go to the documentation of this file.
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2016 Nikolaus Rath <Nikolaus@rath.org>
4
5 This program can be distributed under the terms of the GNU GPLv2.
6 See the file GPL2.txt.
7*/
8
83
84#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
85
86#include <fuse_lowlevel.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90#include <errno.h>
91#include <fcntl.h>
92#include <assert.h>
93#include <signal.h>
94#include <stddef.h>
95#include <sys/stat.h>
96#include <unistd.h>
97#include <pthread.h>
98
99#define MAX_STR_LEN 128
100static char file_name[MAX_STR_LEN];
101static fuse_ino_t file_ino = 2;
102static int lookup_cnt = 0;
103static pthread_t main_thread;
104
105/* Command line parsing */
106struct options {
107 int no_notify;
108 float timeout;
109 int update_interval;
110 int only_expire;
111 int inc_epoch;
112};
113static struct options options = {
114 .timeout = 5,
115 .no_notify = 0,
116 .update_interval = 1,
117 .only_expire = 0,
118 .inc_epoch = 0,
119};
120
121#define OPTION(t, p) { t, offsetof(struct options, p), 1 }
122static const struct fuse_opt option_spec[] = {
123 OPTION("--no-notify", no_notify),
124 OPTION("--update-interval=%d", update_interval),
125 OPTION("--timeout=%f", timeout),
126 OPTION("--only-expire", only_expire),
127 OPTION("--inc-epoch", inc_epoch),
129};
130
131static int tfs_stat(fuse_ino_t ino, struct stat *stbuf)
132{
133 stbuf->st_ino = ino;
134 if (ino == FUSE_ROOT_ID) {
135 stbuf->st_mode = S_IFDIR | 0755;
136 stbuf->st_nlink = 1;
137 }
138
139 else if (ino == file_ino) {
140 stbuf->st_mode = S_IFREG | 0000;
141 stbuf->st_nlink = 1;
142 stbuf->st_size = 0;
143 }
144
145 else
146 return -1;
147
148 return 0;
149}
150
151static void tfs_init(void *userdata, struct fuse_conn_info *conn)
152{
153 (void)userdata;
154
155 /* Disable the receiving and processing of FUSE_INTERRUPT requests */
156 conn->no_interrupt = 1;
157}
158
159static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
160{
161 struct fuse_entry_param e;
162 memset(&e, 0, sizeof(e));
163
164 if (parent != FUSE_ROOT_ID)
165 goto err_out;
166 else if (strcmp(name, file_name) == 0) {
167 e.ino = file_ino;
168 lookup_cnt++;
169 } else
170 goto err_out;
171
172 e.attr_timeout = options.timeout;
173 e.entry_timeout = options.timeout;
174 if (tfs_stat(e.ino, &e.attr) != 0)
175 goto err_out;
176 fuse_reply_entry(req, &e);
177 return;
178
179err_out:
180 fuse_reply_err(req, ENOENT);
181}
182
183static void tfs_forget(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup)
184{
185 (void)req;
186 if (ino == file_ino)
187 lookup_cnt -= nlookup;
188 else
189 assert(ino == FUSE_ROOT_ID);
190 fuse_reply_none(req);
191}
192
193static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
194 struct fuse_file_info *fi)
195{
196 struct stat stbuf;
197
198 (void)fi;
199
200 memset(&stbuf, 0, sizeof(stbuf));
201 if (tfs_stat(ino, &stbuf) != 0)
202 fuse_reply_err(req, ENOENT);
203 else
204 fuse_reply_attr(req, &stbuf, options.timeout);
205}
206
207struct dirbuf {
208 char *p;
209 size_t size;
210};
211
212static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
213 fuse_ino_t ino)
214{
215 struct stat stbuf;
216 size_t oldsize = b->size;
217 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
218 b->p = (char *)realloc(b->p, b->size);
219 memset(&stbuf, 0, sizeof(stbuf));
220 stbuf.st_ino = ino;
221 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
222 b->size);
223}
224
225#define min(x, y) ((x) < (y) ? (x) : (y))
226
227static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
228 off_t off, size_t maxsize)
229{
230 if (off < bufsize)
231 return fuse_reply_buf(req, buf + off,
232 min(bufsize - off, maxsize));
233 else
234 return fuse_reply_buf(req, NULL, 0);
235}
236
237static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
238 struct fuse_file_info *fi)
239{
240 (void)fi;
241
242 if (ino != FUSE_ROOT_ID)
243 fuse_reply_err(req, ENOTDIR);
244 else {
245 struct dirbuf b;
246
247 memset(&b, 0, sizeof(b));
248 dirbuf_add(req, &b, file_name, file_ino);
249 reply_buf_limited(req, b.p, b.size, off, size);
250 free(b.p);
251 }
252}
253
254static const struct fuse_lowlevel_ops tfs_oper = {
255 .init = tfs_init,
256 .lookup = tfs_lookup,
257 .getattr = tfs_getattr,
258 .readdir = tfs_readdir,
259 .forget = tfs_forget,
260};
261
262static void update_fs(void)
263{
264 time_t t;
265 struct tm tmbuf;
266 struct tm *now;
267 ssize_t ret;
268
269 t = time(NULL);
270 now = localtime_r(&t, &tmbuf);
271 assert(now != NULL);
272
273 ret = strftime(file_name, MAX_STR_LEN, "Time_is_%Hh_%Mm_%Ss", now);
274 assert(ret != 0);
275}
276
277static void *update_fs_loop(void *data)
278{
279 struct fuse_session *se = (struct fuse_session *)data;
280 char *old_name;
281 int ret = 0;
282
283 while (!fuse_session_exited(se)) {
284 old_name = strdup(file_name);
285 update_fs();
286
287 if (!options.no_notify && lookup_cnt) {
288 if (options.only_expire) { // expire entry
290 se, FUSE_ROOT_ID, old_name,
291 strlen(old_name));
292
293 // no kernel support
294 if (ret == -ENOSYS) {
295 printf("%s not supported by kernel\n",
296 "fuse_lowlevel_notify_expire_entry");
297 break;
298 }
299
300 // 1) ret == 0: successful expire of an existing entry
301 // 2) ret == -ENOENT: kernel has already expired the entry /
302 // entry does not exist anymore in the kernel
303 assert(ret == 0 || ret == -ENOENT);
304 } else if (options.inc_epoch) { // increment epoch
306
307 if (ret == -ENOSYS) {
308 printf("%s not supported by kernel\n",
309 "fuse_lowlevel_notify_increment_epoch");
310 break;
311 }
312 assert(ret == 0);
313 } else { // invalidate entry
315 se, FUSE_ROOT_ID, old_name,
316 strlen(old_name)) == 0);
317 }
318 }
319 free(old_name);
320 sleep(options.update_interval);
321 }
322
323 if (ret == -ENOSYS) {
324 printf("Exiting...\n");
325
327 // Make sure to exit now, rather than on next request from userspace
328 pthread_kill(main_thread, SIGPIPE);
329 }
330
331 return NULL;
332}
333
334static void show_help(const char *progname)
335{
336 printf("usage: %s [options] <mountpoint>\n\n", progname);
337 printf("File-system specific options:\n"
338 " --timeout=<secs> Timeout for kernel caches\n"
339 " --update-interval=<secs> Update-rate of file system contents\n"
340 " --no-notify Disable kernel notifications\n"
341 " --only-expire Expire entries instead of invalidating them\n"
342 " --inc-epoch Increment epoch, invalidating all dentries\n"
343 "\n");
344}
345
346int main(int argc, char *argv[])
347{
348 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
349 struct fuse_session *se;
350 struct fuse_cmdline_opts opts;
351 struct fuse_loop_config *config;
352 pthread_t updater;
353 int ret = -1;
354
355 if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
356 return 1;
357
358 if (fuse_parse_cmdline(&args, &opts) != 0)
359 return 1;
360 if (opts.show_help) {
361 show_help(argv[0]);
364 ret = 0;
365 goto err_out1;
366 } else if (opts.show_version) {
367 printf("FUSE library version %s\n", fuse_pkgversion());
369 ret = 0;
370 goto err_out1;
371 }
372 if (options.only_expire && options.inc_epoch) {
373 printf("'only-expire' and 'inc-epoch' options are exclusive\n");
374 ret = 0;
375 goto err_out1;
376 }
377
378 /* Initial contents */
379 update_fs();
380
381 se = fuse_session_new(&args, &tfs_oper, sizeof(tfs_oper), &se);
382 if (se == NULL)
383 goto err_out1;
384
385 if (fuse_set_signal_handlers(se) != 0)
386 goto err_out2;
387
388 if (fuse_session_mount(se, opts.mountpoint) != 0)
389 goto err_out3;
390
391 fuse_daemonize(opts.foreground);
392
393 // Needed to ensure that the main thread continues/restarts processing as soon
394 // as the fuse session ends (immediately after calling fuse_session_exit() )
395 // and not only on the next request from userspace
396 main_thread = pthread_self();
397
398 /* Start thread to update file contents */
399 ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
400 if (ret != 0) {
401 fprintf(stderr, "pthread_create failed with %s\n",
402 strerror(ret));
403 goto err_out3;
404 }
405
406 /* Block until ctrl+c or fusermount -u */
407 if (opts.singlethread) {
408 ret = fuse_session_loop(se);
409 } else {
410 config = fuse_loop_cfg_create();
411 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
412 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
413 ret = fuse_session_loop_mt(se, config);
414 fuse_loop_cfg_destroy(config);
415 config = NULL;
416 }
417
419err_out3:
421err_out2:
423err_out1:
424 free(opts.mountpoint);
425 fuse_opt_free_args(&args);
426
427 return ret ? 1 : 0;
428}
429
int fuse_set_signal_handlers(struct fuse_session *se)
const char * fuse_pkgversion(void)
Definition fuse.c:5274
void fuse_remove_signal_handlers(struct fuse_session *se)
int fuse_daemonize(int foreground)
Definition helper.c:253
void fuse_session_destroy(struct fuse_session *se)
void fuse_session_exit(struct fuse_session *se)
int fuse_reply_err(fuse_req_t req, int err)
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
struct fuse_req * fuse_req_t
int fuse_session_exited(struct fuse_session *se)
int fuse_session_loop(struct fuse_session *se)
Definition fuse_loop.c:19
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
void fuse_session_unmount(struct fuse_session *se)
void fuse_cmdline_help(void)
Definition helper.c:130
void fuse_reply_none(fuse_req_t req)
int fuse_lowlevel_notify_expire_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
void fuse_lowlevel_help(void)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_lowlevel_notify_increment_epoch(struct fuse_session *se)
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
void fuse_lowlevel_version(void)
uint64_t fuse_ino_t
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
Definition fuse_opt.c:398
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
#define FUSE_OPT_END
Definition fuse_opt.h:104
char ** argv
Definition fuse_opt.h:114
uint32_t no_interrupt
fuse_ino_t ino