From 64c97de5be7f899e1d36e620b3a5959d122909c7 Mon Sep 17 00:00:00 2001 From: Nikolaus Rath Date: Sun, 9 Oct 2016 20:29:04 -0700 Subject: Renamed timefsN examples to fuse_notify_* This should make it more obvious at first glance what the different examples do. --- ChangeLog.rst | 11 +- example/.gitignore | 5 +- example/Makefile.am | 4 +- example/notify_inval_entry.c | 351 +++++++++++++++++++++++++++++++++ example/notify_inval_inode.c | 373 +++++++++++++++++++++++++++++++++++ example/notify_store_retrieve.c | 417 ++++++++++++++++++++++++++++++++++++++++ example/timefs1.c | 373 ----------------------------------- example/timefs2.c | 417 ---------------------------------------- example/timefs3.c | 351 --------------------------------- test/test_examples.py | 10 +- 10 files changed, 1157 insertions(+), 1155 deletions(-) create mode 100644 example/notify_inval_entry.c create mode 100644 example/notify_inval_inode.c create mode 100644 example/notify_store_retrieve.c delete mode 100644 example/timefs1.c delete mode 100644 example/timefs2.c delete mode 100644 example/timefs3.c diff --git a/ChangeLog.rst b/ChangeLog.rst index e029cd6..da1b1a8 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -5,13 +5,12 @@ Unreleased Changes for unknown reasons -- maybe because it tries to treat the mountpoint as a file rather than a directory? -* There is a new ``timefs3`` example that demonstrates the use of - `fuse_lowlevel_notify_inval_entry`. +* There are several new examples that demonstrate the use of + the ``fuse_lowlevel_notify_*`` functions: -* There are new ``timefs1`` and ``timefs2`` examples that demonstrate - the use of the `fuse_lowlevel_notify_store`, - `fuse_lowlevel_notify_retrieve`, and - `fuse_lowlevel_notify_inval_inode` functions. + - ``example/notify_store_retrieve.c`` + - ``example/notify_inval_inode.c`` + - ``example/notify_inval_entry.c`` * The ``-o big_writes`` mount option has been removed. It is now always active. File systems that want to limit the size of write diff --git a/example/.gitignore b/example/.gitignore index fb1794a..f7cd045 100644 --- a/example/.gitignore +++ b/example/.gitignore @@ -8,5 +8,6 @@ /poll_client /cusexmp /passthrough_ll -/timefs1 -/timefs2 +/notify_inval_inode +/notify_store_retrieve +/notify_inval_entry diff --git a/example/Makefile.am b/example/Makefile.am index 9d780a7..6d93edc 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -4,8 +4,8 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -D_REENTRANT noinst_HEADERS = ioctl.h noinst_PROGRAMS = passthrough passthrough_fh hello hello_ll \ ioctl ioctl_client poll poll_client \ - cusexmp passthrough_ll timefs1 timefs2 \ - timefs3 + cusexmp passthrough_ll notify_inval_inode \ + notify_store_retrieve notify_inval_entry LDADD = ../lib/libfuse3.la passthrough_fh_LDADD = ../lib/libfuse3.la @passthrough_fh_libs@ diff --git a/example/notify_inval_entry.c b/example/notify_inval_entry.c new file mode 100644 index 0000000..fc8bc1e --- /dev/null +++ b/example/notify_inval_entry.c @@ -0,0 +1,351 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2016 Nikolaus Rath + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +/** @file + * @tableofcontents + * + * This example implements a file system with a single file whose + * file name changes dynamically to reflect the current time. + * + * It illustrates the use of the fuse_lowlevel_notify_inval_entry() + * function. + * + * To see the effect, first start the file system with the + * ``--no-notify`` + * + * $ notify_inval_entry --update-interval=1 --timeout 30 --no-notify mnt/ + * + * Observe that `ls` always prints the correct directory contents + * (since `readdir` output is not cached):: + * + * $ ls mnt; sleep 1; ls mnt; sleep 1; ls mnt + * Time_is_15h_48m_33s current_time + * Time_is_15h_48m_34s current_time + * Time_is_15h_48m_35s current_time + * + * However, if you try to access a file by name the kernel will + * report that it still exists: + * + * $ file=$(ls mnt/); echo $file + * Time_is_15h_50m_09s + * $ sleep 5; stat mnt/$file + * File: ‘mnt/Time_is_15h_50m_09s’ + * Size: 32 Blocks: 0 IO Block: 4096 regular file + * Device: 2ah/42d Inode: 3 Links: 1 + * Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) + * Access: 1969-12-31 16:00:00.000000000 -0800 + * Modify: 1969-12-31 16:00:00.000000000 -0800 + * Change: 1969-12-31 16:00:00.000000000 -0800 + * Birth: - + * + * Only once the kernel cache timeout has been reached will the stat + * call fail: + * + * $ sleep 30; stat mnt/$file + * stat: cannot stat ‘mnt/Time_is_15h_50m_09s’: No such file or directory + * + * In contrast, if you enable notifications you will be unable to stat + * the file as soon as the file system updates its name: + * + * $ notify_inval_entry --update-interval=1 --timeout 30 --no-notify mnt/ + * $ file=$(ls mnt/); stat mnt/$file + * File: ‘mnt/Time_is_20h_42m_11s’ + * Size: 0 Blocks: 0 IO Block: 4096 regular empty file + * Device: 2ah/42d Inode: 2 Links: 1 + * Access: (0000/----------) Uid: ( 0/ root) Gid: ( 0/ root) + * Access: 1969-12-31 16:00:00.000000000 -0800 + * Modify: 1969-12-31 16:00:00.000000000 -0800 + * Change: 1969-12-31 16:00:00.000000000 -0800 + * Birth: - + * $ sleep 1; stat mnt/$file + * stat: cannot stat ‘mnt/Time_is_20h_42m_11s’: No such file or directory + * + * \section section_compile compiling this example + * + * gcc -Wall notify_inval_entry.c `pkg-config fuse3 --cflags --libs` -o notify_inval_entry + * + * \section section_source the complete source + * \include notify_inval_entry.c + */ + + +#define FUSE_USE_VERSION 30 + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define MAX_STR_LEN 128 +static char file_name[MAX_STR_LEN]; +static fuse_ino_t file_ino = 2; +static int lookup_cnt = 0; + +/* Command line parsing */ +struct options { + int no_notify; + float timeout; + int update_interval; +}; +static struct options options = { + .timeout = 5, + .no_notify = 0, + .update_interval = 1, +}; + +#define OPTION(t, p) \ + { t, offsetof(struct options, p), 1 } +static const struct fuse_opt option_spec[] = { + OPTION("--no-notify", no_notify), + OPTION("--update-interval=%d", update_interval), + OPTION("--timeout=%f", timeout), + FUSE_OPT_END +}; + +static int opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) { + (void) outargs; (void) data; (void) arg; + (void) key; + return 1; +} + +static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) { + stbuf->st_ino = ino; + if (ino == FUSE_ROOT_ID) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 1; + } + + else if (ino == file_ino) { + stbuf->st_mode = S_IFREG | 0000; + stbuf->st_nlink = 1; + stbuf->st_size = 0; + } + + else + return -1; + + return 0; +} + +static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, + const char *name) { + struct fuse_entry_param e; + memset(&e, 0, sizeof(e)); + + if (parent != FUSE_ROOT_ID) + goto err_out; + else if (strcmp(name, file_name) == 0) { + e.ino = file_ino; + lookup_cnt++; + } else + goto err_out; + + e.attr_timeout = options.timeout; + e.entry_timeout = options.timeout; + if (tfs_stat(e.ino, &e.attr) != 0) + goto err_out; + fuse_reply_entry(req, &e); + return; + +err_out: + fuse_reply_err(req, ENOENT); +} + +static void tfs_forget (fuse_req_t req, fuse_ino_t ino, + uint64_t nlookup) { + (void) req; + assert(ino == file_ino || ino == FUSE_ROOT_ID); + lookup_cnt -= nlookup; + fuse_reply_none(req); +} + + +static void tfs_getattr(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) { + struct stat stbuf; + + (void) fi; + + memset(&stbuf, 0, sizeof(stbuf)); + if (tfs_stat(ino, &stbuf) != 0) + fuse_reply_err(req, ENOENT); + else + fuse_reply_attr(req, &stbuf, options.timeout); +} + +struct dirbuf { + char *p; + size_t size; +}; + +static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, + fuse_ino_t ino) { + struct stat stbuf; + size_t oldsize = b->size; + b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); + b->p = (char *) realloc(b->p, b->size); + memset(&stbuf, 0, sizeof(stbuf)); + stbuf.st_ino = ino; + fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, + b->size); +} + +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, + off_t off, size_t maxsize) { + if (off < bufsize) + return fuse_reply_buf(req, buf + off, + min(bufsize - off, maxsize)); + else + return fuse_reply_buf(req, NULL, 0); +} + +static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *fi) { + (void) fi; + + if (ino != FUSE_ROOT_ID) + fuse_reply_err(req, ENOTDIR); + else { + struct dirbuf b; + + memset(&b, 0, sizeof(b)); + dirbuf_add(req, &b, file_name, file_ino); + reply_buf_limited(req, b.p, b.size, off, size); + free(b.p); + } +} + +static struct fuse_lowlevel_ops tfs_oper = { + .lookup = tfs_lookup, + .getattr = tfs_getattr, + .readdir = tfs_readdir, + .forget = tfs_forget, +}; + +static void* update_fs(void *data) { + struct fuse_session *se = (struct fuse_session*) data; + struct tm *now; + char *old_name; + time_t t; + ssize_t ret; + + while(1) { + t = time(NULL); + now = localtime(&t); + assert(now != NULL); + + old_name = strdup(file_name); + ret = strftime(file_name, MAX_STR_LEN, + "Time_is_%Hh_%Mm_%Ss", now); + assert(ret != 0); + + if (!options.no_notify && lookup_cnt) + assert(fuse_lowlevel_notify_inval_entry + (se, FUSE_ROOT_ID, old_name, strlen(old_name)) == 0); + free(old_name); + sleep(options.update_interval); + } + return NULL; +} + +static void show_help(const char *progname) +{ + printf("usage: %s [options] \n\n", progname); + printf("File-system specific options:\n" + " --timeout= Timeout for kernel caches\n" + " --update-interval= Update-rate of file system contents\n" + " --no-notify Disable kernel notifications\n" + "\n"); +} + +int main(int argc, char *argv[]) { + struct fuse_args args = FUSE_ARGS_INIT(argc, argv); + struct fuse_session *se; + struct fuse_cmdline_opts opts; + pthread_t updater; + int ret = -1; + + if (fuse_opt_parse(&args, &options, option_spec, + opt_proc) == -1) + return 1; + + if (fuse_parse_cmdline(&args, &opts) != 0) + return 1; + if (opts.show_help) { + show_help(argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; + goto err_out1; + } + + se = fuse_session_new(&args, &tfs_oper, + sizeof(tfs_oper), NULL); + if (se == NULL) + goto err_out1; + + if (fuse_set_signal_handlers(se) != 0) + goto err_out2; + + if (fuse_session_mount(se, opts.mountpoint) != 0) + goto err_out3; + + fuse_daemonize(opts.foreground); + + /* Start thread to update file contents */ + ret = pthread_create(&updater, NULL, update_fs, (void *)se); + if (ret != 0) { + fprintf(stderr, "pthread_create failed with %s\n", + strerror(ret)); + goto err_out3; + } + + /* Block until ctrl+c or fusermount -u */ + if (opts.singlethread) + ret = fuse_session_loop(se); + else + ret = fuse_session_loop_mt(se); + + fuse_session_unmount(se); +err_out3: + fuse_remove_signal_handlers(se); +err_out2: + fuse_session_destroy(se); +err_out1: + free(opts.mountpoint); + fuse_opt_free_args(&args); + + return ret ? 1 : 0; +} + + +/** + * Local Variables: + * mode: c + * indent-tabs-mode: nil + * c-basic-offset: 4 + * End: + */ diff --git a/example/notify_inval_inode.c b/example/notify_inval_inode.c new file mode 100644 index 0000000..5dfc225 --- /dev/null +++ b/example/notify_inval_inode.c @@ -0,0 +1,373 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2016 Nikolaus Rath + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +/** @file + * @tableofcontents + * + * This example implements a file system with a single file whose + * contents change dynamically: it always contains the current time. + * + * While notify_store_retrieve.c uses fuse_lowlevel_notify_store() to + * actively push the updated data into the kernel cache, this example + * uses fuse_lowlevel_notify_inval_inode() to notify the kernel that + * the cache has to be invalidated - but the kernel still has to + * explicitly request the updated data on the next read. + * + * To see the effect, first start the file system with the + * ``--no-notify`` option: + * + * $ notify_inval_inode --update-interval=1 --no-notify mnt/ + * + * Observe that the output never changes, even though the file system + * updates it once per second. This is because the contents are cached + * in the kernel: + * + * $ for i in 1 2 3 4 5; do + * > cat mnt/current_time + * > sleep 1 + * > done + * The current time is 15:58:18 + * The current time is 15:58:18 + * The current time is 15:58:18 + * The current time is 15:58:18 + * The current time is 15:58:18 + * + * If you instead enable the notification functions, the changes become + * visible: + * + * $ notify_inval_inode --update-interval=1 mnt/ + * $ for i in 1 2 3 4 5; do + * > cat mnt/current_time + * > sleep 1 + * > done + * The current time is 15:58:40 + * The current time is 15:58:41 + * The current time is 15:58:42 + * The current time is 15:58:43 + * The current time is 15:58:44 + * + * \section section_compile compiling this example + * + * gcc -Wall notify_inval_inode.c `pkg-config fuse3 --cflags --libs` -o notify_inval_inode + * + * \section section_source the complete source + * \include notify_inval_inode.c + */ + + +#define FUSE_USE_VERSION 30 + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* We can't actually tell the kernel that there is no + timeout, so we just send a big value */ +#define NO_TIMEOUT 500000 + +/* We cannot check directly if e.g. O_RDONLY is set, since this is not + * an individual bit (cf. open(2)) */ +#define ACCESS_MASK (O_RDONLY | O_WRONLY | O_RDWR) + +#define MAX_STR_LEN 128 +#define FILE_INO 2 +#define FILE_NAME "current_time" +static char file_contents[MAX_STR_LEN]; +static int lookup_cnt = 0; +static size_t file_size; + +/* Command line parsing */ +struct options { + int no_notify; + int update_interval; +}; +static struct options options = { + .no_notify = 0, + .update_interval = 1, +}; + +#define OPTION(t, p) \ + { t, offsetof(struct options, p), 1 } +static const struct fuse_opt option_spec[] = { + OPTION("--no-notify", no_notify), + OPTION("--update-interval=%d", update_interval), + FUSE_OPT_END +}; + +static int opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) { + (void) outargs; (void) data; (void) arg; + (void) key; + return 1; +} + +static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) { + stbuf->st_ino = ino; + if (ino == FUSE_ROOT_ID) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 1; + } + + else if (ino == FILE_INO) { + stbuf->st_mode = S_IFREG | 0444; + stbuf->st_nlink = 1; + stbuf->st_size = file_size; + } + + else + return -1; + + return 0; +} + +static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, + const char *name) { + struct fuse_entry_param e; + memset(&e, 0, sizeof(e)); + + if (parent != FUSE_ROOT_ID) + goto err_out; + else if (strcmp(name, FILE_NAME) == 0) { + e.ino = FILE_INO; + lookup_cnt++; + } else + goto err_out; + + e.attr_timeout = NO_TIMEOUT; + e.entry_timeout = NO_TIMEOUT; + if (tfs_stat(e.ino, &e.attr) != 0) + goto err_out; + fuse_reply_entry(req, &e); + return; + +err_out: + fuse_reply_err(req, ENOENT); +} + +static void tfs_forget (fuse_req_t req, fuse_ino_t ino, + uint64_t nlookup) { + (void) req; + assert(ino == FILE_INO || ino == FUSE_ROOT_ID); + lookup_cnt -= nlookup; + fuse_reply_none(req); +} + +static void tfs_getattr(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) { + struct stat stbuf; + + (void) fi; + + memset(&stbuf, 0, sizeof(stbuf)); + if (tfs_stat(ino, &stbuf) != 0) + fuse_reply_err(req, ENOENT); + else + fuse_reply_attr(req, &stbuf, NO_TIMEOUT); +} + +struct dirbuf { + char *p; + size_t size; +}; + +static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, + fuse_ino_t ino) { + struct stat stbuf; + size_t oldsize = b->size; + b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); + b->p = (char *) realloc(b->p, b->size); + memset(&stbuf, 0, sizeof(stbuf)); + stbuf.st_ino = ino; + fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, + b->size); +} + +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, + off_t off, size_t maxsize) { + if (off < bufsize) + return fuse_reply_buf(req, buf + off, + min(bufsize - off, maxsize)); + else + return fuse_reply_buf(req, NULL, 0); +} + +static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *fi) { + (void) fi; + + if (ino != FUSE_ROOT_ID) + fuse_reply_err(req, ENOTDIR); + else { + struct dirbuf b; + + memset(&b, 0, sizeof(b)); + dirbuf_add(req, &b, FILE_NAME, FILE_INO); + reply_buf_limited(req, b.p, b.size, off, size); + free(b.p); + } +} + +static void tfs_open(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) { + + /* Make cache persistent even if file is closed, + this makes it easier to see the effects */ + fi->keep_cache = 1; + + if (ino == FUSE_ROOT_ID) + fuse_reply_err(req, EISDIR); + else if ((fi->flags & ACCESS_MASK) != O_RDONLY) + fuse_reply_err(req, EACCES); + else if (ino == FILE_INO) + fuse_reply_open(req, fi); + else { + // This should not happen + fprintf(stderr, "Got open for non-existing inode!\n"); + fuse_reply_err(req, ENOENT); + } +} + +static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *fi) { + (void) fi; + + assert(ino == FILE_INO); + reply_buf_limited(req, file_contents, file_size, off, size); +} + +static struct fuse_lowlevel_ops tfs_oper = { + .lookup = tfs_lookup, + .getattr = tfs_getattr, + .readdir = tfs_readdir, + .open = tfs_open, + .read = tfs_read, + .forget = tfs_forget, +}; + +static void* update_fs(void *data) { + struct fuse_session *se = (struct fuse_session*) data; + struct tm *now; + time_t t; + + while(1) { + t = time(NULL); + now = localtime(&t); + assert(now != NULL); + + file_size = strftime(file_contents, MAX_STR_LEN, + "The current time is %H:%M:%S\n", now); + assert(file_size != 0); + if (!options.no_notify && lookup_cnt) { + /* Only send notification if the kernel + is aware of the inode */ + assert(fuse_lowlevel_notify_inval_inode + (se, FILE_INO, 0, 0) == 0); + } + sleep(options.update_interval); + } + return NULL; +} + +static void show_help(const char *progname) +{ + printf("usage: %s [options] \n\n", progname); + printf("File-system specific options:\n" + " --update-interval= Update-rate of file system contents\n" + " --no-notify Disable kernel notifications\n" + "\n"); +} + +int main(int argc, char *argv[]) { + struct fuse_args args = FUSE_ARGS_INIT(argc, argv); + struct fuse_session *se; + struct fuse_cmdline_opts opts; + pthread_t updater; + int ret = -1; + + if (fuse_opt_parse(&args, &options, option_spec, + opt_proc) == -1) + return 1; + + if (fuse_parse_cmdline(&args, &opts) != 0) { + ret = 1; + goto err_out1; + } + + if (opts.show_help) { + show_help(argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; + goto err_out1; + } + + se = fuse_session_new(&args, &tfs_oper, + sizeof(tfs_oper), NULL); + if (se == NULL) + goto err_out1; + + if (fuse_set_signal_handlers(se) != 0) + goto err_out2; + + if (fuse_session_mount(se, opts.mountpoint) != 0) + goto err_out3; + + fuse_daemonize(opts.foreground); + + /* Start thread to update file contents */ + ret = pthread_create(&updater, NULL, update_fs, (void *)se); + if (ret != 0) { + fprintf(stderr, "pthread_create failed with %s\n", + strerror(ret)); + goto err_out3; + } + + /* Block until ctrl+c or fusermount -u */ + if (opts.singlethread) + ret = fuse_session_loop(se); + else + ret = fuse_session_loop_mt(se); + + fuse_session_unmount(se); +err_out3: + fuse_remove_signal_handlers(se); +err_out2: + fuse_session_destroy(se); +err_out1: + fuse_opt_free_args(&args); + free(opts.mountpoint); + + return ret ? 1 : 0; +} + + +/** + * Local Variables: + * mode: c + * indent-tabs-mode: nil + * c-basic-offset: 4 + * End: + */ diff --git a/example/notify_store_retrieve.c b/example/notify_store_retrieve.c new file mode 100644 index 0000000..a21d117 --- /dev/null +++ b/example/notify_store_retrieve.c @@ -0,0 +1,417 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2016 Nikolaus Rath + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +/** @file + * @tableofcontents + * + * This example implements a file system with a single file whose + * contents change dynamically: it always contains the current time. + * + * While notify_inval_inode.c uses fuse_lowlevel_notify_inval_inode() + * to let the kernel know that it has to invalidate the cache, this + * example actively pushes the updated data into the kernel cache + * using fuse_lowlevel_notify_store(). + * + * To see the effect, first start the file system with the + * ``--no-notify`` option: + * + * $ notify_store_retrieve --update-interval=1 --no-notify mnt/ + * + * Observe that the output never changes, even though the file system + * updates it once per second. This is because the contents are cached + * in the kernel: + * + * $ for i in 1 2 3 4 5; do + * > cat mnt/current_time + * > sleep 1 + * > done + * The current time is 15:58:18 + * The current time is 15:58:18 + * The current time is 15:58:18 + * The current time is 15:58:18 + * The current time is 15:58:18 + * + * If you instead enable the notification functions, the changes become + * visible: + * + * $ notify_store_retrieve --update-interval=1 mnt/ + * $ for i in 1 2 3 4 5; do + * > cat mnt/current_time + * > sleep 1 + * > done + * The current time is 15:58:40 + * The current time is 15:58:41 + * The current time is 15:58:42 + * The current time is 15:58:43 + * The current time is 15:58:44 + * + * \section section_compile compiling this example + * + * gcc -Wall notify_store_retrieve.c `pkg-config fuse3 --cflags --libs` -o notify_store_retrieve + * + * \section section_source the complete source + * \include notify_store_retrieve.c + */ + + +#define FUSE_USE_VERSION 30 + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* We can't actually tell the kernel that there is no + timeout, so we just send a big value */ +#define NO_TIMEOUT 500000 + +/* We cannot check directly if e.g. O_RDONLY is set, since this is not + * an individual bit (cf. open(2)) */ +#define ACCESS_MASK (O_RDONLY | O_WRONLY | O_RDWR) + +#define MAX_STR_LEN 128 +#define FILE_INO 2 +#define FILE_NAME "current_time" +static char file_contents[MAX_STR_LEN]; +static int lookup_cnt = 0; +static size_t file_size; + +/* Keep track if we ever stored data (==1), and + received it back correctly (==2) */ +static int retrieve_status = 0; + +/* Command line parsing */ +struct options { + int no_notify; + int update_interval; +}; +static struct options options = { + .no_notify = 0, + .update_interval = 1, +}; + +#define OPTION(t, p) \ + { t, offsetof(struct options, p), 1 } +static const struct fuse_opt option_spec[] = { + OPTION("--no-notify", no_notify), + OPTION("--update-interval=%d", update_interval), + FUSE_OPT_END +}; + +static int opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) { + (void) outargs; (void) data; (void) arg; + (void) key; + return 1; +} + +static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) { + stbuf->st_ino = ino; + if (ino == FUSE_ROOT_ID) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 1; + } + + else if (ino == FILE_INO) { + stbuf->st_mode = S_IFREG | 0444; + stbuf->st_nlink = 1; + stbuf->st_size = file_size; + } + + else + return -1; + + return 0; +} + +static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, + const char *name) { + struct fuse_entry_param e; + memset(&e, 0, sizeof(e)); + + if (parent != FUSE_ROOT_ID) + goto err_out; + else if (strcmp(name, FILE_NAME) == 0) { + e.ino = FILE_INO; + lookup_cnt++; + } else + goto err_out; + + e.attr_timeout = NO_TIMEOUT; + e.entry_timeout = NO_TIMEOUT; + if (tfs_stat(e.ino, &e.attr) != 0) + goto err_out; + fuse_reply_entry(req, &e); + return; + +err_out: + fuse_reply_err(req, ENOENT); +} + +static void tfs_forget (fuse_req_t req, fuse_ino_t ino, + uint64_t nlookup) { + (void) req; + assert(ino == FILE_INO || ino == FUSE_ROOT_ID); + lookup_cnt -= nlookup; + fuse_reply_none(req); +} + +static void tfs_getattr(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) { + struct stat stbuf; + + (void) fi; + + memset(&stbuf, 0, sizeof(stbuf)); + if (tfs_stat(ino, &stbuf) != 0) + fuse_reply_err(req, ENOENT); + else + fuse_reply_attr(req, &stbuf, NO_TIMEOUT); +} + +struct dirbuf { + char *p; + size_t size; +}; + +static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, + fuse_ino_t ino) { + struct stat stbuf; + size_t oldsize = b->size; + b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); + b->p = (char *) realloc(b->p, b->size); + memset(&stbuf, 0, sizeof(stbuf)); + stbuf.st_ino = ino; + fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, + b->size); +} + +#define min(x, y) ((x) < (y) ? (x) : (y)) + +static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, + off_t off, size_t maxsize) { + if (off < bufsize) + return fuse_reply_buf(req, buf + off, + min(bufsize - off, maxsize)); + else + return fuse_reply_buf(req, NULL, 0); +} + +static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *fi) { + (void) fi; + + if (ino != FUSE_ROOT_ID) + fuse_reply_err(req, ENOTDIR); + else { + struct dirbuf b; + + memset(&b, 0, sizeof(b)); + dirbuf_add(req, &b, FILE_NAME, FILE_INO); + reply_buf_limited(req, b.p, b.size, off, size); + free(b.p); + } +} + +static void tfs_open(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) { + + /* Make cache persistent even if file is closed, + this makes it easier to see the effects */ + fi->keep_cache = 1; + + if (ino == FUSE_ROOT_ID) + fuse_reply_err(req, EISDIR); + else if ((fi->flags & ACCESS_MASK) != O_RDONLY) + fuse_reply_err(req, EACCES); + else if (ino == FILE_INO) + fuse_reply_open(req, fi); + else { + // This should not happen + fprintf(stderr, "Got open for non-existing inode!\n"); + fuse_reply_err(req, ENOENT); + } +} + +static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size, + off_t off, struct fuse_file_info *fi) { + (void) fi; + + assert(ino == FILE_INO); + reply_buf_limited(req, file_contents, file_size, off, size); +} + +static void tfs_retrieve_reply(fuse_req_t req, void *cookie, fuse_ino_t ino, + off_t offset, struct fuse_bufvec *data) { + struct fuse_bufvec bufv; + char buf[MAX_STR_LEN]; + char *expected; + ssize_t ret; + + assert(ino == FILE_INO); + assert(offset == 0); + expected = (char*) cookie; + + bufv.count = 1; + bufv.idx = 0; + bufv.off = 0; + bufv.buf[0].size = MAX_STR_LEN; + bufv.buf[0].mem = buf; + bufv.buf[0].flags = 0; + + ret = fuse_buf_copy(&bufv, data, 0); + assert(ret > 0); + assert(strncmp(buf, expected, ret) == 0); + free(expected); + retrieve_status = 2; + fuse_reply_none(req); +} + + +static struct fuse_lowlevel_ops tfs_oper = { + .lookup = tfs_lookup, + .getattr = tfs_getattr, + .readdir = tfs_readdir, + .open = tfs_open, + .read = tfs_read, + .forget = tfs_forget, + .retrieve_reply = tfs_retrieve_reply, +}; + +static void* update_fs(void *data) { + struct fuse_session *se = (struct fuse_session*) data; + struct tm *now; + time_t t; + struct fuse_bufvec bufv; + + while(1) { + t = time(NULL); + now = localtime(&t); + assert(now != NULL); + + file_size = strftime(file_contents, MAX_STR_LEN, + "The current time is %H:%M:%S\n", now); + assert(file_size != 0); + if (!options.no_notify && lookup_cnt) { + /* Only send notification if the kernel + is aware of the inode */ + bufv.count = 1; + bufv.idx = 0; + bufv.off = 0; + bufv.buf[0].size = file_size; + bufv.buf[0].mem = file_contents; + bufv.buf[0].flags = 0; + assert(fuse_lowlevel_notify_store(se, FILE_INO, 0, + &bufv, 0) == 0); + + /* To make sure that everything worked correctly, ask the + kernel to send us back the stored data */ + assert(fuse_lowlevel_notify_retrieve + (se, FILE_INO, MAX_STR_LEN, 0, + (void*) strdup(file_contents)) == 0); + if(retrieve_status == 0) + retrieve_status = 1; + } + sleep(options.update_interval); + } + return NULL; +} + +static void show_help(const char *progname) +{ + printf("usage: %s [options] \n\n", progname); + printf("File-system specific options:\n" + " --update-interval= Update-rate of file system contents\n" + " --no-notify Disable kernel notifications\n" + "\n"); +} + +int main(int argc, char *argv[]) { + struct fuse_args args = FUSE_ARGS_INIT(argc, argv); + struct fuse_session *se; + struct fuse_cmdline_opts opts; + pthread_t updater; + int ret = -1; + + if (fuse_opt_parse(&args, &options, option_spec, + opt_proc) == -1) + return 1; + + if (fuse_parse_cmdline(&args, &opts) != 0) + return 1; + if (opts.show_help) { + show_help(argv[0]); + fuse_cmdline_help(); + fuse_lowlevel_help(); + fuse_mount_help(); + ret = 0; + goto err_out1; + } else if (opts.show_version) { + printf("FUSE library version %s\n", fuse_pkgversion()); + fuse_lowlevel_version(); + fuse_mount_version(); + ret = 0; + goto err_out1; + } + + se = fuse_session_new(&args, &tfs_oper, + sizeof(tfs_oper), NULL); + if (se == NULL) + goto err_out1; + + if (fuse_set_signal_handlers(se) != 0) + goto err_out2; + + if (fuse_session_mount(se, opts.mountpoint) != 0) + goto err_out3; + + fuse_daemonize(opts.foreground); + + /* Start thread to update file contents */ + ret = pthread_create(&updater, NULL, update_fs, (void *)se); + if (ret != 0) { + fprintf(stderr, "pthread_create failed with %s\n", + strerror(ret)); + goto err_out3; + } + + /* Block until ctrl+c or fusermount -u */ + if (opts.singlethread) + ret = fuse_session_loop(se); + else + ret = fuse_session_loop_mt(se); + + assert(retrieve_status != 1); + fuse_session_unmount(se); +err_out3: + fuse_remove_signal_handlers(se); +err_out2: + fuse_session_destroy(se); +err_out1: + free(opts.mountpoint); + fuse_opt_free_args(&args); + + return ret ? 1 : 0; +} + + +/** + * Local Variables: + * mode: c + * indent-tabs-mode: nil + * c-basic-offset: 4 + * End: + */ diff --git a/example/timefs1.c b/example/timefs1.c deleted file mode 100644 index 50363fd..0000000 --- a/example/timefs1.c +++ /dev/null @@ -1,373 +0,0 @@ -/* - FUSE: Filesystem in Userspace - Copyright (C) 2016 Nikolaus Rath - - This program can be distributed under the terms of the GNU GPL. - See the file COPYING. -*/ - -/** @file - * @tableofcontents - * - * This example implements a file system with a single file whose - * contents change dynamically: it always contains the current time. - * - * While timefs2.c uses fuse_lowlevel_notify_store() to actively push - * the updated data into the kernel cache, this example uses - * fuse_lowlevel_notify_inval_inode() to notify the kernel that the - * cache has to be invalidated - but the kernel still has to explicitly - * request the updated data on the next read. - * - * To see the effect, first start the file system with the - * ``--no-notify`` option: - * - * $ timefs --update-interval=1 --no-notify mnt/ - * - * Observe that the output never changes, even though the file system - * updates it once per second. This is because the contents are cached - * in the kernel: - * - * $ for i in 1 2 3 4 5; do - * > cat mnt/current_time - * > sleep 1 - * > done - * The current time is 15:58:18 - * The current time is 15:58:18 - * The current time is 15:58:18 - * The current time is 15:58:18 - * The current time is 15:58:18 - * - * If you instead enable the notification functions, the changes become - * visible: - * - * $ timefs --update-interval=1 mnt/ - * $ for i in 1 2 3 4 5; do - * > cat mnt/current_time - * > sleep 1 - * > done - * The current time is 15:58:40 - * The current time is 15:58:41 - * The current time is 15:58:42 - * The current time is 15:58:43 - * The current time is 15:58:44 - * - * \section section_compile compiling this example - * - * gcc -Wall timefs1.c `pkg-config fuse3 --cflags --libs` -o timefs1 - * - * \section section_source the complete source - * \include timefs1.c - */ - - -#define FUSE_USE_VERSION 30 - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* We can't actually tell the kernel that there is no - timeout, so we just send a big value */ -#define NO_TIMEOUT 500000 - -/* We cannot check directly if e.g. O_RDONLY is set, since this is not - * an individual bit (cf. open(2)) */ -#define ACCESS_MASK (O_RDONLY | O_WRONLY | O_RDWR) - -#define MAX_STR_LEN 128 -#define FILE_INO 2 -#define FILE_NAME "current_time" -static char file_contents[MAX_STR_LEN]; -static int lookup_cnt = 0; -static size_t file_size; - -/* Command line parsing */ -struct options { - int no_notify; - int update_interval; -}; -static struct options options = { - .no_notify = 0, - .update_interval = 1, -}; - -#define OPTION(t, p) \ - { t, offsetof(struct options, p), 1 } -static const struct fuse_opt option_spec[] = { - OPTION("--no-notify", no_notify), - OPTION("--update-interval=%d", update_interval), - FUSE_OPT_END -}; - -static int opt_proc(void *data, const char *arg, int key, - struct fuse_args *outargs) { - (void) outargs; (void) data; (void) arg; - (void) key; - return 1; -} - -static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) { - stbuf->st_ino = ino; - if (ino == FUSE_ROOT_ID) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 1; - } - - else if (ino == FILE_INO) { - stbuf->st_mode = S_IFREG | 0444; - stbuf->st_nlink = 1; - stbuf->st_size = file_size; - } - - else - return -1; - - return 0; -} - -static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, - const char *name) { - struct fuse_entry_param e; - memset(&e, 0, sizeof(e)); - - if (parent != FUSE_ROOT_ID) - goto err_out; - else if (strcmp(name, FILE_NAME) == 0) { - e.ino = FILE_INO; - lookup_cnt++; - } else - goto err_out; - - e.attr_timeout = NO_TIMEOUT; - e.entry_timeout = NO_TIMEOUT; - if (tfs_stat(e.ino, &e.attr) != 0) - goto err_out; - fuse_reply_entry(req, &e); - return; - -err_out: - fuse_reply_err(req, ENOENT); -} - -static void tfs_forget (fuse_req_t req, fuse_ino_t ino, - uint64_t nlookup) { - (void) req; - assert(ino == FILE_INO || ino == FUSE_ROOT_ID); - lookup_cnt -= nlookup; - fuse_reply_none(req); -} - -static void tfs_getattr(fuse_req_t req, fuse_ino_t ino, - struct fuse_file_info *fi) { - struct stat stbuf; - - (void) fi; - - memset(&stbuf, 0, sizeof(stbuf)); - if (tfs_stat(ino, &stbuf) != 0) - fuse_reply_err(req, ENOENT); - else - fuse_reply_attr(req, &stbuf, NO_TIMEOUT); -} - -struct dirbuf { - char *p; - size_t size; -}; - -static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, - fuse_ino_t ino) { - struct stat stbuf; - size_t oldsize = b->size; - b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); - b->p = (char *) realloc(b->p, b->size); - memset(&stbuf, 0, sizeof(stbuf)); - stbuf.st_ino = ino; - fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, - b->size); -} - -#define min(x, y) ((x) < (y) ? (x) : (y)) - -static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, - off_t off, size_t maxsize) { - if (off < bufsize) - return fuse_reply_buf(req, buf + off, - min(bufsize - off, maxsize)); - else - return fuse_reply_buf(req, NULL, 0); -} - -static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, - off_t off, struct fuse_file_info *fi) { - (void) fi; - - if (ino != FUSE_ROOT_ID) - fuse_reply_err(req, ENOTDIR); - else { - struct dirbuf b; - - memset(&b, 0, sizeof(b)); - dirbuf_add(req, &b, FILE_NAME, FILE_INO); - reply_buf_limited(req, b.p, b.size, off, size); - free(b.p); - } -} - -static void tfs_open(fuse_req_t req, fuse_ino_t ino, - struct fuse_file_info *fi) { - - /* Make cache persistent even if file is closed, - this makes it easier to see the effects */ - fi->keep_cache = 1; - - if (ino == FUSE_ROOT_ID) - fuse_reply_err(req, EISDIR); - else if ((fi->flags & ACCESS_MASK) != O_RDONLY) - fuse_reply_err(req, EACCES); - else if (ino == FILE_INO) - fuse_reply_open(req, fi); - else { - // This should not happen - fprintf(stderr, "Got open for non-existing inode!\n"); - fuse_reply_err(req, ENOENT); - } -} - -static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size, - off_t off, struct fuse_file_info *fi) { - (void) fi; - - assert(ino == FILE_INO); - reply_buf_limited(req, file_contents, file_size, off, size); -} - -static struct fuse_lowlevel_ops tfs_oper = { - .lookup = tfs_lookup, - .getattr = tfs_getattr, - .readdir = tfs_readdir, - .open = tfs_open, - .read = tfs_read, - .forget = tfs_forget, -}; - -static void* update_fs(void *data) { - struct fuse_session *se = (struct fuse_session*) data; - struct tm *now; - time_t t; - - while(1) { - t = time(NULL); - now = localtime(&t); - assert(now != NULL); - - file_size = strftime(file_contents, MAX_STR_LEN, - "The current time is %H:%M:%S\n", now); - assert(file_size != 0); - if (!options.no_notify && lookup_cnt) { - /* Only send notification if the kernel - is aware of the inode */ - assert(fuse_lowlevel_notify_inval_inode - (se, FILE_INO, 0, 0) == 0); - } - sleep(options.update_interval); - } - return NULL; -} - -static void show_help(const char *progname) -{ - printf("usage: %s [options] \n\n", progname); - printf("File-system specific options:\n" - " --update-interval= Update-rate of file system contents\n" - " --no-notify Disable kernel notifications\n" - "\n"); -} - -int main(int argc, char *argv[]) { - struct fuse_args args = FUSE_ARGS_INIT(argc, argv); - struct fuse_session *se; - struct fuse_cmdline_opts opts; - pthread_t updater; - int ret = -1; - - if (fuse_opt_parse(&args, &options, option_spec, - opt_proc) == -1) - return 1; - - if (fuse_parse_cmdline(&args, &opts) != 0) { - ret = 1; - goto err_out1; - } - - if (opts.show_help) { - show_help(argv[0]); - fuse_cmdline_help(); - fuse_lowlevel_help(); - fuse_mount_help(); - ret = 0; - goto err_out1; - } else if (opts.show_version) { - printf("FUSE library version %s\n", fuse_pkgversion()); - fuse_lowlevel_version(); - fuse_mount_version(); - ret = 0; - goto err_out1; - } - - se = fuse_session_new(&args, &tfs_oper, - sizeof(tfs_oper), NULL); - if (se == NULL) - goto err_out1; - - if (fuse_set_signal_handlers(se) != 0) - goto err_out2; - - if (fuse_session_mount(se, opts.mountpoint) != 0) - goto err_out3; - - fuse_daemonize(opts.foreground); - - /* Start thread to update file contents */ - ret = pthread_create(&updater, NULL, update_fs, (void *)se); - if (ret != 0) { - fprintf(stderr, "pthread_create failed with %s\n", - strerror(ret)); - goto err_out3; - } - - /* Block until ctrl+c or fusermount -u */ - if (opts.singlethread) - ret = fuse_session_loop(se); - else - ret = fuse_session_loop_mt(se); - - fuse_session_unmount(se); -err_out3: - fuse_remove_signal_handlers(se); -err_out2: - fuse_session_destroy(se); -err_out1: - fuse_opt_free_args(&args); - free(opts.mountpoint); - - return ret ? 1 : 0; -} - - -/** - * Local Variables: - * mode: c - * indent-tabs-mode: nil - * c-basic-offset: 4 - * End: - */ diff --git a/example/timefs2.c b/example/timefs2.c deleted file mode 100644 index ae3fe99..0000000 --- a/example/timefs2.c +++ /dev/null @@ -1,417 +0,0 @@ -/* - FUSE: Filesystem in Userspace - Copyright (C) 2016 Nikolaus Rath - - This program can be distributed under the terms of the GNU GPL. - See the file COPYING. -*/ - -/** @file - * @tableofcontents - * - * This example implements a file system with a single file whose - * contents change dynamically: it always contains the current time. - * - * While timefs1.c uses fuse_lowlevel_notify_inval_inode() to let the - * kernel know that it has to invalidate the cache, this example - * actively pushes the updated data into the kernel cache using - * fuse_lowlevel_notify_store(). - * - * To see the effect, first start the file system with the - * ``--no-notify`` option: - * - * $ timefs --update-interval=1 --no-notify mnt/ - * - * Observe that the output never changes, even though the file system - * updates it once per second. This is because the contents are cached - * in the kernel: - * - * $ for i in 1 2 3 4 5; do - * > cat mnt/current_time - * > sleep 1 - * > done - * The current time is 15:58:18 - * The current time is 15:58:18 - * The current time is 15:58:18 - * The current time is 15:58:18 - * The current time is 15:58:18 - * - * If you instead enable the notification functions, the changes become - * visible: - * - * $ timefs --update-interval=1 mnt/ - * $ for i in 1 2 3 4 5; do - * > cat mnt/current_time - * > sleep 1 - * > done - * The current time is 15:58:40 - * The current time is 15:58:41 - * The current time is 15:58:42 - * The current time is 15:58:43 - * The current time is 15:58:44 - * - * \section section_compile compiling this example - * - * gcc -Wall timefs2.c `pkg-config fuse3 --cflags --libs` -o timefs2 - * - * \section section_source the complete source - * \include timefs2.c - */ - - -#define FUSE_USE_VERSION 30 - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/* We can't actually tell the kernel that there is no - timeout, so we just send a big value */ -#define NO_TIMEOUT 500000 - -/* We cannot check directly if e.g. O_RDONLY is set, since this is not - * an individual bit (cf. open(2)) */ -#define ACCESS_MASK (O_RDONLY | O_WRONLY | O_RDWR) - -#define MAX_STR_LEN 128 -#define FILE_INO 2 -#define FILE_NAME "current_time" -static char file_contents[MAX_STR_LEN]; -static int lookup_cnt = 0; -static size_t file_size; - -/* Keep track if we ever stored data (==1), and - received it back correctly (==2) */ -static int retrieve_status = 0; - -/* Command line parsing */ -struct options { - int no_notify; - int update_interval; -}; -static struct options options = { - .no_notify = 0, - .update_interval = 1, -}; - -#define OPTION(t, p) \ - { t, offsetof(struct options, p), 1 } -static const struct fuse_opt option_spec[] = { - OPTION("--no-notify", no_notify), - OPTION("--update-interval=%d", update_interval), - FUSE_OPT_END -}; - -static int opt_proc(void *data, const char *arg, int key, - struct fuse_args *outargs) { - (void) outargs; (void) data; (void) arg; - (void) key; - return 1; -} - -static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) { - stbuf->st_ino = ino; - if (ino == FUSE_ROOT_ID) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 1; - } - - else if (ino == FILE_INO) { - stbuf->st_mode = S_IFREG | 0444; - stbuf->st_nlink = 1; - stbuf->st_size = file_size; - } - - else - return -1; - - return 0; -} - -static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, - const char *name) { - struct fuse_entry_param e; - memset(&e, 0, sizeof(e)); - - if (parent != FUSE_ROOT_ID) - goto err_out; - else if (strcmp(name, FILE_NAME) == 0) { - e.ino = FILE_INO; - lookup_cnt++; - } else - goto err_out; - - e.attr_timeout = NO_TIMEOUT; - e.entry_timeout = NO_TIMEOUT; - if (tfs_stat(e.ino, &e.attr) != 0) - goto err_out; - fuse_reply_entry(req, &e); - return; - -err_out: - fuse_reply_err(req, ENOENT); -} - -static void tfs_forget (fuse_req_t req, fuse_ino_t ino, - uint64_t nlookup) { - (void) req; - assert(ino == FILE_INO || ino == FUSE_ROOT_ID); - lookup_cnt -= nlookup; - fuse_reply_none(req); -} - -static void tfs_getattr(fuse_req_t req, fuse_ino_t ino, - struct fuse_file_info *fi) { - struct stat stbuf; - - (void) fi; - - memset(&stbuf, 0, sizeof(stbuf)); - if (tfs_stat(ino, &stbuf) != 0) - fuse_reply_err(req, ENOENT); - else - fuse_reply_attr(req, &stbuf, NO_TIMEOUT); -} - -struct dirbuf { - char *p; - size_t size; -}; - -static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, - fuse_ino_t ino) { - struct stat stbuf; - size_t oldsize = b->size; - b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); - b->p = (char *) realloc(b->p, b->size); - memset(&stbuf, 0, sizeof(stbuf)); - stbuf.st_ino = ino; - fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, - b->size); -} - -#define min(x, y) ((x) < (y) ? (x) : (y)) - -static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, - off_t off, size_t maxsize) { - if (off < bufsize) - return fuse_reply_buf(req, buf + off, - min(bufsize - off, maxsize)); - else - return fuse_reply_buf(req, NULL, 0); -} - -static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, - off_t off, struct fuse_file_info *fi) { - (void) fi; - - if (ino != FUSE_ROOT_ID) - fuse_reply_err(req, ENOTDIR); - else { - struct dirbuf b; - - memset(&b, 0, sizeof(b)); - dirbuf_add(req, &b, FILE_NAME, FILE_INO); - reply_buf_limited(req, b.p, b.size, off, size); - free(b.p); - } -} - -static void tfs_open(fuse_req_t req, fuse_ino_t ino, - struct fuse_file_info *fi) { - - /* Make cache persistent even if file is closed, - this makes it easier to see the effects */ - fi->keep_cache = 1; - - if (ino == FUSE_ROOT_ID) - fuse_reply_err(req, EISDIR); - else if ((fi->flags & ACCESS_MASK) != O_RDONLY) - fuse_reply_err(req, EACCES); - else if (ino == FILE_INO) - fuse_reply_open(req, fi); - else { - // This should not happen - fprintf(stderr, "Got open for non-existing inode!\n"); - fuse_reply_err(req, ENOENT); - } -} - -static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size, - off_t off, struct fuse_file_info *fi) { - (void) fi; - - assert(ino == FILE_INO); - reply_buf_limited(req, file_contents, file_size, off, size); -} - -static void tfs_retrieve_reply(fuse_req_t req, void *cookie, fuse_ino_t ino, - off_t offset, struct fuse_bufvec *data) { - struct fuse_bufvec bufv; - char buf[MAX_STR_LEN]; - char *expected; - ssize_t ret; - - assert(ino == FILE_INO); - assert(offset == 0); - expected = (char*) cookie; - - bufv.count = 1; - bufv.idx = 0; - bufv.off = 0; - bufv.buf[0].size = MAX_STR_LEN; - bufv.buf[0].mem = buf; - bufv.buf[0].flags = 0; - - ret = fuse_buf_copy(&bufv, data, 0); - assert(ret > 0); - assert(strncmp(buf, expected, ret) == 0); - free(expected); - retrieve_status = 2; - fuse_reply_none(req); -} - - -static struct fuse_lowlevel_ops tfs_oper = { - .lookup = tfs_lookup, - .getattr = tfs_getattr, - .readdir = tfs_readdir, - .open = tfs_open, - .read = tfs_read, - .forget = tfs_forget, - .retrieve_reply = tfs_retrieve_reply, -}; - -static void* update_fs(void *data) { - struct fuse_session *se = (struct fuse_session*) data; - struct tm *now; - time_t t; - struct fuse_bufvec bufv; - - while(1) { - t = time(NULL); - now = localtime(&t); - assert(now != NULL); - - file_size = strftime(file_contents, MAX_STR_LEN, - "The current time is %H:%M:%S\n", now); - assert(file_size != 0); - if (!options.no_notify && lookup_cnt) { - /* Only send notification if the kernel - is aware of the inode */ - bufv.count = 1; - bufv.idx = 0; - bufv.off = 0; - bufv.buf[0].size = file_size; - bufv.buf[0].mem = file_contents; - bufv.buf[0].flags = 0; - assert(fuse_lowlevel_notify_store(se, FILE_INO, 0, - &bufv, 0) == 0); - - /* To make sure that everything worked correctly, ask the - kernel to send us back the stored data */ - assert(fuse_lowlevel_notify_retrieve - (se, FILE_INO, MAX_STR_LEN, 0, - (void*) strdup(file_contents)) == 0); - if(retrieve_status == 0) - retrieve_status = 1; - } - sleep(options.update_interval); - } - return NULL; -} - -static void show_help(const char *progname) -{ - printf("usage: %s [options] \n\n", progname); - printf("File-system specific options:\n" - " --update-interval= Update-rate of file system contents\n" - " --no-notify Disable kernel notifications\n" - "\n"); -} - -int main(int argc, char *argv[]) { - struct fuse_args args = FUSE_ARGS_INIT(argc, argv); - struct fuse_session *se; - struct fuse_cmdline_opts opts; - pthread_t updater; - int ret = -1; - - if (fuse_opt_parse(&args, &options, option_spec, - opt_proc) == -1) - return 1; - - if (fuse_parse_cmdline(&args, &opts) != 0) - return 1; - if (opts.show_help) { - show_help(argv[0]); - fuse_cmdline_help(); - fuse_lowlevel_help(); - fuse_mount_help(); - ret = 0; - goto err_out1; - } else if (opts.show_version) { - printf("FUSE library version %s\n", fuse_pkgversion()); - fuse_lowlevel_version(); - fuse_mount_version(); - ret = 0; - goto err_out1; - } - - se = fuse_session_new(&args, &tfs_oper, - sizeof(tfs_oper), NULL); - if (se == NULL) - goto err_out1; - - if (fuse_set_signal_handlers(se) != 0) - goto err_out2; - - if (fuse_session_mount(se, opts.mountpoint) != 0) - goto err_out3; - - fuse_daemonize(opts.foreground); - - /* Start thread to update file contents */ - ret = pthread_create(&updater, NULL, update_fs, (void *)se); - if (ret != 0) { - fprintf(stderr, "pthread_create failed with %s\n", - strerror(ret)); - goto err_out3; - } - - /* Block until ctrl+c or fusermount -u */ - if (opts.singlethread) - ret = fuse_session_loop(se); - else - ret = fuse_session_loop_mt(se); - - assert(retrieve_status != 1); - fuse_session_unmount(se); -err_out3: - fuse_remove_signal_handlers(se); -err_out2: - fuse_session_destroy(se); -err_out1: - free(opts.mountpoint); - fuse_opt_free_args(&args); - - return ret ? 1 : 0; -} - - -/** - * Local Variables: - * mode: c - * indent-tabs-mode: nil - * c-basic-offset: 4 - * End: - */ diff --git a/example/timefs3.c b/example/timefs3.c deleted file mode 100644 index 613407c..0000000 --- a/example/timefs3.c +++ /dev/null @@ -1,351 +0,0 @@ -/* - FUSE: Filesystem in Userspace - Copyright (C) 2016 Nikolaus Rath - - This program can be distributed under the terms of the GNU GPL. - See the file COPYING. -*/ - -/** @file - * @tableofcontents - * - * This example implements a file system with a single file whose - * file name changes dynamically to reflect the current time. - * - * It illustrates the use of the fuse_lowlevel_notify_inval_entry() - * function. - * - * To see the effect, first start the file system with the - * ``--no-notify`` - * - * $ timefs --update-interval=1 --timeout 30 --no-notify mnt/ - * - * Observe that `ls` always prints the correct directory contents - * (since `readdir` output is not cached):: - * - * $ ls mnt; sleep 1; ls mnt; sleep 1; ls mnt - * Time_is_15h_48m_33s current_time - * Time_is_15h_48m_34s current_time - * Time_is_15h_48m_35s current_time - * - * However, if you try to access a file by name the kernel will - * report that it still exists: - * - * $ file=$(ls mnt/); echo $file - * Time_is_15h_50m_09s - * $ sleep 5; stat mnt/$file - * File: ‘mnt/Time_is_15h_50m_09s’ - * Size: 32 Blocks: 0 IO Block: 4096 regular file - * Device: 2ah/42d Inode: 3 Links: 1 - * Access: (0444/-r--r--r--) Uid: ( 0/ root) Gid: ( 0/ root) - * Access: 1969-12-31 16:00:00.000000000 -0800 - * Modify: 1969-12-31 16:00:00.000000000 -0800 - * Change: 1969-12-31 16:00:00.000000000 -0800 - * Birth: - - * - * Only once the kernel cache timeout has been reached will the stat - * call fail: - * - * $ sleep 30; stat mnt/$file - * stat: cannot stat ‘mnt/Time_is_15h_50m_09s’: No such file or directory - * - * In contrast, if you enable notifications you will be unable to stat - * the file as soon as the file system updates its name: - * - * $ timefs --update-interval=1 --timeout 30 --no-notify mnt/ - * $ file=$(ls mnt/); stat mnt/$file - * File: ‘mnt/Time_is_20h_42m_11s’ - * Size: 0 Blocks: 0 IO Block: 4096 regular empty file - * Device: 2ah/42d Inode: 2 Links: 1 - * Access: (0000/----------) Uid: ( 0/ root) Gid: ( 0/ root) - * Access: 1969-12-31 16:00:00.000000000 -0800 - * Modify: 1969-12-31 16:00:00.000000000 -0800 - * Change: 1969-12-31 16:00:00.000000000 -0800 - * Birth: - - * $ sleep 1; stat mnt/$file - * stat: cannot stat ‘mnt/Time_is_20h_42m_11s’: No such file or directory - * - * \section section_compile compiling this example - * - * gcc -Wall timefs3.c `pkg-config fuse3 --cflags --libs` -o timefs3 - * - * \section section_source the complete source - * \include timefs3.c - */ - - -#define FUSE_USE_VERSION 30 - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define MAX_STR_LEN 128 -static char file_name[MAX_STR_LEN]; -static fuse_ino_t file_ino = 2; -static int lookup_cnt = 0; - -/* Command line parsing */ -struct options { - int no_notify; - float timeout; - int update_interval; -}; -static struct options options = { - .timeout = 5, - .no_notify = 0, - .update_interval = 1, -}; - -#define OPTION(t, p) \ - { t, offsetof(struct options, p), 1 } -static const struct fuse_opt option_spec[] = { - OPTION("--no-notify", no_notify), - OPTION("--update-interval=%d", update_interval), - OPTION("--timeout=%f", timeout), - FUSE_OPT_END -}; - -static int opt_proc(void *data, const char *arg, int key, - struct fuse_args *outargs) { - (void) outargs; (void) data; (void) arg; - (void) key; - return 1; -} - -static int tfs_stat(fuse_ino_t ino, struct stat *stbuf) { - stbuf->st_ino = ino; - if (ino == FUSE_ROOT_ID) { - stbuf->st_mode = S_IFDIR | 0755; - stbuf->st_nlink = 1; - } - - else if (ino == file_ino) { - stbuf->st_mode = S_IFREG | 0000; - stbuf->st_nlink = 1; - stbuf->st_size = 0; - } - - else - return -1; - - return 0; -} - -static void tfs_lookup(fuse_req_t req, fuse_ino_t parent, - const char *name) { - struct fuse_entry_param e; - memset(&e, 0, sizeof(e)); - - if (parent != FUSE_ROOT_ID) - goto err_out; - else if (strcmp(name, file_name) == 0) { - e.ino = file_ino; - lookup_cnt++; - } else - goto err_out; - - e.attr_timeout = options.timeout; - e.entry_timeout = options.timeout; - if (tfs_stat(e.ino, &e.attr) != 0) - goto err_out; - fuse_reply_entry(req, &e); - return; - -err_out: - fuse_reply_err(req, ENOENT); -} - -static void tfs_forget (fuse_req_t req, fuse_ino_t ino, - uint64_t nlookup) { - (void) req; - assert(ino == file_ino || ino == FUSE_ROOT_ID); - lookup_cnt -= nlookup; - fuse_reply_none(req); -} - - -static void tfs_getattr(fuse_req_t req, fuse_ino_t ino, - struct fuse_file_info *fi) { - struct stat stbuf; - - (void) fi; - - memset(&stbuf, 0, sizeof(stbuf)); - if (tfs_stat(ino, &stbuf) != 0) - fuse_reply_err(req, ENOENT); - else - fuse_reply_attr(req, &stbuf, options.timeout); -} - -struct dirbuf { - char *p; - size_t size; -}; - -static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name, - fuse_ino_t ino) { - struct stat stbuf; - size_t oldsize = b->size; - b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0); - b->p = (char *) realloc(b->p, b->size); - memset(&stbuf, 0, sizeof(stbuf)); - stbuf.st_ino = ino; - fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf, - b->size); -} - -#define min(x, y) ((x) < (y) ? (x) : (y)) - -static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize, - off_t off, size_t maxsize) { - if (off < bufsize) - return fuse_reply_buf(req, buf + off, - min(bufsize - off, maxsize)); - else - return fuse_reply_buf(req, NULL, 0); -} - -static void tfs_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, - off_t off, struct fuse_file_info *fi) { - (void) fi; - - if (ino != FUSE_ROOT_ID) - fuse_reply_err(req, ENOTDIR); - else { - struct dirbuf b; - - memset(&b, 0, sizeof(b)); - dirbuf_add(req, &b, file_name, file_ino); - reply_buf_limited(req, b.p, b.size, off, size); - free(b.p); - } -} - -static struct fuse_lowlevel_ops tfs_oper = { - .lookup = tfs_lookup, - .getattr = tfs_getattr, - .readdir = tfs_readdir, - .forget = tfs_forget, -}; - -static void* update_fs(void *data) { - struct fuse_session *se = (struct fuse_session*) data; - struct tm *now; - char *old_name; - time_t t; - ssize_t ret; - - while(1) { - t = time(NULL); - now = localtime(&t); - assert(now != NULL); - - old_name = strdup(file_name); - ret = strftime(file_name, MAX_STR_LEN, - "Time_is_%Hh_%Mm_%Ss", now); - assert(ret != 0); - - if (!options.no_notify && lookup_cnt) - assert(fuse_lowlevel_notify_inval_entry - (se, FUSE_ROOT_ID, old_name, strlen(old_name)) == 0); - free(old_name); - sleep(options.update_interval); - } - return NULL; -} - -static void show_help(const char *progname) -{ - printf("usage: %s [options] \n\n", progname); - printf("File-system specific options:\n" - " --timeout= Timeout for kernel caches\n" - " --update-interval= Update-rate of file system contents\n" - " --no-notify Disable kernel notifications\n" - "\n"); -} - -int main(int argc, char *argv[]) { - struct fuse_args args = FUSE_ARGS_INIT(argc, argv); - struct fuse_session *se; - struct fuse_cmdline_opts opts; - pthread_t updater; - int ret = -1; - - if (fuse_opt_parse(&args, &options, option_spec, - opt_proc) == -1) - return 1; - - if (fuse_parse_cmdline(&args, &opts) != 0) - return 1; - if (opts.show_help) { - show_help(argv[0]); - fuse_cmdline_help(); - fuse_lowlevel_help(); - fuse_mount_help(); - ret = 0; - goto err_out1; - } else if (opts.show_version) { - printf("FUSE library version %s\n", fuse_pkgversion()); - fuse_lowlevel_version(); - fuse_mount_version(); - ret = 0; - goto err_out1; - } - - se = fuse_session_new(&args, &tfs_oper, - sizeof(tfs_oper), NULL); - if (se == NULL) - goto err_out1; - - if (fuse_set_signal_handlers(se) != 0) - goto err_out2; - - if (fuse_session_mount(se, opts.mountpoint) != 0) - goto err_out3; - - fuse_daemonize(opts.foreground); - - /* Start thread to update file contents */ - ret = pthread_create(&updater, NULL, update_fs, (void *)se); - if (ret != 0) { - fprintf(stderr, "pthread_create failed with %s\n", - strerror(ret)); - goto err_out3; - } - - /* Block until ctrl+c or fusermount -u */ - if (opts.singlethread) - ret = fuse_session_loop(se); - else - ret = fuse_session_loop_mt(se); - - fuse_session_unmount(se); -err_out3: - fuse_remove_signal_handlers(se); -err_out2: - fuse_session_destroy(se); -err_out1: - free(opts.mountpoint); - fuse_opt_free_args(&args); - - return ret ? 1 : 0; -} - - -/** - * Local Variables: - * mode: c - * indent-tabs-mode: nil - * c-basic-offset: 4 - * End: - */ diff --git a/test/test_examples.py b/test/test_examples.py index 0d754d2..9d83ae9 100755 --- a/test/test_examples.py +++ b/test/test_examples.py @@ -142,10 +142,12 @@ def test_poll(tmpdir): else: umount(mount_process, mnt_dir) -@pytest.mark.parametrize("name", ('timefs1', 'timefs2')) +@pytest.mark.parametrize("name", + ('notify_inval_inode', + 'notify_store_retrieve')) @pytest.mark.parametrize("options", LL_OPTIONS) @pytest.mark.parametrize("notify", (True, False)) -def test_timefs12(tmpdir, name, options, notify): +def test_notify1(tmpdir, name, options, notify): mnt_dir = str(tmpdir) cmdline = base_cmdline + \ [ pjoin(basename, 'example', name), @@ -172,10 +174,10 @@ def test_timefs12(tmpdir, name, options, notify): umount(mount_process, mnt_dir) @pytest.mark.parametrize("notify", (True, False)) -def test_timefs3(tmpdir, notify): +def test_notify_inval_entry(tmpdir, notify): mnt_dir = str(tmpdir) cmdline = base_cmdline + \ - [ pjoin(basename, 'example', 'timefs3'), + [ pjoin(basename, 'example', 'notify_inval_entry'), '-f', '--update-interval=2', '--timeout=4', mnt_dir ] if not notify: -- cgit v1.2.3