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