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