libfuse: fuse-3.17.1-rc0/example/notify_inval_entry.c Source File

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),
121 FUSE_OPT_END
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
274 int ret = fuse_lowlevel_notify_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
282 fuse_session_exit(se);
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
293 assert(fuse_lowlevel_notify_inval_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]);
329 fuse_cmdline_help();
330 fuse_lowlevel_help();
331 ret = 0;
332 goto err_out1;
333 } else if (opts.show_version) {
334 printf("FUSE library version %s\n", fuse_pkgversion());
335 fuse_lowlevel_version();
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
381 fuse_session_unmount(se);
382err_out3:
383 fuse_remove_signal_handlers(se);
384err_out2:
385 fuse_session_destroy(se);
386err_out1:
387 free(opts.mountpoint);
388 fuse_opt_free_args(&args);
389
390 return ret ? 1 : 0;
391}
392
393
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:138
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition fuse.c:5218
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:180
fuse_daemonize
int fuse_daemonize(int foreground)
Definition helper.c:253
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition fuse_lowlevel.c:2967
fuse_session_exit
void fuse_session_exit(struct fuse_session *se)
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition fuse_lowlevel.c:334
fuse_reply_buf
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
Definition fuse_lowlevel.c:527
fuse_req_t
struct fuse_req * fuse_req_t
Definition fuse_lowlevel.h:50
fuse_session_exited
int fuse_session_exited(struct fuse_session *se)
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition fuse_loop.c:19
fuse_reply_entry
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
Definition fuse_lowlevel.c:431
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition fuse_lowlevel.c:3479
fuse_cmdline_help
void fuse_cmdline_help(void)
Definition helper.c:130
fuse_reply_none
void fuse_reply_none(fuse_req_t req)
Definition fuse_lowlevel.c:339
fuse_lowlevel_notify_expire_entry
int fuse_lowlevel_notify_expire_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
Definition fuse_lowlevel.c:2506
fuse_lowlevel_help
void fuse_lowlevel_help(void)
Definition fuse_lowlevel.c:2957
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition fuse_lowlevel.c:3419
fuse_lowlevel_notify_inval_entry
int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen)
Definition fuse_lowlevel.c:2500
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition fuse_lowlevel.c:2950
fuse_ino_t
uint64_t fuse_ino_t
Definition fuse_lowlevel.h:47
fuse_add_direntry
size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, const char *name, const struct stat *stbuf, off_t off)
Definition fuse_lowlevel.c:289
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition fuse_lowlevel.c:463
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
fuse_opt_parse
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
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
FUSE_OPT_END
#define FUSE_OPT_END
Definition fuse_opt.h:104
FUSE_ROOT_ID
#define FUSE_ROOT_ID
Definition fuse_lowlevel.h:44
fuse_args
Definition fuse_opt.h:109
fuse_args::argc
int argc
Definition fuse_opt.h:111
fuse_args::argv
char ** argv
Definition fuse_opt.h:114
fuse_cmdline_opts
Definition fuse_lowlevel.h:2001
fuse_conn_info
Definition fuse_common.h:555
fuse_conn_info::no_interrupt
uint32_t no_interrupt
Definition fuse_common.h:694
fuse_entry_param
Definition fuse_lowlevel.h:60
fuse_entry_param::ino
fuse_ino_t ino
Definition fuse_lowlevel.h:68
fuse_file_info
Definition fuse_common.h:58
fuse_loop_config
Definition fuse_common.h:149
fuse_lowlevel_ops
Definition fuse_lowlevel.h:206
fuse_lowlevel_ops::init
void(* init)(void *userdata, struct fuse_conn_info *conn)
Definition fuse_lowlevel.h:223
fuse_opt
Definition fuse_opt.h:77