10#define FUSE_USE_VERSION 30 
   12#include <fuse_config.h> 
   28#include <linux/limits.h> 
   32#define FILE_NAME "write_me" 
   46    { t, offsetof(struct options, p), 1 } 
   47static const struct fuse_opt option_spec[] = {
 
   48    OPTION(
"writeback_cache", writeback),
 
   49    OPTION(
"--data-size=%d", data_size),
 
   50    OPTION(
"--delay_ms=%d", delay_ms),
 
   55pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
 
   56pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 
   57static int write_start, write_done;
 
   63    if(options.writeback) {
 
   69static int tfs_stat(
fuse_ino_t ino, 
struct stat *stbuf) {
 
   72        stbuf->st_mode = S_IFDIR | 0755;
 
   76    else if (ino == FILE_INO) {
 
   77        stbuf->st_mode = S_IFREG | 0222;
 
   91    memset(&e, 0, 
sizeof(e));
 
   95    else if (strcmp(name, FILE_NAME) == 0)
 
  100    if (tfs_stat(e.ino, &e.attr) != 0)
 
  115    memset(&stbuf, 0, 
sizeof(stbuf));
 
  116    if (tfs_stat(ino, &stbuf) != 0)
 
  127        assert(ino == FILE_INO);
 
  129        fi->
noflush = !options.writeback && options.delay_ms &&
 
  130                      (fi->
flags & O_ACCMODE) == O_RDONLY;
 
  137    (void) fi; (void) buf; (void) off;
 
  140    assert(ino == FILE_INO);
 
  141    expected = options.data_size;
 
  142    if(options.writeback)
 
  146        fprintf(stderr, 
"ERROR: Expected %zd bytes, got %zd\n!",
 
  152    if (options.delay_ms) {
 
  153        pthread_mutex_lock(&lock);
 
  155        pthread_cond_signal(&cond);
 
  156        pthread_mutex_unlock(&lock);
 
  158        usleep(options.delay_ms * 1000);
 
  160        pthread_mutex_lock(&lock);
 
  162        pthread_cond_signal(&cond);
 
  163        pthread_mutex_unlock(&lock);
 
  171    .lookup     = tfs_lookup,
 
  172    .getattr    = tfs_getattr,
 
  177static void* close_rofd(
void *data) {
 
  178    int rofd = (int)(
long) data;
 
  181    pthread_mutex_lock(&lock);
 
  182    while (!write_start && !write_done)
 
  183        pthread_cond_wait(&cond, &lock);
 
  184    pthread_mutex_unlock(&lock);
 
  187    printf(
"rofd closed. write_start: %d write_done: %d\n", write_start, write_done);
 
  191        fprintf(stderr, 
"ERROR: close(rofd) blocked on write!\n");
 
  196static void* run_fs(
void *data) {
 
  197    struct fuse_session *se = (
struct fuse_session*) data;
 
  202static void test_fs(
char *mountpoint) {
 
  203    char fname[PATH_MAX];
 
  205    size_t dsize = options.data_size;
 
  207    pthread_t rofd_thread;
 
  211    assert((fd = open(
"/dev/urandom", O_RDONLY)) != -1);
 
  212    assert(read(fd, buf, dsize) == dsize);
 
  215    assert(snprintf(fname, PATH_MAX, 
"%s/" FILE_NAME,
 
  217    fd = open(fname, O_WRONLY);
 
  223    if (options.delay_ms) {
 
  225        rofd = open(fname, O_RDONLY);
 
  226        assert(pthread_create(&rofd_thread, NULL, close_rofd, (
void *)(
long)rofd) == 0);
 
  228        usleep(options.delay_ms * 1000);
 
  231    assert(write(fd, buf, dsize) == dsize);
 
  232    assert(write(fd, buf, dsize) == dsize);
 
  236    if (options.delay_ms) {
 
  237        printf(
"rwfd closed. write_start: %d write_done: %d\n", write_start, write_done);
 
  238        assert(pthread_join(rofd_thread, NULL) == 0);
 
  242int main(
int argc, 
char *argv[]) {
 
  244    struct fuse_session *se;
 
  249    assert(fuse_parse_cmdline(&args, &fuse_opts) == 0);
 
  254                          sizeof(tfs_oper), NULL);
 
  261    assert(pthread_create(&fs_thread, NULL, run_fs, (
void *)se) == 0);
 
  264    test_fs(fuse_opts.mountpoint);
 
  265    free(fuse_opts.mountpoint);
 
  270    assert(pthread_join(fs_thread, NULL) == 0);
 
  272    assert(got_write == 1);
 
  276    printf(
"Test completed successfully.\n");
 
int fuse_set_signal_handlers(struct fuse_session *se)
#define FUSE_CAP_WRITEBACK_CACHE
void fuse_remove_signal_handlers(struct fuse_session *se)
void fuse_session_destroy(struct fuse_session *se)
int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi)
void fuse_session_exit(struct fuse_session *se)
int fuse_reply_err(fuse_req_t req, int err)
struct fuse_req * fuse_req_t
int fuse_session_loop(struct fuse_session *se)
int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e)
void fuse_session_unmount(struct fuse_session *se)
struct fuse_session * fuse_session_new(struct fuse_args *args, const struct fuse_lowlevel_ops *op, size_t op_size, void *userdata)
int fuse_reply_write(fuse_req_t req, size_t count)
int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
int fuse_reply_attr(fuse_req_t req, const struct stat *attr, double attr_timeout)
int fuse_opt_add_arg(struct fuse_args *args, const char *arg)
void fuse_opt_free_args(struct fuse_args *args)
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
#define FUSE_ARGS_INIT(argc, argv)
void(* init)(void *userdata, struct fuse_conn_info *conn)