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 34
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 <stddef.h>
89 #include <unistd.h>
90 #include <pthread.h>
91 
92 #define MAX_STR_LEN 128
93 static char file_name[MAX_STR_LEN];
94 static fuse_ino_t file_ino = 2;
95 static int lookup_cnt = 0;
96 
97 /* Command line parsing */
98 struct options {
99  int no_notify;
100  float timeout;
101  int update_interval;
102  int only_expire;
103 };
104 static struct options options = {
105  .timeout = 5,
106  .no_notify = 0,
107  .update_interval = 1,
108  .only_expire = 0,
109 };
110 
111 #define OPTION(t, p) \
112  { t, offsetof(struct options, p), 1 }
113 static const struct fuse_opt option_spec[] = {
114  OPTION("--no-notify", no_notify),
115  OPTION("--update-interval=%d", update_interval),
116  OPTION("--timeout=%f", timeout),
117  OPTION("--only-expire", only_expire),
119 };
120 
121 static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) {
122  stbuf->st_ino = ino;
123  if (ino == FUSE_ROOT_ID) {
124  stbuf->st_mode = S_IFDIR | 0755;
125  stbuf->st_nlink = 1;
126  }
127 
128  else if (ino == file_ino) {
129  stbuf->st_mode = S_IFREG | 0000;
130  stbuf->st_nlink = 1;
131  stbuf->st_size = 0;
132  }
133 
134  else
135  return -1;
136 
137  return 0;
138 }
139 
140 static 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 = options.timeout;
154  e.entry_timeout = options.timeout;
155  if (tfs_stat(e.ino, &e.attr) != 0)
156  goto err_out;
157  fuse_reply_entry(req, &e);
158  return;
159 
160 err_out:
161  fuse_reply_err(req, ENOENT);
162 }
163 
164 static 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 
174 static 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, options.timeout);
185 }
186 
187 struct dirbuf {
188  char *p;
189  size_t size;
190 };
191 
192 static 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 
206 static 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 
215 static 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 
231 static const struct fuse_lowlevel_ops tfs_oper = {
232  .lookup = tfs_lookup,
233  .getattr = tfs_getattr,
234  .readdir = tfs_readdir,
235  .forget = tfs_forget,
236 };
237 
238 static void update_fs(void) {
239  time_t t;
240  struct tm *now;
241  ssize_t ret;
242 
243  t = time(NULL);
244  now = localtime(&t);
245  assert(now != NULL);
246 
247  ret = strftime(file_name, MAX_STR_LEN,
248  "Time_is_%Hh_%Mm_%Ss", now);
249  assert(ret != 0);
250 }
251 
252 static void* update_fs_loop(void *data) {
253  struct fuse_session *se = (struct fuse_session*) data;
254  char *old_name;
255 
256  while(1) {
257  old_name = strdup(file_name);
258  update_fs();
259  if (!options.no_notify && lookup_cnt) {
260  if(options.only_expire) {
262  (se, FUSE_ROOT_ID, old_name, strlen(old_name), FUSE_LL_EXPIRE_ONLY) == 0);
263  } else {
265  (se, FUSE_ROOT_ID, old_name, strlen(old_name)) == 0);
266  }
267  }
268  free(old_name);
269  sleep(options.update_interval);
270  }
271  return NULL;
272 }
273 
274 static void show_help(const char *progname)
275 {
276  printf("usage: %s [options] <mountpoint>\n\n", progname);
277  printf("File-system specific options:\n"
278  " --timeout=<secs> Timeout for kernel caches\n"
279  " --update-interval=<secs> Update-rate of file system contents\n"
280  " --no-notify Disable kernel notifications\n"
281  " --only-expire Expire entries instead of invalidating them\n"
282  "\n");
283 }
284 
285 int main(int argc, char *argv[]) {
286  struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
287  struct fuse_session *se;
288  struct fuse_cmdline_opts opts;
289  struct fuse_loop_config config;
290  pthread_t updater;
291  int ret = -1;
292 
293  if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
294  return 1;
295 
296  if (fuse_parse_cmdline(&args, &opts) != 0)
297  return 1;
298  if (opts.show_help) {
299  show_help(argv[0]);
302  ret = 0;
303  goto err_out1;
304  } else if (opts.show_version) {
305  printf("FUSE library version %s\n", fuse_pkgversion());
307  ret = 0;
308  goto err_out1;
309  }
310 
311  /* Initial contents */
312  update_fs();
313 
314  se = fuse_session_new(&args, &tfs_oper,
315  sizeof(tfs_oper), NULL);
316  if (se == NULL)
317  goto err_out1;
318 
319  if (fuse_set_signal_handlers(se) != 0)
320  goto err_out2;
321 
322  if (fuse_session_mount(se, opts.mountpoint) != 0)
323  goto err_out3;
324 
325  fuse_daemonize(opts.foreground);
326 
327  /* Start thread to update file contents */
328  ret = pthread_create(&updater, NULL, update_fs_loop, (void *)se);
329  if (ret != 0) {
330  fprintf(stderr, "pthread_create failed with %s\n",
331  strerror(ret));
332  goto err_out3;
333  }
334 
335  /* Block until ctrl+c or fusermount -u */
336  if (opts.singlethread)
337  ret = fuse_session_loop(se);
338  else {
339  config.clone_fd = opts.clone_fd;
340  config.max_idle_threads = opts.max_idle_threads;
341  ret = fuse_session_loop_mt(se, &config);
342  }
343 
345 err_out3:
347 err_out2:
349 err_out1:
350  free(opts.mountpoint);
351  fuse_opt_free_args(&args);
352 
353  return ret ? 1 : 0;
354 }
355 
356 
int fuse_set_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:62
const char * fuse_pkgversion(void)
Definition: fuse.c:5113
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition: fuse_signals.c:79
int fuse_daemonize(int foreground)
Definition: helper.c:253
void fuse_session_destroy(struct fuse_session *se)
#define FUSE_ROOT_ID
Definition: fuse_lowlevel.h:43
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
Definition: fuse_lowlevel.h:49
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)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_lowlevel_notify_expire_entry(struct fuse_session *se, fuse_ino_t parent, const char *name, size_t namelen, enum fuse_expire_flags flags)
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
Definition: fuse_lowlevel.h:46
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
int argc
Definition: fuse_opt.h:111
char ** argv
Definition: fuse_opt.h:114
Definition: fuse_lowlevel.h:59
fuse_ino_t ino
Definition: fuse_lowlevel.h:67
void(* lookup)(fuse_req_t req, fuse_ino_t parent, const char *name)