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