libfuse: example/notify_inval_inode.c Source File

libfuse

notify_inval_inode.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
62#define FUSE_USE_VERSION FUSE_MAKE_VERSION(3, 12)
63
64#include <fuse_lowlevel.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <assert.h>
71#include <stddef.h>
72#include <unistd.h>
73#include <pthread.h>
74#include <stdbool.h>
75#include <stdatomic.h>
76
77/* We can't actually tell the kernel that there is no
78 timeout, so we just send a big value */
79#define NO_TIMEOUT 500000
80
81#define MAX_STR_LEN 128
82#define FILE_INO 2
83#define FILE_NAME "current_time"
84static char file_contents[MAX_STR_LEN];
85static int lookup_cnt = 0;
86static size_t file_size;
87static _Atomic bool is_stop = false;
88
89/* Command line parsing */
90struct options {
91 int no_notify;
92 int update_interval;
93};
94static struct options options = {
95 .no_notify = 0,
96 .update_interval = 1,
97};
98
99#define OPTION(t, p) \
100 { t, offsetof(struct options, p), 1 }
101static const struct fuse_opt option_spec[] = {
102 OPTION("--no-notify", no_notify),
103 OPTION("--update-interval=%d", update_interval),
104 FUSE_OPT_END
105};
106
107static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) {
108 stbuf->st_ino = ino;
109 if (ino == FUSE_ROOT_ID) {
110 stbuf->st_mode = S_IFDIR | 0755;
111 stbuf->st_nlink = 1;
112 }
113
114 else if (ino == FILE_INO) {
115 stbuf->st_mode = S_IFREG | 0444;
116 stbuf->st_nlink = 1;
117 stbuf->st_size = file_size;
118 }
119
120 else
121 return -1;
122
123 return 0;
124}
125
126static void tfs_init(void *userdata, struct fuse_conn_info *conn) {
127 (void)userdata;
128
129 /* Disable the receiving and processing of FUSE_INTERRUPT requests */
130 conn->no_interrupt = 1;
131}
132
133static void tfs_destroy(void *userarg)
134{
135 (void)userarg;
136
137 is_stop = true;
138}
139
140static void tfs_lookup(fuse_req_t req, fuse_ino_t parent,
141 const char *name) {
142 struct fuse_entry_param e;
143 memset(&e, 0, sizeof(e));
144
145 if (parent != FUSE_ROOT_ID)
146 goto err_out;
147 else if (strcmp(name, FILE_NAME) == 0) {
148 e.ino = FILE_INO;
149 lookup_cnt++;
150 } else
151 goto err_out;
152
153 e.attr_timeout = NO_TIMEOUT;
154 e.entry_timeout = NO_TIMEOUT;
155 if (tfs_stat(e.ino, &e.attr) != 0)
156 goto err_out;
157 fuse_reply_entry(req, &e);
158 return;
159
160err_out:
161 fuse_reply_err(req, ENOENT);
162}
163
164static void tfs_forget (fuse_req_t req, fuse_ino_t ino,
165 uint64_t nlookup) {
166 (void) req;
167 if(ino == FILE_INO)
168 lookup_cnt -= nlookup;
169 else
170 assert(ino == FUSE_ROOT_ID);
171 fuse_reply_none(req);
172}
173
174static void tfs_getattr(fuse_req_t req, fuse_ino_t ino,
175 struct fuse_file_info *fi) {
176 struct stat stbuf;
177
178 (void) fi;
179
180 memset(&stbuf, 0, sizeof(stbuf));
181 if (tfs_stat(ino, &stbuf) != 0)
182 fuse_reply_err(req, ENOENT);
183 else
184 fuse_reply_attr(req, &stbuf, NO_TIMEOUT);
185}
186
187struct dirbuf {
188 char *p;
189 size_t size;
190};
191
192static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
193 fuse_ino_t ino) {
194 struct stat stbuf;
195 size_t oldsize = b->size;
196 b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
197 b->p = (char *) realloc(b->p, b->size);
198 memset(&stbuf, 0, sizeof(stbuf));
199 stbuf.st_ino = ino;
200 fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
201 b->size);
202}
203
204#define min(x, y) ((x) < (y) ? (x) : (y))
205
206static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
207 off_t off, size_t maxsize) {
208 if (off < bufsize)
209 return fuse_reply_buf(req, buf + off,
210 min(bufsize - off, maxsize));
211 else
212 return fuse_reply_buf(req, NULL, 0);
213}
214
215static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
216 off_t off, struct fuse_file_info *fi) {
217 (void) fi;
218
219 if (ino != FUSE_ROOT_ID)
220 fuse_reply_err(req, ENOTDIR);
221 else {
222 struct dirbuf b;
223
224 memset(&b, 0, sizeof(b));
225 dirbuf_add(req, &b, FILE_NAME, FILE_INO);
226 reply_buf_limited(req, b.p, b.size, off, size);
227 free(b.p);
228 }
229}
230
231static void tfs_open(fuse_req_t req, fuse_ino_t ino,
232 struct fuse_file_info *fi) {
233
234 /* Make cache persistent even if file is closed,
235 this makes it easier to see the effects */
236 fi->keep_cache = 1;
237
238 if (ino == FUSE_ROOT_ID)
239 fuse_reply_err(req, EISDIR);
240 else if ((fi->flags & O_ACCMODE) != O_RDONLY)
241 fuse_reply_err(req, EACCES);
242 else if (ino == FILE_INO)
243 fuse_reply_open(req, fi);
244 else {
245 // This should not happen
246 fprintf(stderr, "Got open for non-existing inode!\n");
247 fuse_reply_err(req, ENOENT);
248 }
249}
250
251static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size,
252 off_t off, struct fuse_file_info *fi) {
253 (void) fi;
254
255 assert(ino == FILE_INO);
256 reply_buf_limited(req, file_contents, file_size, off, size);
257}
258
259static const struct fuse_lowlevel_ops tfs_oper = {
260 .init = tfs_init,
261 .destroy = tfs_destroy,
262 .lookup = tfs_lookup,
263 .getattr = tfs_getattr,
264 .readdir = tfs_readdir,
265 .open = tfs_open,
266 .read = tfs_read,
267 .forget = tfs_forget,
268};
269
270static void update_fs(void) {
271 struct tm *now;
272 time_t t;
273 t = time(NULL);
274 now = localtime(&t);
275 assert(now != NULL);
276
277 file_size = strftime(file_contents, MAX_STR_LEN,
278 "The current time is %H:%M:%S\n", now);
279 assert(file_size != 0);
280}
281
282static void* update_fs_loop(void *data) {
283 struct fuse_session *se = (struct fuse_session*) data;
284
285 while(!is_stop) {
286 update_fs();
287 if (!options.no_notify && lookup_cnt) {
288 /* Only send notification if the kernel is aware of the inode */
289
290 /* Some errors (ENOENT, EBADF, ENODEV) have to be accepted as they
291 * might come up during umount, when kernel side already releases
292 * all inodes, but does not send FUSE_DESTROY yet.
293 */
294 int ret =
295 fuse_lowlevel_notify_inval_inode(se, FILE_INO, 0, 0);
296 if ((ret != 0 && !is_stop) &&
297 ret != -ENOENT && ret != -EBADF && ret != -ENODEV) {
298 fprintf(stderr,
299 "ERROR: fuse_lowlevel_notify_store() failed with %s (%d)\n",
300 strerror(-ret), -ret);
301 abort();
302 }
303 }
304 sleep(options.update_interval);
305 }
306 return NULL;
307}
308
309static void show_help(const char *progname)
310{
311 printf("usage: %s [options] <mountpoint>\n\n", progname);
312 printf("File-system specific options:\n"
313 " --update-interval=<secs> Update-rate of file system contents\n"
314 " --no-notify Disable kernel notifications\n"
315 "\n");
316}
317
318int main(int argc, char *argv[]) {
319 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
320 struct fuse_session *se;
321 struct fuse_cmdline_opts opts;
322 struct fuse_loop_config *config;
323 pthread_t updater;
324 int ret = -1;
325
326 if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
327 return 1;
328
329 if (fuse_parse_cmdline(&args, &opts) != 0) {
330 ret = 1;
331 goto err_out1;
332 }
333
334 if (opts.show_help) {
335 show_help(argv[0]);
336 fuse_cmdline_help();
337 fuse_lowlevel_help();
338 ret = 0;
339 goto err_out1;
340 } else if (opts.show_version) {
341 printf("FUSE library version %s\n", fuse_pkgversion());
342 fuse_lowlevel_version();
343 ret = 0;
344 goto err_out1;
345 }
346
347 /* Initial contents */
348 update_fs();
349
350 se = fuse_session_new(&args, &tfs_oper,
351 sizeof(tfs_oper), NULL);
352 if (se == NULL)
353 goto err_out1;
354
355 if (fuse_set_signal_handlers(se) != 0)
356 goto err_out2;
357
358 if (fuse_session_mount(se, opts.mountpoint) != 0)
359 goto err_out3;
360
361 fuse_daemonize(opts.foreground);
362
363 /* Start thread to update file contents */
364 ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
365 if (ret != 0) {
366 fprintf(stderr, "pthread_create failed with %s\n",
367 strerror(ret));
368 goto err_out3;
369 }
370
371 /* Block until ctrl+c or fusermount -u */
372 if (opts.singlethread)
373 ret = fuse_session_loop(se);
374 else {
375 config = fuse_loop_cfg_create();
376 fuse_loop_cfg_set_clone_fd(config, opts.clone_fd);
377 fuse_loop_cfg_set_max_threads(config, opts.max_threads);
378 ret = fuse_session_loop_mt(se, config);
379 fuse_loop_cfg_destroy(config);
380 config = NULL;
381 }
382
383 fuse_session_unmount(se);
384err_out3:
385 fuse_remove_signal_handlers(se);
386err_out2:
387 fuse_session_destroy(se);
388err_out1:
389 fuse_opt_free_args(&args);
390 free(opts.mountpoint);
391
392 return ret ? 1 : 0;
393}
394
395
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:140
fuse_pkgversion
const char * fuse_pkgversion(void)
Definition fuse.c:5211
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:187
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:3010
fuse_reply_open
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
Definition fuse_lowlevel.c:505
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition fuse_lowlevel.c:331
fuse_reply_buf
int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
Definition fuse_lowlevel.c:524
fuse_req_t
struct fuse_req * fuse_req_t
Definition fuse_lowlevel.h:50
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:428
fuse_session_unmount
void fuse_session_unmount(struct fuse_session *se)
Definition fuse_lowlevel.c:3539
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:336
fuse_lowlevel_help
void fuse_lowlevel_help(void)
Definition fuse_lowlevel.c:3000
fuse_lowlevel_notify_inval_inode
int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, off_t off, off_t len)
Definition fuse_lowlevel.c:2475
fuse_session_mount
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
Definition fuse_lowlevel.c:3473
fuse_lowlevel_version
void fuse_lowlevel_version(void)
Definition fuse_lowlevel.c:2993
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:286
fuse_reply_attr
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
Definition fuse_lowlevel.c:460
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:2003
fuse_conn_info
Definition fuse_common.h:538
fuse_conn_info::no_interrupt
uint32_t no_interrupt
Definition fuse_common.h:677
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:50
fuse_file_info::keep_cache
uint32_t keep_cache
Definition fuse_common.h:69
fuse_file_info::flags
int32_t flags
Definition fuse_common.h:52
fuse_loop_config
Definition fuse_common.h:139
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